Skip to content

[API Proposal]: Children, Files and Directories enumeration without the globbing package #137

Description

@dennisdoomen

Background and motivation

Listing the contents of a directory currently requires either the separate Pathy.Globbing package (and therefore a dependency on Microsoft.Extensions.FileSystemGlobbing) or a drop down to ToDirectoryInfo(). For the simple, extremely common case of "give me the entries in this directory", neither is appropriate. The core package can offer this with no new dependencies, since it is a thin wrapper over Directory.Enumerate*.

API Proposal

namespace Pathy
{
    public static class ChainablePathExtensions
    {
        public static IEnumerable<ChainablePath> Children(this ChainablePath path, bool recursive = false);
        public static IEnumerable<ChainablePath> Files(this ChainablePath path, bool recursive = false);
        public static IEnumerable<ChainablePath> Directories(this ChainablePath path, bool recursive = false);
    }
}

API Usage

foreach (var project in (ChainablePath.Current / "src").Directories())
{
    Console.WriteLine(project.Name);
}

var configFiles = (ChainablePath.Current / "config").Files()
    .Where(x => x.HasExtension(".json"));

var everything = ChainablePath.Current.Children(recursive: true);

Alternative Designs

  • Point people at Pathy.Globbing and GlobFiles("*"). That forces a package and a transitive dependency on consumers who only wanted to list a folder.
  • Expose these as properties. Enumeration hits the file system and can be expensive, so methods communicate the cost better.

Risks

Behaviour on a path that does not exist, or that points at a file rather than a directory, has to be defined: returning an empty sequence is friendlier than throwing but can hide mistakes. Recursive enumeration also has to state what happens when a subdirectory cannot be accessed.

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions