From f74d3386c76e320e25ce024e2d2eef8539a5d294 Mon Sep 17 00:00:00 2001 From: Bruno Massa Date: Wed, 10 Apr 2024 18:33:48 -0500 Subject: [PATCH 1/2] chore: move commands to a proper folder --- source/{ => Commands}/BaseGeneratorCommand.cs | 0 source/{ => Commands}/BuildCommand.cs | 0 source/{ => Commands}/CheckLinkCommand.cs | 16 ++++++++-------- source/{ => Commands}/NewSiteCommand.cs | 0 source/{ => Commands}/NewThemeCommand.cs | 0 source/{ => Commands}/ServeCommand.cs | 7 +++++-- 6 files changed, 13 insertions(+), 10 deletions(-) rename source/{ => Commands}/BaseGeneratorCommand.cs (100%) rename source/{ => Commands}/BuildCommand.cs (100%) rename source/{ => Commands}/CheckLinkCommand.cs (94%) rename source/{ => Commands}/NewSiteCommand.cs (100%) rename source/{ => Commands}/NewThemeCommand.cs (100%) rename source/{ => Commands}/ServeCommand.cs (99%) diff --git a/source/BaseGeneratorCommand.cs b/source/Commands/BaseGeneratorCommand.cs similarity index 100% rename from source/BaseGeneratorCommand.cs rename to source/Commands/BaseGeneratorCommand.cs diff --git a/source/BuildCommand.cs b/source/Commands/BuildCommand.cs similarity index 100% rename from source/BuildCommand.cs rename to source/Commands/BuildCommand.cs diff --git a/source/CheckLinkCommand.cs b/source/Commands/CheckLinkCommand.cs similarity index 94% rename from source/CheckLinkCommand.cs rename to source/Commands/CheckLinkCommand.cs index a98dc92..b8fd4c3 100644 --- a/source/CheckLinkCommand.cs +++ b/source/Commands/CheckLinkCommand.cs @@ -12,7 +12,6 @@ namespace SuCoS; /// public sealed partial class CheckLinkCommand(CheckLinkOptions settings, ILogger logger) { - [GeneratedRegex(@"https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/=]*)")] private static partial Regex URLRegex(); private static readonly Regex urlRegex = URLRegex(); @@ -40,7 +39,7 @@ public sealed partial class CheckLinkCommand(CheckLinkOptions settings, ILogger httpClient = GetHttpClient(); var files = GetFiles(directoryPath, settings.Filters); - var linksAreValid = await CheckLinks(directoryPath, files, httpClient); + var linksAreValid = await CheckLinks(directoryPath, files, httpClient).ConfigureAwait(false); if (!linksAreValid) { @@ -120,7 +119,7 @@ public sealed partial class CheckLinkCommand(CheckLinkOptions settings, ILogger var linkIsValid = false; for (var j = 0; j < retriesCount && !linkIsValid; j++) { - linkIsValid |= await CheckLink(fileNameSanitized, link, httpClient); + linkIsValid |= await CheckLink(fileNameSanitized, link, httpClient).ConfigureAwait(false); if (!linkIsValid && j < retriesCount - 1) { LogInformation("{fileName}: {link} retrying...", fileNameSanitized, link); @@ -140,7 +139,7 @@ public sealed partial class CheckLinkCommand(CheckLinkOptions settings, ILogger result &= linkIsValid; } - }); + }).ConfigureAwait(false); return result; } @@ -201,22 +200,23 @@ public sealed partial class CheckLinkCommand(CheckLinkOptions settings, ILogger return files; } - void LogInformation(string message, string fileName, string? link = null, string? arg = null) + private void LogInformation(string message, string fileName, string? link = null, string? arg = null) { - if (settings.Verbose && false) + if (settings.Verbose) { logger.Information(message, fileName, link, arg); } } - void LogError(string message, string fileName, string? link = null, string? arg = null) + private void LogError(string message, string fileName, string? link = null, string? arg = null) { if (settings.Verbose) { logger.Error(message, fileName, link, arg); } } - void LogError(string message, string fileName, string? link, HttpStatusCode arg) + + private void LogError(string message, string fileName, string? link, HttpStatusCode arg) { if (settings.Verbose) { diff --git a/source/NewSiteCommand.cs b/source/Commands/NewSiteCommand.cs similarity index 100% rename from source/NewSiteCommand.cs rename to source/Commands/NewSiteCommand.cs diff --git a/source/NewThemeCommand.cs b/source/Commands/NewThemeCommand.cs similarity index 100% rename from source/NewThemeCommand.cs rename to source/Commands/NewThemeCommand.cs diff --git a/source/ServeCommand.cs b/source/Commands/ServeCommand.cs similarity index 99% rename from source/ServeCommand.cs rename to source/Commands/ServeCommand.cs index 6d77574..9665933 100644 --- a/source/ServeCommand.cs +++ b/source/Commands/ServeCommand.cs @@ -205,7 +205,7 @@ public sealed class ServeCommand : BaseGeneratorCommand, IDisposable if (resultType is null) { resultType = "404"; - await HandleNotFoundRequest(context).ConfigureAwait(true); + await HandleNotFoundRequest(context).ConfigureAwait(false); } else { @@ -228,7 +228,10 @@ public sealed class ServeCommand : BaseGeneratorCommand, IDisposable /// The FileSystemEventArgs containing information about the file change. private void OnSourceFileChanged(object sender, FileSystemEventArgs e) { - if (e.FullPath.Contains("\\.git\\", StringComparison.InvariantCulture)) return; + if (e.FullPath.Contains("\\.git\\", StringComparison.InvariantCulture)) + { + return; + } // File changes are firing multiple events in a short time. // Debounce the event handler to prevent multiple events from firing in a short time -- GitLab From 2b15a4c8be126779d56266a928d55fc25ad49fa8 Mon Sep 17 00:00:00 2001 From: Bruno Massa Date: Wed, 10 Apr 2024 18:34:23 -0500 Subject: [PATCH 2/2] perf: ConfigureAwait(false) to allow more tasks --- source/Helpers/FileUtils.cs | 2 ++ source/Helpers/SourceFileWatcher.cs | 4 +--- source/Models/CommandLineOptions/NewOptions.cs | 11 ----------- source/Models/IFrontMatter.cs | 1 - source/Models/Page.cs | 10 ++++++++-- source/Program.cs | 4 ++-- source/ServerHandlers/RegisteredPageRequest.cs | 2 +- 7 files changed, 14 insertions(+), 20 deletions(-) delete mode 100644 source/Models/CommandLineOptions/NewOptions.cs diff --git a/source/Helpers/FileUtils.cs b/source/Helpers/FileUtils.cs index 0afcb81..fd1ae1d 100644 --- a/source/Helpers/FileUtils.cs +++ b/source/Helpers/FileUtils.cs @@ -26,7 +26,9 @@ public static class FileUtils // Check if the template content is already cached if (cache.TryGetValue(index, out var content)) + { return content; + } var templatePaths = GetTemplateLookupOrder(themePath, page, isBaseTemplate); content = GetTemplate(templatePaths); diff --git a/source/Helpers/SourceFileWatcher.cs b/source/Helpers/SourceFileWatcher.cs index 2a3bcce..2a8e215 100644 --- a/source/Helpers/SourceFileWatcher.cs +++ b/source/Helpers/SourceFileWatcher.cs @@ -1,5 +1,3 @@ -using System; - namespace SuCoS.Helpers; /// @@ -48,7 +46,7 @@ public sealed class SourceFileWatcher : IFileWatcher, IDisposable GC.SuppressFinalize(this); } - void Dispose(bool disposing) + private void Dispose(bool disposing) { if (disposing) { diff --git a/source/Models/CommandLineOptions/NewOptions.cs b/source/Models/CommandLineOptions/NewOptions.cs deleted file mode 100644 index 71d5be2..0000000 --- a/source/Models/CommandLineOptions/NewOptions.cs +++ /dev/null @@ -1,11 +0,0 @@ -using CommandLine; - -namespace SuCoS.Models.CommandLineOptions; - -/// -/// Command line options to generate a simple site from scratch. -/// -[Verb("new", false, HelpText = "Generate a simple theme from scratch")] -public class NewOptions -{ -} diff --git a/source/Models/IFrontMatter.cs b/source/Models/IFrontMatter.cs index 27d6c2a..2a6f176 100644 --- a/source/Models/IFrontMatter.cs +++ b/source/Models/IFrontMatter.cs @@ -1,4 +1,3 @@ -using System.Collections.ObjectModel; using SuCoS.Models.CommandLineOptions; namespace SuCoS.Models; diff --git a/source/Models/Page.cs b/source/Models/Page.cs index 503ef96..cec47bf 100644 --- a/source/Models/Page.cs +++ b/source/Models/Page.cs @@ -393,8 +393,14 @@ endif private void ScanForResources() { - if (string.IsNullOrEmpty(SourceFullPathDirectory)) return; - if (BundleType == BundleType.none) return; + if (string.IsNullOrEmpty(SourceFullPathDirectory)) + { + return; + } + if (BundleType == BundleType.none) + { + return; + } try { diff --git a/source/Program.cs b/source/Program.cs index 53c15ff..d99daa7 100644 --- a/source/Program.cs +++ b/source/Program.cs @@ -32,7 +32,7 @@ public class Program(ILogger logger) public static async Task Main(string[] args) { var program = new Program(CreateLogger()); - return await program.RunCommandLine(args); + return await program.RunCommandLine(args).ConfigureAwait(false); } /// @@ -112,7 +112,7 @@ public class Program(ILogger logger) return Task.FromResult(command.Run()); }, errs => Task.FromResult(0) - ); + ).ConfigureAwait(false); } /// diff --git a/source/ServerHandlers/RegisteredPageRequest.cs b/source/ServerHandlers/RegisteredPageRequest.cs index 7c285aa..12e06c7 100644 --- a/source/ServerHandlers/RegisteredPageRequest.cs +++ b/source/ServerHandlers/RegisteredPageRequest.cs @@ -8,7 +8,7 @@ namespace SuCoS.ServerHandlers; /// public class RegisteredPageRequest : IServerHandlers { - readonly ISite site; + private readonly ISite site; /// /// Constructor -- GitLab