Skip to content

Commit

Permalink
Handle missing runtime identifier chain file in workload resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
dsplaisted committed Dec 3, 2020
1 parent 26b1a0f commit 89ef7df
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 23 deletions.
5 changes: 1 addition & 4 deletions src/Microsoft.DotNet.TemplateLocator/TemplateLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@ public IReadOnlyCollection<IOptionalSdkTemplatePackageInfo> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,29 @@ public class WorkloadResolver : IWorkloadResolver
private readonly Dictionary<WorkloadDefinitionId, WorkloadDefinition> _workloads = new Dictionary<WorkloadDefinitionId, WorkloadDefinition>();
private readonly Dictionary<WorkloadPackId, WorkloadPack> _packs = new Dictionary<WorkloadPackId, WorkloadPack>();
private string[] _currentRuntimeIdentifiers;
private readonly string _dotNetRootPath;
private readonly string _dotnetRootPath;

private Func<string, bool>? _fileExistOverride;
private Func<string, bool>? _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;

Expand Down Expand Up @@ -86,7 +101,7 @@ internal void ReplaceFilesystemChecksForTest(Func<string, bool> 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(),
Expand Down Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 89ef7df

Please sign in to comment.