Add TryGetMostFitRuntimeIdentifier (#8997)
This commit is contained in:
parent
56c10f65c7
commit
2f01bb4fad
2 changed files with 220 additions and 0 deletions
|
@ -2,6 +2,7 @@
|
|||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.DotNet.PlatformAbstractions;
|
||||
|
@ -45,6 +46,78 @@ namespace Microsoft.DotNet.Cli.Utils
|
|||
?.Version;
|
||||
}
|
||||
|
||||
public bool TryGetMostFitRuntimeIdentifier(
|
||||
string alternativeCurrentRuntimeIdentifier,
|
||||
string[] candidateRuntimeIdentifiers,
|
||||
out string mostFitRuntimeIdentifier)
|
||||
{
|
||||
return TryGetMostFitRuntimeIdentifier(
|
||||
RuntimeEnvironment.GetRuntimeIdentifier(),
|
||||
alternativeCurrentRuntimeIdentifier,
|
||||
DependencyContext.RuntimeGraph,
|
||||
candidateRuntimeIdentifiers,
|
||||
out mostFitRuntimeIdentifier);
|
||||
}
|
||||
|
||||
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))
|
||||
|
|
|
@ -0,0 +1,147 @@
|
|||
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using FluentAssertions;
|
||||
using Microsoft.DotNet.Tools.Test.Utilities;
|
||||
using Microsoft.Extensions.DependencyModel;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.DotNet.Cli.Utils.Tests
|
||||
{
|
||||
public class GivenAFrameworkDependencyFile
|
||||
{
|
||||
private readonly IReadOnlyList<RuntimeFallbacks> _testRuntimeGraph;
|
||||
|
||||
public GivenAFrameworkDependencyFile()
|
||||
{
|
||||
_testRuntimeGraph = new List<RuntimeFallbacks>
|
||||
{
|
||||
new RuntimeFallbacks("win-x64", new [] { "win", "any", "base" }),
|
||||
new RuntimeFallbacks("win8", new [] { "win7", "win", "any", "base" }),
|
||||
new RuntimeFallbacks("win7", new [] { "win", "any", "base" }),
|
||||
new RuntimeFallbacks("win", new [] { "any", "base" }),
|
||||
};
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WhenPassSeveralCompatibleRuntimeIdentifiersItOutMostFitRid()
|
||||
{
|
||||
FrameworkDependencyFile.TryGetMostFitRuntimeIdentifier(
|
||||
currentRuntimeIdentifier: "win7",
|
||||
alternativeCurrentRuntimeIdentifier : "win",
|
||||
runtimeGraph : _testRuntimeGraph,
|
||||
candidateRuntimeIdentifiers : new [] { "win", "any" },
|
||||
mostFitRuntimeIdentifier : out string mostFitRid)
|
||||
.Should().BeTrue();
|
||||
|
||||
mostFitRid.Should().Be("win");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WhenPassSeveralCompatibleRuntimeIdentifiersItOutMostFitRid2()
|
||||
{
|
||||
FrameworkDependencyFile.TryGetMostFitRuntimeIdentifier(
|
||||
currentRuntimeIdentifier: "win",
|
||||
alternativeCurrentRuntimeIdentifier: null,
|
||||
runtimeGraph: _testRuntimeGraph,
|
||||
candidateRuntimeIdentifiers: new[] { "win", "any" },
|
||||
mostFitRuntimeIdentifier: out string mostFitRid)
|
||||
.Should().BeTrue();
|
||||
|
||||
mostFitRid.Should().Be("win");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WhenPassSeveralCompatibleRuntimeIdentifiersAndCurrentRuntimeIdentifierIsNullReturnsFalse()
|
||||
{
|
||||
FrameworkDependencyFile.TryGetMostFitRuntimeIdentifier(
|
||||
currentRuntimeIdentifier: null,
|
||||
alternativeCurrentRuntimeIdentifier: null,
|
||||
runtimeGraph: _testRuntimeGraph,
|
||||
candidateRuntimeIdentifiers: new[] { "win", "any" },
|
||||
mostFitRuntimeIdentifier: out string mostFitRid)
|
||||
.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WhenPassSeveralCompatibleRuntimeIdentifiersItOutMostFitRidWithCasingPreserved()
|
||||
{
|
||||
FrameworkDependencyFile.TryGetMostFitRuntimeIdentifier(
|
||||
currentRuntimeIdentifier: "win7",
|
||||
alternativeCurrentRuntimeIdentifier : null,
|
||||
runtimeGraph : _testRuntimeGraph,
|
||||
candidateRuntimeIdentifiers : new [] { "Win", "any" },
|
||||
mostFitRuntimeIdentifier : out string mostFitRid)
|
||||
.Should().BeTrue();
|
||||
|
||||
mostFitRid.Should().Be("Win");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WhenPassSeveralCompatibleRuntimeIdentifiersWithDuplicationItOutMostFitRid()
|
||||
{
|
||||
FrameworkDependencyFile.TryGetMostFitRuntimeIdentifier(
|
||||
currentRuntimeIdentifier: "win7",
|
||||
alternativeCurrentRuntimeIdentifier : null,
|
||||
runtimeGraph : _testRuntimeGraph,
|
||||
candidateRuntimeIdentifiers : new [] { "win", "win", "any" },
|
||||
mostFitRuntimeIdentifier : out string mostFitRid)
|
||||
.Should().BeTrue();
|
||||
|
||||
mostFitRid.Should().Be("win");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WhenPassSeveralCompatibleRuntimeIdentifiersAndDuplicationItOutMostFitRidWithCasingPreservedTheFirstIsFavoriated()
|
||||
{
|
||||
FrameworkDependencyFile.TryGetMostFitRuntimeIdentifier(
|
||||
currentRuntimeIdentifier: "win7",
|
||||
alternativeCurrentRuntimeIdentifier: null,
|
||||
runtimeGraph: _testRuntimeGraph,
|
||||
candidateRuntimeIdentifiers: new[] { "Win", "win", "win", "any" },
|
||||
mostFitRuntimeIdentifier: out string mostFitRid)
|
||||
.Should().BeTrue();
|
||||
|
||||
mostFitRid.Should().Be("Win");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WhenPassSeveralNonCompatibleRuntimeIdentifiersItReturnsFalse()
|
||||
{
|
||||
FrameworkDependencyFile.TryGetMostFitRuntimeIdentifier(
|
||||
currentRuntimeIdentifier: "win7",
|
||||
alternativeCurrentRuntimeIdentifier : null,
|
||||
runtimeGraph : _testRuntimeGraph,
|
||||
candidateRuntimeIdentifiers : new [] { "centos", "debian" },
|
||||
mostFitRuntimeIdentifier : out string mostFitRid)
|
||||
.Should().BeFalse();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WhenCurrentRuntimeIdentifierIsNotSupportedItUsesAlternative()
|
||||
{
|
||||
FrameworkDependencyFile.TryGetMostFitRuntimeIdentifier(
|
||||
currentRuntimeIdentifier: "win-vnext",
|
||||
alternativeCurrentRuntimeIdentifier: "win8",
|
||||
runtimeGraph: _testRuntimeGraph,
|
||||
candidateRuntimeIdentifiers: new[] { "win", "any" },
|
||||
mostFitRuntimeIdentifier: out string mostFitRid)
|
||||
.Should().BeTrue();
|
||||
|
||||
mostFitRid.Should().Be("win");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WhenCurrentRuntimeIdentifierIsNotSupportedSoIsTheAlternativeItReturnsFalse()
|
||||
{
|
||||
FrameworkDependencyFile.TryGetMostFitRuntimeIdentifier(
|
||||
currentRuntimeIdentifier: "osx10.13-x64",
|
||||
alternativeCurrentRuntimeIdentifier: "osx-x64",
|
||||
runtimeGraph: _testRuntimeGraph,
|
||||
candidateRuntimeIdentifiers: new[] { "win", "any" },
|
||||
mostFitRuntimeIdentifier: out string mostFitRid)
|
||||
.Should().BeFalse();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue