16 static JsonElement? runtimeJsonElement;
17 static string runtimeIdentifier;
22 public static bool IsMono {
get; } =
Type.GetType(
"Mono.Runtime") !=
null;
27 public static bool IsWindows {
get; } = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
32 public static bool IsLinux {
get; } = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
37 public static bool IsOSX {
get; } = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
48 static string GetRuntimeIdentifier()
51 if (Environment.GetEnvironmentVariable(
"DOTNET_RUNTIME_ID") is
string rid1 &&
string.IsNullOrWhiteSpace(rid1) ==
false)
52 if (RuntimeJson.TryGetProperty(rid1, out _))
56 if (RuntimeInformation.RuntimeIdentifier is
string rid2 &&
string.IsNullOrWhiteSpace(rid2) ==
false)
57 if (RuntimeJson.TryGetProperty(rid2, out _))
61 return GetFallbackRuntimeIdentifier();
68 static string GetFallbackRuntimeIdentifier()
70 var arch = RuntimeInformation.ProcessArchitecture
switch
72 Architecture.X86 =>
"x86",
73 Architecture.X64 =>
"x64",
74 Architecture.Arm =>
"arm",
75 Architecture.Arm64 =>
"arm64",
83 return $
"linux-{arch}";
94 static JsonElement RuntimeJson => runtimeJsonElement ??= GetRuntimeJson();
100 static JsonElement GetRuntimeJson()
102 using var stream = typeof(RuntimeUtil).Assembly.GetManifestResourceStream(
"runtime.json");
103 var g = JsonDocument.Parse(stream);
104 return g.RootElement.GetProperty(
"runtimes");
116 static IEnumerable<string> GetSupportedRuntimeIdentifiers()
126 static IEnumerable<string> GetRuntimeIdentifierIterator(
string rid)
128 if (RuntimeJson.TryGetProperty(rid, out var node) ==
false)
132 var v =
new HashSet<string>();
133 var q =
new Queue<(string, JsonElement)>();
135 q.Enqueue((rid, node));
141 var (thisRid, thisNode) = q.Dequeue();
142 yield
return thisRid;
145 if (thisNode.TryGetProperty(
"#import", out var
import))
146 foreach (var imports
in import.EnumerateArray())
147 if (imports.GetString() is
string nextRid &&
string.IsNullOrWhiteSpace(nextRid) ==
false)
149 if (RuntimeJson.TryGetProperty(nextRid, out var nextNode))
150 q.Enqueue((nextRid, nextNode));