Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Sources/Kysect.Configuin.CodeStyleDoc/CodeStyleGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public CodeStyleGenerator(ILogger logger)
_logger = logger;
}

public CodeStyle Generate(EditorConfigSettings editorConfigSettings, RoslynRules roslynRules)
public CodeStyle Generate(DotnetConfigSettings dotnetConfigSettings, RoslynRules roslynRules)
{
ArgumentNullException.ThrowIfNull(editorConfigSettings);
ArgumentNullException.ThrowIfNull(dotnetConfigSettings);
ArgumentNullException.ThrowIfNull(roslynRules);

_logger.LogInformation("Start code style generating.");

IReadOnlyCollection<RoslynStyleRuleOption> roslynRuleOptions = roslynRules.GetOptions();
IReadOnlyCollection<IEditorConfigSetting> notProcessedSettings = editorConfigSettings.Settings;
IReadOnlyCollection<IEditorConfigSetting> notProcessedSettings = dotnetConfigSettings.Settings;

_logger.LogInformation("Try parse {count} settings", notProcessedSettings.Count);
notProcessedSettings = notProcessedSettings.Where(IsSupported).ToList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ namespace Kysect.Configuin.CodeStyleDoc;

public interface ICodeStyleGenerator
{
CodeStyle Generate(EditorConfigSettings editorConfigSettings, RoslynRules roslynRules);
CodeStyle Generate(DotnetConfigSettings dotnetConfigSettings, RoslynRules roslynRules);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace Kysect.Configuin.ConfigurationRoot;

// TODO: add tests for configuration
public static class DependencyBuilder
{
public static IServiceCollection InitializeServiceProvider()
Expand All @@ -31,8 +32,7 @@ public static IServiceCollection InitializeServiceProvider()
serviceCollection.AddOptionsWithValidation<EditorConfigApplyConfiguration>(nameof(EditorConfigApplyConfiguration));
serviceCollection.AddSingleton(CreateLogger);

serviceCollection.AddSingleton<IEditorConfigContentProvider, EditorConfigFileContentProvider>();
serviceCollection.AddSingleton<IEditorConfigSettingsParser, EditorConfigSettingsParser>();
serviceCollection.AddSingleton<IDotnetConfigSettingsParser, DotnetConfigSettingsParser>();
serviceCollection.AddSingleton<IMsLearnDocumentationInfoReader, MsLearnDocumentationInfoLocalReader>();

serviceCollection.AddSingleton<IMsLearnDocumentationParser>(sp =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Kysect.CommonLib.BaseTypes.Extensions;
using Kysect.Configuin.EditorConfig;
using Kysect.Configuin.EditorConfig.Analyzing;
using Kysect.Configuin.EditorConfig.DocumentModel;
using Kysect.Configuin.EditorConfig.DocumentModel.Nodes;
using Kysect.Configuin.MsLearn;
using Kysect.Configuin.MsLearn.Models;
using Kysect.Configuin.RoslynModels;
Expand All @@ -11,7 +13,13 @@

namespace Kysect.Configuin.Console.Commands;

internal sealed class AnalyzeEditorConfigCommand : Command<AnalyzeEditorConfigCommand.Settings>
internal sealed class AnalyzeEditorConfigCommand(
IDotnetConfigSettingsParser dotnetConfigSettingsParser,
IMsLearnDocumentationInfoReader msLearnDocumentationInfoReader,
IMsLearnDocumentationParser msLearnDocumentationParser,
ILogger logger,
EditorConfigDocumentParser editorConfigDocumentParser)
: Command<AnalyzeEditorConfigCommand.Settings>
{
public sealed class Settings : CommandSettings
{
Expand All @@ -24,37 +32,23 @@ public sealed class Settings : CommandSettings
public string? MsLearnRepositoryPath { get; init; }
}

private readonly IEditorConfigContentProvider _editorConfigContentProvider;
private readonly IEditorConfigSettingsParser _editorConfigSettingsParser;
private readonly IMsLearnDocumentationInfoReader _msLearnDocumentationInfoReader;
private readonly IMsLearnDocumentationParser _msLearnDocumentationParser;
private readonly ILogger _logger;

public AnalyzeEditorConfigCommand(IEditorConfigContentProvider editorConfigContentProvider, IEditorConfigSettingsParser editorConfigSettingsParser, IMsLearnDocumentationInfoReader msLearnDocumentationInfoReader, IMsLearnDocumentationParser msLearnDocumentationParser, ILogger logger)
{
_editorConfigContentProvider = editorConfigContentProvider;
_editorConfigSettingsParser = editorConfigSettingsParser;
_msLearnDocumentationInfoReader = msLearnDocumentationInfoReader;
_msLearnDocumentationParser = msLearnDocumentationParser;
_logger = logger;
}

public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings)
{
settings.EditorConfigPath.ThrowIfNull();
settings.MsLearnRepositoryPath.ThrowIfNull();

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

string editorConfigContent = _editorConfigContentProvider.Provide(settings.EditorConfigPath);
EditorConfigSettings editorConfigSettings = _editorConfigSettingsParser.Parse(editorConfigContent);
MsLearnDocumentationRawInfo msLearnDocumentationRawInfo = _msLearnDocumentationInfoReader.Provide(settings.MsLearnRepositoryPath);
RoslynRules roslynRules = _msLearnDocumentationParser.Parse(msLearnDocumentationRawInfo);
string editorConfigContent = File.ReadAllText(settings.EditorConfigPath);
EditorConfigDocument editorConfigDocument = editorConfigDocumentParser.Parse(editorConfigContent);
DotnetConfigSettings dotnetConfigSettings = dotnetConfigSettingsParser.Parse(editorConfigDocument);
MsLearnDocumentationRawInfo msLearnDocumentationRawInfo = msLearnDocumentationInfoReader.Provide(settings.MsLearnRepositoryPath);
RoslynRules roslynRules = msLearnDocumentationParser.Parse(msLearnDocumentationRawInfo);

EditorConfigMissedConfiguration editorConfigMissedConfiguration = editorConfigAnalyzer.GetMissedConfigurations(editorConfigSettings, roslynRules);
IReadOnlyCollection<EditorConfigInvalidOptionValue> incorrectOptionValues = editorConfigAnalyzer.GetIncorrectOptionValues(editorConfigSettings, roslynRules);
IReadOnlyCollection<RoslynRuleId> incorrectOptionSeverity = editorConfigAnalyzer.GetIncorrectOptionSeverity(editorConfigSettings, roslynRules);
EditorConfigMissedConfiguration editorConfigMissedConfiguration = editorConfigAnalyzer.GetMissedConfigurations(dotnetConfigSettings, roslynRules);
IReadOnlyCollection<EditorConfigInvalidOptionValue> incorrectOptionValues = editorConfigAnalyzer.GetIncorrectOptionValues(dotnetConfigSettings, roslynRules);
IReadOnlyCollection<RoslynRuleId> incorrectOptionSeverity = editorConfigAnalyzer.GetIncorrectOptionSeverity(dotnetConfigSettings, roslynRules);

reporter.ReportMissedConfigurations(editorConfigMissedConfiguration);
reporter.ReportIncorrectOptionValues(incorrectOptionValues);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Kysect.Configuin.CodeStyleDoc;
using Kysect.Configuin.CodeStyleDoc.Models;
using Kysect.Configuin.EditorConfig;
using Kysect.Configuin.EditorConfig.DocumentModel;
using Kysect.Configuin.EditorConfig.DocumentModel.Nodes;
using Kysect.Configuin.MsLearn;
using Kysect.Configuin.MsLearn.Models;
using Kysect.Configuin.RoslynModels;
Expand All @@ -11,7 +13,14 @@

namespace Kysect.Configuin.Console.Commands;

internal sealed class GenerateCodeStyleDocCommand : Command<GenerateCodeStyleDocCommand.Settings>
internal sealed class GenerateCodeStyleDocCommand(
IDotnetConfigSettingsParser dotnetConfigSettingsParser,
IMsLearnDocumentationInfoReader msLearnDocumentationInfoReader,
IMsLearnDocumentationParser msLearnDocumentationParser,
ICodeStyleGenerator codeStyleGenerator,
ICodeStyleWriter codeStyleWriter,
EditorConfigDocumentParser documentParser)
: Command<GenerateCodeStyleDocCommand.Settings>
{
public sealed class Settings : CommandSettings
{
Expand All @@ -28,43 +37,21 @@ public sealed class Settings : CommandSettings
public string? MsLearnRepositoryPath { get; init; }
}

private readonly IEditorConfigContentProvider _editorConfigContentProvider;
private readonly IEditorConfigSettingsParser _editorConfigSettingsParser;
private readonly IMsLearnDocumentationInfoReader _msLearnDocumentationInfoReader;
private readonly IMsLearnDocumentationParser _msLearnDocumentationParser;
private readonly ICodeStyleGenerator _codeStyleGenerator;
private readonly ICodeStyleWriter _codeStyleWriter;

public GenerateCodeStyleDocCommand(
IEditorConfigContentProvider editorConfigContentProvider,
IEditorConfigSettingsParser editorConfigSettingsParser,
IMsLearnDocumentationInfoReader msLearnDocumentationInfoReader,
IMsLearnDocumentationParser msLearnDocumentationParser,
ICodeStyleGenerator codeStyleGenerator,
ICodeStyleWriter codeStyleWriter)
{
_editorConfigContentProvider = editorConfigContentProvider;
_editorConfigSettingsParser = editorConfigSettingsParser;
_msLearnDocumentationInfoReader = msLearnDocumentationInfoReader;
_msLearnDocumentationParser = msLearnDocumentationParser;
_codeStyleGenerator = codeStyleGenerator;
_codeStyleWriter = codeStyleWriter;
}

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

string editorConfigContent = _editorConfigContentProvider.Provide(settings.EditorConfigPath);
EditorConfigSettings editorConfigSettings = _editorConfigSettingsParser.Parse(editorConfigContent);
string editorConfigContent = File.ReadAllText(settings.EditorConfigPath);
EditorConfigDocument editorConfigDocument = documentParser.Parse(editorConfigContent);
DotnetConfigSettings dotnetConfigSettings = dotnetConfigSettingsParser.Parse(editorConfigDocument);

MsLearnDocumentationRawInfo msLearnDocumentationRawInfo = _msLearnDocumentationInfoReader.Provide(settings.MsLearnRepositoryPath);
RoslynRules roslynRules = _msLearnDocumentationParser.Parse(msLearnDocumentationRawInfo);
MsLearnDocumentationRawInfo msLearnDocumentationRawInfo = msLearnDocumentationInfoReader.Provide(settings.MsLearnRepositoryPath);
RoslynRules roslynRules = msLearnDocumentationParser.Parse(msLearnDocumentationRawInfo);

CodeStyle codeStyle = _codeStyleGenerator.Generate(editorConfigSettings, roslynRules);
_codeStyleWriter.Write(settings.OutputPath, codeStyle);
CodeStyle codeStyle = codeStyleGenerator.Generate(dotnetConfigSettings, roslynRules);
codeStyleWriter.Write(settings.OutputPath, codeStyle);

return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ namespace Kysect.Configuin.EditorConfig.Analyzing;

public class EditorConfigAnalyzer
{
public EditorConfigMissedConfiguration GetMissedConfigurations(EditorConfigSettings editorConfigSettings, RoslynRules roslynRules)
public EditorConfigMissedConfiguration GetMissedConfigurations(DotnetConfigSettings dotnetConfigSettings, RoslynRules roslynRules)
{
editorConfigSettings.ThrowIfNull();
dotnetConfigSettings.ThrowIfNull();
roslynRules.ThrowIfNull();

var selectedSeverity = editorConfigSettings
var selectedSeverity = dotnetConfigSettings
.Settings
.OfType<RoslynSeverityEditorConfigSetting>()
.Select(c => c.RuleId)
.ToHashSet();

var selectedOptions = editorConfigSettings
var selectedOptions = dotnetConfigSettings
.Settings
.OfType<RoslynOptionEditorConfigSetting>()
.Select(c => c.Key)
Expand Down Expand Up @@ -47,16 +47,16 @@ public EditorConfigMissedConfiguration GetMissedConfigurations(EditorConfigSetti
return new EditorConfigMissedConfiguration(missedStyleRules, missedQualityRules, missedOptions);
}

public IReadOnlyCollection<EditorConfigInvalidOptionValue> GetIncorrectOptionValues(EditorConfigSettings editorConfigSettings, RoslynRules roslynRules)
public IReadOnlyCollection<EditorConfigInvalidOptionValue> GetIncorrectOptionValues(DotnetConfigSettings dotnetConfigSettings, RoslynRules roslynRules)
{
ArgumentNullException.ThrowIfNull(editorConfigSettings);
ArgumentNullException.ThrowIfNull(dotnetConfigSettings);
ArgumentNullException.ThrowIfNull(roslynRules);

var result = new List<EditorConfigInvalidOptionValue>();

var optionAvailableValues = roslynRules.GetOptions().ToDictionary(o => o.Name, o => o.Values);

foreach ((string key, string value) in editorConfigSettings.Settings.OfType<RoslynOptionEditorConfigSetting>())
foreach ((string key, string value) in dotnetConfigSettings.Settings.OfType<RoslynOptionEditorConfigSetting>())
{
if (!optionAvailableValues.TryGetValue(key, out IReadOnlyCollection<RoslynStyleRuleOptionValue>? values))
values = Array.Empty<RoslynStyleRuleOptionValue>();
Expand All @@ -68,17 +68,17 @@ public IReadOnlyCollection<EditorConfigInvalidOptionValue> GetIncorrectOptionVal
return result;
}

public IReadOnlyCollection<RoslynRuleId> GetIncorrectOptionSeverity(EditorConfigSettings editorConfigSettings, RoslynRules roslynRules)
public IReadOnlyCollection<RoslynRuleId> GetIncorrectOptionSeverity(DotnetConfigSettings dotnetConfigSettings, RoslynRules roslynRules)
{
ArgumentNullException.ThrowIfNull(editorConfigSettings);
ArgumentNullException.ThrowIfNull(dotnetConfigSettings);
ArgumentNullException.ThrowIfNull(roslynRules);

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

var result = new List<RoslynRuleId>();
foreach ((RoslynRuleId roslynRuleId, RoslynRuleSeverity _) in editorConfigSettings.Settings.OfType<RoslynSeverityEditorConfigSetting>())
foreach ((RoslynRuleId roslynRuleId, RoslynRuleSeverity _) in dotnetConfigSettings.Settings.OfType<RoslynSeverityEditorConfigSetting>())
{
if (!ruleIds.Contains(roslynRuleId))
result.Add(roslynRuleId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Kysect.Configuin.EditorConfig.Diff;

public class EditorConfigSettingsComparator
{
public EditorConfigSettingsDiff Compare(EditorConfigSettings left, EditorConfigSettings right)
public EditorConfigSettingsDiff Compare(DotnetConfigSettings left, DotnetConfigSettings right)
{
ArgumentNullException.ThrowIfNull(left);
ArgumentNullException.ThrowIfNull(right);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using Kysect.CommonLib.BaseTypes.Extensions;
using Kysect.Configuin.EditorConfig.DocumentModel.Nodes;
using System.Collections.Immutable;

namespace Kysect.Configuin.EditorConfig.DocumentModel;

public static class EditorConfigDocumentNodeRewriter
{
public static EditorConfigDocument RemoveNodes(this EditorConfigDocument document, IReadOnlyCollection<IEditorConfigNode> nodes)
{
return (EditorConfigDocument) document.FilterChildren(nodes);
}

public static EditorConfigDocument ReplaceNodes(this EditorConfigDocument document, IEditorConfigNode oldNode, IEditorConfigNode newNode)
{
return (EditorConfigDocument) document.ReplaceChildren(oldNode, newNode);
}

public static EditorConfigDocument UpdateNodes(this EditorConfigDocument document, Func<IEditorConfigNode, IEditorConfigNode> morphimsm)
{
document.ThrowIfNull();
morphimsm.ThrowIfNull();

return (EditorConfigDocument) document.UpdateChildren(morphimsm);
}


private static IEditorConfigNode FilterChildren(this IEditorConfigNode node, IReadOnlyCollection<IEditorConfigNode> nodes)
{
if (node is not IEditorConfigContainerNode containerNode)
return node;

ImmutableList<IEditorConfigNode> newChildren = [];
foreach (IEditorConfigNode editorConfigNode in containerNode.Children)
{
if (nodes.Contains(editorConfigNode))
continue;

newChildren = newChildren.Add(editorConfigNode.FilterChildren(nodes));
}

return containerNode.WithChildren(newChildren);
}

private static IEditorConfigNode ReplaceChildren(this IEditorConfigNode currentNode, IEditorConfigNode oldNode, IEditorConfigNode newNode)
{
if (currentNode == oldNode)
return newNode;

if (currentNode is not IEditorConfigContainerNode containerNode)
return currentNode;

ImmutableList<IEditorConfigNode> newChildren = [];
foreach (IEditorConfigNode editorConfigNode in containerNode.Children)
newChildren = newChildren.Add(editorConfigNode.ReplaceChildren(oldNode, newNode));

return containerNode.WithChildren(newChildren);
}

private static IEditorConfigNode UpdateChildren(this IEditorConfigNode currentNode, Func<IEditorConfigNode, IEditorConfigNode> morphimsm)
{
currentNode = morphimsm(currentNode);

if (currentNode is not IEditorConfigContainerNode containerNode)
return currentNode;

ImmutableList<IEditorConfigNode> newChildren = containerNode
.Children
.Select(morphimsm)
.ToImmutableList();
return containerNode.WithChildren(newChildren);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ IEditorConfigContainerNode IEditorConfigContainerNode.AddChild(IEditorConfigNode
return AddChild(child);
}

public IEditorConfigContainerNode WithChildren(ImmutableList<IEditorConfigNode> children)
{
return this with { Children = children };
}

public EditorConfigCategoryNode AddChild(IEditorConfigNode child)
{
return this with { Children = Children.Add(child) };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ namespace Kysect.Configuin.EditorConfig.DocumentModel.Nodes;

public record EditorConfigDocument(ImmutableList<IEditorConfigNode> Children, ImmutableList<string> TrailingTrivia) : IEditorConfigContainerNode
{
public EditorConfigDocument() : this([])
{
}

public EditorConfigDocument(ImmutableList<IEditorConfigNode> children) : this(children, ImmutableList<string>.Empty)
{
}
Expand All @@ -14,6 +18,11 @@ IEditorConfigContainerNode IEditorConfigContainerNode.AddChild(IEditorConfigNode
return AddChild(child);
}

public IEditorConfigContainerNode WithChildren(ImmutableList<IEditorConfigNode> children)
{
return this with { Children = children };
}

public EditorConfigDocument AddChild(IEditorConfigNode child)
{
return this with { Children = Children.Add(child) };
Expand Down
Loading