-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathPlatformUtilities.cs
executable file
·118 lines (101 loc) · 3.14 KB
/
PlatformUtilities.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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace MICore
{
public static class PlatformUtilities
{
private enum RuntimePlatform
{
Unset = 0,
Unknown,
Windows,
MacOSX,
Unix,
}
private static RuntimePlatform s_runtimePlatform;
private static RuntimePlatform GetRuntimePlatform()
{
if (s_runtimePlatform == RuntimePlatform.Unset)
{
s_runtimePlatform = CalculateRuntimePlatform();
}
return s_runtimePlatform;
}
private static RuntimePlatform CalculateRuntimePlatform()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return RuntimePlatform.Windows;
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return RuntimePlatform.MacOSX;
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return RuntimePlatform.Unix;
}
return RuntimePlatform.Unknown;
}
/*
* Is this Windows?
*/
public static bool IsWindows()
{
return GetRuntimePlatform() == RuntimePlatform.Windows;
}
/*
* Is this OS X?
*/
public static bool IsOSX()
{
return GetRuntimePlatform() == RuntimePlatform.MacOSX;
}
/*
* Is this Linux?
*/
public static bool IsLinux()
{
return GetRuntimePlatform() == RuntimePlatform.Unix;
}
// Abstract API call to add an environment variable to a new process
public static void SetEnvironmentVariable(this ProcessStartInfo processStartInfo, string key, string value)
{
processStartInfo.Environment[key] = value;
}
// Abstract API call to add an environment variable to a new process
public static string GetEnvironmentVariable(this ProcessStartInfo processStartInfo, string key)
{
if (processStartInfo.Environment.ContainsKey(key))
return processStartInfo.Environment[key];
return null;
}
public static string UnixPathToWindowsPath(string unixPath)
{
return unixPath.Replace('/', '\\');
}
public static string WindowsPathToUnixPath(string windowsPath)
{
return windowsPath.Replace('\\', '/');
}
public static string PathToHostOSPath(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
return path;
}
if (IsWindows())
{
return UnixPathToWindowsPath(path);
}
else
{
return WindowsPathToUnixPath(path);
}
}
}
}