-
Notifications
You must be signed in to change notification settings - Fork 651
/
Copy pathFileSystemHelper.cs
154 lines (119 loc) · 6.19 KB
/
FileSystemHelper.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
using System.IO.Abstractions;
using System.Runtime.InteropServices;
namespace GitVersion.Helpers;
internal static class FileSystemHelper
{
private static readonly FileSystem fileSystem = new();
internal static class File
{
public static bool Exists(string path) => fileSystem.File.Exists(path);
public static void Delete(string path) => fileSystem.File.Delete(path);
public static string ReadAllText(string path) => fileSystem.File.ReadAllText(path);
public static void WriteAllText(string path, string? contents) => fileSystem.File.WriteAllText(path, contents);
}
internal static class Directory
{
public static bool Exists(string directoryPath) => fileSystem.Directory.Exists(directoryPath);
public static void DeleteDirectory(string directoryPath)
{
// From http://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true/329502#329502
if (!fileSystem.Directory.Exists(directoryPath))
{
Trace.WriteLine($"Directory '{directoryPath}' is missing and can't be removed.");
return;
}
var files = fileSystem.Directory.GetFiles(directoryPath);
var dirs = fileSystem.Directory.GetDirectories(directoryPath);
foreach (var file in files)
{
fileSystem.File.SetAttributes(file, FileAttributes.Normal);
fileSystem.File.Delete(file);
}
foreach (var dir in dirs)
{
DeleteDirectory(dir);
}
fileSystem.File.SetAttributes(directoryPath, FileAttributes.Normal);
try
{
fileSystem.Directory.Delete(directoryPath, false);
}
catch (IOException)
{
Trace.WriteLine(string.Format("{0}The directory '{1}' could not be deleted!" +
"{0}Most of the time, this is due to an external process accessing the files in the temporary repositories created during the test runs, and keeping a handle on the directory, thus preventing the deletion of those files." +
"{0}Known and common causes include:" +
"{0}- Windows Search Indexer (go to the Indexing Options, in the Windows Control Panel, and exclude the bin folder of LibGit2Sharp.Tests)" +
"{0}- Antivirus (exclude the bin folder of LibGit2Sharp.Tests from the paths scanned by your real-time antivirus){0}",
Path.NewLine, fileSystem.Path.GetFullPath(directoryPath)));
}
}
}
internal static class Path
{
public static string NewLine => SysEnv.NewLine;
public static char DirectorySeparatorChar => fileSystem.Path.DirectorySeparatorChar;
private static readonly StringComparison OsDependentComparison =
RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
? StringComparison.Ordinal
: StringComparison.OrdinalIgnoreCase;
public static string GetCurrentDirectory() => AppContext.BaseDirectory ?? throw new InvalidOperationException();
public static string GetTempPathLegacy() => fileSystem.Path.GetTempPath().TrimEnd(DirectorySeparatorChar);
public static string GetTempPath()
{
var tempPath = GetCurrentDirectory();
if (!string.IsNullOrWhiteSpace(SysEnv.GetEnvironmentVariable("RUNNER_TEMP")))
{
tempPath = SysEnv.GetEnvironmentVariable("RUNNER_TEMP");
}
return tempPath!;
}
public static string GetRepositoryTempPath() => Combine(GetTempPath(), "TestRepositories", Guid.NewGuid().ToString());
public static string GetDirectoryName(string? path)
{
ArgumentNullException.ThrowIfNull(path, nameof(path));
return fileSystem.Path.GetDirectoryName(path)!;
}
public static string GetFileName(string? path)
{
ArgumentNullException.ThrowIfNull(path, nameof(path));
return fileSystem.Path.GetFileName(path);
}
public static string? GetFileNameWithoutExtension(string? path) => fileSystem.Path.GetFileNameWithoutExtension(path);
public static string? GetExtension(string? path) => fileSystem.Path.GetExtension(path);
public static string GetFullPath(string? path) => fileSystem.Path.GetFullPath(path!);
public static string Combine(string? path1, string? path2)
{
ArgumentException.ThrowIfNullOrWhiteSpace(path1);
ArgumentException.ThrowIfNullOrWhiteSpace(path2);
return fileSystem.Path.Combine(path1, path2);
}
public static string Combine(string? path1)
{
ArgumentNullException.ThrowIfNull(path1, nameof(path1));
return fileSystem.Path.Combine(path1);
}
public static string Combine(string? path1, string? path2, string? path3)
{
ArgumentException.ThrowIfNullOrWhiteSpace(path1);
ArgumentException.ThrowIfNullOrWhiteSpace(path2);
ArgumentException.ThrowIfNullOrWhiteSpace(path3);
return fileSystem.Path.Combine(path1, path2, path3);
}
public static string Combine(string? path1, string? path2, string? path3, string? path4)
{
ArgumentException.ThrowIfNullOrWhiteSpace(path1);
ArgumentException.ThrowIfNullOrWhiteSpace(path2);
ArgumentException.ThrowIfNullOrWhiteSpace(path3);
ArgumentException.ThrowIfNullOrWhiteSpace(path4);
return fileSystem.Path.Combine(path1, path2, path3, path4);
}
public static bool Equal(string? path, string? otherPath) =>
string.Equals(
GetFullPath(path).TrimEnd('\\').TrimEnd('/'),
GetFullPath(otherPath).TrimEnd('\\').TrimEnd('/'),
OsDependentComparison);
public static string GetRandomFileName() => fileSystem.Path.GetRandomFileName();
public static bool IsPathRooted(string? path) => fileSystem.Path.IsPathRooted(path);
}
}