Consume portable RID graph from runtime (#17760)

This commit is contained in:
Viktor Hofer 2023-11-16 17:40:29 +01:00 committed by GitHub
parent eae1413076
commit 5037a63d97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 8 additions and 112 deletions

View file

@ -1,94 +0,0 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using NuGet.Packaging;
using NuGet.Packaging.Core;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;
using NuGet.Versioning;
namespace Microsoft.DotNet.Build.Tasks
{
public class AddRidToRuntimeJson:Task
{
/// <summary>
/// [OS name].[version]-[architecture]
/// </summary>
[Required]
public string Rid { get; set; }
[Required]
public string RuntimeJson { get; set; }
private string runtimesIdentifier = "runtimes";
public override bool Execute()
{
string[] ridParts = Rid.Split('-');
string osNameAndVersion = ridParts[0];
string[] osParts = osNameAndVersion.Split(new char[] { '.' }, 2);
if (ridParts.Length < 1 || osParts.Length < 2)
{
throw new System.InvalidOperationException($"Unknown rid format {Rid}.");
}
// Acquire Rid parts:
// osName
// version
// arch
string arch = ridParts[1];
string osName = osParts[0];
string version = osParts[1];
JObject projectRoot = ReadProject(RuntimeJson);
if (projectRoot.SelectToken($"{runtimesIdentifier}.{osName}") == null)
{
AddRidToRuntimeGraph(projectRoot, osName, "linux");
AddRidToRuntimeGraph(projectRoot, $"{osName}-{arch}", osName, $"linux-{arch}");
}
if(projectRoot.SelectToken($"{runtimesIdentifier}.{osName}.{version}") == null)
{
AddRidToRuntimeGraph(projectRoot, $"{osName}.{version}", osName);
AddRidToRuntimeGraph(projectRoot, $"{osName}.{version}-{arch}", $"{osName}.{version}", $"{osName}-{arch}");
}
WriteProject(projectRoot, RuntimeJson);
return true;
}
private void AddRidToRuntimeGraph(JObject projectRoot, string name, params string[] imports)
{
projectRoot[runtimesIdentifier][name] = new JObject(new JProperty("#import", new JArray(imports)));
}
private static JObject ReadProject(string projectJsonPath)
{
using (TextReader projectFileReader = File.OpenText(projectJsonPath))
{
var projectJsonReader = new JsonTextReader(projectFileReader);
var serializer = new JsonSerializer();
return serializer.Deserialize<JObject>(projectJsonReader);
}
}
private static void WriteProject(JObject projectRoot, string projectJsonPath)
{
string projectJson = JsonConvert.SerializeObject(projectRoot, Formatting.Indented) + Environment.NewLine;
if (!File.Exists(projectJsonPath) || !projectJson.Equals(File.ReadAllText(projectJsonPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(projectJsonPath));
File.WriteAllText(projectJsonPath, projectJson);
}
}
}
}

View file

@ -543,8 +543,8 @@
<ReferencePackageFiles Include="$(ReferencePackagesDir)**/*.nupkg" />
<!-- Check all RIDs from all restored Microsoft.NETCore.Platforms packages. -->
<PlatformsRuntimeJsonFiles Include="$(LocalNuGetPackagesRoot)*/microsoft.netcore.platforms/*/runtime.json" />
<PlatformsRuntimeJsonFiles Include="$(PackagesDir)microsoft.netcore.platforms/*/runtime.json" />
<PlatformsRuntimeJsonFiles Include="$(LocalNuGetPackagesRoot)*/microsoft.netcore.platforms/*/PortableRuntimeIdentifierGraph.json" />
<PlatformsRuntimeJsonFiles Include="$(PackagesDir)microsoft.netcore.platforms/*/PortableRuntimeIdentifierGraph.json" />
<!-- Add some other potential top-level project directories for a more specific report. -->
<ProjectDirectories Include="$(SourceBuiltSdksDir);$(TaskDirectory);$(BaseIntermediatePath)" />

View file

@ -16,7 +16,7 @@
<RuntimeOS>$(NETCoreSdkRuntimeIdentifier.Substring(0, $(_platformIndex)))</RuntimeOS>
<_platformIndex>$(NETCoreSdkPortableRuntimeIdentifier.LastIndexOf('-'))</_platformIndex>
<BaseOS>$(NETCoreSdkPortableRuntimeIdentifier.Substring(0, $(_platformIndex)))</BaseOS>
<BaseOS>$(NETCoreSdkPortableRuntimeIdentifier.Substring(0, $(_platformIndex)))-$(Platform)</BaseOS>
<PortableBuild Condition="'$(PortableBuild)' == ''">false</PortableBuild>
<BuildNonPortable>true</BuildNonPortable>
@ -54,8 +54,6 @@
<RepositoryReference Include="source-build-externals" />
</ItemGroup>
<UsingTask AssemblyFile="$(XPlatSourceBuildTasksAssembly)" TaskName="AddRidToRuntimeJson" />
<Target Name="SetOutputList" AfterTargets="Package" BeforeTargets="GatherBuiltPackages">
<ItemGroup>
<PackagesOutputList Include="$(ShippingPackagesOutput)" />
@ -63,17 +61,5 @@
</ItemGroup>
</Target>
<Target Name="UpdateRuntimeGraph"
BeforeTargets="Build"
Condition="'$(_IsBootstrapping)' == 'true'">
<PropertyGroup>
<RuntimeJsonFile>$(ProjectDirectory)pkg/Microsoft.NETCore.Platforms/runtime.json</RuntimeJsonFile>
</PropertyGroup>
<Message Importance="High" Text="Adding rid, $(TargetRid), to $(RuntimeJsonFile)" />
<AddRidToRuntimeJson RuntimeJson="$(RuntimeJsonFile)"
Rid="$(TargetRid)-$(Platform)" />
</Target>
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
</Project>

View file

@ -1195,9 +1195,13 @@ Copyright (c) .NET Foundation. All rights reserved.
DestinationFiles="$(SdkOutputDirectory)RuntimeIdentifierGraph.json"
SkipUnchangedFiles="true"/>
<Copy SourceFiles="$(NuGetPackageRoot)/microsoft.netcore.platforms/$(_NETCorePlatformsPackageVersion)/PortableRuntimeIdentifierGraph.json"
DestinationFolder="$(SdkOutputDirectory)"
SkipUnchangedFiles="true"/>
<GenerateSdkRuntimeIdentifierChain
RuntimeIdentifier="$(PortableProductMonikerRid)"
RuntimeIdentifierGraphPath="$(SdkOutputDirectory)RuntimeIdentifierGraph.json"
RuntimeIdentifierGraphPath="$(SdkOutputDirectory)PortableRuntimeIdentifierGraph.json"
RuntimeIdentifierChainOutputPath="$(SdkOutputDirectory)NETCoreSdkRuntimeIdentifierChain.txt"/>
</Target>