From 6adbba446ab57ad475d1d1b60abfc38bd3c95264 Mon Sep 17 00:00:00 2001 From: Bruno Massa Date: Fri, 8 Mar 2024 19:27:14 -0500 Subject: [PATCH 1/2] feat: build is default command and source is also an argument --- .../Models/CommandLineOptions/BuildOptions.cs | 2 +- .../CommandLineOptions/GenerateOptions.cs | 9 +++++- .../CommandLineOptions/IGenerateOptions.cs | 10 +++++++ source/Program.cs | 28 +++++++------------ 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/source/Models/CommandLineOptions/BuildOptions.cs b/source/Models/CommandLineOptions/BuildOptions.cs index 9c51c14..5bf4b46 100644 --- a/source/Models/CommandLineOptions/BuildOptions.cs +++ b/source/Models/CommandLineOptions/BuildOptions.cs @@ -5,7 +5,7 @@ namespace SuCoS.Models.CommandLineOptions; /// /// Command line options for the build command. /// -[Verb("build", HelpText = "Builds the site")] +[Verb("build", true, HelpText = "Builds the site")] public class BuildOptions : GenerateOptions { /// diff --git a/source/Models/CommandLineOptions/GenerateOptions.cs b/source/Models/CommandLineOptions/GenerateOptions.cs index 704c3ac..8bb12bb 100644 --- a/source/Models/CommandLineOptions/GenerateOptions.cs +++ b/source/Models/CommandLineOptions/GenerateOptions.cs @@ -11,9 +11,16 @@ public class GenerateOptions : IGenerateOptions [Option('v', "verbose", Required = false, HelpText = "How verbose it must be")] public bool Verbose { get; init; } + /// + public string Source => string.IsNullOrEmpty(SourceOption) ? SourceArgument : SourceOption; + + /// + [Value(0)] + public required string SourceArgument { private get; init; } = string.Empty; + /// [Option('s', "source", Required = false, HelpText = "Source directory path")] - public required string Source { get; init; } = "."; + public required string SourceOption { private get; init; } = string.Empty; /// [Option('d', "draft", Required = false, HelpText = "Include draft content")] diff --git a/source/Models/CommandLineOptions/IGenerateOptions.cs b/source/Models/CommandLineOptions/IGenerateOptions.cs index 6aa93e0..ddac589 100644 --- a/source/Models/CommandLineOptions/IGenerateOptions.cs +++ b/source/Models/CommandLineOptions/IGenerateOptions.cs @@ -15,6 +15,16 @@ public interface IGenerateOptions /// string Source { get; } + /// + /// The path of the source files + /// + string SourceArgument { init; } + + /// + /// The path of the source files, as --source commandline option + /// + string SourceOption { init; } + /// /// Include draft content /// diff --git a/source/Program.cs b/source/Program.cs index 7464da1..b71b277 100644 --- a/source/Program.cs +++ b/source/Program.cs @@ -28,14 +28,6 @@ public class Program(ILogger logger) \/_____/\/___/ \/___/ \/___/ \/_____/ "; - private ILogger Logger { get; set; } = logger; - private static readonly string[] aliases = ["--source", "-s"]; - private static readonly string[] aliasesArray = ["--draft", "-d"]; - private static readonly string[] aliasesArray0 = ["--future", "-f"]; - private static readonly string[] aliasesArray1 = ["--expired", "-e"]; - private static readonly string[] aliasesArray2 = ["--verbose", "-v"]; - private static readonly string[] aliasesArray3 = ["--output", "-o"]; - /// /// Entry point of the program /// @@ -43,7 +35,7 @@ public class Program(ILogger logger) /// public static async Task Main(string[] args) { - var program = new Program(CreateLogger()); + var program = new Program(Createlogger()); return await program.RunCommandLine(args); } @@ -57,7 +49,7 @@ public class Program(ILogger logger) return await CommandLine.Parser.Default.ParseArguments(args) .WithParsed(options => { - Logger = CreateLogger(options.Verbose); + logger = Createlogger(options.Verbose); }) .WithParsed(options => { @@ -68,11 +60,11 @@ public class Program(ILogger logger) { try { - _ = new BuildCommand(options, Logger); + _ = new BuildCommand(options, logger); } catch (Exception ex) { - Logger.Error($"Build failed: {ex.Message}"); + logger.Error($"Build failed: {ex.Message}"); return Task.FromResult(1); } return Task.FromResult(0); @@ -81,7 +73,7 @@ public class Program(ILogger logger) { try { - var serveCommand = new ServeCommand(options, Logger, new SourceFileWatcher()); + var serveCommand = new ServeCommand(options, logger, new SourceFileWatcher()); serveCommand.StartServer(); await Task.Delay(-1).ConfigureAwait(false); // Wait forever. } @@ -89,11 +81,11 @@ public class Program(ILogger logger) { if (options.Verbose) { - Logger.Error(ex, "Serving failed"); + logger.Error(ex, "Serving failed"); } else { - Logger.Error($"Serving failed: {ex.Message}"); + logger.Error($"Serving failed: {ex.Message}"); } return 1; } @@ -108,7 +100,7 @@ public class Program(ILogger logger) /// /// /// - public static ILogger CreateLogger(bool verbose = false) + public static ILogger Createlogger(bool verbose = false) { return new LoggerConfiguration() .MinimumLevel.Is(verbose ? LogEventLevel.Debug : LogEventLevel.Information) @@ -126,7 +118,7 @@ public class Program(ILogger logger) var assemblyName = assembly?.GetName(); var appName = assemblyName?.Name; var appVersion = assemblyName?.Version; - Logger.Information("{name} v{version}", appName, appVersion); + logger.Information("{name} v{version}", appName, appVersion); } /// @@ -134,6 +126,6 @@ public class Program(ILogger logger) /// public void OutputLogo() { - Logger.Information(helloWorld); + logger.Information(helloWorld); } } -- GitLab From 9c8c2bf367f8a300e5333b8ac4251d9299121d52 Mon Sep 17 00:00:00 2001 From: Bruno Massa Date: Fri, 8 Mar 2024 19:47:46 -0500 Subject: [PATCH 2/2] fix: tests now use SourceArgument --- .../CommandLineOptions/GenerateOptions.cs | 4 +-- source/Program.cs | 17 +++++------- test/BaseGeneratorCommandTests.cs | 2 +- test/Models/SiteTests.cs | 26 +++++++++---------- test/ProgramTest.cs | 2 +- .../RegisteredPageRequestHandlerTests.cs | 4 +-- 6 files changed, 26 insertions(+), 29 deletions(-) diff --git a/source/Models/CommandLineOptions/GenerateOptions.cs b/source/Models/CommandLineOptions/GenerateOptions.cs index 8bb12bb..d7c2263 100644 --- a/source/Models/CommandLineOptions/GenerateOptions.cs +++ b/source/Models/CommandLineOptions/GenerateOptions.cs @@ -16,11 +16,11 @@ public class GenerateOptions : IGenerateOptions /// [Value(0)] - public required string SourceArgument { private get; init; } = string.Empty; + public string SourceArgument { private get; init; } = "./"; /// [Option('s', "source", Required = false, HelpText = "Source directory path")] - public required string SourceOption { private get; init; } = string.Empty; + public string SourceOption { private get; init; } = string.Empty; /// [Option('d', "draft", Required = false, HelpText = "Include draft content")] diff --git a/source/Program.cs b/source/Program.cs index b71b277..df5e474 100644 --- a/source/Program.cs +++ b/source/Program.cs @@ -35,7 +35,7 @@ public class Program(ILogger logger) /// public static async Task Main(string[] args) { - var program = new Program(Createlogger()); + var program = new Program(CreateLogger()); return await program.RunCommandLine(args); } @@ -49,7 +49,7 @@ public class Program(ILogger logger) return await CommandLine.Parser.Default.ParseArguments(args) .WithParsed(options => { - logger = Createlogger(options.Verbose); + logger = CreateLogger(options.Verbose); }) .WithParsed(options => { @@ -100,14 +100,11 @@ public class Program(ILogger logger) /// /// /// - public static ILogger Createlogger(bool verbose = false) - { - return new LoggerConfiguration() - .MinimumLevel.Is(verbose ? LogEventLevel.Debug : LogEventLevel.Information) - // .WriteTo.Async(a => a.Console(formatProvider: System.Globalization.CultureInfo.CurrentCulture)) - .WriteTo.Console(formatProvider: System.Globalization.CultureInfo.CurrentCulture) - .CreateLogger(); - } + public static ILogger CreateLogger(bool verbose = false) => new LoggerConfiguration() + .MinimumLevel.Is(verbose ? LogEventLevel.Debug : LogEventLevel.Information) + // .WriteTo.Async(a => a.Console(formatProvider: System.Globalization.CultureInfo.CurrentCulture)) + .WriteTo.Console(formatProvider: System.Globalization.CultureInfo.CurrentCulture) + .CreateLogger(); /// /// Print the name and version of the program. diff --git a/test/BaseGeneratorCommandTests.cs b/test/BaseGeneratorCommandTests.cs index e4f4681..ee68c57 100644 --- a/test/BaseGeneratorCommandTests.cs +++ b/test/BaseGeneratorCommandTests.cs @@ -10,7 +10,7 @@ public class BaseGeneratorCommandTests { private static readonly IGenerateOptions testOptions = new GenerateOptions { - Source = "test_source" + SourceArgument = "test_source" }; private static readonly ILogger testLogger = new LoggerConfiguration().CreateLogger(); diff --git a/test/Models/SiteTests.cs b/test/Models/SiteTests.cs index e328aba..123d348 100644 --- a/test/Models/SiteTests.cs +++ b/test/Models/SiteTests.cs @@ -18,7 +18,7 @@ public class SiteTests : TestSetup var siteFullPath = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST01)); site.Options = new GenerateOptions { - Source = siteFullPath + SourceArgument = siteFullPath }; // Act @@ -36,7 +36,7 @@ public class SiteTests : TestSetup { GenerateOptions options = new() { - Source = Path.GetFullPath(Path.Combine(testSitesPath, sitePath)) + SourceArgument = Path.GetFullPath(Path.Combine(testSitesPath, sitePath)) }; site.Options = options; @@ -57,7 +57,7 @@ public class SiteTests : TestSetup { GenerateOptions options = new() { - Source = Path.GetFullPath(Path.Combine(testSitesPath, sitePath)) + SourceArgument = Path.GetFullPath(Path.Combine(testSitesPath, sitePath)) }; site.Options = options; @@ -77,7 +77,7 @@ public class SiteTests : TestSetup { GenerateOptions options = new() { - Source = Path.GetFullPath(Path.Combine(testSitesPath, sitePath)) + SourceArgument = Path.GetFullPath(Path.Combine(testSitesPath, sitePath)) }; site.Options = options; @@ -97,7 +97,7 @@ public class SiteTests : TestSetup { GenerateOptions options = new() { - Source = Path.GetFullPath(Path.Combine(testSitesPath, sitePath)) + SourceArgument = Path.GetFullPath(Path.Combine(testSitesPath, sitePath)) }; site.Options = options; @@ -113,7 +113,7 @@ public class SiteTests : TestSetup { GenerateOptions options = new() { - Source = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST03)) + SourceArgument = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST03)) }; site.Options = options; @@ -130,7 +130,7 @@ public class SiteTests : TestSetup { GenerateOptions options = new() { - Source = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST01)) + SourceArgument = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST01)) }; site.Options = options; @@ -147,7 +147,7 @@ public class SiteTests : TestSetup { GenerateOptions options = new() { - Source = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST04)) + SourceArgument = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST04)) }; site.Options = options; @@ -170,7 +170,7 @@ public class SiteTests : TestSetup { GenerateOptions options = new() { - Source = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST04)) + SourceArgument = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST04)) }; site.Options = options; @@ -195,7 +195,7 @@ public class SiteTests : TestSetup { GenerateOptions options = new() { - Source = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST04)) + SourceArgument = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST04)) }; site.Options = options; @@ -230,7 +230,7 @@ public class SiteTests : TestSetup { GenerateOptions options = new() { - Source = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST05)) + SourceArgument = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST05)) }; site.Options = options; @@ -256,7 +256,7 @@ public class SiteTests : TestSetup { GenerateOptions options = new() { - Source = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST07)) + SourceArgument = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST07)) }; site.Options = options; @@ -296,7 +296,7 @@ public class SiteTests : TestSetup { GenerateOptions options = new() { - Source = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST06)) + SourceArgument = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST06)) }; site.Options = options; diff --git a/test/ProgramTest.cs b/test/ProgramTest.cs index 285f83a..ae18ada 100644 --- a/test/ProgramTest.cs +++ b/test/ProgramTest.cs @@ -14,7 +14,7 @@ public class ProgramTests : TestSetup { // Act var logger = Program.CreateLogger(verbose); - + // Assert Assert.True(logger.IsEnabled(expected)); } diff --git a/test/ServerHandlers/RegisteredPageRequestHandlerTests.cs b/test/ServerHandlers/RegisteredPageRequestHandlerTests.cs index 21fbf14..e1b4fc8 100644 --- a/test/ServerHandlers/RegisteredPageRequestHandlerTests.cs +++ b/test/ServerHandlers/RegisteredPageRequestHandlerTests.cs @@ -16,7 +16,7 @@ public class RegisteredPageRequestHandlerTests : TestSetup var siteFullPath = Path.GetFullPath(Path.Combine(testSitesPath, testSitePathCONST06)); site.Options = new GenerateOptions { - Source = siteFullPath + SourceArgument = siteFullPath }; var registeredPageRequest = new RegisteredPageRequest(site); @@ -36,7 +36,7 @@ public class RegisteredPageRequestHandlerTests : TestSetup var siteFullPath = Path.GetFullPath(Path.Combine(testSitesPath, testSitePath)); site.Options = new GenerateOptions { - Source = siteFullPath + SourceArgument = siteFullPath }; var registeredPageRequest = new RegisteredPageRequest(site); -- GitLab