-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualDirectory.cs
More file actions
285 lines (261 loc) · 13.8 KB
/
VirtualDirectory.cs
File metadata and controls
285 lines (261 loc) · 13.8 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
using Ramstack.Globbing.Traversal;
namespace Ramstack.FileSystem;
/// <summary>
/// Represents a virtual directory within a specified file system.
/// </summary>
[DebuggerDisplay("{FullName,nq}")]
public abstract class VirtualDirectory : VirtualNode
{
/// <summary>
/// Gets a value indicating whether the current directory is the root directory.
/// </summary>
public bool IsRoot => FullName == "/";
/// <summary>
/// Gets a <see cref="VirtualDirectory"/> instance representing the parent directory.
/// </summary>
public VirtualDirectory? Parent
{
get
{
if (IsRoot)
return null;
var directoryName = VirtualPath.GetDirectoryName(FullName);
return FileSystem.GetDirectory(directoryName);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="VirtualDirectory"/> class with the specified path.
/// </summary>
/// <param name="path">The full path of the directory.</param>
protected VirtualDirectory(string path) : base(path)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="VirtualDirectory"/> class with the specified path and properties.
/// </summary>
/// <param name="path">The full path of the directory.</param>
/// <param name="properties">The properties of the directory.</param>
protected VirtualDirectory(string path, VirtualNodeProperties? properties) : base(path, properties)
{
}
/// <summary>
/// Asynchronously creates the current directory.
/// </summary>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
public ValueTask CreateAsync(CancellationToken cancellationToken = default)
{
EnsureWritable();
Refresh();
return CreateCoreAsync(cancellationToken);
}
/// <summary>
/// Asynchronously deletes the current directory, including all its subdirectories and files.
/// </summary>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
public ValueTask DeleteAsync(CancellationToken cancellationToken = default)
{
EnsureWritable();
Refresh();
return DeleteCoreAsync(cancellationToken);
}
/// <summary>
/// Asynchronously returns an async-enumerable collection of file nodes (both directories and files) within the current directory.
/// </summary>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// An async-enumerable collection of <see cref="VirtualNode"/> instances.
/// </returns>
public IAsyncEnumerable<VirtualNode> GetFileNodesAsync(CancellationToken cancellationToken = default) =>
GetFileNodesCoreAsync(cancellationToken);
/// <summary>
/// Asynchronously returns an async-enumerable collection of files within the current directory.
/// </summary>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// An async-enumerable collection of <see cref="VirtualFile"/> instances.
/// </returns>
public IAsyncEnumerable<VirtualFile> GetFilesAsync(CancellationToken cancellationToken = default) =>
GetFilesCoreAsync(cancellationToken);
/// <summary>
/// Asynchronously returns an async-enumerable collection of directories within the current directory.
/// </summary>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// An async-enumerable collection of <see cref="VirtualDirectory"/> instances.
/// </returns>
public IAsyncEnumerable<VirtualDirectory> GetDirectoriesAsync(CancellationToken cancellationToken = default) =>
GetDirectoriesCoreAsync(cancellationToken);
/// <summary>
/// Asynchronously returns an async-enumerable collection of file nodes (both directories and files)
/// within the current directory that match any of the specified glob patterns.
/// </summary>
/// <param name="patterns">An array of glob patterns to match against the names of file nodes.</param>
/// <param name="excludes">An optional array of glob patterns to exclude file nodes.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// An async-enumerable collection of <see cref="VirtualNode"/> instances.
/// </returns>
public IAsyncEnumerable<VirtualNode> GetFileNodesAsync(string[] patterns, string[]? excludes, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(patterns);
return GetFileNodesCoreAsync(patterns, excludes, cancellationToken);
}
/// <summary>
/// Asynchronously returns an async-enumerable collection of files within the current directory
/// that match any of the specified glob patterns.
/// </summary>
/// <param name="patterns">An array of glob patterns to match against the names of files.</param>
/// <param name="excludes">An optional array of glob patterns to exclude files.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// An async-enumerable collection of <see cref="VirtualFile"/> instances.
/// </returns>
public IAsyncEnumerable<VirtualFile> GetFilesAsync(string[] patterns, string[]? excludes, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(patterns);
return GetFilesCoreAsync(patterns, excludes, cancellationToken);
}
/// <summary>
/// Asynchronously returns an async-enumerable collection of directories within the current directory
/// that match any of the specified glob patterns.
/// </summary>
/// <param name="patterns">An array of glob patterns to match against the names of directories.</param>
/// <param name="excludes">An optional array of glob patterns to exclude directories.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// An async-enumerable collection of <see cref="VirtualDirectory"/> instances.
/// </returns>
public IAsyncEnumerable<VirtualDirectory> GetDirectoriesAsync(string[] patterns, string[]? excludes, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(patterns);
return GetDirectoriesCoreAsync(patterns, excludes, cancellationToken);
}
/// <summary>
/// Core implementation for asynchronously creating the current directory.
/// </summary>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
protected abstract ValueTask CreateCoreAsync(CancellationToken cancellationToken);
/// <summary>
/// Core implementation for asynchronously deleting the current directory, including all its subdirectories and files.
/// </summary>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
protected abstract ValueTask DeleteCoreAsync(CancellationToken cancellationToken);
/// <summary>
/// Core implementation for asynchronously returning an async-enumerable collection of file nodes (both directories and files) within the current directory.
/// </summary>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// An async-enumerable collection of <see cref="VirtualNode"/> instances.
/// </returns>
protected abstract IAsyncEnumerable<VirtualNode> GetFileNodesCoreAsync(CancellationToken cancellationToken);
/// <summary>
/// Core implementation for asynchronously returning an async-enumerable collection of files within the current directory.
/// </summary>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// An async-enumerable collection of <see cref="VirtualFile"/> instances.
/// </returns>
protected virtual async IAsyncEnumerable<VirtualFile> GetFilesCoreAsync([EnumeratorCancellation] CancellationToken cancellationToken)
{
await foreach (var node in GetFileNodesCoreAsync(cancellationToken).ConfigureAwait(false))
{
if (node is VirtualFile file)
yield return file;
}
}
/// <summary>
/// Core implementation for asynchronously returning an async-enumerable collection of directories within the current directory.
/// </summary>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// An async-enumerable collection of <see cref="VirtualDirectory"/> instances.
/// </returns>
protected virtual async IAsyncEnumerable<VirtualDirectory> GetDirectoriesCoreAsync([EnumeratorCancellation] CancellationToken cancellationToken)
{
await foreach (var node in GetFileNodesCoreAsync(cancellationToken).ConfigureAwait(false))
{
if (node is VirtualDirectory directory)
yield return directory;
}
}
/// <summary>
/// Core implementation for asynchronously returning an async-enumerable collection of file nodes (both directories and files)
/// within the current directory that match any of the specified glob patterns.
/// </summary>
/// <param name="patterns">An array of glob patterns to match against the names of file nodes.</param>
/// <param name="excludes">An optional array of glob patterns to exclude file nodes.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// An async-enumerable collection of <see cref="VirtualNode"/> instances.
/// </returns>
protected virtual IAsyncEnumerable<VirtualNode> GetFileNodesCoreAsync(string[] patterns, string[]? excludes, CancellationToken cancellationToken)
{
return new FileTreeAsyncEnumerable<VirtualNode, VirtualNode>(this, cancellationToken)
{
Patterns = patterns,
Excludes = excludes ?? [],
FileNameSelector = node => node.Name,
ShouldRecursePredicate = node => node is VirtualDirectory,
ChildrenSelector = (node, token) => ((VirtualDirectory)node).GetFileNodesCoreAsync(token),
ResultSelector = node => node
};
}
/// <summary>
/// Core implementation for asynchronously returning an async-enumerable collection of files within the current directory
/// that match any of the specified glob patterns.
/// </summary>
/// <param name="patterns">An array of glob patterns to match against the names of files.</param>
/// <param name="excludes">An optional array of glob patterns to exclude files.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// An async-enumerable collection of <see cref="VirtualFile"/> instances.
/// </returns>
protected virtual IAsyncEnumerable<VirtualFile> GetFilesCoreAsync(string[] patterns, string[]? excludes, CancellationToken cancellationToken)
{
return new FileTreeAsyncEnumerable<VirtualNode, VirtualFile>(this, cancellationToken)
{
Patterns = patterns,
Excludes = excludes ?? [],
FileNameSelector = node => node.Name,
ShouldIncludePredicate = node => node is VirtualFile,
ShouldRecursePredicate = node => node is VirtualDirectory,
ChildrenSelector = (node, token) => ((VirtualDirectory)node).GetFileNodesCoreAsync(token),
ResultSelector = node => (VirtualFile)node
};
}
/// <summary>
/// Core implementation for asynchronously returning an async-enumerable collection of directories within the current directory
/// that match any of the specified glob patterns.
/// </summary>
/// <param name="patterns">An array of glob patterns to match against the names of directories.</param>
/// <param name="excludes">An optional array of glob patterns to exclude directories.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// An async-enumerable collection of <see cref="VirtualDirectory"/> instances.
/// </returns>
protected virtual IAsyncEnumerable<VirtualDirectory> GetDirectoriesCoreAsync(string[] patterns, string[]? excludes, CancellationToken cancellationToken)
{
return new FileTreeAsyncEnumerable<VirtualNode, VirtualDirectory>(this, cancellationToken)
{
Patterns = patterns,
Excludes = excludes ?? [],
FileNameSelector = node => node.Name,
ShouldIncludePredicate = node => node is VirtualDirectory,
ShouldRecursePredicate = node => node is VirtualDirectory,
ChildrenSelector = (node, token) => ((VirtualDirectory)node).GetFileNodesCoreAsync(token),
ResultSelector = node => (VirtualDirectory)node
};
}
}