From 26b1a0fca1b3ac754b6545ff57b8e559c2b3a66d Mon Sep 17 00:00:00 2001 From: Daniel Plaisted Date: Wed, 2 Dec 2020 20:47:22 -0800 Subject: [PATCH] Read current runtime identifiers for workload resolver --- .../redist/targets/OverlaySdkOnLKG.targets | 1 + .../TemplateLocator.cs | 5 +- .../WorkloadSdkResolver.cs | 5 +- .../WorkloadPack.cs | 6 +-- .../WorkloadResolver.cs | 49 ++----------------- .../ManifestReaderFunctionalTests.cs | 6 +-- .../ManifestTests.cs | 5 +- 7 files changed, 22 insertions(+), 55 deletions(-) diff --git a/src/Layout/redist/targets/OverlaySdkOnLKG.targets b/src/Layout/redist/targets/OverlaySdkOnLKG.targets index 9d7b137f9036..d41b279aee33 100644 --- a/src/Layout/redist/targets/OverlaySdkOnLKG.targets +++ b/src/Layout/redist/targets/OverlaySdkOnLKG.targets @@ -14,6 +14,7 @@ + diff --git a/src/Microsoft.DotNet.TemplateLocator/TemplateLocator.cs b/src/Microsoft.DotNet.TemplateLocator/TemplateLocator.cs index 13230a15214b..e31191481652 100644 --- a/src/Microsoft.DotNet.TemplateLocator/TemplateLocator.cs +++ b/src/Microsoft.DotNet.TemplateLocator/TemplateLocator.cs @@ -50,8 +50,11 @@ public IReadOnlyCollection GetDotnetSdkTemplate nameof(dotnetRootPath)); } + string runtimeIdentifierChainPath = Path.Combine(dotnetRootPath, "sdk", sdkVersion, "NETCoreSdkRuntimeIdentifierChain.txt"); + string[] currentRuntimeIdentifiers = File.ReadAllLines(runtimeIdentifierChainPath).Where(l => !string.IsNullOrEmpty(l)).ToArray(); + _workloadManifestProvider ??= new SdkDirectoryWorkloadManifestProvider(dotnetRootPath, sdkVersion); - _workloadResolver ??= new WorkloadResolver(_workloadManifestProvider, dotnetRootPath); + _workloadResolver ??= new WorkloadResolver(_workloadManifestProvider, dotnetRootPath, currentRuntimeIdentifiers); return _workloadResolver.GetInstalledWorkloadPacksOfKind(WorkloadPackKind.Template) .Select(pack => new OptionalSdkTemplatePackageInfo(pack.Id, pack.Version, pack.Path)).ToList(); diff --git a/src/Resolvers/Microsoft.NET.Sdk.WorkloadMSBuildSdkResolver/WorkloadSdkResolver.cs b/src/Resolvers/Microsoft.NET.Sdk.WorkloadMSBuildSdkResolver/WorkloadSdkResolver.cs index 3cef352ed9e1..53fdb691684c 100644 --- a/src/Resolvers/Microsoft.NET.Sdk.WorkloadMSBuildSdkResolver/WorkloadSdkResolver.cs +++ b/src/Resolvers/Microsoft.NET.Sdk.WorkloadMSBuildSdkResolver/WorkloadSdkResolver.cs @@ -71,9 +71,12 @@ private void InitializeWorkloadResolver(SdkResolverContext context) // The SDK version is the name of the SDK directory (ie dotnet\sdk\5.0.100) var sdkVersion = Path.GetFileName(sdkDirectory); + string runtimeIdentifierChainPath = Path.Combine(sdkDirectory, "NETCoreSdkRuntimeIdentifierChain.txt"); + string[] currentRuntimeIdentifiers = File.ReadAllLines(runtimeIdentifierChainPath).Where(l => !string.IsNullOrEmpty(l)).ToArray(); + _workloadManifestProvider ??= new SdkDirectoryWorkloadManifestProvider(dotnetRootPath, sdkVersion); - _workloadResolver ??= new WorkloadResolver(_workloadManifestProvider, dotnetRootPath); + _workloadResolver ??= new WorkloadResolver(_workloadManifestProvider, dotnetRootPath, currentRuntimeIdentifiers); } public override SdkResult Resolve(SdkReference sdkReference, SdkResolverContext resolverContext, SdkResultFactory factory) diff --git a/src/Resolvers/Microsoft.NET.Sdk.WorkloadManifestReader/WorkloadPack.cs b/src/Resolvers/Microsoft.NET.Sdk.WorkloadManifestReader/WorkloadPack.cs index 2287d9145460..f9743ad0c832 100644 --- a/src/Resolvers/Microsoft.NET.Sdk.WorkloadManifestReader/WorkloadPack.cs +++ b/src/Resolvers/Microsoft.NET.Sdk.WorkloadManifestReader/WorkloadPack.cs @@ -21,16 +21,16 @@ public WorkloadPack(WorkloadPackId id, string version, WorkloadPackKind kind, Di public bool IsAlias => AliasTo != null && AliasTo.Count > 0; public Dictionary? AliasTo { get; } - public WorkloadPackId? TryGetAliasForPlatformIds (IEnumerable platformIds) + public WorkloadPackId? TryGetAliasForRuntimeIdentifiers(IEnumerable runtimeIdentifiers) { if (AliasTo == null || AliasTo.Count == 0) { return null; } - foreach (var platformId in platformIds) + foreach (var runtimeIdentifier in runtimeIdentifiers) { - if (AliasTo.TryGetValue(platformId, out WorkloadPackId alias)) + if (AliasTo.TryGetValue(runtimeIdentifier, out WorkloadPackId alias)) { return alias; } diff --git a/src/Resolvers/Microsoft.NET.Sdk.WorkloadManifestReader/WorkloadResolver.cs b/src/Resolvers/Microsoft.NET.Sdk.WorkloadManifestReader/WorkloadResolver.cs index 67b0852f6369..3b7d1920a1ba 100644 --- a/src/Resolvers/Microsoft.NET.Sdk.WorkloadManifestReader/WorkloadResolver.cs +++ b/src/Resolvers/Microsoft.NET.Sdk.WorkloadManifestReader/WorkloadResolver.cs @@ -17,27 +17,17 @@ public class WorkloadResolver : IWorkloadResolver { private readonly Dictionary _workloads = new Dictionary(); private readonly Dictionary _packs = new Dictionary(); - private string[] _platformIds; + private string[] _currentRuntimeIdentifiers; private readonly string _dotNetRootPath; private Func? _fileExistOverride; private Func? _directoryExistOverride; - public WorkloadResolver(IWorkloadManifestProvider manifestProvider, string dotNetRootPath) + public WorkloadResolver(IWorkloadManifestProvider manifestProvider, string dotNetRootPath, string [] currentRuntimeIdentifiers) { this._dotNetRootPath = dotNetRootPath; - // eventually we may want a series of fallbacks here, as rids have in general - // but for now, keep it simple - var platformId = GetHostPlatformId(); - if (platformId != null) - { - _platformIds = new[] { platformId, "*" }; - } - else - { - _platformIds = new[] { "*" }; - } + _currentRuntimeIdentifiers = currentRuntimeIdentifiers; var manifests = new List(); @@ -63,32 +53,6 @@ public WorkloadResolver(IWorkloadManifestProvider manifestProvider, string dotNe } } - - // rather that forcing all consumers to depend on and parse the RID catalog, or doing that here, for now just bake in a small - // subset of dev host platform rids for now for the workloads that are likely to need this functionality soonest - private string? GetHostPlatformId() - { - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - { - return RuntimeInformation.OSArchitecture switch - { - Architecture.X64 => "osx-x64", - Architecture.Arm64 => "osx-arm64", - _ => null - }; - } - - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - if (RuntimeInformation.OSArchitecture == Architecture.X64) - { - return "win-x64"; - } - } - - return null; - } - /// /// Gets the installed workload packs of a particular kind /// @@ -119,14 +83,9 @@ internal void ReplaceFilesystemChecksForTest(Func fileExists, Func _directoryExistOverride = directoryExists; } - internal void ReplacePlatformIdsForTest(string[] platformIds) - { - this._platformIds = platformIds; - } - private PackInfo CreatePackInfo(WorkloadPack pack) { - var aliasedId = pack.TryGetAliasForPlatformIds(_platformIds) ?? pack.Id; + var aliasedId = pack.TryGetAliasForRuntimeIdentifiers(_currentRuntimeIdentifiers) ?? pack.Id; var packPath = GetPackPath(_dotNetRootPath, aliasedId, pack.Version, pack.Kind); return new PackInfo( diff --git a/src/Tests/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/ManifestReaderFunctionalTests.cs b/src/Tests/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/ManifestReaderFunctionalTests.cs index c44a19faa2f6..416344c71c2b 100644 --- a/src/Tests/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/ManifestReaderFunctionalTests.cs +++ b/src/Tests/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/ManifestReaderFunctionalTests.cs @@ -43,7 +43,7 @@ private static WorkloadResolver SetUp() { var workloadResolver = new WorkloadResolver(new FakeManifestProvider(new[] {Path.Combine("Manifests", "Sample.json")}), - "fakepath"); + "fakepath", ManifestTests.TEST_RUNTIME_IDENTIFIER_CHAIN); workloadResolver.ReplaceFilesystemChecksForTest(fileExists: (_) => true, directoryExists: (_) => true); return workloadResolver; @@ -54,7 +54,7 @@ public void GivenTemplateNupkgDoesNotExistOnDiskItShouldReturnEmpty() { var workloadResolver = new WorkloadResolver(new FakeManifestProvider(new[] {Path.Combine("Manifests", "Sample.json")}), - "fakepath"); + "fakepath", ManifestTests.TEST_RUNTIME_IDENTIFIER_CHAIN); workloadResolver.ReplaceFilesystemChecksForTest(fileExists: (_) => false, directoryExists: (_) => true); var result = workloadResolver.GetInstalledWorkloadPacksOfKind(WorkloadPackKind.Template); result.Should().HaveCount(0); @@ -65,7 +65,7 @@ public void GivenWorkloadSDKsDirectoryNotExistOnDiskItShouldReturnEmpty() { var workloadResolver = new WorkloadResolver(new FakeManifestProvider(new[] {Path.Combine("Manifests", "Sample.json")}), - "fakepath"); + "fakepath", ManifestTests.TEST_RUNTIME_IDENTIFIER_CHAIN); workloadResolver.ReplaceFilesystemChecksForTest(fileExists: (_) => true, directoryExists: (_) => false); var result = workloadResolver.GetInstalledWorkloadPacksOfKind(WorkloadPackKind.Sdk); result.Should().HaveCount(0); diff --git a/src/Tests/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/ManifestTests.cs b/src/Tests/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/ManifestTests.cs index 84b1f85d2a19..00fb1fb4357c 100644 --- a/src/Tests/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/ManifestTests.cs +++ b/src/Tests/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/ManifestTests.cs @@ -13,6 +13,8 @@ public class ManifestTests { private const string fakeRootPath = "fakeRootPath"; + public static readonly string[] TEST_RUNTIME_IDENTIFIER_CHAIN = new[] { "win-x64", "win", "any", "base" }; + [Fact] public void ItCanDeserialize() { @@ -33,10 +35,9 @@ public void ItCanDeserialize() public void AliasedPackPath() { var manifestProvider = new FakeManifestProvider(Path.Combine("Manifests", "Sample.json")); - var resolver = new WorkloadResolver(manifestProvider, fakeRootPath); + var resolver = new WorkloadResolver(manifestProvider, fakeRootPath, TEST_RUNTIME_IDENTIFIER_CHAIN); resolver.ReplaceFilesystemChecksForTest(_ => true, _ => true); - resolver.ReplacePlatformIdsForTest(new[] { "win-x64", "*" }); var buildToolsPack = resolver.GetInstalledWorkloadPacksOfKind(WorkloadPackKind.Sdk).FirstOrDefault(pack => pack.Id == "Xamarin.Android.BuildTools");