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.
Background and motivation
Listing the contents of a directory currently requires either the separate
Pathy.Globbingpackage (and therefore a dependency onMicrosoft.Extensions.FileSystemGlobbing) or a drop down toToDirectoryInfo(). 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 overDirectory.Enumerate*.API Proposal
API Usage
Alternative Designs
Pathy.GlobbingandGlobFiles("*"). That forces a package and a transitive dependency on consumers who only wanted to list a folder.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.