-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
FrameworkDependencyFile.cs
121 lines (102 loc) · 4.3 KB
/
FrameworkDependencyFile.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Extensions.DependencyModel;
namespace Microsoft.DotNet.Cli.Utils
{
/// <summary>
/// Represents the .deps.json file in the shared framework
/// that the CLI is running against.
/// </summary>
internal class FrameworkDependencyFile
{
private readonly string _depsFilePath;
private readonly Lazy<DependencyContext> _dependencyContext;
private DependencyContext DependencyContext => _dependencyContext.Value;
public FrameworkDependencyFile()
{
_depsFilePath = Muxer.GetDataFromAppDomain("FX_DEPS_FILE");
_dependencyContext = new Lazy<DependencyContext>(CreateDependencyContext);
}
public bool IsRuntimeSupported(string runtimeIdentifier)
{
return DependencyContext.RuntimeGraph.Any(g => g.Runtime == runtimeIdentifier);
}
public string GetNetStandardLibraryVersion()
{
return DependencyContext
.RuntimeLibraries
.FirstOrDefault(l => "netstandard.library".Equals(l.Name, StringComparison.OrdinalIgnoreCase))
?.Version;
}
#if NETCOREAPP
public bool TryGetMostFitRuntimeIdentifier(
string alternativeCurrentRuntimeIdentifier,
string[] candidateRuntimeIdentifiers,
out string mostFitRuntimeIdentifier)
{
return TryGetMostFitRuntimeIdentifier(
RuntimeInformation.RuntimeIdentifier,
alternativeCurrentRuntimeIdentifier,
DependencyContext.RuntimeGraph,
candidateRuntimeIdentifiers,
out mostFitRuntimeIdentifier);
}
#endif
internal static bool TryGetMostFitRuntimeIdentifier(
string currentRuntimeIdentifier,
string alternativeCurrentRuntimeIdentifier,
IReadOnlyList<RuntimeFallbacks> runtimeGraph,
string[] candidateRuntimeIdentifiers,
out string mostFitRuntimeIdentifier)
{
mostFitRuntimeIdentifier = null;
RuntimeFallbacks[] runtimeFallbacksCandidates;
if (!string.IsNullOrEmpty(currentRuntimeIdentifier))
{
runtimeFallbacksCandidates =
runtimeGraph
.Where(g => string.Equals(g.Runtime, currentRuntimeIdentifier, StringComparison.OrdinalIgnoreCase))
.ToArray();
}
else
{
runtimeFallbacksCandidates = Array.Empty<RuntimeFallbacks>();
}
if (runtimeFallbacksCandidates.Length == 0 && !string.IsNullOrEmpty(alternativeCurrentRuntimeIdentifier))
{
runtimeFallbacksCandidates =
runtimeGraph
.Where(g => string.Equals(g.Runtime, alternativeCurrentRuntimeIdentifier, StringComparison.OrdinalIgnoreCase))
.ToArray();
}
if (runtimeFallbacksCandidates.Length == 0)
{
return false;
}
RuntimeFallbacks runtimeFallbacks = runtimeFallbacksCandidates[0];
var runtimeFallbacksIncludesRuntime = new List<string>();
runtimeFallbacksIncludesRuntime.Add(runtimeFallbacks.Runtime);
runtimeFallbacksIncludesRuntime.AddRange(runtimeFallbacks.Fallbacks);
var candidateMap = candidateRuntimeIdentifiers
.Distinct(comparer: StringComparer.OrdinalIgnoreCase)
.ToDictionary(x => x, StringComparer.OrdinalIgnoreCase);
foreach (var fallback in runtimeFallbacksIncludesRuntime)
{
if (candidateMap.TryGetValue(fallback, out string match))
{
mostFitRuntimeIdentifier = match;
return true;
}
}
return false;
}
private DependencyContext CreateDependencyContext()
{
using (Stream depsFileStream = File.OpenRead(_depsFilePath))
using (DependencyContextJsonReader reader = new DependencyContextJsonReader())
{
return reader.Read(depsFileStream);
}
}
}
}