Skip to content

Commit

Permalink
Drastically speed up formatting when ignore or config file exist. (#597)
Browse files Browse the repository at this point in the history
closes #594
  • Loading branch information
belav authored Feb 8, 2022
1 parent 3f252d8 commit a338a6c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 51 deletions.
112 changes: 61 additions & 51 deletions Src/CSharpier.Cli/CommandLineFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,31 @@ CancellationToken cancellationToken
{
try
{
async Task<(IgnoreFile, PrinterOptions)> GetIgnoreFileAndPrinterOptions(
string directoryOrFile
)
{
var baseDirectoryPath =
fileSystem.Path.GetExtension(directoryOrFile) == string.Empty
? directoryOrFile
: fileSystem.Path.GetDirectoryName(directoryOrFile);

var ignoreFile = await IgnoreFile.Create(
baseDirectoryPath,
fileSystem,
logger,
cancellationToken
);

var printerOptions = ConfigurationFileOptions.CreatePrinterOptions(
baseDirectoryPath,
fileSystem,
logger
);

return (ignoreFile, printerOptions);
}

var stopwatch = Stopwatch.StartNew();
var commandLineFormatterResult = new CommandLineFormatterResult();

Expand All @@ -29,30 +54,34 @@ CancellationToken cancellationToken
console.InputEncoding
);

var loggerAndOptions = await GetLoggerAndOptions(
filePath,
filePath,
fileSystem,
logger,
cancellationToken
);
var (ignoreFile, printerOptions) = await GetIgnoreFileAndPrinterOptions(filePath);

if (loggerAndOptions != null)
if (
!GeneratedCodeUtilities.IsGeneratedCodeFile(filePath)
&& !ignoreFile.IsIgnored(filePath)
)
{
var fileIssueLogger = GetFileIssueLogger(
filePath,
filePath,
fileSystem,
logger
);

await PerformFormattingSteps(
fileToFormatInfo,
new StdOutFormattedFileWriter(console),
commandLineFormatterResult,
loggerAndOptions.Value.fileIssueLogger,
loggerAndOptions.Value.printerOptions,
fileIssueLogger,
printerOptions,
commandLineOptions,
cancellationToken
);
}
}
else
{
IFormattedFileWriter? writer = null;
IFormattedFileWriter? writer;
if (commandLineOptions.WriteStdout)
{
writer = new StdOutFormattedFileWriter(console);
Expand All @@ -72,8 +101,20 @@ var directoryOrFile in commandLineOptions.DirectoryOrFilePaths.Select(
)
)
{
var (ignoreFile, printerOptions) = await GetIgnoreFileAndPrinterOptions(
directoryOrFile
);

async Task FormatFile(string filePath)
{
if (
GeneratedCodeUtilities.IsGeneratedCodeFile(filePath)
|| ignoreFile.IsIgnored(filePath)
)
{
return;
}

await FormatPhysicalFile(
filePath,
directoryOrFile,
Expand All @@ -82,6 +123,7 @@ await FormatPhysicalFile(
commandLineFormatterResult,
writer,
commandLineOptions,
printerOptions,
cancellationToken
);
}
Expand Down Expand Up @@ -161,6 +203,7 @@ private static async Task FormatPhysicalFile(
CommandLineFormatterResult commandLineFormatterResult,
IFormattedFileWriter writer,
CommandLineOptions commandLineOptions,
PrinterOptions printerOptions,
CancellationToken cancellationToken
)
{
Expand All @@ -170,42 +213,30 @@ CancellationToken cancellationToken
cancellationToken
);

var loggerAndOptions = await GetLoggerAndOptions(
directoryOrFile,
filePath,
fileSystem,
logger,
cancellationToken
);

if (loggerAndOptions == null)
{
return;
}
var fileIssueLogger = GetFileIssueLogger(directoryOrFile, filePath, fileSystem, logger);

if (!filePath.EndsWithIgnoreCase(".cs") && !filePath.EndsWithIgnoreCase(".cst"))
{
loggerAndOptions.Value.fileIssueLogger.WriteError("Is an unsupported file type.");
fileIssueLogger.WriteError("Is an unsupported file type.");
return;
}

await PerformFormattingSteps(
fileToFormatInfo,
writer,
commandLineFormatterResult,
loggerAndOptions.Value.fileIssueLogger,
loggerAndOptions.Value.printerOptions,
fileIssueLogger,
printerOptions,
commandLineOptions,
cancellationToken
);
}

private static async Task<(FileIssueLogger fileIssueLogger, PrinterOptions printerOptions)?> GetLoggerAndOptions(
private static FileIssueLogger GetFileIssueLogger(
string pathToDirectoryOrFile,
string pathToFile,
IFileSystem fileSystem,
ILogger logger,
CancellationToken cancellationToken
ILogger logger
)
{
var normalizedPath = pathToDirectoryOrFile.Replace('\\', '/');
Expand All @@ -220,33 +251,12 @@ CancellationToken cancellationToken
);
}

var printerOptions = ConfigurationFileOptions.CreatePrinterOptions(
baseDirectoryPath,
fileSystem,
logger
);

var ignoreFile = await IgnoreFile.Create(
baseDirectoryPath,
fileSystem,
logger,
cancellationToken
);

if (
GeneratedCodeUtilities.IsGeneratedCodeFile(pathToFile)
|| ignoreFile.IsIgnored(pathToFile)
)
{
return null;
}

var filePathLogger = new FileIssueLogger(
pathToFile.Replace('\\', '/')[baseDirectoryPath.Length..],
logger
);

return (filePathLogger, printerOptions);
return filePathLogger;
}

private static int ReturnExitCode(
Expand Down
2 changes: 2 additions & 0 deletions Src/CSharpier.Cli/ConfigurationFileOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ internal static PrinterOptions CreatePrinterOptions(
ILogger logger
)
{
DebugLogger.Log("Creating printer options for " + baseDirectoryPath);

var configurationFileOptions = Create(baseDirectoryPath, fileSystem, logger);

List<string[]> preprocessorSymbolSets;
Expand Down
2 changes: 2 additions & 0 deletions Src/CSharpier.Cli/IgnoreFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public static async Task<IgnoreFile> Create(
CancellationToken cancellationToken
)
{
DebugLogger.Log("Creating ignore file for " + baseDirectoryPath);

var ignore = new Ignore.Ignore();
var ignoreFilePath = FindIgnorePath(baseDirectoryPath, fileSystem);
if (ignoreFilePath == null)
Expand Down

0 comments on commit a338a6c

Please sign in to comment.