Skip to content

Commit

Permalink
Add CLI command for formatting editorconfig file
Browse files Browse the repository at this point in the history
  • Loading branch information
FrediKats committed May 17, 2024
1 parent e8551a2 commit aff0231
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 46 deletions.

This file was deleted.

This file was deleted.

10 changes: 5 additions & 5 deletions Sources/Kysect.Configuin.ConfigurationRoot/DependencyBuilder.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -28,8 +28,6 @@ public static IServiceCollection InitializeServiceProvider()

IServiceCollection serviceCollection = builder.Services;

serviceCollection.AddOptionsWithValidation<ConfiguinConfiguration>(nameof(ConfiguinConfiguration));
serviceCollection.AddOptionsWithValidation<EditorConfigApplyConfiguration>(nameof(EditorConfigApplyConfiguration));
serviceCollection.AddSingleton(CreateLogger);

serviceCollection.AddSingleton<IDotnetConfigSettingsParser, DotnetConfigSettingsParser>();
Expand All @@ -48,6 +46,8 @@ public static IServiceCollection InitializeServiceProvider()
serviceCollection.AddSingleton<DotnetFormatReportComparator>();
serviceCollection.AddSingleton<DotnetFormatPreviewGenerator>();
serviceCollection.AddSingleton<EditorConfigTemplateGenerator>();
serviceCollection.AddSingleton<EditorConfigDocumentParser>();
serviceCollection.AddSingleton<EditorConfigFormatter>();

return serviceCollection;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<PackageReference Include="codeessentials.Extensions.Logging.Demystifier" />
<PackageReference Include="Kysect.CommonLib" />
Expand Down Expand Up @@ -26,6 +26,7 @@
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="..\.editorconfig" CopyToOutputDirectory="Always" Condition="$(Configuration) == Debug" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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<FormatEditorconfigCommand.Settings>
{
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;
}
}
17 changes: 8 additions & 9 deletions Sources/Kysect.Configuin.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -27,19 +25,20 @@ public static void Main(string[] args)
config.AddCommand<EditorConfigApplyPreviewCommand>("preview");
config.AddCommand<AnalyzeEditorConfigCommand>("analyze");
config.AddCommand<GenerateEditorConfigTemplateTemplate>("template");
config.AddCommand<FormatEditorconfigCommand>("format");
});

app.Run(args);
}

private static string[] PrepareTestCommand(IServiceCollection registrations)
private static string[] PrepareTestCommand()
{
ServiceProvider serviceProvider = registrations.BuildServiceProvider();
ConfiguinConfiguration configurationOption = serviceProvider.GetRequiredService<IOptions<ConfiguinConfiguration>>().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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> currentTrivia = new List<string>();

foreach (string line in lines)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit aff0231

Please sign in to comment.