-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileTreeEnumerable.cs
More file actions
107 lines (85 loc) · 3.91 KB
/
FileTreeEnumerable.cs
File metadata and controls
107 lines (85 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System.Buffers;
using System.Collections;
using Ramstack.Globbing.Internal;
namespace Ramstack.Globbing.Traversal;
/// <summary>
/// Represents an enumerable file tree structure with customizable filtering and selection options.
/// </summary>
/// <typeparam name="TEntry">The type of the entry in the file tree.</typeparam>
/// <typeparam name="TResult">The type of the result.</typeparam>
public sealed class FileTreeEnumerable<TEntry, TResult> : IEnumerable<TResult>
{
private readonly TEntry _directory;
/// <summary>
/// The default capacity of the character buffer for paths rented from the shared array pool.
/// </summary>
internal const int DefaultBufferCapacity = 512;
/// <summary>
/// Gets or sets the glob patterns to include in the enumeration.
/// </summary>
public required string[] Patterns { get; init; }
/// <summary>
/// Gets or sets the patterns to exclude from the enumeration.
/// Defaults to an empty array.
/// </summary>
public string[] Excludes { get; init; } = [];
/// <summary>
/// Gets or sets the matching options to use. Defaults to <see cref="MatchFlags.Auto"/>.
/// </summary>
public MatchFlags Flags { get; init; } = MatchFlags.Auto;
/// <summary>
/// Gets or sets the predicate that determines whether to recurse into a directory.
/// </summary>
public Func<TEntry, bool>? ShouldRecursePredicate { get; init; }
/// <summary>
/// Gets or sets the predicate that determines whether to include a file entry in the result set.
/// </summary>
public Func<TEntry, bool>? ShouldIncludePredicate { get; init; }
/// <summary>
/// Gets or sets a function to extract the name for a file entry.
/// </summary>
public required Func<TEntry, string> FileNameSelector { get; init; }
/// <summary>
/// Gets or sets a function that retrieves the child entries of an entry.
/// </summary>
public required Func<TEntry, IEnumerable<TEntry>> ChildrenSelector { get; init; }
/// <summary>
/// Gets or sets a function that transforms a file entry into a result.
/// </summary>
public required Func<TEntry, TResult> ResultSelector { get; init; }
/// <summary>
/// Initializes a new instance of the <see cref="FileTreeEnumerable{TEntry, TResult}"/> class.
/// </summary>
/// <param name="directory">The root directory to start the enumeration from.</param>
public FileTreeEnumerable(TEntry directory) =>
_directory = directory;
/// <inheritdoc />
IEnumerator<TResult> IEnumerable<TResult>.GetEnumerator() =>
Enumerate().GetEnumerator();
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() =>
Enumerate().GetEnumerator();
private IEnumerable<TResult> Enumerate()
{
var chars = ArrayPool<char>.Shared.Rent(DefaultBufferCapacity);
var queue = new Queue<(TEntry Directory, string Path)>();
queue.Enqueue((_directory, ""));
while (queue.TryDequeue(out var e))
{
foreach (var entry in ChildrenSelector(e.Directory))
{
var name = FileNameSelector(entry);
var fullName = FileTreeHelper.GetFullName(ref chars, e.Path, name);
if (PathHelper.IsMatch(fullName, Excludes, Flags))
continue;
if (ShouldRecursePredicate == null || ShouldRecursePredicate(entry))
if (PathHelper.IsPartialMatch(fullName, Patterns, Flags))
queue.Enqueue((entry, fullName.ToString()));
if (ShouldIncludePredicate == null || ShouldIncludePredicate(entry))
if (PathHelper.IsMatch(fullName, Patterns, Flags))
yield return ResultSelector(entry);
}
}
ArrayPool<char>.Shared.Return(chars);
}
}