|
| 1 | +#r "Newtonsoft.Json.dll" |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | +using System.Reflection; |
| 8 | +using System.Runtime.InteropServices; |
| 9 | +using System.Runtime.CompilerServices; |
| 10 | +using Newtonsoft.Json.Linq; |
| 11 | + |
| 12 | +// The RIDs supported by LibGit2 - these match the directory names (runtimes/{name}/native). |
| 13 | +var availableRids = new[] |
| 14 | +{ |
| 15 | + "alpine-x64", |
| 16 | + "debian.9-x64", |
| 17 | + "fedora-x64", |
| 18 | + "linux-x64", |
| 19 | + "osx", |
| 20 | + "rhel-x64", |
| 21 | + "win-x64", |
| 22 | + "win-x86", |
| 23 | +}; |
| 24 | + |
| 25 | +// The path to runtime.json file retrieved from https://raw.githubusercontent.com/dotnet/corefx/master/pkg/Microsoft.NETCore.Platforms/runtime.json. |
| 26 | +var runtimeJsonPath = Path.Combine(GetScriptDir(), "runtime.json"); |
| 27 | + |
| 28 | +var runtimes = BuildRuntimeGraph(); |
| 29 | + |
| 30 | +var map = new Dictionary<string, string>(); |
| 31 | + |
| 32 | +foreach (var entry in runtimes) |
| 33 | +{ |
| 34 | + var rid = entry.Key; |
| 35 | + var compatibleRids = GetCompatibleRuntimeIdentifiers(runtimes, rid); |
| 36 | + foreach (var availableRid in availableRids) |
| 37 | + { |
| 38 | + if (compatibleRids.Contains(availableRid)) |
| 39 | + { |
| 40 | + // use the first rid, it is the most specific: |
| 41 | + if (!map.TryGetValue(rid, out var existing)) |
| 42 | + { |
| 43 | + map.Add(rid, availableRid); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +var orderedMap = map.OrderBy(e => e.Key); |
| 50 | + |
| 51 | +Console.WriteLine("private static readonly string[] s_rids = new[]"); |
| 52 | +Console.WriteLine("{"); |
| 53 | + |
| 54 | +foreach (var entry in orderedMap) |
| 55 | +{ |
| 56 | + Console.WriteLine($" \"{entry.Key}\","); |
| 57 | +} |
| 58 | + |
| 59 | +Console.WriteLine("}"); |
| 60 | +Console.WriteLine(); |
| 61 | +Console.WriteLine("private static readonly string[] s_directories = new[]"); |
| 62 | +Console.WriteLine("{"); |
| 63 | + |
| 64 | +foreach (var entry in orderedMap) |
| 65 | +{ |
| 66 | + Console.WriteLine($" \"{entry.Value}\","); |
| 67 | +} |
| 68 | + |
| 69 | +Console.WriteLine("}"); |
| 70 | + |
| 71 | +string GetScriptDir([CallerFilePath] string path = null) => Path.GetDirectoryName(path); |
| 72 | + |
| 73 | +Dictionary<string, Runtime> BuildRuntimeGraph() |
| 74 | +{ |
| 75 | + var rids = new Dictionary<string, Runtime>(); |
| 76 | + |
| 77 | + var json = JObject.Parse(File.ReadAllText(runtimeJsonPath)); |
| 78 | + var runtimes = (JObject)json["runtimes"]; |
| 79 | + |
| 80 | + foreach (var runtime in runtimes) |
| 81 | + { |
| 82 | + var imports = (JArray)((JObject)runtime.Value)["#import"]; |
| 83 | + rids.Add(runtime.Key, new Runtime(runtime.Key, imports.Select(import => (string)import).ToArray())); |
| 84 | + } |
| 85 | + |
| 86 | + return rids; |
| 87 | +} |
| 88 | + |
| 89 | +struct Runtime |
| 90 | +{ |
| 91 | + public string RuntimeIdentifier { get; } |
| 92 | + public string[] ImportedRuntimeIdentifiers { get; } |
| 93 | + |
| 94 | + public Runtime(string runtimeIdentifier, string[] importedRuntimeIdentifiers) |
| 95 | + { |
| 96 | + RuntimeIdentifier = runtimeIdentifier; |
| 97 | + ImportedRuntimeIdentifiers = importedRuntimeIdentifiers; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +List<string> GetCompatibleRuntimeIdentifiers(Dictionary<string, Runtime> runtimes, string runtimeIdentifier) |
| 102 | +{ |
| 103 | + var result = new List<string>(); |
| 104 | + |
| 105 | + if (runtimes.TryGetValue(runtimeIdentifier, out var initialRuntime)) |
| 106 | + { |
| 107 | + var queue = new Queue<Runtime>(); |
| 108 | + var hash = new HashSet<string>(); |
| 109 | + |
| 110 | + hash.Add(runtimeIdentifier); |
| 111 | + queue.Enqueue(initialRuntime); |
| 112 | + |
| 113 | + while (queue.Count > 0) |
| 114 | + { |
| 115 | + var runtime = queue.Dequeue(); |
| 116 | + result.Add(runtime.RuntimeIdentifier); |
| 117 | + |
| 118 | + foreach (var item in runtime.ImportedRuntimeIdentifiers) |
| 119 | + { |
| 120 | + if (hash.Add(item)) |
| 121 | + { |
| 122 | + queue.Enqueue(runtimes[item]); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + result.Add(runtimeIdentifier); |
| 130 | + } |
| 131 | + |
| 132 | + return result; |
| 133 | +} |
0 commit comments