Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions Src/CSharpier.Cli/CommandLineFormatter.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -292,12 +293,33 @@ await FormatPhysicalFile(
return 1;
}

var tasks = fileSystem
.Directory.EnumerateFiles(
var filePaths = new List<string>();

// 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))
)
Expand Down
49 changes: 49 additions & 0 deletions Src/CSharpier.Cli/ValidFilesEnumerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.IO.Enumeration;

namespace CSharpier.Cli;

internal class ValidFilesEnumerator(string directory, EnumerationOptions? options = null)
: FileSystemEnumerator<string>(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;
}
}