Skip to content

Commit

Permalink
Add possibility to execute commands without cloned MS Learn repository
Browse files Browse the repository at this point in the history
  • Loading branch information
FrediKats committed May 18, 2024
1 parent af9b5a9 commit dff6e83
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="roslyn-rules.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="..\.editorconfig" CopyToOutputDirectory="Always" Condition="$(Configuration) == Debug" />
</ItemGroup>

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@ public sealed class Settings : CommandSettings
public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings)
{
settings.EditorConfigPath.ThrowIfNull();
settings.MsLearnRepositoryPath.ThrowIfNull();

EditorConfigAnalyzer editorConfigAnalyzer = new EditorConfigAnalyzer();
IEditorConfigAnalyzeReporter reporter = new EditorConfigAnalyzeLogReporter(logger);

RoslynRules roslynRules = settings.MsLearnRepositoryPath is null
? RoslynRuleDocumentationCache.ReadFromCache()
: roslynRuleDocumentationParser.Parse(settings.MsLearnRepositoryPath);

string editorConfigContent = File.ReadAllText(settings.EditorConfigPath);
EditorConfigDocument editorConfigDocument = editorConfigDocumentParser.Parse(editorConfigContent);
DotnetConfigSettings dotnetConfigSettings = dotnetConfigSettingsParser.Parse(editorConfigDocument);
RoslynRules roslynRules = roslynRuleDocumentationParser.Parse(settings.MsLearnRepositoryPath);

EditorConfigMissedConfiguration editorConfigMissedConfiguration = editorConfigAnalyzer.GetMissedConfigurations(dotnetConfigSettings, roslynRules);
IReadOnlyCollection<EditorConfigInvalidOptionValue> incorrectOptionValues = editorConfigAnalyzer.GetIncorrectOptionValues(dotnetConfigSettings, roslynRules);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ public sealed class Settings : CommandSettings
public override int Execute(CommandContext context, Settings settings)
{
settings.EditorConfigPath.ThrowIfNull();
settings.MsLearnRepositoryPath.ThrowIfNull();

string[] editorConfigContentLines = File.ReadAllLines(settings.EditorConfigPath);
RoslynRules roslynRules = roslynRuleDocumentationParser.Parse(settings.MsLearnRepositoryPath);
RoslynRules roslynRules = settings.MsLearnRepositoryPath is null
? RoslynRuleDocumentationCache.ReadFromCache()
: roslynRuleDocumentationParser.Parse(settings.MsLearnRepositoryPath);

EditorConfigDocument editorConfigDocument = editorConfigDocumentParser.Parse(editorConfigContentLines);
EditorConfigDocument formattedDocument = editorConfigFormatter.FormatAccordingToRuleDefinitions(editorConfigDocument, roslynRules, settings.GroupQualityRulesByCategory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ public sealed class Settings : CommandSettings
public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings)
{
settings.EditorConfigPath.ThrowIfNull();
settings.MsLearnRepositoryPath.ThrowIfNull();
settings.OutputPath.ThrowIfNull();

string editorConfigContent = File.ReadAllText(settings.EditorConfigPath);
EditorConfigDocument editorConfigDocument = documentParser.Parse(editorConfigContent);
DotnetConfigSettings dotnetConfigSettings = dotnetConfigSettingsParser.Parse(editorConfigDocument);

RoslynRules roslynRules = roslynRuleDocumentationParser.Parse(settings.MsLearnRepositoryPath);
RoslynRules roslynRules = settings.MsLearnRepositoryPath is null
? RoslynRuleDocumentationCache.ReadFromCache()
: roslynRuleDocumentationParser.Parse(settings.MsLearnRepositoryPath);

CodeStyle codeStyle = codeStyleGenerator.Generate(dotnetConfigSettings, roslynRules);
codeStyleWriter.Write(settings.OutputPath, codeStyle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ public sealed class Settings : CommandSettings
public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings)
{
settings.EditorConfigPath.ThrowIfNull();
settings.MsLearnRepositoryPath.ThrowIfNull();

RoslynRules roslynRules = roslynRuleDocumentationParser.Parse(settings.MsLearnRepositoryPath);
RoslynRules roslynRules = settings.MsLearnRepositoryPath is null
? RoslynRuleDocumentationCache.ReadFromCache()
: roslynRuleDocumentationParser.Parse(settings.MsLearnRepositoryPath);

string editorconfigContent = editorConfigTemplateGenerator.GenerateTemplate(roslynRules);
// TODO: move to interface?
logger.LogInformation("Writing .editorconfig template to {path}", settings.EditorConfigPath);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Kysect.CommonLib.BaseTypes.Extensions;
using Kysect.Configuin.Learn.Abstraction;
using Kysect.Configuin.RoslynModels;
using Spectre.Console.Cli;
using System.ComponentModel;
using System.Text.Json;

namespace Kysect.Configuin.Console.Commands;

public class GenerateRoslynRuleDocumentationFile(
IRoslynRuleDocumentationParser roslynRuleDocumentationParser
) : Command<GenerateRoslynRuleDocumentationFile.Settings>
{
public sealed class Settings : CommandSettings
{
[Description("Path to cloned MS Learn repository.")]
[CommandArgument(0, "[ms-repo-path]")]
public string? MsLearnRepositoryPath { get; init; }

[Description("Output path.")]
[CommandArgument(0, "[output-path]")]
public string? OutputPath { get; init; }
}

public override int Execute(CommandContext context, Settings settings)
{
settings.ThrowIfNull();
settings.MsLearnRepositoryPath.ThrowIfNull();
settings.OutputPath.ThrowIfNull();

RoslynRules roslynRules = roslynRuleDocumentationParser.Parse(settings.MsLearnRepositoryPath);
string documentation = JsonSerializer.Serialize(roslynRules);
File.WriteAllText(settings.OutputPath, documentation);
return 0;
}
}
4 changes: 3 additions & 1 deletion Sources/Kysect.Configuin.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static void Main(string[] args)
config.AddCommand<AnalyzeEditorConfigCommand>("analyze");
config.AddCommand<GenerateEditorConfigTemplateTemplate>("template");
config.AddCommand<FormatEditorconfigCommand>("format");
config.AddCommand<GenerateRoslynRuleDocumentationFile>("generate-roslyn-documentation");
});

app.Run(args);
Expand All @@ -37,7 +38,8 @@ private static string[] PrepareTestCommand()
string[] analyzeCommand = new[] { "analyze", ".editorconfig", "-d", msLearnRepositoryPath };
string[] templateGenerateCommand = new[] { "template", ".editorconfig", "-d", msLearnRepositoryPath };
string[] formatCommand = new[] { "format", ".editorconfig", "-d", msLearnRepositoryPath };
string[] generateRoslynDocumentationCommand = new[] { "generate-roslyn-documentation", msLearnRepositoryPath, "roslyn-rules.json" };

return formatCommand;
return generateRoslynDocumentationCommand;
}
}
21 changes: 21 additions & 0 deletions Sources/Kysect.Configuin.Console/RoslynRuleDocumentationCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Kysect.CommonLib.BaseTypes.Extensions;
using Kysect.Configuin.Common;
using Kysect.Configuin.RoslynModels;
using System.Text.Json;

namespace Kysect.Configuin.Console;

public static class RoslynRuleDocumentationCache
{
public static RoslynRules ReadFromCache()
{
var fileName = "roslyn-rules.json";
if (!File.Exists(fileName))
{
throw new ConfiguinException($"File with {fileName} was not found");
}

string readAllText = File.ReadAllText(fileName);
return JsonSerializer.Deserialize<RoslynRules>(readAllText).ThrowIfNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public EditorConfigMissedConfiguration GetMissedConfigurations(DotnetConfigSetti
.ToHashSet();

var missedStyleRules = roslynRules
.StyleRules
.GetStyleRules()
.Where(rule => !selectedSeverity.Contains(rule.RuleId))
.Select(r => r.RuleId)
.Order()
Expand Down Expand Up @@ -74,7 +74,7 @@ public IReadOnlyCollection<RoslynRuleId> GetIncorrectOptionSeverity(DotnetConfig
ArgumentNullException.ThrowIfNull(roslynRules);

var ruleIds = new HashSet<RoslynRuleId>();
ruleIds.AddEach(roslynRules.StyleRules.Select(r => r.RuleId));
ruleIds.AddEach(roslynRules.GetStyleRules().Select(r => r.RuleId));
ruleIds.AddEach(roslynRules.QualityRules.Select(r => r.RuleId));

var result = new List<RoslynRuleId>();
Expand Down
2 changes: 1 addition & 1 deletion Sources/Kysect.Configuin.Learn/LearnDocumentationParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public RoslynRules Parse(string learnRepositoryPath)
private List<RoslynStyleRuleGroup> AddNamingRule(List<RoslynStyleRuleGroup> roslynStyleRules)
{
var namingRule = new RoslynStyleRule(RoslynNameRuleInfo.RuleId, "Code-style naming rules", "Style");
var roslynStyleRuleGroup = new RoslynStyleRuleGroup(namingRule, string.Empty, null);
var roslynStyleRuleGroup = new RoslynStyleRuleGroup([namingRule], [], string.Empty, null);

roslynStyleRules.Add(roslynStyleRuleGroup);
return roslynStyleRules;
Expand Down
17 changes: 5 additions & 12 deletions Sources/Kysect.Configuin.RoslynModels/RoslynRules.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
namespace Kysect.Configuin.RoslynModels;

public class RoslynRules
public record RoslynRules(
IReadOnlyCollection<RoslynQualityRule> QualityRules,
IReadOnlyCollection<RoslynStyleRuleGroup> StyleRuleGroups)
{
public IReadOnlyCollection<RoslynQualityRule> QualityRules { get; }
public IReadOnlyCollection<RoslynStyleRule> StyleRules { get; }
public IReadOnlyCollection<RoslynStyleRuleGroup> StyleRuleGroups { get; }

public RoslynRules(
IReadOnlyCollection<RoslynQualityRule> qualityRules,
IReadOnlyCollection<RoslynStyleRuleGroup> styleRuleGroups)
public IReadOnlyCollection<RoslynStyleRule> GetStyleRules()
{
QualityRules = qualityRules;
StyleRuleGroups = styleRuleGroups;

StyleRules = styleRuleGroups.SelectMany(r => r.Rules).ToList();
return StyleRuleGroups.SelectMany(r => r.Rules).ToList();
}

public IReadOnlyCollection<RoslynStyleRuleOption> GetOptions()
Expand Down
20 changes: 1 addition & 19 deletions Sources/Kysect.Configuin.RoslynModels/RoslynStyleRuleGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,4 @@ public record RoslynStyleRuleGroup(
IReadOnlyCollection<RoslynStyleRule> Rules,
IReadOnlyCollection<RoslynStyleRuleOption> Options,
string Overview,
string? Example)
{
public RoslynStyleRuleGroup(
RoslynStyleRule rule,
string Overview,
string? Example)
: this(new[] { rule }, Array.Empty<RoslynStyleRuleOption>(), Overview, Example)
{
}

public RoslynStyleRuleGroup(
RoslynStyleRule rule,
IReadOnlyCollection<RoslynStyleRuleOption> Options,
string Overview,
string? Example)
: this(new[] { rule }, Options, Overview, Example)
{
}
}
string? Example);
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class C
Category: "Style");

return new RoslynStyleRuleGroup(
rule,
[rule],
[],
Overview: overview,
Example: example);
}
Expand Down Expand Up @@ -114,7 +115,7 @@ class MyClass
"Add accessibility modifiers",
"Style");

return new RoslynStyleRuleGroup(styleRule, new[] { expectedOption }, overview, Example: null);
return new RoslynStyleRuleGroup([styleRule], [expectedOption], overview, Example: null);
}

public static RoslynQualityRule CA1064()
Expand Down

0 comments on commit dff6e83

Please sign in to comment.