Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
belav committed Aug 16, 2024
2 parents 6719fcd + cb64721 commit 6af0e81
Show file tree
Hide file tree
Showing 29 changed files with 727 additions and 372 deletions.
25 changes: 25 additions & 0 deletions Src/CSharpier.Cli.Tests/CliTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ public async Task With_Check_Should_Write_Unformatted_File()
result.ExitCode.Should().Be(1);
}

// TODO overrides tests for piping files
[TestCase("\n")]
[TestCase("\r\n")]
public async Task Should_Format_Multiple_Piped_Files(string lineEnding)
Expand Down Expand Up @@ -312,6 +313,30 @@ public async Task Should_Support_Config_With_Multiple_Piped_Files()
result.Output.TrimEnd('\u0003').Should().Be("var myVariable =\n someLongValue;\n");
}

[Test]
public async Task Should_Support_Override_Config_With_Multiple_Piped_Files()
{
const string fileContent = "var myVariable = someLongValue;";
var fileName = Path.Combine(testFileDirectory, "TooWide.cst");
await this.WriteFileAsync(
".csharpierrc",
"""
overrides:
- files: "*.cst"
formatter: "csharp"
printWidth: 10
"""
);

var result = await new CsharpierProcess()
.WithArguments("--pipe-multiple-files")
.WithPipedInput($"{fileName}{'\u0003'}{fileContent}{'\u0003'}")
.ExecuteAsync();

result.ErrorOutput.Should().BeEmpty();
result.Output.TrimEnd('\u0003').Should().Be("var myVariable =\n someLongValue;\n");
}

[Test]
public async Task Should_Not_Fail_On_Empty_File()
{
Expand Down
84 changes: 44 additions & 40 deletions Src/CSharpier.Cli/CommandLineFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,21 @@ CancellationToken cancellationToken
);

var printerOptions = optionsProvider.GetPrinterOptionsFor(filePath);
printerOptions.IncludeGenerated = commandLineOptions.IncludeGenerated;

await PerformFormattingSteps(
fileToFormatInfo,
new StdOutFormattedFileWriter(console),
commandLineFormatterResult,
fileIssueLogger,
printerOptions,
commandLineOptions,
FormattingCacheFactory.NullCache,
cancellationToken
);
if (printerOptions is not null)
{
printerOptions.IncludeGenerated = commandLineOptions.IncludeGenerated;

await PerformFormattingSteps(
fileToFormatInfo,
new StdOutFormattedFileWriter(console),
commandLineFormatterResult,
fileIssueLogger,
printerOptions,
commandLineOptions,
FormattingCacheFactory.NullCache,
cancellationToken
);
}
}
}
else
Expand Down Expand Up @@ -193,7 +196,11 @@ CancellationToken cancellationToken
}
}

async Task FormatFile(string actualFilePath, string originalFilePath)
async Task FormatFile(
string actualFilePath,
string originalFilePath,
bool warnForUnsupported = false
)
{
if (
(
Expand All @@ -206,25 +213,33 @@ async Task FormatFile(string actualFilePath, string originalFilePath)
}

var printerOptions = optionsProvider.GetPrinterOptionsFor(actualFilePath);
printerOptions.IncludeGenerated = commandLineOptions.IncludeGenerated;

await FormatPhysicalFile(
actualFilePath,
originalFilePath,
fileSystem,
logger,
commandLineFormatterResult,
writer,
commandLineOptions,
printerOptions,
formattingCache,
cancellationToken
);
if (printerOptions is not null)
{
printerOptions.IncludeGenerated = commandLineOptions.IncludeGenerated;
await FormatPhysicalFile(
actualFilePath,
originalFilePath,
fileSystem,
logger,
commandLineFormatterResult,
writer,
commandLineOptions,
printerOptions,
formattingCache,
cancellationToken
);
}
else if (warnForUnsupported)
{
var fileIssueLogger = new FileIssueLogger(originalFilePath, logger);
fileIssueLogger.WriteWarning("Is an unsupported file type.");
}
}

if (isFile)
{
await FormatFile(directoryOrFilePath, originalDirectoryOrFile);
await FormatFile(directoryOrFilePath, originalDirectoryOrFile, true);
}
else if (isDirectory)
{
Expand All @@ -246,7 +261,6 @@ await FormatPhysicalFile(
"*.*",
SearchOption.AllDirectories
)
.Where(o => o.EndsWithIgnoreCase(".cs") || o.EndsWithIgnoreCase(".csx"))
.Select(o =>
{
var normalizedPath = o.Replace("\\", "/");
Expand Down Expand Up @@ -296,16 +310,6 @@ CancellationToken cancellationToken

var fileIssueLogger = new FileIssueLogger(originalFilePath, logger);

if (
!actualFilePath.EndsWithIgnoreCase(".cs")
&& !actualFilePath.EndsWithIgnoreCase(".cst")
&& !actualFilePath.EndsWithIgnoreCase(".csx")
)
{
fileIssueLogger.WriteWarning("Is an unsupported file type.");
return;
}

logger.LogDebug(
commandLineOptions.Check
? $"Checking - {originalFilePath}"
Expand All @@ -330,7 +334,7 @@ CommandLineFormatterResult result
)
{
if (
(commandLineOptions.StandardInFileContents != null && result.FailedCompilation > 0)
(!commandLineOptions.CompilationErrorsAsWarnings && result.FailedCompilation > 0)
|| (commandLineOptions.Check && result.UnformattedFiles > 0)
|| result.FailedSyntaxTreeValidation > 0
|| result.ExceptionsFormatting > 0
Expand Down Expand Up @@ -411,7 +415,7 @@ CancellationToken cancellationToken
errorMessage.AppendLine(message.ToString());
}

if (commandLineOptions.WriteStdout)
if (!commandLineOptions.CompilationErrorsAsWarnings)
{
fileIssueLogger.WriteError(errorMessage.ToString());
}
Expand Down
6 changes: 6 additions & 0 deletions Src/CSharpier.Cli/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal class CommandLineOptions
public bool WriteStdout { get; init; }
public bool NoCache { get; init; }
public bool NoMSBuildCheck { get; init; }
public bool CompilationErrorsAsWarnings { get; init; }
public bool IncludeGenerated { get; init; }
public string? StandardInFileContents { get; init; }
public string? ConfigPath { get; init; }
Expand All @@ -30,6 +31,7 @@ internal delegate Task<int> Handler(
bool noCache,
bool noMSBuildCheck,
bool includeGenerated,
bool compilationErrorsAsWarnings,
string config,
LogLevel logLevel,
CancellationToken cancellationToken
Expand Down Expand Up @@ -93,6 +95,10 @@ public static RootCommand Create()
new Option<string>(
new[] { "--config-path" },
"Path to the CSharpier configuration file"
),
new Option(
new[] { "--compilation-errors-as-warnings" },
"Treat compilation errors from files as warnings instead of errors."
)
};

Expand Down
15 changes: 11 additions & 4 deletions Src/CSharpier.Cli/EditorConfig/EditorConfigSections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ internal class EditorConfigSections
public required string DirectoryName { get; init; }
public required IReadOnlyCollection<Section> SectionsIncludingParentFiles { get; init; }

public PrinterOptions ConvertToPrinterOptions(string filePath)
public PrinterOptions? ConvertToPrinterOptions(string filePath)
{
if (!(filePath.EndsWith(".cs") || filePath.EndsWith(".csx")))
{
return null;
}

var sections = this.SectionsIncludingParentFiles.Where(o => o.IsMatch(filePath)).ToList();
var resolvedConfiguration = new ResolvedConfiguration(sections);
var printerOptions = new PrinterOptions();

var printerOptions = new PrinterOptions { Formatter = "csharp" };

if (resolvedConfiguration.MaxLineLength is { } maxLineLength)
{
Expand All @@ -27,11 +33,12 @@ public PrinterOptions ConvertToPrinterOptions(string filePath)

if (printerOptions.UseTabs)
{
printerOptions.TabWidth = resolvedConfiguration.TabWidth ?? printerOptions.TabWidth;
printerOptions.IndentSize = resolvedConfiguration.TabWidth ?? printerOptions.IndentSize;
}
else
{
printerOptions.TabWidth = resolvedConfiguration.IndentSize ?? printerOptions.TabWidth;
printerOptions.IndentSize =
resolvedConfiguration.IndentSize ?? printerOptions.IndentSize;
}

if (resolvedConfiguration.EndOfLine is { } endOfLine)
Expand Down
36 changes: 36 additions & 0 deletions Src/CSharpier.Cli/EditorConfig/Globber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace CSharpier.Cli.EditorConfig;

internal static class Globber
{
private static readonly GlobMatcherOptions globOptions =
new()
{
MatchBase = true,
Dot = true,
AllowWindowsPaths = true,
AllowSingleBraceSets = true,
};

public static GlobMatcher Create(string files, string directory)
{
var pattern = FixGlob(files, directory);
return GlobMatcher.Create(pattern, globOptions);
}

private static string FixGlob(string glob, string directory)
{
glob = glob.IndexOf('/') switch
{
-1 => "**/" + glob,
0 => glob[1..],
_ => glob
};
directory = directory.Replace(@"\", "/");
if (!directory.EndsWith("/"))
{
directory += "/";
}

return directory + glob;
}
}
29 changes: 1 addition & 28 deletions Src/CSharpier.Cli/EditorConfig/Section.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@ namespace CSharpier.Cli.EditorConfig;

internal class Section
{
private static readonly GlobMatcherOptions globOptions =
new()
{
MatchBase = true,
Dot = true,
AllowWindowsPaths = true,
AllowSingleBraceSets = true,
};

private readonly GlobMatcher matcher;

public string? IndentStyle { get; }
Expand All @@ -23,8 +14,7 @@ internal class Section

public Section(SectionData section, string directory)
{
var pattern = FixGlob(section.SectionName, directory);
this.matcher = GlobMatcher.Create(pattern, globOptions);
this.matcher = Globber.Create(section.SectionName, directory);
this.IndentStyle = section.Keys["indent_style"];
this.IndentSize = section.Keys["indent_size"];
this.TabWidth = section.Keys["tab_width"];
Expand All @@ -36,21 +26,4 @@ public bool IsMatch(string fileName)
{
return this.matcher.IsMatch(fileName);
}

private static string FixGlob(string glob, string directory)
{
glob = glob.IndexOf('/') switch
{
-1 => "**/" + glob,
0 => glob[1..],
_ => glob
};
directory = directory.Replace(@"\", "/");
if (!directory.EndsWith("/"))
{
directory += "/";
}

return directory + glob;
}
}
Loading

0 comments on commit 6af0e81

Please sign in to comment.