-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPathHelper.cs
75 lines (67 loc) · 2.3 KB
/
PathHelper.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
using System;
using System.Globalization;
using System.IO;
namespace XCom2ModTool
{
internal class PathHelper
{
public static bool IsAbsolute(string path)
{
return Path.IsPathRooted(path);
}
public static bool IsRelative(string path)
{
return !IsAbsolute(path);
}
public static string MakeAbsolute(string path)
{
return Path.GetFullPath(path);
}
public static string MakeAbsolute(string path, string parentFolderPath)
{
if (IsAbsolute(path))
{
return path;
}
return MakeAbsolute(Path.Combine(parentFolderPath, path));
}
public static string MakeRelative(string path, string parentFolderPath)
{
if (IsRelative(path))
{
throw new ArgumentException($"{nameof(path)} is a relative path");
}
if (IsRelative(parentFolderPath))
{
throw new ArgumentException($"{nameof(parentFolderPath)} is a relative path");
}
var pathUri = new Uri(path);
if (!parentFolderPath.EndsWith(Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture)))
{
parentFolderPath += Path.DirectorySeparatorChar;
}
var folderUri = new Uri(parentFolderPath);
return Uri.UnescapeDataString(folderUri.MakeRelativeUri(pathUri).ToString().Replace('/', Path.DirectorySeparatorChar));
}
public static string EscapeAndJoinArguments(string[] args)
{
var newArgs = new string[args.Length];
for (var i = 0; i < args.Length; ++i)
{
var arg = args[i];
var containsQuotes = arg.IndexOf("\"", 0, StringComparison.Ordinal) > 0;
var containsSpaces = arg.IndexOf(" ", 0, StringComparison.Ordinal) > 0;
if (containsQuotes)
{
arg = arg.Replace("\"", "\"\"");
}
if (containsQuotes || containsSpaces)
{
arg = $"\"{arg}\"";
}
newArgs[i] = arg;
}
return string.Join(" ", newArgs);
}
}
}