-
-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding support for editorconfig #927
Changes from all commits
df793ca
e942cbf
cb6cb93
cfa0916
06b885c
24a5876
b8d3ad2
a0c5c5d
b40eef5
cdc5048
e615840
df6b76b
2ae81a9
7e2191b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# running this seems to screw up the nuget restore, but provides a way to figure out why a test is failing on linux while working on windows. | ||
# you have to run this from the root, IE powershell ./Scripts/RunLinuxTests.ps1 | ||
# also a lot of these tests fail due to line endings in your local files being \r\n but the writeLine using \n | ||
docker run --rm -v ${pwd}:/app -e "NormalizeLineEndings=1" -w /app/tests mcr.microsoft.com/dotnet/sdk:6.0 dotnet test /app/Src/CSharpier.Tests/CSharpier.Tests.csproj --logger:trx | ||
docker run --rm -v ${pwd}:/app -e "NormalizeLineEndings=1" -w /app/tests mcr.microsoft.com/dotnet/sdk:7.0 dotnet test /app/Src/CSharpier.Tests/CSharpier.Tests.csproj --logger:trx | ||
|
||
# gross way to run csharpier against the csharpier-repos | ||
#docker run --rm -v ${pwd}:/app -e "NormalizeLineEndings=1" -w /app mcr.microsoft.com/dotnet/sdk:5.0 dotnet ./csharpier/Src/CSharpier/bin/Debug/net6.0/dotnet-csharpier.dll csharpier-repos --skip-write |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace CSharpier.Cli.EditorConfig; | ||
|
||
internal class ConfigFile | ||
{ | ||
public required IReadOnlyCollection<Section> Sections { get; init; } | ||
public bool IsRoot { get; init; } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System.IO.Abstractions; | ||
using IniParser; | ||
|
||
namespace CSharpier.Cli.EditorConfig; | ||
|
||
internal static class ConfigFileParser | ||
{ | ||
public static ConfigFile Parse(string filePath, IFileSystem fileSystem) | ||
{ | ||
var parser = new FileIniDataParser(); | ||
|
||
using var stream = fileSystem.File.OpenRead(filePath); | ||
using var streamReader = new StreamReader(stream); | ||
var configData = parser.ReadData(streamReader); | ||
|
||
var directory = fileSystem.Path.GetDirectoryName(filePath); | ||
var sections = new List<Section>(); | ||
foreach (var section in configData.Sections) | ||
{ | ||
sections.Add(new Section(section, directory)); | ||
} | ||
return new ConfigFile { IsRoot = configData.Global["root"] == "true", Sections = sections }; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System.IO.Abstractions; | ||
|
||
namespace CSharpier.Cli.EditorConfig; | ||
|
||
internal static class EditorConfigParser | ||
{ | ||
/// <summary>Finds all configs above the given directory as well as within the subtree of this directory</summary> | ||
public static List<EditorConfigSections> FindForDirectoryName( | ||
string directoryName, | ||
IFileSystem fileSystem | ||
) | ||
{ | ||
if (directoryName is "") | ||
{ | ||
return new List<EditorConfigSections>(); | ||
} | ||
|
||
var directoryInfo = fileSystem.DirectoryInfo.FromDirectoryName(directoryName); | ||
var editorConfigFiles = directoryInfo | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thus only a part of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comes from the doc on AnalyzerConfig, so I don't think there need to be any changes to support it.
I wasn't aware of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It sounds like usage of There is an easy workaround of just adding an |
||
.EnumerateFiles(".editorconfig", SearchOption.AllDirectories) | ||
.ToList(); | ||
|
||
// already found any in this directory above | ||
directoryInfo = directoryInfo.Parent; | ||
|
||
while (directoryInfo is not null) | ||
{ | ||
var file = fileSystem.FileInfo.FromFileName( | ||
fileSystem.Path.Combine(directoryInfo.FullName, ".editorconfig") | ||
); | ||
if (file.Exists) | ||
{ | ||
editorConfigFiles.Add(file); | ||
} | ||
|
||
directoryInfo = directoryInfo.Parent; | ||
} | ||
|
||
return editorConfigFiles | ||
.Select( | ||
o => | ||
new EditorConfigSections | ||
{ | ||
DirectoryName = fileSystem.Path.GetDirectoryName(o.FullName), | ||
SectionsIncludingParentFiles = FindSections(o.FullName, fileSystem) | ||
} | ||
) | ||
.OrderByDescending(o => o.DirectoryName.Length) | ||
.ToList(); | ||
} | ||
|
||
private static List<Section> FindSections(string filePath, IFileSystem fileSystem) | ||
{ | ||
return ParseConfigFiles(fileSystem.Path.GetDirectoryName(filePath), fileSystem) | ||
.Reverse() | ||
.SelectMany(configFile => configFile.Sections) | ||
.ToList(); | ||
belav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private static IEnumerable<ConfigFile> ParseConfigFiles( | ||
string directoryPath, | ||
IFileSystem fileSystem | ||
) | ||
{ | ||
var directory = fileSystem.DirectoryInfo.FromDirectoryName(directoryPath); | ||
while (directory != null) | ||
{ | ||
var potentialPath = fileSystem.Path.Combine(directory.FullName, ".editorconfig"); | ||
if (fileSystem.File.Exists(potentialPath)) | ||
{ | ||
var configFile = ConfigFileParser.Parse(potentialPath, fileSystem); | ||
|
||
DebugLogger.Log(potentialPath); | ||
yield return configFile; | ||
if (configFile.IsRoot) | ||
{ | ||
yield break; | ||
} | ||
} | ||
|
||
directory = directory.Parent; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I feel like this whole loop could have been a
.Select()
.