-
Notifications
You must be signed in to change notification settings - Fork 695
/
Copy pathRuntimeEnvironmentHelper.cs
160 lines (133 loc) · 4.38 KB
/
RuntimeEnvironmentHelper.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
160
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
namespace NuGet.Common
{
public static class RuntimeEnvironmentHelper
{
private static readonly string[] VisualStudioProcesses = { "DEVENV", "BLEND" };
private static Lazy<bool> _isMono = new Lazy<bool>(() => Type.GetType("Mono.Runtime") != null);
private static Lazy<bool> _isWindows = new Lazy<bool>(() => GetIsWindows());
private static Lazy<bool> _IsMacOSX = new Lazy<bool>(() => GetIsMacOSX());
private static Lazy<bool> _IsLinux = new Lazy<bool>(() => GetIsLinux());
private static Lazy<bool> _isRunningInVisualStudio = new Lazy<bool>(() =>
{
if (!IsWindows)
{
return false;
}
var currentProcessName = Path.GetFileNameWithoutExtension(GetCurrentProcessFilePath());
return VisualStudioProcesses.Any(
process => process.Equals(currentProcessName, StringComparison.OrdinalIgnoreCase));
});
[DllImport("libc")]
static extern int uname(IntPtr buf);
public static bool IsWindows
{
get => _isWindows.Value;
}
private static bool GetIsWindows()
{
#if IS_CORECLR
// This API does work on full framework but it requires a newer nuget client (RID aware)
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
{
return true;
}
return false;
#else
var platform = (int)Environment.OSVersion.Platform;
return (platform != 4) && (platform != 6) && (platform != 128);
#endif
}
public static bool IsMono
{
get
{
if (IsRunningInVisualStudio)
{
// skip Mono type check if current process is Devenv
return false;
}
return _isMono.Value;
}
}
public static bool IsRunningInVisualStudio
{
get
{
return _isRunningInVisualStudio.Value;
}
}
private static string GetCurrentProcessFilePath()
{
using (var process = Process.GetCurrentProcess())
{
return process.MainModule.FileName;
}
}
public static bool IsMacOSX
{
get => _IsMacOSX.Value;
}
private static bool GetIsMacOSX()
{
#if IS_CORECLR
// This API does work on full framework but it requires a newer nuget client (RID aware)
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX))
{
return true;
}
return false;
#else
var buf = IntPtr.Zero;
try
{
buf = Marshal.AllocHGlobal(8192);
// This is a hacktastic way of getting sysname from uname ()
if (uname(buf) == 0)
{
var os = Marshal.PtrToStringAnsi(buf);
if (os == "Darwin")
{
return true;
}
}
}
catch
{
}
finally
{
if (buf != IntPtr.Zero)
{
Marshal.FreeHGlobal(buf);
}
}
return false;
#endif
}
public static bool IsLinux
{
get => _IsLinux.Value;
}
private static bool GetIsLinux()
{
#if IS_CORECLR
// This API does work on full framework but it requires a newer nuget client (RID aware)
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux))
{
return true;
}
return false;
#else
var platform = (int)Environment.OSVersion.Platform;
return platform == 4;
#endif
}
}
}