Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use list of current Runtime Identifiers for workload resolver #14820

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"tools": {
"dotnet": "5.0.100",
"dotnet": "5.0.200-preview.20601.7",
"runtimes": {
"dotnet": [
"$(MicrosoftNETCoreAppInternalPackageVersion)"
Expand Down
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
2 changes: 1 addition & 1 deletion src/Microsoft.DotNet.TemplateLocator/TemplateLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public IReadOnlyCollection<IOptionalSdkTemplatePackageInfo> GetDotnetSdkTemplate
}

_workloadManifestProvider ??= new SdkDirectoryWorkloadManifestProvider(dotnetRootPath, sdkVersion);
_workloadResolver ??= new WorkloadResolver(_workloadManifestProvider, dotnetRootPath);
_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 @@ -72,8 +72,7 @@ private void InitializeWorkloadResolver(SdkResolverContext context)
var sdkVersion = Path.GetFileName(sdkDirectory);

_workloadManifestProvider ??= new SdkDirectoryWorkloadManifestProvider(dotnetRootPath, sdkVersion);

_workloadResolver ??= new WorkloadResolver(_workloadManifestProvider, dotnetRootPath);
_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 @@ -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,32 @@ 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 readonly string _dotNetRootPath;
private string[] _currentRuntimeIdentifiers;
private readonly string _dotnetRootPath;

private Func<string, bool>? _fileExistOverride;
private Func<string, bool>? _directoryExistOverride;

public WorkloadResolver(IWorkloadManifestProvider manifestProvider, string dotNetRootPath)
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[] { };

// 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[] { "*" };
}
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;

var manifests = new List<WorkloadManifest>();

Expand All @@ -63,32 +68,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,15 +98,10 @@ 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 packPath = GetPackPath(_dotNetRootPath, aliasedId, pack.Version, pack.Kind);
var aliasedId = pack.TryGetAliasForRuntimeIdentifiers(_currentRuntimeIdentifiers) ?? pack.Id;
var packPath = GetPackPath(_dotnetRootPath, aliasedId, pack.Version, pack.Kind);

return new PackInfo(
pack.Id.ToString(),
Expand Down Expand Up @@ -155,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,8 +42,8 @@ public void ItShouldGetAllSdkPacks()
private static WorkloadResolver SetUp()
{
var workloadResolver =
new WorkloadResolver(new FakeManifestProvider(new[] {Path.Combine("Manifests", "Sample.json")}),
"fakepath");
WorkloadResolver.CreateForTests(new FakeManifestProvider(new[] {Path.Combine("Manifests", "Sample.json")}),
"fakepath", ManifestTests.TEST_RUNTIME_IDENTIFIER_CHAIN);

workloadResolver.ReplaceFilesystemChecksForTest(fileExists: (_) => true, directoryExists: (_) => true);
return workloadResolver;
Expand All @@ -53,8 +53,8 @@ private static WorkloadResolver SetUp()
public void GivenTemplateNupkgDoesNotExistOnDiskItShouldReturnEmpty()
{
var workloadResolver =
new WorkloadResolver(new FakeManifestProvider(new[] {Path.Combine("Manifests", "Sample.json")}),
"fakepath");
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);
result.Should().HaveCount(0);
Expand All @@ -64,8 +64,8 @@ public void GivenTemplateNupkgDoesNotExistOnDiskItShouldReturnEmpty()
public void GivenWorkloadSDKsDirectoryNotExistOnDiskItShouldReturnEmpty()
{
var workloadResolver =
new WorkloadResolver(new FakeManifestProvider(new[] {Path.Combine("Manifests", "Sample.json")}),
"fakepath");
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);
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 = WorkloadResolver.CreateForTests(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