diff --git a/src/Microsoft.DotNet.TemplateLocator/TemplateLocator.cs b/src/Microsoft.DotNet.TemplateLocator/TemplateLocator.cs index e31191481652..f751449afd35 100644 --- a/src/Microsoft.DotNet.TemplateLocator/TemplateLocator.cs +++ b/src/Microsoft.DotNet.TemplateLocator/TemplateLocator.cs @@ -50,11 +50,8 @@ 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, currentRuntimeIdentifiers); + _workloadResolver ??= WorkloadResolver.Create(_workloadManifestProvider, dotnetRootPath, sdkVersion); 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 53fdb691684c..a66af486f287 100644 --- a/src/Resolvers/Microsoft.NET.Sdk.WorkloadMSBuildSdkResolver/WorkloadSdkResolver.cs +++ b/src/Resolvers/Microsoft.NET.Sdk.WorkloadMSBuildSdkResolver/WorkloadSdkResolver.cs @@ -71,12 +71,8 @@ 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, currentRuntimeIdentifiers); + _workloadResolver ??= WorkloadResolver.Create(_workloadManifestProvider, dotnetRootPath, sdkVersion); } public override SdkResult Resolve(SdkReference sdkReference, SdkResolverContext resolverContext, SdkResultFactory factory) diff --git a/src/Resolvers/Microsoft.NET.Sdk.WorkloadManifestReader/WorkloadResolver.cs b/src/Resolvers/Microsoft.NET.Sdk.WorkloadManifestReader/WorkloadResolver.cs index 3b7d1920a1ba..ea9b30342855 100644 --- a/src/Resolvers/Microsoft.NET.Sdk.WorkloadManifestReader/WorkloadResolver.cs +++ b/src/Resolvers/Microsoft.NET.Sdk.WorkloadManifestReader/WorkloadResolver.cs @@ -18,14 +18,29 @@ public class WorkloadResolver : IWorkloadResolver private readonly Dictionary _workloads = new Dictionary(); private readonly Dictionary _packs = new Dictionary(); private string[] _currentRuntimeIdentifiers; - private readonly string _dotNetRootPath; + private readonly string _dotnetRootPath; private Func? _fileExistOverride; private Func? _directoryExistOverride; - public WorkloadResolver(IWorkloadManifestProvider manifestProvider, string dotNetRootPath, string [] currentRuntimeIdentifiers) + public static WorkloadResolver Create(IWorkloadManifestProvider manifestProvider, string dotnetRootPath, string sdkVersion) { - this._dotNetRootPath = dotNetRootPath; + string runtimeIdentifierChainPath = Path.Combine(dotnetRootPath, "sdk", sdkVersion, "NETCoreSdkRuntimeIdentifierChain.txt"); + string[] currentRuntimeIdentifiers = File.Exists(runtimeIdentifierChainPath) ? + File.ReadAllLines(runtimeIdentifierChainPath).Where(l => !string.IsNullOrEmpty(l)).ToArray() : + new string[] { }; + + return new WorkloadResolver(manifestProvider, dotnetRootPath, currentRuntimeIdentifiers); + } + + public static WorkloadResolver CreateForTests(IWorkloadManifestProvider manifestProvider, string dotNetRootPath, string[] currentRuntimeIdentifiers) + { + return new WorkloadResolver(manifestProvider, dotNetRootPath, currentRuntimeIdentifiers); + } + + private WorkloadResolver(IWorkloadManifestProvider manifestProvider, string dotnetRootPath, string [] currentRuntimeIdentifiers) + { + this._dotnetRootPath = dotnetRootPath; _currentRuntimeIdentifiers = currentRuntimeIdentifiers; @@ -86,7 +101,7 @@ internal void ReplaceFilesystemChecksForTest(Func fileExists, Func private PackInfo CreatePackInfo(WorkloadPack pack) { var aliasedId = pack.TryGetAliasForRuntimeIdentifiers(_currentRuntimeIdentifiers) ?? pack.Id; - var packPath = GetPackPath(_dotNetRootPath, aliasedId, pack.Version, pack.Kind); + var packPath = GetPackPath(_dotnetRootPath, aliasedId, pack.Version, pack.Kind); return new PackInfo( pack.Id.ToString(), @@ -114,19 +129,19 @@ private bool PackExists (PackInfo packInfo) } } - private static string GetPackPath (string dotNetRootPath, WorkloadPackId packageId, string packageVersion, WorkloadPackKind kind) + private static string GetPackPath (string dotnetRootPath, WorkloadPackId packageId, string packageVersion, WorkloadPackKind kind) { switch (kind) { case WorkloadPackKind.Framework: case WorkloadPackKind.Sdk: - return Path.Combine(dotNetRootPath, "packs", packageId.ToString(), packageVersion); + return Path.Combine(dotnetRootPath, "packs", packageId.ToString(), packageVersion); case WorkloadPackKind.Template: - return Path.Combine(dotNetRootPath, "template-packs", packageId.GetNuGetCanonicalId() + "." + packageVersion.ToLowerInvariant() + ".nupkg"); + return Path.Combine(dotnetRootPath, "template-packs", packageId.GetNuGetCanonicalId() + "." + packageVersion.ToLowerInvariant() + ".nupkg"); case WorkloadPackKind.Library: - return Path.Combine(dotNetRootPath, "library-packs", packageId.GetNuGetCanonicalId() + "." + packageVersion.ToLowerInvariant() + ".nupkg"); + return Path.Combine(dotnetRootPath, "library-packs", packageId.GetNuGetCanonicalId() + "." + packageVersion.ToLowerInvariant() + ".nupkg"); case WorkloadPackKind.Tool: - return Path.Combine(dotNetRootPath, "tool-packs", packageId.ToString(), packageVersion); + return Path.Combine(dotnetRootPath, "tool-packs", packageId.ToString(), packageVersion); default: throw new ArgumentException($"The package kind '{kind}' is not known", nameof(kind)); } diff --git a/src/Tests/Microsoft.DotNet.TemplateLocator.Tests/GivenAnTemplateLocator.cs b/src/Tests/Microsoft.DotNet.TemplateLocator.Tests/GivenAnTemplateLocator.cs index 890853532dea..260fa23c4173 100644 --- a/src/Tests/Microsoft.DotNet.TemplateLocator.Tests/GivenAnTemplateLocator.cs +++ b/src/Tests/Microsoft.DotNet.TemplateLocator.Tests/GivenAnTemplateLocator.cs @@ -23,8 +23,16 @@ public GivenAnTemplateLocator(ITestOutputHelper logger) : base(logger) _resolver = new TemplateLocator(Environment.GetEnvironmentVariable, VSSettings.Ambient, null, null); _fakeDotnetRootDirectory = Path.Combine(TestContext.Current.TestExecutionDirectory, Path.GetRandomFileName()); + + var fakeSdkDirectory = Path.Combine(_fakeDotnetRootDirectory, "sdk", "5.0.102"); + Directory.CreateDirectory(fakeSdkDirectory); + var fakeRuntimeIdentifierChainPath = Path.Combine(fakeSdkDirectory, "NETCoreSdkRuntimeIdentifierChain.txt"); + File.WriteAllLines(fakeRuntimeIdentifierChainPath, + new[] { "win-x64", "win", "any", "base" }); + _manifestDirectory = Path.Combine(_fakeDotnetRootDirectory, "sdk-manifests", "5.0.100"); Directory.CreateDirectory(_manifestDirectory); + } [Fact] @@ -64,7 +72,7 @@ public void GivenNoManifestDirectoryItShouldReturnEmpty() { var fakeDotnetRootDirectory = Path.Combine(TestContext.Current.TestExecutionDirectory, Path.GetRandomFileName()); - var result = _resolver.GetDotnetSdkTemplatePackages("5.1.102", fakeDotnetRootDirectory); + var result = _resolver.GetDotnetSdkTemplatePackages("5.0.102", fakeDotnetRootDirectory); result.Should().BeEmpty(); } } diff --git a/src/Tests/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/ManifestReaderFunctionalTests.cs b/src/Tests/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/ManifestReaderFunctionalTests.cs index 416344c71c2b..ed4347bb98a1 100644 --- a/src/Tests/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/ManifestReaderFunctionalTests.cs +++ b/src/Tests/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/ManifestReaderFunctionalTests.cs @@ -42,7 +42,7 @@ public void ItShouldGetAllSdkPacks() private static WorkloadResolver SetUp() { var workloadResolver = - new WorkloadResolver(new FakeManifestProvider(new[] {Path.Combine("Manifests", "Sample.json")}), + WorkloadResolver.CreateForTests(new FakeManifestProvider(new[] {Path.Combine("Manifests", "Sample.json")}), "fakepath", ManifestTests.TEST_RUNTIME_IDENTIFIER_CHAIN); workloadResolver.ReplaceFilesystemChecksForTest(fileExists: (_) => true, directoryExists: (_) => true); @@ -53,7 +53,7 @@ private static WorkloadResolver SetUp() public void GivenTemplateNupkgDoesNotExistOnDiskItShouldReturnEmpty() { var workloadResolver = - new WorkloadResolver(new FakeManifestProvider(new[] {Path.Combine("Manifests", "Sample.json")}), + WorkloadResolver.CreateForTests(new FakeManifestProvider(new[] {Path.Combine("Manifests", "Sample.json")}), "fakepath", ManifestTests.TEST_RUNTIME_IDENTIFIER_CHAIN); workloadResolver.ReplaceFilesystemChecksForTest(fileExists: (_) => false, directoryExists: (_) => true); var result = workloadResolver.GetInstalledWorkloadPacksOfKind(WorkloadPackKind.Template); @@ -64,7 +64,7 @@ public void GivenTemplateNupkgDoesNotExistOnDiskItShouldReturnEmpty() public void GivenWorkloadSDKsDirectoryNotExistOnDiskItShouldReturnEmpty() { var workloadResolver = - new WorkloadResolver(new FakeManifestProvider(new[] {Path.Combine("Manifests", "Sample.json")}), + WorkloadResolver.CreateForTests(new FakeManifestProvider(new[] {Path.Combine("Manifests", "Sample.json")}), "fakepath", ManifestTests.TEST_RUNTIME_IDENTIFIER_CHAIN); workloadResolver.ReplaceFilesystemChecksForTest(fileExists: (_) => true, directoryExists: (_) => false); var result = workloadResolver.GetInstalledWorkloadPacksOfKind(WorkloadPackKind.Sdk); diff --git a/src/Tests/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/ManifestTests.cs b/src/Tests/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/ManifestTests.cs index 00fb1fb4357c..9c54eb0849d2 100644 --- a/src/Tests/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/ManifestTests.cs +++ b/src/Tests/Microsoft.NET.Sdk.WorkloadManifestReader.Tests/ManifestTests.cs @@ -35,7 +35,7 @@ public void ItCanDeserialize() public void AliasedPackPath() { var manifestProvider = new FakeManifestProvider(Path.Combine("Manifests", "Sample.json")); - var resolver = new WorkloadResolver(manifestProvider, fakeRootPath, TEST_RUNTIME_IDENTIFIER_CHAIN); + var resolver = WorkloadResolver.CreateForTests(manifestProvider, fakeRootPath, TEST_RUNTIME_IDENTIFIER_CHAIN); resolver.ReplaceFilesystemChecksForTest(_ => true, _ => true);