Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit 2f01bb4

Browse files
author
William Li
authored
Add TryGetMostFitRuntimeIdentifier (#8997)
1 parent 56c10f6 commit 2f01bb4

File tree

2 files changed

+220
-0
lines changed

2 files changed

+220
-0
lines changed

src/Microsoft.DotNet.Cli.Utils/FrameworkDependencyFile.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.IO;
67
using System.Linq;
78
using Microsoft.DotNet.PlatformAbstractions;
@@ -45,6 +46,78 @@ public string GetNetStandardLibraryVersion()
4546
?.Version;
4647
}
4748

49+
public bool TryGetMostFitRuntimeIdentifier(
50+
string alternativeCurrentRuntimeIdentifier,
51+
string[] candidateRuntimeIdentifiers,
52+
out string mostFitRuntimeIdentifier)
53+
{
54+
return TryGetMostFitRuntimeIdentifier(
55+
RuntimeEnvironment.GetRuntimeIdentifier(),
56+
alternativeCurrentRuntimeIdentifier,
57+
DependencyContext.RuntimeGraph,
58+
candidateRuntimeIdentifiers,
59+
out mostFitRuntimeIdentifier);
60+
}
61+
62+
internal static bool TryGetMostFitRuntimeIdentifier(
63+
string currentRuntimeIdentifier,
64+
string alternativeCurrentRuntimeIdentifier,
65+
IReadOnlyList<RuntimeFallbacks> runtimeGraph,
66+
string[] candidateRuntimeIdentifiers,
67+
out string mostFitRuntimeIdentifier)
68+
{
69+
mostFitRuntimeIdentifier = null;
70+
RuntimeFallbacks[] runtimeFallbacksCandidates;
71+
72+
if (!string.IsNullOrEmpty(currentRuntimeIdentifier))
73+
{
74+
runtimeFallbacksCandidates =
75+
runtimeGraph
76+
.Where(g => string.Equals(g.Runtime, currentRuntimeIdentifier, StringComparison.OrdinalIgnoreCase))
77+
.ToArray();
78+
}
79+
else
80+
{
81+
runtimeFallbacksCandidates = Array.Empty<RuntimeFallbacks>();
82+
}
83+
84+
if (runtimeFallbacksCandidates.Length == 0 && !string.IsNullOrEmpty(alternativeCurrentRuntimeIdentifier))
85+
{
86+
runtimeFallbacksCandidates =
87+
runtimeGraph
88+
.Where(g => string.Equals(g.Runtime, alternativeCurrentRuntimeIdentifier, StringComparison.OrdinalIgnoreCase))
89+
.ToArray();
90+
}
91+
92+
if (runtimeFallbacksCandidates.Length == 0)
93+
{
94+
return false;
95+
}
96+
97+
RuntimeFallbacks runtimeFallbacks = runtimeFallbacksCandidates[0];
98+
99+
var runtimeFallbacksIncludesRuntime = new List<string>();
100+
runtimeFallbacksIncludesRuntime.Add(runtimeFallbacks.Runtime);
101+
runtimeFallbacksIncludesRuntime.AddRange(runtimeFallbacks.Fallbacks);
102+
103+
104+
var candidateMap = candidateRuntimeIdentifiers
105+
.Distinct(comparer: StringComparer.OrdinalIgnoreCase)
106+
.ToDictionary(x => x, StringComparer.OrdinalIgnoreCase);
107+
108+
foreach (var fallback in runtimeFallbacksIncludesRuntime)
109+
{
110+
if (candidateMap.TryGetValue(fallback, out string match))
111+
{
112+
mostFitRuntimeIdentifier = match;
113+
114+
return true;
115+
}
116+
}
117+
118+
return false;
119+
}
120+
48121
private DependencyContext CreateDependencyContext()
49122
{
50123
using (Stream depsFileStream = File.OpenRead(_depsFilePath))
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Collections.Generic;
5+
using FluentAssertions;
6+
using Microsoft.DotNet.Tools.Test.Utilities;
7+
using Microsoft.Extensions.DependencyModel;
8+
using Xunit;
9+
10+
namespace Microsoft.DotNet.Cli.Utils.Tests
11+
{
12+
public class GivenAFrameworkDependencyFile
13+
{
14+
private readonly IReadOnlyList<RuntimeFallbacks> _testRuntimeGraph;
15+
16+
public GivenAFrameworkDependencyFile()
17+
{
18+
_testRuntimeGraph = new List<RuntimeFallbacks>
19+
{
20+
new RuntimeFallbacks("win-x64", new [] { "win", "any", "base" }),
21+
new RuntimeFallbacks("win8", new [] { "win7", "win", "any", "base" }),
22+
new RuntimeFallbacks("win7", new [] { "win", "any", "base" }),
23+
new RuntimeFallbacks("win", new [] { "any", "base" }),
24+
};
25+
}
26+
27+
[Fact]
28+
public void WhenPassSeveralCompatibleRuntimeIdentifiersItOutMostFitRid()
29+
{
30+
FrameworkDependencyFile.TryGetMostFitRuntimeIdentifier(
31+
currentRuntimeIdentifier: "win7",
32+
alternativeCurrentRuntimeIdentifier : "win",
33+
runtimeGraph : _testRuntimeGraph,
34+
candidateRuntimeIdentifiers : new [] { "win", "any" },
35+
mostFitRuntimeIdentifier : out string mostFitRid)
36+
.Should().BeTrue();
37+
38+
mostFitRid.Should().Be("win");
39+
}
40+
41+
[Fact]
42+
public void WhenPassSeveralCompatibleRuntimeIdentifiersItOutMostFitRid2()
43+
{
44+
FrameworkDependencyFile.TryGetMostFitRuntimeIdentifier(
45+
currentRuntimeIdentifier: "win",
46+
alternativeCurrentRuntimeIdentifier: null,
47+
runtimeGraph: _testRuntimeGraph,
48+
candidateRuntimeIdentifiers: new[] { "win", "any" },
49+
mostFitRuntimeIdentifier: out string mostFitRid)
50+
.Should().BeTrue();
51+
52+
mostFitRid.Should().Be("win");
53+
}
54+
55+
[Fact]
56+
public void WhenPassSeveralCompatibleRuntimeIdentifiersAndCurrentRuntimeIdentifierIsNullReturnsFalse()
57+
{
58+
FrameworkDependencyFile.TryGetMostFitRuntimeIdentifier(
59+
currentRuntimeIdentifier: null,
60+
alternativeCurrentRuntimeIdentifier: null,
61+
runtimeGraph: _testRuntimeGraph,
62+
candidateRuntimeIdentifiers: new[] { "win", "any" },
63+
mostFitRuntimeIdentifier: out string mostFitRid)
64+
.Should().BeFalse();
65+
}
66+
67+
[Fact]
68+
public void WhenPassSeveralCompatibleRuntimeIdentifiersItOutMostFitRidWithCasingPreserved()
69+
{
70+
FrameworkDependencyFile.TryGetMostFitRuntimeIdentifier(
71+
currentRuntimeIdentifier: "win7",
72+
alternativeCurrentRuntimeIdentifier : null,
73+
runtimeGraph : _testRuntimeGraph,
74+
candidateRuntimeIdentifiers : new [] { "Win", "any" },
75+
mostFitRuntimeIdentifier : out string mostFitRid)
76+
.Should().BeTrue();
77+
78+
mostFitRid.Should().Be("Win");
79+
}
80+
81+
[Fact]
82+
public void WhenPassSeveralCompatibleRuntimeIdentifiersWithDuplicationItOutMostFitRid()
83+
{
84+
FrameworkDependencyFile.TryGetMostFitRuntimeIdentifier(
85+
currentRuntimeIdentifier: "win7",
86+
alternativeCurrentRuntimeIdentifier : null,
87+
runtimeGraph : _testRuntimeGraph,
88+
candidateRuntimeIdentifiers : new [] { "win", "win", "any" },
89+
mostFitRuntimeIdentifier : out string mostFitRid)
90+
.Should().BeTrue();
91+
92+
mostFitRid.Should().Be("win");
93+
}
94+
95+
[Fact]
96+
public void WhenPassSeveralCompatibleRuntimeIdentifiersAndDuplicationItOutMostFitRidWithCasingPreservedTheFirstIsFavoriated()
97+
{
98+
FrameworkDependencyFile.TryGetMostFitRuntimeIdentifier(
99+
currentRuntimeIdentifier: "win7",
100+
alternativeCurrentRuntimeIdentifier: null,
101+
runtimeGraph: _testRuntimeGraph,
102+
candidateRuntimeIdentifiers: new[] { "Win", "win", "win", "any" },
103+
mostFitRuntimeIdentifier: out string mostFitRid)
104+
.Should().BeTrue();
105+
106+
mostFitRid.Should().Be("Win");
107+
}
108+
109+
[Fact]
110+
public void WhenPassSeveralNonCompatibleRuntimeIdentifiersItReturnsFalse()
111+
{
112+
FrameworkDependencyFile.TryGetMostFitRuntimeIdentifier(
113+
currentRuntimeIdentifier: "win7",
114+
alternativeCurrentRuntimeIdentifier : null,
115+
runtimeGraph : _testRuntimeGraph,
116+
candidateRuntimeIdentifiers : new [] { "centos", "debian" },
117+
mostFitRuntimeIdentifier : out string mostFitRid)
118+
.Should().BeFalse();
119+
}
120+
121+
[Fact]
122+
public void WhenCurrentRuntimeIdentifierIsNotSupportedItUsesAlternative()
123+
{
124+
FrameworkDependencyFile.TryGetMostFitRuntimeIdentifier(
125+
currentRuntimeIdentifier: "win-vnext",
126+
alternativeCurrentRuntimeIdentifier: "win8",
127+
runtimeGraph: _testRuntimeGraph,
128+
candidateRuntimeIdentifiers: new[] { "win", "any" },
129+
mostFitRuntimeIdentifier: out string mostFitRid)
130+
.Should().BeTrue();
131+
132+
mostFitRid.Should().Be("win");
133+
}
134+
135+
[Fact]
136+
public void WhenCurrentRuntimeIdentifierIsNotSupportedSoIsTheAlternativeItReturnsFalse()
137+
{
138+
FrameworkDependencyFile.TryGetMostFitRuntimeIdentifier(
139+
currentRuntimeIdentifier: "osx10.13-x64",
140+
alternativeCurrentRuntimeIdentifier: "osx-x64",
141+
runtimeGraph: _testRuntimeGraph,
142+
candidateRuntimeIdentifiers: new[] { "win", "any" },
143+
mostFitRuntimeIdentifier: out string mostFitRid)
144+
.Should().BeFalse();
145+
}
146+
}
147+
}

0 commit comments

Comments
 (0)