From 99c7727ee19b6b07299806b318b9dd15b0305317 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Mon, 9 Jan 2023 12:29:18 -0600 Subject: [PATCH] WIP #777 --- Src/CSharpier.Cli.Tests/CliTests.cs | 17 +++++++++++++ Src/CSharpier.Cli/CommandLineFormatter.cs | 11 ++++---- Src/CSharpier.Cli/CommandLineOptions.cs | 9 +++++++ Src/CSharpier.Cli/ConfigurationFileOptions.cs | 25 ++++++++++++++++++- Src/CSharpier.Cli/Program.cs | 12 ++++++--- 5 files changed, 64 insertions(+), 10 deletions(-) diff --git a/Src/CSharpier.Cli.Tests/CliTests.cs b/Src/CSharpier.Cli.Tests/CliTests.cs index 1c87d4bdf..74c745131 100644 --- a/Src/CSharpier.Cli.Tests/CliTests.cs +++ b/Src/CSharpier.Cli.Tests/CliTests.cs @@ -98,6 +98,23 @@ public async Task Should_Respect_Ignore_File_With_Subdirectory_When_DirectorOrFi result.Should().Be(unformattedContent, $"The file at {filePath} should have been ignored"); } + [Test] + public async Task Should_Support_Config_Path() + { + const string fileContent = "var myVariable = someLongValue;"; + var fileName = "TooWide.cs"; + await this.WriteFileAsync(fileName, fileContent); + await this.WriteFileAsync("config/.csharpierrc", "printWidth: 10"); + + await new CsharpierProcess() + .WithArguments("--config config/.csharpierrc . ") + .ExecuteAsync(); + + var result = await this.ReadAllTextAsync(fileName); + + result.Should().Be("var myVariable =\n someLongValue;\n"); + } + [Test] public async Task Should_Return_Error_When_No_DirectoryOrFile_And_Not_Piping_StdIn() { diff --git a/Src/CSharpier.Cli/CommandLineFormatter.cs b/Src/CSharpier.Cli/CommandLineFormatter.cs index 4c4b8efe4..5dcb75288 100644 --- a/Src/CSharpier.Cli/CommandLineFormatter.cs +++ b/Src/CSharpier.Cli/CommandLineFormatter.cs @@ -31,6 +31,7 @@ CancellationToken cancellationToken var (ignoreFile, printerOptions) = await GetIgnoreFileAndPrinterOptions( filePath, + commandLineOptions.ConfigPath, fileSystem, logger, cancellationToken @@ -131,6 +132,7 @@ CancellationToken cancellationToken var (ignoreFile, printerOptions) = await GetIgnoreFileAndPrinterOptions( directoryOrFile, + commandLineOptions.ConfigPath, fileSystem, logger, cancellationToken @@ -225,6 +227,7 @@ await FormatPhysicalFile( private static async Task<(IgnoreFile, PrinterOptions)> GetIgnoreFileAndPrinterOptions( string directoryOrFile, + string? configPath, IFileSystem fileSystem, ILogger logger, CancellationToken cancellationToken @@ -243,11 +246,9 @@ CancellationToken cancellationToken cancellationToken ); - var printerOptions = ConfigurationFileOptions.CreatePrinterOptions( - baseDirectoryPath, - fileSystem, - logger - ); + var printerOptions = configPath is null + ? ConfigurationFileOptions.FindPrinterOptionsForDirectory(baseDirectoryPath, fileSystem, logger) + : ConfigurationFileOptions.CreatePrinterOptionsFromPath(configPath, fileSystem, logger); return (ignoreFile, printerOptions); } diff --git a/Src/CSharpier.Cli/CommandLineOptions.cs b/Src/CSharpier.Cli/CommandLineOptions.cs index 22d3867bb..b9bbccbb7 100644 --- a/Src/CSharpier.Cli/CommandLineOptions.cs +++ b/Src/CSharpier.Cli/CommandLineOptions.cs @@ -12,6 +12,7 @@ public class CommandLineOptions public bool PipeMultipleFiles { get; init; } public bool NoCache { get; init; } public string? StandardInFileContents { get; init; } + public string? ConfigPath { get; init; } public string[] OriginalDirectoryOrFilePaths { get; init; } = Array.Empty(); internal delegate Task Handler( @@ -22,6 +23,7 @@ internal delegate Task Handler( bool writeStdout, bool pipeMultipleFiles, bool noCache, + string? config, CancellationToken cancellationToken ); @@ -58,6 +60,13 @@ public static RootCommand Create() new Option( new[] { "--pipe-multiple-files" }, "Keep csharpier running so that multiples files can be piped to it via stdin" + ), + // TODO this doesn't seem to get passed yet? + // TODO update the docs + // TODO maybe create tickets for having the plugins make use of this by storing it + new Option( + new[] { "--config-path" }, + "Path to the CSharpier configuration file" ) }; diff --git a/Src/CSharpier.Cli/ConfigurationFileOptions.cs b/Src/CSharpier.Cli/ConfigurationFileOptions.cs index 08b410024..a1f31d740 100644 --- a/Src/CSharpier.Cli/ConfigurationFileOptions.cs +++ b/Src/CSharpier.Cli/ConfigurationFileOptions.cs @@ -15,7 +15,7 @@ public class ConfigurationFileOptions private static readonly string[] validExtensions = { ".csharpierrc", ".json", ".yml", ".yaml" }; - internal static PrinterOptions CreatePrinterOptions( + internal static PrinterOptions FindPrinterOptionsForDirectory( string baseDirectoryPath, IFileSystem fileSystem, ILogger logger @@ -25,6 +25,29 @@ ILogger logger var configurationFileOptions = Create(baseDirectoryPath, fileSystem, logger); + return ConvertToPrinterOptions(configurationFileOptions); + } + + internal static PrinterOptions CreatePrinterOptionsFromPath( + string configPath, + IFileSystem fileSystem, + ILogger logger + ) + { + // TODO this should load the file directly, instead of trying to find it in the directory + var configurationFileOptions = Create( + Path.GetDirectoryName(configPath), + fileSystem, + logger + ); + + return ConvertToPrinterOptions(configurationFileOptions); + } + + private static PrinterOptions ConvertToPrinterOptions( + ConfigurationFileOptions configurationFileOptions + ) + { List preprocessorSymbolSets; if (configurationFileOptions.PreprocessorSymbolSets == null) { diff --git a/Src/CSharpier.Cli/Program.cs b/Src/CSharpier.Cli/Program.cs index eca051c2d..b36e6457a 100644 --- a/Src/CSharpier.Cli/Program.cs +++ b/Src/CSharpier.Cli/Program.cs @@ -26,6 +26,7 @@ public static async Task Run( bool writeStdout, bool pipeMultipleFiles, bool noCache, + string configPath, CancellationToken cancellationToken ) { @@ -35,10 +36,10 @@ CancellationToken cancellationToken if (pipeMultipleFiles) { - return await PipeMultipleFiles(console, logger, cancellationToken); + return await PipeMultipleFiles(console, logger, configPath, cancellationToken); } - var directoryOrFileNotProvided = (directoryOrFile is null or { Length: 0 }); + var directoryOrFileNotProvided = directoryOrFile is null or { Length: 0 }; var originalDirectoryOrFile = directoryOrFile; string? standardInFileContents = null; @@ -75,7 +76,8 @@ CancellationToken cancellationToken NoCache = noCache, Fast = fast, SkipWrite = skipWrite, - WriteStdout = writeStdout || standardInFileContents != null + WriteStdout = writeStdout || standardInFileContents != null, + ConfigPath = configPath }; return await CommandLineFormatter.Format( @@ -90,6 +92,7 @@ CancellationToken cancellationToken private static async Task PipeMultipleFiles( SystemConsole console, ILogger logger, + string? configPath, CancellationToken cancellationToken ) { @@ -146,7 +149,8 @@ CancellationToken cancellationToken }, StandardInFileContents = stringBuilder.ToString(), Fast = true, - WriteStdout = true + WriteStdout = true, + ConfigPath = configPath }; try