From aff0231d7732d5d36885dd8ee2749bb292c0a551 Mon Sep 17 00:00:00 2001 From: Fredi Kats Date: Fri, 17 May 2024 10:27:03 +0200 Subject: [PATCH] Add CLI command for formatting editorconfig file --- .../Configuration/ConfiguinConfiguration.cs | 15 ------ .../EditorConfigApplyConfiguration.cs | 13 ------ .../DependencyBuilder.cs | 10 ++-- .../Kysect.Configuin.ConfigurationRoot.csproj | 3 +- .../Commands/FormatEditorconfigCommand.cs | 46 +++++++++++++++++++ Sources/Kysect.Configuin.Console/Program.cs | 17 ++++--- .../EditorConfigDocumentParser.cs | 8 +++- .../Formatter/EditorConfigFormatter.cs | 4 +- .../IDotnetConfigSettingsParser.cs | 2 + 9 files changed, 72 insertions(+), 46 deletions(-) delete mode 100644 Sources/Kysect.Configuin.ConfigurationRoot/Configuration/ConfiguinConfiguration.cs delete mode 100644 Sources/Kysect.Configuin.ConfigurationRoot/Configuration/EditorConfigApplyConfiguration.cs create mode 100644 Sources/Kysect.Configuin.Console/Commands/FormatEditorconfigCommand.cs diff --git a/Sources/Kysect.Configuin.ConfigurationRoot/Configuration/ConfiguinConfiguration.cs b/Sources/Kysect.Configuin.ConfigurationRoot/Configuration/ConfiguinConfiguration.cs deleted file mode 100644 index c439542..0000000 --- a/Sources/Kysect.Configuin.ConfigurationRoot/Configuration/ConfiguinConfiguration.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace Kysect.Configuin.ConfigurationRoot.Configuration; - -public class ConfiguinConfiguration -{ - [Required] - public string MsLearnRepositoryPath { get; init; } = null!; - - [Required] - public string EditorConfigFile { get; init; } = null!; - - [Required] - public string OutputPath { get; init; } = null!; -} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.ConfigurationRoot/Configuration/EditorConfigApplyConfiguration.cs b/Sources/Kysect.Configuin.ConfigurationRoot/Configuration/EditorConfigApplyConfiguration.cs deleted file mode 100644 index 7c78e62..0000000 --- a/Sources/Kysect.Configuin.ConfigurationRoot/Configuration/EditorConfigApplyConfiguration.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace Kysect.Configuin.ConfigurationRoot.Configuration; - -public class EditorConfigApplyConfiguration -{ - [Required] - public string SolutionPath { get; init; } = null!; - [Required] - public string SourceEditorConfig { get; init; } = null!; - [Required] - public string NewEditorConfig { get; init; } = null!; -} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.ConfigurationRoot/DependencyBuilder.cs b/Sources/Kysect.Configuin.ConfigurationRoot/DependencyBuilder.cs index 2144bb1..672c93b 100644 --- a/Sources/Kysect.Configuin.ConfigurationRoot/DependencyBuilder.cs +++ b/Sources/Kysect.Configuin.ConfigurationRoot/DependencyBuilder.cs @@ -1,11 +1,11 @@ -using Kysect.CommonLib.DependencyInjection; -using Kysect.Configuin.CodeStyleDoc; +using Kysect.Configuin.CodeStyleDoc; using Kysect.Configuin.CodeStyleDoc.Markdown; -using Kysect.Configuin.ConfigurationRoot.Configuration; using Kysect.Configuin.DotnetFormatIntegration; using Kysect.Configuin.DotnetFormatIntegration.Cli; using Kysect.Configuin.DotnetFormatIntegration.FileSystem; using Kysect.Configuin.EditorConfig; +using Kysect.Configuin.EditorConfig.DocumentModel; +using Kysect.Configuin.EditorConfig.Formatter; using Kysect.Configuin.EditorConfig.Template; using Kysect.Configuin.Markdown.TextExtractor; using Kysect.Configuin.MsLearn; @@ -28,8 +28,6 @@ public static IServiceCollection InitializeServiceProvider() IServiceCollection serviceCollection = builder.Services; - serviceCollection.AddOptionsWithValidation(nameof(ConfiguinConfiguration)); - serviceCollection.AddOptionsWithValidation(nameof(EditorConfigApplyConfiguration)); serviceCollection.AddSingleton(CreateLogger); serviceCollection.AddSingleton(); @@ -48,6 +46,8 @@ public static IServiceCollection InitializeServiceProvider() serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(); + serviceCollection.AddSingleton(); return serviceCollection; } diff --git a/Sources/Kysect.Configuin.ConfigurationRoot/Kysect.Configuin.ConfigurationRoot.csproj b/Sources/Kysect.Configuin.ConfigurationRoot/Kysect.Configuin.ConfigurationRoot.csproj index da981e0..47ebe8a 100644 --- a/Sources/Kysect.Configuin.ConfigurationRoot/Kysect.Configuin.ConfigurationRoot.csproj +++ b/Sources/Kysect.Configuin.ConfigurationRoot/Kysect.Configuin.ConfigurationRoot.csproj @@ -1,4 +1,4 @@ - + @@ -26,6 +26,7 @@ PreserveNewest + \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Console/Commands/FormatEditorconfigCommand.cs b/Sources/Kysect.Configuin.Console/Commands/FormatEditorconfigCommand.cs new file mode 100644 index 0000000..426cba8 --- /dev/null +++ b/Sources/Kysect.Configuin.Console/Commands/FormatEditorconfigCommand.cs @@ -0,0 +1,46 @@ +using Kysect.CommonLib.BaseTypes.Extensions; +using Kysect.Configuin.EditorConfig.DocumentModel; +using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; +using Kysect.Configuin.EditorConfig.Formatter; +using Kysect.Configuin.MsLearn; +using Kysect.Configuin.MsLearn.Models; +using Kysect.Configuin.RoslynModels; +using Spectre.Console.Cli; +using System.ComponentModel; + +namespace Kysect.Configuin.Console.Commands; + +internal sealed class FormatEditorconfigCommand( + IMsLearnDocumentationInfoReader repositoryPathReader, + IMsLearnDocumentationParser msLearnDocumentationParser, + EditorConfigDocumentParser editorConfigDocumentParser, + EditorConfigFormatter editorConfigFormatter) : Command +{ + public sealed class Settings : CommandSettings + { + [Description("Path to editorconfig.")] + [CommandArgument(0, "[editor config path]")] + public string? EditorConfigPath { get; init; } + + [Description("Path to cloned MS Learn repository.")] + [CommandOption("-d|--documentation")] + public string? MsLearnRepositoryPath { get; init; } + } + + public override int Execute(CommandContext context, Settings settings) + { + settings.EditorConfigPath.ThrowIfNull(); + settings.MsLearnRepositoryPath.ThrowIfNull(); + + string editorConfigContent = File.ReadAllText(settings.EditorConfigPath); + string[] editorConfigContentLines = File.ReadAllLines(settings.EditorConfigPath); + + MsLearnDocumentationRawInfo msLearnDocumentationRawInfo = repositoryPathReader.Provide(settings.MsLearnRepositoryPath); + RoslynRules roslynRules = msLearnDocumentationParser.Parse(msLearnDocumentationRawInfo); + EditorConfigDocument editorConfigDocument = editorConfigDocumentParser.Parse(editorConfigContentLines); + EditorConfigDocument formattedDocument = editorConfigFormatter.FormatAccordingToRuleDefinitions(editorConfigDocument, roslynRules); + File.WriteAllText(settings.EditorConfigPath, formattedDocument.ToFullString()); + + return 0; + } +} \ No newline at end of file diff --git a/Sources/Kysect.Configuin.Console/Program.cs b/Sources/Kysect.Configuin.Console/Program.cs index e57af06..947bc4f 100644 --- a/Sources/Kysect.Configuin.Console/Program.cs +++ b/Sources/Kysect.Configuin.Console/Program.cs @@ -1,8 +1,6 @@ using Kysect.Configuin.ConfigurationRoot; -using Kysect.Configuin.ConfigurationRoot.Configuration; using Kysect.Configuin.Console.Commands; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; using Spectre.Console.Cli; namespace Kysect.Configuin.Console; @@ -15,7 +13,7 @@ public static void Main(string[] args) IServiceCollection registrations = DependencyBuilder.InitializeServiceProvider(); if (args.Length == 0) - args = PrepareTestCommand(registrations); + args = PrepareTestCommand(); var registrar = new TypeRegistrar(registrations); var app = new CommandApp(registrar); @@ -27,19 +25,20 @@ public static void Main(string[] args) config.AddCommand("preview"); config.AddCommand("analyze"); config.AddCommand("template"); + config.AddCommand("format"); }); app.Run(args); } - private static string[] PrepareTestCommand(IServiceCollection registrations) + private static string[] PrepareTestCommand() { - ServiceProvider serviceProvider = registrations.BuildServiceProvider(); - ConfiguinConfiguration configurationOption = serviceProvider.GetRequiredService>().Value; + var msLearnRepositoryPath = Path.Combine("..", "..", "..", "..", "..", "ms-learn"); - string[] analyzeCommand = new[] { "analyze", configurationOption.EditorConfigFile, "-d", configurationOption.MsLearnRepositoryPath }; - string[] templateGenerateCommand = new[] { "template", ".editorconfig", "-d", configurationOption.MsLearnRepositoryPath }; + string[] analyzeCommand = new[] { "analyze", ".editorconfig", "-d", msLearnRepositoryPath }; + string[] templateGenerateCommand = new[] { "template", ".editorconfig", "-d", msLearnRepositoryPath }; + string[] formatCommand = new[] { "format", ".editorconfig", "-d", msLearnRepositoryPath }; - return templateGenerateCommand; + return formatCommand; } } \ No newline at end of file diff --git a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParser.cs b/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParser.cs index 53f23a0..e95617a 100644 --- a/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParser.cs +++ b/Sources/Kysect.Configuin.EditorConfig/DocumentModel/EditorConfigDocumentParser.cs @@ -9,10 +9,16 @@ public EditorConfigDocument Parse(string content) { content.ThrowIfNull(); + // KB: sometimes Environment.NewLine is not the same as in the file string[] lines = content.Split(Environment.NewLine); + return Parse(lines); + } + + public EditorConfigDocument Parse(string[] lines) + { + lines.ThrowIfNull(); var context = new EditorConfigDocumentParsingContext(); - //List currentTrivia = new List(); foreach (string line in lines) { diff --git a/Sources/Kysect.Configuin.EditorConfig/Formatter/EditorConfigFormatter.cs b/Sources/Kysect.Configuin.EditorConfig/Formatter/EditorConfigFormatter.cs index a14e1b2..4b31b46 100644 --- a/Sources/Kysect.Configuin.EditorConfig/Formatter/EditorConfigFormatter.cs +++ b/Sources/Kysect.Configuin.EditorConfig/Formatter/EditorConfigFormatter.cs @@ -9,9 +9,9 @@ namespace Kysect.Configuin.EditorConfig.Formatter; public class EditorConfigFormatter { - private readonly DotnetConfigSettingsParser _settingsParser; + private readonly IDotnetConfigSettingsParser _settingsParser; - public EditorConfigFormatter(DotnetConfigSettingsParser settingsParser) + public EditorConfigFormatter(IDotnetConfigSettingsParser settingsParser) { _settingsParser = settingsParser; } diff --git a/Sources/Kysect.Configuin.EditorConfig/IDotnetConfigSettingsParser.cs b/Sources/Kysect.Configuin.EditorConfig/IDotnetConfigSettingsParser.cs index 19aaa84..ba07fe1 100644 --- a/Sources/Kysect.Configuin.EditorConfig/IDotnetConfigSettingsParser.cs +++ b/Sources/Kysect.Configuin.EditorConfig/IDotnetConfigSettingsParser.cs @@ -1,8 +1,10 @@ using Kysect.Configuin.EditorConfig.DocumentModel.Nodes; +using Kysect.Configuin.EditorConfig.Settings; namespace Kysect.Configuin.EditorConfig; public interface IDotnetConfigSettingsParser { DotnetConfigSettings Parse(EditorConfigDocument editorConfigDocument); + IEditorConfigSetting ParseSetting(EditorConfigPropertyNode line); } \ No newline at end of file