Background and motivation
Pathy.Globbing only exposes GlobFiles. Two capabilities that Microsoft.Extensions.FileSystemGlobbing already provides are not reachable: matching directories, and excluding patterns. Exclusions in particular are close to essential in real projects, because almost every glob over a source tree needs to skip bin, obj, node_modules or .git. Today callers have to post-filter the results, which is both slower and more verbose than letting the matcher do it.
API Proposal
namespace Pathy
{
public static class ChainablePathGlobbingExtensions
{
public static ChainablePath[] GlobDirectories(this ChainablePath path, params string[] globPatterns);
public static ChainablePath[] Glob(this ChainablePath path, params string[] globPatterns);
public static ChainablePath[] GlobFiles(this ChainablePath path, string[] include, string[] exclude);
}
}
Glob matches both files and directories. The include/exclude overload maps directly onto the matcher's AddInclude and AddExclude.
API Usage
var sourceFiles = ChainablePath.Current.GlobFiles(
include: new[] { "**/*.cs" },
exclude: new[] { "**/bin/**", "**/obj/**" });
var testProjects = ChainablePath.Current.GlobDirectories("**/*.Specs");
var everything = (ChainablePath.Current / "artifacts").Glob("**/*");
Alternative Designs
- A small options object or builder instead of an
include/exclude pair. More extensible, but heavier for the common case.
- Leave exclusion to LINQ after the fact. That is what people do today, and it means walking directories that are then thrown away.
Risks
Two positional string array parameters are easy to transpose, so named arguments should be encouraged in the documentation, or the shape reconsidered. Adding an overload next to the existing params string[] version may create ambiguity at the call site and needs checking.
Background and motivation
Pathy.Globbingonly exposesGlobFiles. Two capabilities thatMicrosoft.Extensions.FileSystemGlobbingalready provides are not reachable: matching directories, and excluding patterns. Exclusions in particular are close to essential in real projects, because almost every glob over a source tree needs to skipbin,obj,node_modulesor.git. Today callers have to post-filter the results, which is both slower and more verbose than letting the matcher do it.API Proposal
Globmatches both files and directories. Theinclude/excludeoverload maps directly onto the matcher'sAddIncludeandAddExclude.API Usage
Alternative Designs
include/excludepair. More extensible, but heavier for the common case.Risks
Two positional string array parameters are easy to transpose, so named arguments should be encouraged in the documentation, or the shape reconsidered. Adding an overload next to the existing
params string[]version may create ambiguity at the call site and needs checking.