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 85b136f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 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);
}

internal 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

0 comments on commit 85b136f

Please sign in to comment.