-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for editorconfig (#927)
* Adding support for reading editorconfigs closes #630 * Some self review * Fixing some issues. Switching to strong name signed glob * Finishing up code changes * Some self review * Fixing tests when run on linux * And more issues fixed * Fixing last linux issue * Use ini-parser for editorconfig. Clean up a couple things * get these sorted --------- Co-authored-by: Lasath Fernando <devel@lasath.org>
- Loading branch information
1 parent
c1bd5db
commit e3c5fdd
Showing
24 changed files
with
1,026 additions
and
369 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
.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(); | ||
} | ||
|
||
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.