Browse Source

Add DirectoryPath Enumeration methods

pull/240/head
Ionite 1 year ago
parent
commit
47421b2d1c
No known key found for this signature in database
  1. 47
      StabilityMatrix.Core/Models/FileInterfaces/DirectoryPath.cs

47
StabilityMatrix.Core/Models/FileInterfaces/DirectoryPath.cs

@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis; using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using StabilityMatrix.Core.Converters.Json; using StabilityMatrix.Core.Converters.Json;
@ -6,7 +7,7 @@ namespace StabilityMatrix.Core.Models.FileInterfaces;
[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")] [SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
[JsonConverter(typeof(StringJsonConverter<DirectoryPath>))] [JsonConverter(typeof(StringJsonConverter<DirectoryPath>))]
public class DirectoryPath : FileSystemPath, IPathObject public class DirectoryPath : FileSystemPath, IPathObject, IEnumerable<FileSystemPath>
{ {
private DirectoryInfo? info; private DirectoryInfo? info;
@ -133,8 +134,50 @@ public class DirectoryPath : FileSystemPath, IPathObject
public FilePath JoinFile(params FilePath[] paths) => public FilePath JoinFile(params FilePath[] paths) =>
new(Path.Combine(FullPath, Path.Combine(paths.Select(path => path.FullPath).ToArray()))); new(Path.Combine(FullPath, Path.Combine(paths.Select(path => path.FullPath).ToArray())));
/// <summary>
/// Returns an enumerable collection of files that matches
/// a specified search pattern and search subdirectory option.
/// </summary>
public IEnumerable<FilePath> EnumerateFiles(
string searchPattern = "*",
SearchOption searchOption = SearchOption.TopDirectoryOnly
) => Info.EnumerateFiles(searchPattern, searchOption).Select(file => new FilePath(file));
/// <summary>
/// Returns an enumerable collection of directories that matches
/// a specified search pattern and search subdirectory option.
/// </summary>
public IEnumerable<DirectoryPath> EnumerateDirectories(
string searchPattern = "*",
SearchOption searchOption = SearchOption.TopDirectoryOnly
) =>
Info.EnumerateDirectories(searchPattern, searchOption)
.Select(directory => new DirectoryPath(directory));
public override string ToString() => FullPath; public override string ToString() => FullPath;
/// <inheritdoc />
public IEnumerator<FileSystemPath> GetEnumerator()
{
return Info.EnumerateFileSystemInfos("*", SearchOption.TopDirectoryOnly)
.Select<FileSystemInfo, FileSystemPath>(
fsInfo =>
fsInfo switch
{
FileInfo file => new FilePath(file),
DirectoryInfo directory => new DirectoryPath(directory),
_ => throw new InvalidOperationException("Unknown file system info type")
}
)
.GetEnumerator();
}
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
// DirectoryPath + DirectoryPath = DirectoryPath // DirectoryPath + DirectoryPath = DirectoryPath
public static DirectoryPath operator +(DirectoryPath path, DirectoryPath other) => public static DirectoryPath operator +(DirectoryPath path, DirectoryPath other) =>
new(Path.Combine(path, other.FullPath)); new(Path.Combine(path, other.FullPath));

Loading…
Cancel
Save