-
-
Notifications
You must be signed in to change notification settings - Fork 731
/
EnvironmentHelper.cs
163 lines (149 loc) · 4.74 KB
/
EnvironmentHelper.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
161
162
163
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
#if NETCORE
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
#endif
using System.Runtime.Versioning;
namespace Cake.Core.Polyfill
{
internal static class EnvironmentHelper
{
#if !NETCORE
private static bool? _isRunningOnMac;
#else
private static readonly FrameworkName NetStandardFramework = new FrameworkName(".NETStandard,Version=v2.0");
private static bool? _isCoreClr;
private static FrameworkName netCoreAppFramwork;
#endif
public static bool Is64BitOperativeSystem()
{
#if NETCORE
return RuntimeInformation.OSArchitecture == Architecture.X64
|| RuntimeInformation.OSArchitecture == Architecture.Arm64;
#else
return Environment.Is64BitOperatingSystem;
#endif
}
public static PlatformFamily GetPlatformFamily()
{
#if NETCORE
try
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return PlatformFamily.OSX;
}
}
catch (PlatformNotSupportedException)
{
}
try
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return PlatformFamily.Linux;
}
}
catch (PlatformNotSupportedException)
{
}
try
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return PlatformFamily.Windows;
}
}
catch (PlatformNotSupportedException)
{
}
#else
var platform = (int)Environment.OSVersion.Platform;
if (platform <= 3 || platform == 5)
{
return PlatformFamily.Windows;
}
if (!_isRunningOnMac.HasValue)
{
_isRunningOnMac = Native.MacOSX.IsRunningOnMac();
}
if (_isRunningOnMac ?? false || platform == (int)PlatformID.MacOSX)
{
return PlatformFamily.OSX;
}
if (platform == 4 || platform == 6 || platform == 128)
{
return PlatformFamily.Linux;
}
#endif
return PlatformFamily.Unknown;
}
public static bool IsCoreClr()
{
#if NETCORE
if (_isCoreClr == null)
{
_isCoreClr = RuntimeInformation.FrameworkDescription.StartsWith(".NET Core");
}
return _isCoreClr.Value;
#else
return false;
#endif
}
public static bool IsUnix()
{
return IsUnix(GetPlatformFamily());
}
public static bool IsUnix(PlatformFamily family)
{
return family == PlatformFamily.Linux
|| family == PlatformFamily.OSX;
}
public static bool IsOSX(PlatformFamily family)
{
return family == PlatformFamily.OSX;
}
public static bool IsLinux(PlatformFamily family)
{
return family == PlatformFamily.Linux;
}
public static Runtime GetRuntime()
{
if (IsCoreClr())
{
return Runtime.CoreClr;
}
return Runtime.Clr;
}
public static FrameworkName GetBuiltFramework()
{
#if NETCORE
if (netCoreAppFramwork != null)
{
return netCoreAppFramwork;
}
var assemblyPath = typeof(System.Runtime.GCSettings)?.GetTypeInfo()?.Assembly?.CodeBase;
if (string.IsNullOrEmpty(assemblyPath))
{
return NetStandardFramework;
}
const string microsoftNetCoreApp = "Microsoft.NETCore.App";
var runtimeBasePathLength = assemblyPath.IndexOf(microsoftNetCoreApp) + microsoftNetCoreApp.Length + 1;
var netCoreAppVersion = string.Concat(assemblyPath.Skip(runtimeBasePathLength).Take(3));
if (string.IsNullOrEmpty(netCoreAppVersion))
{
return NetStandardFramework;
}
return netCoreAppFramwork = Version.TryParse(netCoreAppVersion, out var version)
? new FrameworkName(".NETCoreApp", version)
: NetStandardFramework;
#else
return new FrameworkName(".NETFramework,Version=v4.6.1");
#endif
}
}
}