diff --git a/Src/CSharpier.Cli/CommandLineFormatter.cs b/Src/CSharpier.Cli/CommandLineFormatter.cs index 2220c09d0..c38a78ece 100644 --- a/Src/CSharpier.Cli/CommandLineFormatter.cs +++ b/Src/CSharpier.Cli/CommandLineFormatter.cs @@ -1,5 +1,6 @@ using System.Diagnostics; using System.IO.Abstractions; +using System.IO.Abstractions.TestingHelpers; using System.Text; using CSharpier.Cli.Options; using CSharpier.Core; @@ -292,12 +293,33 @@ await FormatPhysicalFile( return 1; } - var tasks = fileSystem - .Directory.EnumerateFiles( + var filePaths = new List(); + + // special case for testing as we don't call the real file system + if (fileSystem is MockFileSystem) + { + filePaths = fileSystem + .Directory.EnumerateFiles( + directoryOrFilePath, + "*.*", + SearchOption.AllDirectories + ) + .ToList(); + } + else + { + var fileEnumerator = new ValidFilesEnumerator( directoryOrFilePath, - "*.*", - SearchOption.AllDirectories - ) + new EnumerationOptions() { RecurseSubdirectories = true } + ); + + while (fileEnumerator.MoveNext()) + { + filePaths.Add(fileEnumerator.Current); + } + } + + var tasks = filePaths .Select(o => FormatFile(o, o.Replace(directoryOrFilePath, originalDirectoryOrFile)) ) diff --git a/Src/CSharpier.Cli/ValidFilesEnumerator.cs b/Src/CSharpier.Cli/ValidFilesEnumerator.cs new file mode 100644 index 000000000..51d84098c --- /dev/null +++ b/Src/CSharpier.Cli/ValidFilesEnumerator.cs @@ -0,0 +1,49 @@ +using System.IO.Enumeration; + +namespace CSharpier.Cli; + +internal class ValidFilesEnumerator(string directory, EnumerationOptions? options = null) + : FileSystemEnumerator(directory, options) +{ + protected override string TransformEntry(ref FileSystemEntry entry) + { + return entry.ToSpecifiedFullPath(); + } + + protected override bool ShouldIncludeEntry(ref FileSystemEntry entry) + { + if (entry.IsDirectory) + { + return false; + } + + var extension = Path.GetExtension(entry.FileName); + if ( + extension + is ".cs" + or ".csx" + or ".config" + or ".csproj" + or ".props" + or ".slnx" + or ".targets" + or ".xaml" + or ".xml" + ) + { + return true; + } + + return false; + } + + protected override bool ShouldRecurseIntoEntry(ref FileSystemEntry entry) + { + if (entry.FileName is ".git" or "bin" or "node_modules" or "obj") + { + return false; + } + + return true; + } +}