|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using Microsoft.Extensions.DependencyModel; |
| 9 | +using Newtonsoft.Json.Linq; |
| 10 | + |
| 11 | +namespace Microsoft.Azure.WebJobs.Script.Description |
| 12 | +{ |
| 13 | + public static class DependencyHelper |
| 14 | + { |
| 15 | + private static readonly Lazy<Dictionary<string, string[]>> _ridGraph = new Lazy<Dictionary<string, string[]>>(BuildRuntimesGraph); |
| 16 | + |
| 17 | + private static Dictionary<string, string[]> BuildRuntimesGraph() |
| 18 | + { |
| 19 | + var ridGraph = new Dictionary<string, string[]>(); |
| 20 | + string runtimesJson = GetRuntimesGraphJson(); |
| 21 | + var runtimes = (JObject)JObject.Parse(runtimesJson)["runtimes"]; |
| 22 | + |
| 23 | + foreach (var runtime in runtimes) |
| 24 | + { |
| 25 | + string[] imports = ((JObject)runtime.Value)["#import"] |
| 26 | + ?.Values<string>() |
| 27 | + .ToArray(); |
| 28 | + |
| 29 | + ridGraph.Add(runtime.Key, imports); |
| 30 | + } |
| 31 | + |
| 32 | + return ridGraph; |
| 33 | + } |
| 34 | + |
| 35 | + public static IEnumerable<string> GetFallbacks(Dictionary<string, string[]> ridGraph, string rid) |
| 36 | + { |
| 37 | + var fallbacks = new HashSet<string>(); |
| 38 | + var queue = new Queue<string>(ridGraph[rid]); |
| 39 | + |
| 40 | + while (queue.Count > 0) |
| 41 | + { |
| 42 | + var currentRid = queue.Dequeue(); |
| 43 | + |
| 44 | + if (fallbacks.Contains(currentRid)) |
| 45 | + { |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + fallbacks.Add(currentRid); |
| 50 | + |
| 51 | + foreach (var fallbackRid in ridGraph[currentRid]) |
| 52 | + { |
| 53 | + if (!fallbacks.Contains(fallbackRid)) |
| 54 | + { |
| 55 | + queue.Enqueue(fallbackRid); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return fallbacks; |
| 61 | + } |
| 62 | + |
| 63 | + private static string GetRuntimeAssembliesJson() |
| 64 | + { |
| 65 | + return GetResourceFileContents("runtimeassemblies.json"); |
| 66 | + } |
| 67 | + |
| 68 | + private static string GetRuntimesGraphJson() |
| 69 | + { |
| 70 | + return GetResourceFileContents("runtimes.json"); |
| 71 | + } |
| 72 | + |
| 73 | + private static string GetResourceFileContents(string fileName) |
| 74 | + { |
| 75 | + var assembly = typeof(DependencyHelper).Assembly; |
| 76 | + using (Stream resource = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.{fileName}")) |
| 77 | + using (var reader = new StreamReader(resource)) |
| 78 | + { |
| 79 | + return reader.ReadToEnd(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + internal static Dictionary<string, ScriptRuntimeAssembly> GetRuntimeAssemblies() |
| 84 | + { |
| 85 | + string assembliesJson = GetRuntimeAssembliesJson(); |
| 86 | + JObject assemblies = JObject.Parse(assembliesJson); |
| 87 | + |
| 88 | + return assemblies["runtimeAssemblies"] |
| 89 | + .ToObject<ScriptRuntimeAssembly[]>() |
| 90 | + .ToDictionary(a => a.Name, StringComparer.OrdinalIgnoreCase); |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Gets the default runtime fallback RIDs for a given RID. |
| 95 | + /// The graph used to build the fallback list is static and |
| 96 | + /// useful in self-contained scenarios, where this information |
| 97 | + /// is not available at runtime |
| 98 | + /// </summary> |
| 99 | + /// <param name="rid">The runtime identifier to lookup.</param> |
| 100 | + /// <returns>The runtime fallbacks for the provided identifier.</returns> |
| 101 | + public static RuntimeFallbacks GetDefaultRuntimeFallbacks(string rid) |
| 102 | + { |
| 103 | + IEnumerable<string> fallbacks = GetFallbacks(_ridGraph.Value, rid); |
| 104 | + |
| 105 | + return new RuntimeFallbacks(rid, fallbacks); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments