Skip to content

Commit 6e18233

Browse files
committed
Improving performance of HasMismatchedCliAndMsBuildVersions
closes #1781
1 parent e8f5f09 commit 6e18233

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

Src/CSharpier.Cli/CommandLineFormatter.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,14 @@ await FormatPhysicalFile(
301301
}
302302
else if (isDirectory)
303303
{
304+
var stopwatch = Stopwatch.StartNew();
304305
if (
305306
!commandLineOptions.NoMSBuildCheck
306-
&& HasMismatchedCliAndMsBuildVersions.Check(
307+
&& await HasMismatchedCliAndMsBuildVersions.Check(
307308
directoryOrFilePath,
308309
fileSystem,
309-
logger
310+
logger,
311+
cancellationToken
310312
)
311313
)
312314
{

Src/CSharpier.Cli/HasMismatchedCliAndMsBuildVersions.cs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ namespace CSharpier.Cli;
77

88
internal static class HasMismatchedCliAndMsBuildVersions
99
{
10-
public static bool Check(string directoryName, IFileSystem fileSystem, ILogger logger)
10+
public static async Task<bool> Check(
11+
string directoryName,
12+
IFileSystem fileSystem,
13+
ILogger logger,
14+
CancellationToken cancellationToken
15+
)
1116
{
12-
var csProjPaths = fileSystem
13-
.Directory.EnumerateFiles(directoryName, "*.csproj", SearchOption.AllDirectories)
14-
.ToArray();
15-
1617
var versionOfDotnetTool = typeof(CommandLineFormatter)
1718
.Assembly.GetName()
1819
.Version!.ToString(3);
@@ -51,14 +52,38 @@ public static bool Check(string directoryName, IFileSystem fileSystem, ILogger l
5152
return null;
5253
}
5354

54-
foreach (var pathToCsProj in csProjPaths)
55+
IEnumerable<string> EnumerateFiles(string directory)
56+
{
57+
// using optionsProvider is slower so just hard coding the ones that could cause performance issues
58+
if (fileSystem.Path.GetDirectoryName(directory) is "node_modules" or ".git")
59+
{
60+
yield break;
61+
}
62+
63+
foreach (var file in fileSystem.Directory.EnumerateFiles(directory, "*.csproj"))
64+
{
65+
yield return file;
66+
}
67+
68+
foreach (var subDirectory in fileSystem.Directory.EnumerateDirectories(directory))
69+
{
70+
foreach (var file in EnumerateFiles(subDirectory))
71+
{
72+
yield return file;
73+
}
74+
}
75+
}
76+
77+
foreach (var pathToCsProj in EnumerateFiles(directoryName))
5578
{
5679
// this could potentially use the Microsoft.Build.Evaluation class, but that was
5780
// throwing exceptions trying to load project files with "The SDK 'Microsoft.NET.Sdk' specified could not be found."
5881
XElement csProjXElement;
5982
try
6083
{
61-
csProjXElement = XElement.Parse(fileSystem.File.ReadAllText(pathToCsProj));
84+
csProjXElement = XElement.Parse(
85+
await fileSystem.File.ReadAllTextAsync(pathToCsProj, cancellationToken)
86+
);
6287
}
6388
catch (Exception ex)
6489
{

0 commit comments

Comments
 (0)