-
Notifications
You must be signed in to change notification settings - Fork 369
/
PhysicalFileSystem.cs
159 lines (133 loc) · 4.86 KB
/
PhysicalFileSystem.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.TemplateEngine.Abstractions.PhysicalFileSystem;
namespace Microsoft.TemplateEngine.Utils
{
/// <summary>
/// Local file system implementation of <see cref="IPhysicalFileSystem"/>.
/// </summary>
/// <seealso cref="Abstractions.ITemplateEngineHost"/>
public class PhysicalFileSystem : IPhysicalFileSystem
{
public bool DirectoryExists(string directory)
{
return Directory.Exists(directory);
}
public bool FileExists(string file)
{
return File.Exists(file);
}
public Stream CreateFile(string path)
{
return File.Create(path);
}
public void CreateDirectory(string path)
{
_ = Directory.CreateDirectory(path);
}
public string GetCurrentDirectory()
{
return Directory.GetCurrentDirectory();
}
public IEnumerable<string> EnumerateFileSystemEntries(string directoryName, string pattern, SearchOption searchOption)
{
return Directory.EnumerateFileSystemEntries(directoryName, pattern, searchOption);
}
public void FileCopy(string source, string target, bool overwrite)
{
File.Copy(source, target, overwrite);
}
public void DirectoryDelete(string path, bool recursive)
{
Directory.Delete(path, recursive);
}
public string ReadAllText(string path)
{
return File.ReadAllText(path);
}
public byte[] ReadAllBytes(string path)
{
return File.ReadAllBytes(path);
}
public void WriteAllText(string path, string value)
{
File.WriteAllText(path, value);
}
public IEnumerable<string> EnumerateDirectories(string path, string pattern, SearchOption searchOption)
{
return Directory.EnumerateDirectories(path, pattern, searchOption);
}
public IEnumerable<string> EnumerateFiles(string path, string pattern, SearchOption searchOption)
{
return Directory.EnumerateFiles(path, pattern, searchOption);
}
public Stream OpenRead(string path)
{
return File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
}
public void FileDelete(string path)
{
File.Delete(path);
}
public FileAttributes GetFileAttributes(string file)
{
return File.GetAttributes(file);
}
public void SetFileAttributes(string file, FileAttributes attributes)
{
File.SetAttributes(file, attributes);
}
public DateTime GetLastWriteTimeUtc(string file)
{
return File.GetLastWriteTimeUtc(file);
}
public void SetLastWriteTimeUtc(string file, DateTime lastWriteTimeUtc)
{
File.SetLastWriteTimeUtc(file, lastWriteTimeUtc);
}
public string PathRelativeTo(string target, string relativeTo)
{
string resultPath;
try
{
string basePath = Path.GetFullPath(relativeTo);
string sourceFullPath = Path.GetFullPath(target);
resultPath = PathRelativeToInternal(sourceFullPath, basePath);
}
catch (Exception)
{
resultPath = NormalizePath(target);
}
return resultPath;
}
public IDisposable WatchFileChanges(string filePath, FileSystemEventHandler fileChanged)
{
FileSystemWatcher watcher = new FileSystemWatcher(Path.GetDirectoryName(filePath), Path.GetFileName(filePath));
watcher.Changed += fileChanged;
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.EnableRaisingEvents = true;
return watcher;
}
internal static string PathRelativeToInternal(string target, string relativeTo)
{
string resultPath = target;
relativeTo = relativeTo.TrimEnd('/', '\\');
if (target.StartsWith(relativeTo, StringComparison.CurrentCulture))
{
resultPath = target.Substring(relativeTo.Length + 1);
}
return NormalizePath(resultPath);
}
private static string NormalizePath(string path)
{
char desiredDirectorySeparator = Path.DirectorySeparatorChar;
char undesiredSeparatorChar = desiredDirectorySeparator switch
{
'/' => '\\',
'\\' => '/',
_ => desiredDirectorySeparator,
};
return path.Replace(undesiredSeparatorChar, desiredDirectorySeparator);
}
}
}