-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileLikeArgsProcessing.cs
More file actions
59 lines (51 loc) · 2 KB
/
FileLikeArgsProcessing.cs
File metadata and controls
59 lines (51 loc) · 2 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
namespace VsCodeInstallerHelper;
internal static class FileLikeArgsProcessing
{
static string [] PathTemplates() =>
[
$"{Path.DirectorySeparatorChar}",
$"{Path.AltDirectorySeparatorChar}",
$".{Path.DirectorySeparatorChar}",
$".{Path.AltDirectorySeparatorChar}",
$"..{Path.DirectorySeparatorChar}",
$"..{Path.AltDirectorySeparatorChar}"
];
internal static IEnumerable<string> UpdateFileArgs(IReadOnlyList<string>? executeParams, string? workPath, bool resolveFullPath,
bool checkFileExists)
{
if (executeParams is null)
yield break;
if (!resolveFullPath && !checkFileExists)
{
foreach (var executeParam in executeParams)
yield return executeParam;
yield break;
}
var pathTemplates = PathTemplates();
foreach (var executeParam in executeParams)
{
var param = executeParam.Trim();
if (pathTemplates.Any(template => param.StartsWith(template)))
param = UpdateFileSystemPath(param, workPath, resolveFullPath, checkFileExists);
yield return param;
}
}
internal static string UpdateFileSystemPath(string fileSystemPath, string? workPath, bool resolveFullPath, bool checkFileExists)
{
if (!resolveFullPath && !checkFileExists)
return fileSystemPath;
var updatedPath = fileSystemPath;
if (resolveFullPath)
{
if (Path.IsPathFullyQualified(fileSystemPath))
return fileSystemPath;
updatedPath = workPath is null
? Path.GetFullPath(fileSystemPath)
: Path.GetFullPath(Path.Combine(workPath, fileSystemPath));
}
if (!checkFileExists || Path.Exists(updatedPath))
return updatedPath;
Error($"The specified file '{updatedPath}' does not exist.");
throw new FileNotFoundException($"The specified file '{updatedPath}' does not exist.");
}
}