diff --git a/Directory.Packages.props b/Directory.Packages.props
index 7f1df29..5f81ce8 100644
--- a/Directory.Packages.props
+++ b/Directory.Packages.props
@@ -1,16 +1,17 @@
-
- true
-
-
-
-
-
-
-
-
-
-
-
-
+
+ true
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/OrchardCoreContrib.PoExtractor/OrchardCoreContrib.PoExtractor.csproj b/src/OrchardCoreContrib.PoExtractor/OrchardCoreContrib.PoExtractor.csproj
index 183c158..0d5607a 100644
--- a/src/OrchardCoreContrib.PoExtractor/OrchardCoreContrib.PoExtractor.csproj
+++ b/src/OrchardCoreContrib.PoExtractor/OrchardCoreContrib.PoExtractor.csproj
@@ -25,6 +25,9 @@
+
+
+
diff --git a/src/OrchardCoreContrib.PoExtractor/Program.cs b/src/OrchardCoreContrib.PoExtractor/Program.cs
index 45b6e03..cbb87fa 100644
--- a/src/OrchardCoreContrib.PoExtractor/Program.cs
+++ b/src/OrchardCoreContrib.PoExtractor/Program.cs
@@ -1,230 +1,140 @@
-using OrchardCoreContrib.PoExtractor.DotNet;
+using McMaster.Extensions.CommandLineUtils;
+using OrchardCoreContrib.PoExtractor.DotNet;
using OrchardCoreContrib.PoExtractor.DotNet.CS;
using OrchardCoreContrib.PoExtractor.DotNet.VB;
using OrchardCoreContrib.PoExtractor.Liquid;
using OrchardCoreContrib.PoExtractor.Razor;
-using System;
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
namespace OrchardCoreContrib.PoExtractor;
public class Program
{
- private static readonly string _defaultLanguage = Language.CSharp;
- private static readonly string _defaultTemplateEngine = TemplateEngine.Both;
-
public static void Main(string[] args)
{
- if (args.Length < 2 || args.Length > 10 || args.Length % 2 == 1)
- {
- ShowHelp();
+ var app = new CommandLineApplication();
- return;
- }
+ app.HelpOption();
- var inputPath = new DirectoryInfo(args[0]).FullName;
- var outputPath = new DirectoryInfo(args[1]).FullName;
+ // Arguments
+ var inputPath = app.Argument("Input Path", "The path to the input directory, all projects at the the path will be processed.")
+ .IsRequired();
+ var outputPath = app.Argument("Output Path", "The path to a directory where POT files will be generated.")
+ .IsRequired();
- if (!Directory.Exists(inputPath))
+ // Options
+ var language = app.Option("-l|--language ", "Specifies the code language to extracts translatable strings from.", CommandOptionType.SingleValue, options =>
{
- ShowHelp();
-
- return;
- }
-
- (string language, string templateEngine, string singleOutputFile) = GetCliOptions(args);
-
- if (language == null || templateEngine == null)
+ options.Accepts(cfg => cfg.Values("C#", "VB"));
+ options.DefaultValue = "C#";
+ });
+ var template = app.Option("-t|--template ", "Specifies the template engine to extract the translatable strings from.", CommandOptionType.SingleValue, options =>
+ options.Accepts(cfg => cfg.Values("Razor", "Liquid"))
+ );
+ var ignoredProjects = app.Option("-i|--ignore ", "Ignores extracting PO files from a given project(s).", CommandOptionType.MultipleValue);
+ var localizers = app.Option("--localizer ", "Specifies the name of the localizer(s) that will be used during the extraction process.", CommandOptionType.MultipleValue);
+ var single = app.Option("-s|--single ", "Specifies the single output file.", CommandOptionType.SingleValue);
+
+ app.OnExecute(() =>
{
- ShowHelp();
-
- return;
- }
+ if (!Directory.Exists(inputPath.Value))
+ {
+ Console.WriteLine($"The input path '{inputPath.Value}' does not exist.");
- var projectFiles = new List();
- var projectProcessors = new List();
+ return;
+ }
- if (language == Language.CSharp)
- {
- projectProcessors.Add(new CSharpProjectProcessor());
+ foreach (var ignoredProject in ignoredProjects.Values)
+ {
+ IgnoredProject.Add(ignoredProject);
+ }
- projectFiles.AddRange(Directory
- .EnumerateFiles(inputPath, $"*{ProjectExtension.CS}", SearchOption.AllDirectories)
- .OrderBy(f => f));
- }
- else
- {
- projectProcessors.Add(new VisualBasicProjectProcessor());
+ LocalizerAccessors.LocalizerIdentifiers = [.. localizers.Values];
- projectFiles.AddRange(Directory
- .EnumerateFiles(inputPath, $"*{ProjectExtension.VB}", SearchOption.AllDirectories)
- .OrderBy(f => f));
- }
+ var projectFiles = new List();
+ var projectProcessors = new List();
- if (templateEngine == TemplateEngine.Both)
- {
- projectProcessors.Add(new RazorProjectProcessor());
- projectProcessors.Add(new LiquidProjectProcessor());
- }
- else if (templateEngine == TemplateEngine.Razor)
- {
- projectProcessors.Add(new RazorProjectProcessor());
- }
- else if (templateEngine == TemplateEngine.Liquid)
- {
- projectProcessors.Add(new LiquidProjectProcessor());
- }
+ if (language.Value() == Language.CSharp)
+ {
+ projectProcessors.Add(new CSharpProjectProcessor());
- var isSingleFileOutput = !string.IsNullOrEmpty(singleOutputFile);
- var localizableStrings = new LocalizableStringCollection();
- foreach (var projectFile in projectFiles)
- {
- var projectPath = Path.GetDirectoryName(projectFile);
- var projectBasePath = Path.GetDirectoryName(projectPath) + Path.DirectorySeparatorChar;
- var projectRelativePath = projectPath[projectBasePath.Length..];
- var rootedProject = projectPath[(projectPath.IndexOf(inputPath) + inputPath.Length + 1)..];
- if (IgnoredProject.ToList().Any(p => rootedProject.StartsWith(p)))
+ projectFiles.AddRange(Directory
+ .EnumerateFiles(inputPath.Value, $"*{ProjectExtension.CS}", SearchOption.AllDirectories)
+ .OrderBy(f => f));
+ }
+ else
{
- continue;
+ projectProcessors.Add(new VisualBasicProjectProcessor());
+
+ projectFiles.AddRange(Directory
+ .EnumerateFiles(inputPath.Value, $"*{ProjectExtension.VB}", SearchOption.AllDirectories)
+ .OrderBy(f => f));
}
- foreach (var projectProcessor in projectProcessors)
+ if (template.Value() == TemplateEngine.Both)
{
- projectProcessor.Process(projectPath, projectBasePath, localizableStrings);
+ projectProcessors.Add(new RazorProjectProcessor());
+ projectProcessors.Add(new LiquidProjectProcessor());
}
-
- if (!isSingleFileOutput)
+ else if (template.Value() == TemplateEngine.Razor)
{
- if (localizableStrings.Values.Any())
- {
- var potPath = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(projectFile) + PoWriter.PortaleObjectTemplateExtension);
-
- Directory.CreateDirectory(Path.GetDirectoryName(potPath));
-
- using var potFile = new PoWriter(potPath);
- potFile.WriteRecord(localizableStrings.Values);
- }
-
- Console.WriteLine($"{Path.GetFileName(projectPath)}: Found {localizableStrings.Values.Count()} strings.");
- localizableStrings.Clear();
+ projectProcessors.Add(new RazorProjectProcessor());
}
- }
-
- if (isSingleFileOutput)
- {
- if (localizableStrings.Values.Any())
+ else if (template.Value() == TemplateEngine.Liquid)
{
- var potPath = Path.Combine(outputPath, singleOutputFile);
-
- Directory.CreateDirectory(Path.GetDirectoryName(potPath));
-
- using var potFile = new PoWriter(potPath);
- potFile.WriteRecord(localizableStrings.Values);
+ projectProcessors.Add(new LiquidProjectProcessor());
}
- Console.WriteLine($"Found {localizableStrings.Values.Count()} strings.");
- }
- }
-
- private static (string language, string templateEngine, string singleOutputFile) GetCliOptions(string[] args)
- {
- var language = _defaultLanguage;
- var templateEngine = _defaultTemplateEngine;
- string singleOutputFile = null;
- for (int i = 4; i <= args.Length; i += 2)
- {
- switch (args[i - 2])
+ var isSingleFileOutput = !string.IsNullOrEmpty(single.Value());
+ var localizableStrings = new LocalizableStringCollection();
+ foreach (var projectFile in projectFiles)
{
- case "-l":
- case "--language":
- if (args[i - 1].Equals(Language.CSharp, StringComparison.CurrentCultureIgnoreCase))
- {
- language = Language.CSharp;
- }
- else if (args[i - 1].Equals(Language.VisualBasic, StringComparison.CurrentCultureIgnoreCase))
- {
- language = Language.VisualBasic;
- }
- else
- {
- language = null;
- }
+ var projectPath = Path.GetDirectoryName(projectFile);
+ var projectBasePath = Path.GetDirectoryName(projectPath) + Path.DirectorySeparatorChar;
+ var projectRelativePath = projectPath[projectBasePath.Length..];
+ var rootedProject = projectPath[(projectPath.IndexOf(inputPath.Value) + inputPath.Value.Length + 1)..];
+ if (IgnoredProject.ToList().Any(p => rootedProject.StartsWith(p)))
+ {
+ continue;
+ }
- break;
- case "-t":
- case "--template":
- if (args[i - 1].Equals(TemplateEngine.Razor, StringComparison.CurrentCultureIgnoreCase))
- {
- templateEngine = TemplateEngine.Razor;
- }
- else if (args[i - 1].Equals(TemplateEngine.Liquid, StringComparison.CurrentCultureIgnoreCase))
- {
- templateEngine = TemplateEngine.Liquid;
- }
- else
- {
- templateEngine = null;
- }
+ foreach (var projectProcessor in projectProcessors)
+ {
+ projectProcessor.Process(projectPath, projectBasePath, localizableStrings);
+ }
- break;
- case "-i":
- case "--ignore":
- if (!string.IsNullOrEmpty(args[i - 1]))
+ if (isSingleFileOutput)
+ {
+ if (localizableStrings.Values.Any())
{
- var ignoredProjects = args[i - 1].Split(',', StringSplitOptions.RemoveEmptyEntries);
+ var potPath = Path.Combine(outputPath.Value, Path.GetFileNameWithoutExtension(projectFile) + PoWriter.PortaleObjectTemplateExtension);
- foreach (var ignoredProject in ignoredProjects)
- {
- IgnoredProject.Add(ignoredProject);
- }
+ Directory.CreateDirectory(Path.GetDirectoryName(potPath));
+
+ using var potFile = new PoWriter(potPath);
+ potFile.WriteRecord(localizableStrings.Values);
}
- break;
- case "--localizer":
- if (!string.IsNullOrEmpty(args[i - 1]))
- {
- var localizerIdentifiers = args[i - 1].Split(',', StringSplitOptions.RemoveEmptyEntries);
+ Console.WriteLine($"{Path.GetFileName(projectPath)}: Found {localizableStrings.Values.Count()} strings.");
+ localizableStrings.Clear();
+ }
+ }
- LocalizerAccessors.LocalizerIdentifiers = localizerIdentifiers;
- }
+ if (isSingleFileOutput)
+ {
+ if (localizableStrings.Values.Any())
+ {
+ var potPath = Path.Combine(outputPath.Value, single.Value());
- break;
- case "-s":
- case "--single":
- if (!string.IsNullOrEmpty(args[i - 1]))
- {
- singleOutputFile = args[i - 1];
- }
+ Directory.CreateDirectory(Path.GetDirectoryName(potPath));
- break;
- default:
- language = null;
- templateEngine = null;
- break;
- }
- }
+ using var potFile = new PoWriter(potPath);
+ potFile.WriteRecord(localizableStrings.Values);
+ }
- return (language, templateEngine, singleOutputFile);
- }
+ Console.WriteLine($"Found {localizableStrings.Values.Count()} strings.");
+ }
+ });
- private static void ShowHelp()
- {
- Console.WriteLine("Usage:");
- Console.WriteLine(" extractpo [options]");
- Console.WriteLine();
- Console.WriteLine("Arguments:");
- Console.WriteLine(" The path to the input directory, all projects at the the path will be processed.");
- Console.WriteLine(" The path to a directory where POT files will be generated.");
- Console.WriteLine();
- Console.WriteLine("Options:");
- Console.WriteLine(" -l, --language Specifies the code language to extracts translatable strings from.");
- Console.WriteLine(" Default: C# language");
- Console.WriteLine(" -t, --template Specifies the template engine to extract the translatable strings from.");
- Console.WriteLine(" Default: Razor & Liquid templates");
- Console.WriteLine(" -i, --ignore project1,project2 Ignores extracting PO filed from a given project(s).");
- Console.WriteLine(" --localizer localizer1,localizer2 Specifies the name of the localizer(s) that will be used during the extraction process.");
- Console.WriteLine(" -s, --single Specifies the single output file.");
+ app.Execute(args);
}
}