This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 217
/
RuntimeEnvironment.cs
110 lines (97 loc) · 3.57 KB
/
RuntimeEnvironment.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
// Copyright(c) .NET Foundation and contributors.All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Runtime.InteropServices;
using Microsoft.DotNet.PlatformAbstractions.Native;
namespace Microsoft.DotNet.PlatformAbstractions
{
public static class RuntimeEnvironment
{
private static readonly string OverrideEnvironmentVariableName = "DOTNET_RUNTIME_ID";
public static Platform OperatingSystemPlatform { get; } = PlatformApis.GetOSPlatform();
public static string OperatingSystemVersion { get; } = PlatformApis.GetOSVersion();
public static string OperatingSystem { get; } = PlatformApis.GetOSName();
public static string RuntimeArchitecture { get; } = GetArch();
private static string GetArch()
{
#if NET45
return Environment.Is64BitProcess ? "x64" : "x86";
#else
return RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant();
#endif
}
public static string GetRuntimeIdentifier()
{
return
Environment.GetEnvironmentVariable(OverrideEnvironmentVariableName) ??
(GetRIDOS() + GetRIDVersion() + GetRIDArch());
}
private static string GetRIDArch()
{
return $"-{RuntimeArchitecture}";
}
private static string GetRIDVersion()
{
// Windows RIDs do not separate OS name and version by "." due to legacy
// Others do, that's why we have the "." prefix on them below
switch (OperatingSystemPlatform)
{
case Platform.Windows:
return GetWindowsProductVersion();
case Platform.Linux:
if (string.IsNullOrEmpty(OperatingSystemVersion))
{
return string.Empty;
}
return $".{OperatingSystemVersion}";
case Platform.Darwin:
return $".{OperatingSystemVersion}";
case Platform.FreeBSD:
return $".{OperatingSystemVersion}";
default:
return string.Empty; // Unknown Platform? Unknown Version!
}
}
private static string GetWindowsProductVersion()
{
var ver = Version.Parse(OperatingSystemVersion);
if (ver.Major == 6)
{
if (ver.Minor == 1)
{
return "7";
}
else if (ver.Minor == 2)
{
return "8";
}
else if (ver.Minor == 3)
{
return "81";
}
}
else if (ver.Major >= 10)
{
// Return the major version for use in RID computation without applying any cap.
return ver.Major.ToString();
}
return string.Empty; // Unknown version
}
private static string GetRIDOS()
{
switch (OperatingSystemPlatform)
{
case Platform.Windows:
return "win";
case Platform.Linux:
return OperatingSystem.ToLowerInvariant();
case Platform.Darwin:
return "osx";
case Platform.FreeBSD:
return "freebsd";
default:
return "unknown";
}
}
}
}