Skip to content

Commit

Permalink
Read current runtime identifiers for workload resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
dsplaisted committed Dec 3, 2020
1 parent 3fbac9a commit 26b1a0f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 55 deletions.
1 change: 1 addition & 0 deletions src/Layout/redist/targets/OverlaySdkOnLKG.targets
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<OverlaySDK Include="$(_DotNetHiveRoot)/**/*" Exclude="$(_DotNetHiveRoot)sdk/**/*"/>
<OverlaySdkFilesFromStage0 Include="$(_DotNetHiveRoot)/sdk/$(Stage0SdkVersion)/Microsoft.NETCoreSdk.BundledCliTools.props" />
<OverlaySdkFilesFromStage0 Include="$(_DotNetHiveRoot)/sdk/$(Stage0SdkVersion)/RuntimeIdentifierGraph.json" />
<OverlaySdkFilesFromStage0 Include="$(_DotNetHiveRoot)/sdk/$(Stage0SdkVersion)/NETCoreSdkRuntimeIdentifierChain.txt" />
<OverlaySdkFilesFromStage0 Include="$(_DotNetHiveRoot)/sdk/$(Stage0SdkVersion)/DotnetTools/**/*" RelativeDestination="DotnetTools"/>
<OverlaySdkFilesFromStage0 Include="$(_DotNetHiveRoot)/sdk/$(Stage0SdkVersion)/AppHostTemplate/**/*" RelativeDestination="AppHostTemplate"/>
<ToolsetToOverlay Include="$(OutputPath)/**/*" />
Expand Down
5 changes: 4 additions & 1 deletion src/Microsoft.DotNet.TemplateLocator/TemplateLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ 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);
_workloadResolver ??= new WorkloadResolver(_workloadManifestProvider, dotnetRootPath, currentRuntimeIdentifiers);

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,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ public WorkloadPack(WorkloadPackId id, string version, WorkloadPackKind kind, Di
public bool IsAlias => AliasTo != null && AliasTo.Count > 0;
public Dictionary<string, WorkloadPackId>? AliasTo { get; }

public WorkloadPackId? TryGetAliasForPlatformIds (IEnumerable<string> platformIds)
public WorkloadPackId? TryGetAliasForRuntimeIdentifiers(IEnumerable<string> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,17 @@ 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[] _platformIds;
private string[] _currentRuntimeIdentifiers;
private readonly string _dotNetRootPath;

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

Expand All @@ -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;
}

/// <summary>
/// Gets the installed workload packs of a particular kind
/// </summary>
Expand Down Expand Up @@ -119,14 +83,9 @@ internal void ReplaceFilesystemChecksForTest(Func<string, bool> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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");

Expand Down

0 comments on commit 26b1a0f

Please sign in to comment.