Skip to content

Commit 8c442df

Browse files
perf: use FileSystemEnumerator
1 parent 9236689 commit 8c442df

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

Src/CSharpier.Cli/CommandLineFormatter.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Diagnostics;
22
using System.IO.Abstractions;
3+
using System.IO.Abstractions.TestingHelpers;
34
using System.Text;
45
using CSharpier.Cli.Options;
56
using CSharpier.Core;
@@ -292,12 +293,28 @@ await FormatPhysicalFile(
292293
return 1;
293294
}
294295

295-
var tasks = fileSystem
296-
.Directory.EnumerateFiles(
297-
directoryOrFilePath,
298-
"*.*",
299-
SearchOption.AllDirectories
300-
)
296+
var fileEnumerator = new ValidFilesEnumerator(
297+
directoryOrFilePath,
298+
new EnumerationOptions() { RecurseSubdirectories = true }
299+
);
300+
301+
var filePaths = new List<string>();
302+
while (fileEnumerator.MoveNext())
303+
{
304+
filePaths.Add(fileEnumerator.Current);
305+
}
306+
307+
if (fileSystem is MockFileSystem)
308+
{
309+
filePaths = fileSystem
310+
.Directory.EnumerateFiles(
311+
directoryOrFilePath,
312+
"*.*",
313+
SearchOption.AllDirectories
314+
).ToList();
315+
}
316+
317+
var tasks = filePaths
301318
.Select(o =>
302319
FormatFile(o, o.Replace(directoryOrFilePath, originalDirectoryOrFile))
303320
)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System.IO.Enumeration;
2+
3+
namespace CSharpier.Cli;
4+
5+
internal class ValidFilesEnumerator(string directory, EnumerationOptions? options = null)
6+
: FileSystemEnumerator<string>(directory, options)
7+
{
8+
protected override string TransformEntry(ref FileSystemEntry entry)
9+
{
10+
return entry.ToSpecifiedFullPath();
11+
}
12+
13+
protected override bool ShouldIncludeEntry(ref FileSystemEntry entry)
14+
{
15+
if (entry.IsDirectory)
16+
{
17+
return false;
18+
}
19+
20+
var extension = Path.GetExtension(entry.FileName);
21+
if (
22+
extension
23+
is ".cs"
24+
or ".csx"
25+
or ".config"
26+
or ".csproj"
27+
or ".props"
28+
or ".slnx"
29+
or ".targets"
30+
or ".xaml"
31+
or ".xml"
32+
)
33+
{
34+
return true;
35+
}
36+
37+
return false;
38+
}
39+
40+
protected override bool ShouldRecurseIntoEntry(ref FileSystemEntry entry)
41+
{
42+
if (entry.FileName is ".git" or "bin" or "node_modules" or "obj")
43+
{
44+
return false;
45+
}
46+
47+
return true;
48+
}
49+
}

0 commit comments

Comments
 (0)