-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualFileSystemExtensions.cs
More file actions
405 lines (375 loc) · 24.4 KB
/
VirtualFileSystemExtensions.cs
File metadata and controls
405 lines (375 loc) · 24.4 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
using System.Text;
namespace Ramstack.FileSystem;
/// <summary>
/// Provides extension methods for the <see cref="IVirtualFileSystem"/> interface.
/// </summary>
public static partial class VirtualFileSystemExtensions
{
/// <summary>
/// Asynchronously opens the file at the specified path for reading.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the file to open.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation, containing a stream for reading the file content.
/// </returns>
public static ValueTask<Stream> OpenReadAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.GetFile(path).OpenReadAsync(cancellationToken);
/// <summary>
/// Asynchronously returns a <see cref="StreamReader"/> with <see cref="Encoding.UTF8"/> encoding for a file at the specified path.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the file to open.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="Task{TResult}"/> representing the asynchronous operation,
/// containing a <see cref="StreamReader"/> that reads from the text file.
/// </returns>
public static Task<StreamReader> OpenTextAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.OpenTextAsync(path, Encoding.UTF8, cancellationToken);
/// <summary>
/// Asynchronously returns a <see cref="StreamReader"/> with the specified character encoding for a file at the specified path.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the file to open.</param>
/// <param name="encoding">The encoding applied to the contents.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="Task{TResult}"/> representing the asynchronous operation,
/// containing a <see cref="StreamReader"/> that reads from the text file.
/// </returns>
public static async Task<StreamReader> OpenTextAsync(this IVirtualFileSystem fs, string path, Encoding encoding, CancellationToken cancellationToken = default)
{
var stream = await fs.OpenReadAsync(path, cancellationToken).ConfigureAwait(false);
return new StreamReader(stream, encoding, detectEncodingFromByteOrderMarks: true, bufferSize: -1, leaveOpen: false);
}
/// <summary>
/// Asynchronously opens the file at the specified path for writing.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the file to open or create.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation, containing a stream for writing to the file content.
/// </returns>
public static ValueTask<Stream> OpenWriteAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.GetFile(path).OpenWriteAsync(cancellationToken);
/// <summary>
/// Asynchronously writes the specified content to a file at the specified path. If the file exists, an exception will be thrown.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the file.</param>
/// <param name="stream">A <see cref="Stream"/> containing the content to write to the file.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
public static ValueTask WriteAsync(this IVirtualFileSystem fs, string path, Stream stream, CancellationToken cancellationToken = default) =>
fs.GetFile(path).WriteAsync(stream, overwrite: false, cancellationToken);
/// <summary>
/// Asynchronously writes the specified content to a file at the specified path, creating a new file or overwriting an existing one.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the file.</param>
/// <param name="stream">A <see cref="Stream"/> containing the content to write to the file.</param>
/// <param name="overwrite"><see langword="true"/> to overwrite an existing file;
/// <see langword="false"/> to throw an exception if the file already exists.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
/// <remarks>
/// <list type="bullet">
/// <item><description>If the file does not exist, it will be created.</description></item>
/// <item><description>If it exists and <paramref name="overwrite"/> is <see langword="true"/>, the existing file will be overwritten.</description></item>
/// <item><description>If <paramref name="overwrite"/> is <see langword="false"/> and the file exists, an exception will be thrown.</description></item>
/// </list>
/// </remarks>
public static ValueTask WriteAsync(this IVirtualFileSystem fs, string path, Stream stream, bool overwrite, CancellationToken cancellationToken = default) =>
fs.GetFile(path).WriteAsync(stream, overwrite, cancellationToken);
/// <summary>
/// Asynchronously reads all the text in the file.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The file from which to read the entire text content.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation,
/// containing the full text from the current file.
/// </returns>
public static ValueTask<string> ReadAllTextAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.GetFile(path).ReadAllTextAsync(cancellationToken);
/// <summary>
/// Asynchronously reads all the text in the file with the specified encoding.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The file from which to read the entire text content.</param>
/// <param name="encoding">The encoding applied to the contents.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation,
/// containing the full text from the current file.
/// </returns>
public static ValueTask<string> ReadAllTextAsync(this IVirtualFileSystem fs, string path, Encoding? encoding, CancellationToken cancellationToken = default) =>
fs.GetFile(path).ReadAllTextAsync(encoding, cancellationToken);
/// <summary>
/// Asynchronously reads all lines of the file.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The file to read from.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation,
/// containing an array of all lines in the current file.
/// </returns>
public static ValueTask<string[]> ReadAllLinesAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.GetFile(path).ReadAllLinesAsync(cancellationToken);
/// <summary>
/// Asynchronously reads all lines of the file with the specified encoding.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The file to read from.</param>
/// <param name="encoding">The encoding applied to the contents.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation,
/// containing an array of all lines in the current file.
/// </returns>
public static ValueTask<string[]> ReadAllLinesAsync(this IVirtualFileSystem fs, string path, Encoding? encoding, CancellationToken cancellationToken = default) =>
fs.GetFile(path).ReadAllLinesAsync(encoding, cancellationToken);
/// <summary>
/// Asynchronously reads the entire contents of the specified file into a byte array.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The file to read from.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation,
/// containing an array of the file's bytes.
/// </returns>
public static ValueTask<byte[]> ReadAllBytesAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.GetFile(path).ReadAllBytesAsync(cancellationToken);
/// <summary>
/// Asynchronously writes the specified string to the specified file. If the file already exists, it is truncated and overwritten.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The file to write to.</param>
/// <param name="contents">The contents to write to the file.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
public static ValueTask WriteAllTextAsync(this IVirtualFileSystem fs, string path, string contents, CancellationToken cancellationToken = default) =>
fs.GetFile(path).WriteAllTextAsync(contents, cancellationToken);
/// <summary>
/// Asynchronously writes the specified string to the specified file. If the file already exists, it is truncated and overwritten.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The file to write to.</param>
/// <param name="contents">The contents to write to the file.</param>
/// <param name="encoding">The encoding to apply to the string.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
public static ValueTask WriteAllTextAsync(this IVirtualFileSystem fs, string path, string contents, Encoding? encoding, CancellationToken cancellationToken = default) =>
fs.GetFile(path).WriteAllTextAsync(contents, encoding, cancellationToken);
/// <summary>
/// Asynchronously writes the specified string to the specified file. If the file already exists, it is truncated and overwritten.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The file to write to.</param>
/// <param name="contents">The contents to write to the file.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
public static ValueTask WriteAllTextAsync(this IVirtualFileSystem fs, string path, ReadOnlyMemory<char> contents, CancellationToken cancellationToken = default) =>
fs.GetFile(path).WriteAllTextAsync(contents, cancellationToken);
/// <summary>
/// Asynchronously writes the specified string to the specified file. If the file already exists, it is truncated and overwritten.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The file to write to.</param>
/// <param name="contents">The contents to write to the file.</param>
/// <param name="encoding">The encoding to apply to the string.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
public static ValueTask WriteAllTextAsync(this IVirtualFileSystem fs, string path, ReadOnlyMemory<char> contents, Encoding? encoding, CancellationToken cancellationToken = default) =>
fs.GetFile(path).WriteAllTextAsync(contents, encoding, cancellationToken);
/// <summary>
/// Asynchronously writes the specified lines to the specified file. If the file already exists, it is truncated and overwritten.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The file to write to.</param>
/// <param name="contents">The contents to write to the file.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
public static ValueTask WriteAllLinesAsync(this IVirtualFileSystem fs, string path, IEnumerable<string> contents, CancellationToken cancellationToken = default) =>
fs.GetFile(path).WriteAllLinesAsync(contents, cancellationToken);
/// <summary>
/// Asynchronously writes the specified lines to the specified file. If the file already exists, it is truncated and overwritten.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The file to write to.</param>
/// <param name="contents">The contents to write to the file.</param>
/// <param name="encoding">The encoding to apply to the string.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
public static ValueTask WriteAllLinesAsync(this IVirtualFileSystem fs, string path, IEnumerable<string> contents, Encoding? encoding, CancellationToken cancellationToken = default) =>
fs.GetFile(path).WriteAllLinesAsync(contents, encoding, cancellationToken);
/// <summary>
/// Asynchronously writes the specified byte array to the specified file. If the file already exists, it is truncated and overwritten.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The file to write to.</param>
/// <param name="bytes">The bytes to write to the file.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
public static ValueTask WriteAllBytesAsync(this IVirtualFileSystem fs, string path, byte[] bytes, CancellationToken cancellationToken = default) =>
fs.GetFile(path).WriteAllBytesAsync(bytes, cancellationToken);
/// <summary>
/// Asynchronously writes the specified byte array to the specified file. If the file already exists, it is truncated and overwritten.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The file to write to.</param>
/// <param name="bytes">The bytes to write to the file.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
public static ValueTask WriteAllBytesAsync(this IVirtualFileSystem fs, string path, ReadOnlyMemory<byte> bytes, CancellationToken cancellationToken = default) =>
fs.GetFile(path).WriteAllBytesAsync(bytes, cancellationToken);
/// <summary>
/// Asynchronously determines whether the file exists.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the file.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation.
/// The task result is <see langword="true"/> if the file exists; otherwise, <see langword="false"/>.
/// </returns>
public static ValueTask<bool> FileExistsAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.GetFile(path).ExistsAsync(cancellationToken);
/// <summary>
/// Asynchronously deletes the file at the specified path. No exception is thrown if the file does not exist.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the file to delete.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
public static ValueTask DeleteFileAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.GetFile(path).DeleteAsync(cancellationToken);
/// <summary>
/// Asynchronously copies a file within the file system to the specified destination path.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the file to copy.</param>
/// <param name="destinationPath">The path where the file will be copied to.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
public static ValueTask CopyFileAsync(this IVirtualFileSystem fs, string path, string destinationPath, CancellationToken cancellationToken = default) =>
fs.GetFile(path).CopyToAsync(destinationPath, overwrite: false, cancellationToken);
/// <summary>
/// Asynchronously copies a file within the file system to the specified destination path.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the file to copy.</param>
/// <param name="destinationPath">The path where the file will be copied to.</param>
/// <param name="overwrite"><see langword="true"/> to overwrite an existing file; <see langword="false"/> to throw an exception if the file already exists.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
/// <remarks>
/// <list type="bullet">
/// <item><description>If the file does not exist, it will be created.</description></item>
/// <item><description>If it exists and <paramref name="overwrite"/> is <see langword="true"/>, the existing file will be overwritten.</description></item>
/// <item><description>If <paramref name="overwrite"/> is <see langword="false"/> and the file exists, an exception will be thrown.</description></item>
/// </list>
/// </remarks>
public static ValueTask CopyFileAsync(this IVirtualFileSystem fs, string path, string destinationPath, bool overwrite, CancellationToken cancellationToken = default) =>
fs.GetFile(path).CopyToAsync(destinationPath, overwrite, cancellationToken);
/// <summary>
/// Asynchronously determines whether the directory exists.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the directory.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation.
/// The task result is <see langword="true"/> if the directory exists; otherwise, <see langword="false"/>.
/// </returns>
public static ValueTask<bool> DirectoryExistsAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.GetDirectory(path).ExistsAsync(cancellationToken);
/// <summary>
/// Asynchronously creates a directory at the specified path.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the directory to create.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
public static ValueTask CreateDirectoryAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.GetDirectory(path).CreateAsync(cancellationToken);
/// <summary>
/// Asynchronously deletes a directory at the specified path, including its subdirectories and all files.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the directory to delete.</param>
/// <param name="cancellationToken">An optional cancellation token to cancel the operation.</param>
/// <returns>
/// A <see cref="ValueTask"/> representing the asynchronous operation.
/// </returns>
public static ValueTask DeleteDirectoryAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.GetDirectory(path).DeleteAsync(cancellationToken);
/// <summary>
/// Asynchronously returns an async-enumerable collection of file nodes (both directories and files)
/// within the directory at the specified path.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the directory.</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 static IAsyncEnumerable<VirtualNode> GetFileNodesAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.GetDirectory(path).GetFileNodesAsync(cancellationToken);
/// <summary>
/// Asynchronously returns an async-enumerable collection of files within the directory at the specified path.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the directory.</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 static IAsyncEnumerable<VirtualFile> GetFilesAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.GetDirectory(path).GetFilesAsync(cancellationToken);
/// <summary>
/// Asynchronously returns an async-enumerable collection of directories within the directory at the specified path.
/// </summary>
/// <param name="fs">The file system to use.</param>
/// <param name="path">The path of the directory.</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 static IAsyncEnumerable<VirtualDirectory> GetDirectoriesAsync(this IVirtualFileSystem fs, string path, CancellationToken cancellationToken = default) =>
fs.GetDirectory(path).GetDirectoriesAsync(cancellationToken);
}