Skip to content

[API Proposal]: GlobDirectories, Glob and exclude patterns #141

Description

@dennisdoomen

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions