-
Notifications
You must be signed in to change notification settings - Fork 19
/
VirtualFileManager.cs
160 lines (127 loc) · 3.55 KB
/
VirtualFileManager.cs
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
using System;
using LibSassHost;
using BundleTransformer.Core.FileSystem;
using BundleTransformer.Core.Utilities;
using CoreStrings = BundleTransformer.Core.Resources.Strings;
namespace BundleTransformer.SassAndScss.Internal
{
/// <summary>
/// Virtual file manager
/// </summary>
internal sealed class VirtualFileManager : IFileManager
{
/// <summary>
/// Virtual file system wrapper
/// </summary>
private readonly IVirtualFileSystemWrapper _virtualFileSystemWrapper;
/// <summary>
/// Constructs an instance of virtual file manager
/// </summary>
/// <param name="virtualFileSystemWrapper">Virtual file system wrapper</param>
public VirtualFileManager(IVirtualFileSystemWrapper virtualFileSystemWrapper)
{
_virtualFileSystemWrapper = virtualFileSystemWrapper;
}
/// <summary>
/// Determines whether the beginning of specified path matches the '~/'
/// </summary>
/// <param name="path">The path</param>
/// <returns>true if path starts with the '~/'; otherwise, false</returns>
private static bool IsAppRelativePath(string path)
{
if (path == null)
{
throw new ArgumentNullException(
nameof(path),
string.Format(CoreStrings.Common_ArgumentIsNull, nameof(path))
);
}
bool result = path.Length >= 2 && path[0] == '~' && (path[1] == '/' || path[1] == '\\');
return result;
}
#region IFileManager implementation
public bool SupportsConversionToAbsolutePath
{
get { return true; }
}
public string GetCurrentDirectory()
{
string currentDirectoryName = _virtualFileSystemWrapper.ToAbsolutePath("~/");
return currentDirectoryName;
}
public bool FileExists(string path)
{
if (path == null)
{
throw new ArgumentNullException(
nameof(path),
string.Format(CoreStrings.Common_ArgumentIsNull, nameof(path))
);
}
bool result = _virtualFileSystemWrapper.FileExists(path);
return result;
}
public bool IsAbsolutePath(string path)
{
if (path == null)
{
throw new ArgumentNullException(
nameof(path),
string.Format(CoreStrings.Common_ArgumentIsNull, nameof(path))
);
}
bool result = false;
if (path.Length > 0)
{
int charPosition = 0;
char charValue;
if (path.Length >= 2)
{
// check if we have a protocol
if (path.TryGetChar(charPosition, out charValue) && charValue.IsAlpha())
{
charPosition++;
// skip over all alphanumeric characters
while (path.TryGetChar(charPosition, out charValue) && charValue.IsAlphaNumeric())
{
charPosition++;
}
charPosition = charValue == ':' ? charPosition + 1 : 0;
}
}
path.TryGetChar(charPosition, out charValue);
result = charValue == '/';
}
return result;
}
public string ToAbsolutePath(string path)
{
if (path == null)
{
throw new ArgumentNullException(
nameof(path),
string.Format(CoreStrings.Common_ArgumentIsNull, nameof(path))
);
}
if (!IsAppRelativePath(path))
{
return path;
}
string absolutePath = _virtualFileSystemWrapper.ToAbsolutePath(path);
return absolutePath;
}
public string ReadFile(string path)
{
if (path == null)
{
throw new ArgumentNullException(
nameof(path),
string.Format(CoreStrings.Common_ArgumentIsNull, nameof(path))
);
}
string content = _virtualFileSystemWrapper.GetFileTextContent(path);
return content;
}
#endregion
}
}