-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualDirectoryAdapter.cs
More file actions
118 lines (100 loc) · 4.5 KB
/
VirtualDirectoryAdapter.cs
File metadata and controls
118 lines (100 loc) · 4.5 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
using Microsoft.Extensions.FileProviders;
using Ramstack.FileSystem.Utilities;
namespace Ramstack.FileSystem.Adapters;
/// <summary>
/// Represents an adapter for the <see cref="IFileInfo"/> instance.
/// </summary>
internal sealed class VirtualDirectoryAdapter : VirtualDirectory
{
private readonly VirtualFileSystemAdapter _fs;
private IFileInfo? _file;
private IDirectoryContents? _directory;
/// <inheritdoc />
public override IVirtualFileSystem FileSystem => _fs;
/// <summary>
/// Initializes a new instance of the <see cref="VirtualDirectoryAdapter"/> class.
/// </summary>
/// <param name="fileSystem">The file system associated with this directory.</param>
/// <param name="path">The path of the directory.</param>
/// <param name="file">The <see cref="IFileInfo"/> associated with this directory.</param>
public VirtualDirectoryAdapter(VirtualFileSystemAdapter fileSystem, string path, IFileInfo file) : base(path) =>
// ReSharper disable once SuspiciousTypeConversion.Global
(_fs, _file, _directory) = (fileSystem, file, file as IDirectoryContents);
/// <summary>
/// Initializes a new instance of the <see cref="VirtualDirectoryAdapter"/> class.
/// </summary>
/// <param name="fileSystem">The file system associated with this directory.</param>
/// <param name="path">The path of the directory.</param>
/// <param name="directory">The <see cref="IDirectoryContents"/> associated with this directory.</param>
public VirtualDirectoryAdapter(VirtualFileSystemAdapter fileSystem, string path, IDirectoryContents directory) : base(path) =>
(_fs, _directory) = (fileSystem, directory);
/// <inheritdoc />
protected override void RefreshCore()
{
_file = null;
_directory = _fs.FileProvider.GetDirectoryContents(FullName);
}
/// <inheritdoc />
protected override ValueTask<VirtualNodeProperties?> GetPropertiesCoreAsync(CancellationToken cancellationToken)
{
var properties = _file?.Exists ?? _directory!.Exists
? VirtualNodeProperties.None
: VirtualNodeProperties.Unavailable;
return new ValueTask<VirtualNodeProperties?>(properties);
}
/// <inheritdoc />
protected override ValueTask<bool> ExistsCoreAsync(CancellationToken cancellationToken)
{
var exists = _file?.Exists ?? _directory!.Exists;
return new ValueTask<bool>(exists);
}
/// <inheritdoc />
protected override ValueTask CreateCoreAsync(CancellationToken cancellationToken) =>
default;
/// <inheritdoc />
protected override ValueTask DeleteCoreAsync(CancellationToken cancellationToken) =>
default;
/// <inheritdoc />
protected override IAsyncEnumerable<VirtualNode> GetFileNodesCoreAsync(CancellationToken cancellationToken)
{
return GetFileNodes().ToAsyncEnumerable();
IEnumerable<VirtualNode> GetFileNodes()
{
foreach (var fi in _directory ??= _fs.FileProvider.GetDirectoryContents(FullName))
{
var path = VirtualPath.Join(FullName, fi.Name);
yield return fi.IsDirectory
? new VirtualDirectoryAdapter(_fs, path, fi)
: new VirtualFileAdapter(_fs, path, fi);
}
}
}
/// <inheritdoc />
protected override IAsyncEnumerable<VirtualFile> GetFilesCoreAsync(CancellationToken cancellationToken)
{
return GetFiles().ToAsyncEnumerable();
IEnumerable<VirtualFile> GetFiles()
{
foreach (var fi in _directory ??= _fs.FileProvider.GetDirectoryContents(FullName))
{
var path = VirtualPath.Join(FullName, fi.Name);
if (!fi.IsDirectory)
yield return new VirtualFileAdapter(_fs, path, fi);
}
}
}
/// <inheritdoc />
protected override IAsyncEnumerable<VirtualDirectory> GetDirectoriesCoreAsync(CancellationToken cancellationToken)
{
return GetDirectories().ToAsyncEnumerable();
IEnumerable<VirtualDirectory> GetDirectories()
{
foreach (var fi in _directory ??= _fs.FileProvider.GetDirectoryContents(FullName))
{
var path = VirtualPath.Join(FullName, fi.Name);
if (fi.IsDirectory)
yield return new VirtualDirectoryAdapter(_fs, path, fi);
}
}
}
}