From 3b95c7c73b1d1d5061c55ebf8fcfc27d3d9c16bd Mon Sep 17 00:00:00 2001 From: Chet Husk Date: Wed, 9 Apr 2025 20:48:28 -0500 Subject: [PATCH 1/3] Use an MSBuild Task for RID selection instead of shelling out --- Aspire.sln | 16 +++- Directory.Packages.props | 1 + .../Aspire.AppHost.Sdk.csproj | 23 ++--- .../Aspire.AppHost.Tasks.csproj} | 7 +- .../NuGetUtils.cs | 0 .../Aspire.AppHost.Tasks/PickBestRid.cs | 63 ++++++++++++++ .../Aspire.RuntimeIdentifier.Tool/Program.cs | 86 ------------------- src/Aspire.AppHost.Sdk/SDK/Sdk.in.targets | 19 ++-- 8 files changed, 93 insertions(+), 122 deletions(-) rename src/Aspire.AppHost.Sdk/{Aspire.RuntimeIdentifier.Tool/Aspire.RuntimeIdentifier.Tool.csproj => Aspire.AppHost.Tasks/Aspire.AppHost.Tasks.csproj} (58%) rename src/Aspire.AppHost.Sdk/{Aspire.RuntimeIdentifier.Tool => Aspire.AppHost.Tasks}/NuGetUtils.cs (100%) create mode 100644 src/Aspire.AppHost.Sdk/Aspire.AppHost.Tasks/PickBestRid.cs delete mode 100644 src/Aspire.AppHost.Sdk/Aspire.RuntimeIdentifier.Tool/Program.cs diff --git a/Aspire.sln b/Aspire.sln index fe5408bd274..6e3a99914e6 100644 --- a/Aspire.sln +++ b/Aspire.sln @@ -1,4 +1,3 @@ - Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.0.31903.59 @@ -675,6 +674,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.Azure.Npgsql.EntityF EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.Components.Common.Tests", "tests\Aspire.Components.Common.Tests\Aspire.Components.Common.Tests.csproj", "{30950CEB-2232-F9FC-04FF-ADDCB8AC30A7}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.AppHost.Tasks", "src\Aspire.AppHost.Sdk\Aspire.AppHost.Tasks\Aspire.AppHost.Tasks.csproj", "{AC030549-F2E8-08A5-9B7E-FF116B7E696A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -3961,6 +3962,18 @@ Global {30950CEB-2232-F9FC-04FF-ADDCB8AC30A7}.Release|x64.Build.0 = Release|Any CPU {30950CEB-2232-F9FC-04FF-ADDCB8AC30A7}.Release|x86.ActiveCfg = Release|Any CPU {30950CEB-2232-F9FC-04FF-ADDCB8AC30A7}.Release|x86.Build.0 = Release|Any CPU + {AC030549-F2E8-08A5-9B7E-FF116B7E696A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AC030549-F2E8-08A5-9B7E-FF116B7E696A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AC030549-F2E8-08A5-9B7E-FF116B7E696A}.Debug|x64.ActiveCfg = Debug|Any CPU + {AC030549-F2E8-08A5-9B7E-FF116B7E696A}.Debug|x64.Build.0 = Debug|Any CPU + {AC030549-F2E8-08A5-9B7E-FF116B7E696A}.Debug|x86.ActiveCfg = Debug|Any CPU + {AC030549-F2E8-08A5-9B7E-FF116B7E696A}.Debug|x86.Build.0 = Debug|Any CPU + {AC030549-F2E8-08A5-9B7E-FF116B7E696A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AC030549-F2E8-08A5-9B7E-FF116B7E696A}.Release|Any CPU.Build.0 = Release|Any CPU + {AC030549-F2E8-08A5-9B7E-FF116B7E696A}.Release|x64.ActiveCfg = Release|Any CPU + {AC030549-F2E8-08A5-9B7E-FF116B7E696A}.Release|x64.Build.0 = Release|Any CPU + {AC030549-F2E8-08A5-9B7E-FF116B7E696A}.Release|x86.ActiveCfg = Release|Any CPU + {AC030549-F2E8-08A5-9B7E-FF116B7E696A}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -4285,6 +4298,7 @@ Global {192747A2-9338-DECF-5C8C-28EB8E13829B} = {27381127-6C45-4B4C-8F18-41FF48DFE4B2} {8FCA0CFA-7823-6A2F-342A-107A994915B0} = {C424395C-1235-41A4-BF55-07880A04368C} {30950CEB-2232-F9FC-04FF-ADDCB8AC30A7} = {C424395C-1235-41A4-BF55-07880A04368C} + {AC030549-F2E8-08A5-9B7E-FF116B7E696A} = {F534D4F8-5E3A-42FC-BCD7-4C2D6060F9C8} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {47DCFECF-5631-4BDE-A1EC-BE41E90F60C4} diff --git a/Directory.Packages.props b/Directory.Packages.props index b3825726499..42672741d9f 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -83,6 +83,7 @@ + diff --git a/src/Aspire.AppHost.Sdk/Aspire.AppHost.Sdk.csproj b/src/Aspire.AppHost.Sdk/Aspire.AppHost.Sdk.csproj index c7fdbe2220c..e9819cd37a3 100644 --- a/src/Aspire.AppHost.Sdk/Aspire.AppHost.Sdk.csproj +++ b/src/Aspire.AppHost.Sdk/Aspire.AppHost.Sdk.csproj @@ -1,4 +1,4 @@ - + @@ -20,25 +20,16 @@ - + - - - - <_publishContentToPackage Include="$(DotNetOutputPath)Aspire.RuntimeIdentifier.Tool/$(Configuration)/$(DefaultTargetFramework)/publish/**/*" /> - - + + + + - + diff --git a/src/Aspire.AppHost.Sdk/Aspire.RuntimeIdentifier.Tool/Aspire.RuntimeIdentifier.Tool.csproj b/src/Aspire.AppHost.Sdk/Aspire.AppHost.Tasks/Aspire.AppHost.Tasks.csproj similarity index 58% rename from src/Aspire.AppHost.Sdk/Aspire.RuntimeIdentifier.Tool/Aspire.RuntimeIdentifier.Tool.csproj rename to src/Aspire.AppHost.Sdk/Aspire.AppHost.Tasks/Aspire.AppHost.Tasks.csproj index 5eb2ac1cadc..b167f76a135 100644 --- a/src/Aspire.AppHost.Sdk/Aspire.RuntimeIdentifier.Tool/Aspire.RuntimeIdentifier.Tool.csproj +++ b/src/Aspire.AppHost.Sdk/Aspire.AppHost.Tasks/Aspire.AppHost.Tasks.csproj @@ -1,17 +1,14 @@ - Exe - $(DefaultTargetFramework) - Major + netstandard2.0 false false false - false - + diff --git a/src/Aspire.AppHost.Sdk/Aspire.RuntimeIdentifier.Tool/NuGetUtils.cs b/src/Aspire.AppHost.Sdk/Aspire.AppHost.Tasks/NuGetUtils.cs similarity index 100% rename from src/Aspire.AppHost.Sdk/Aspire.RuntimeIdentifier.Tool/NuGetUtils.cs rename to src/Aspire.AppHost.Sdk/Aspire.AppHost.Tasks/NuGetUtils.cs diff --git a/src/Aspire.AppHost.Sdk/Aspire.AppHost.Tasks/PickBestRid.cs b/src/Aspire.AppHost.Sdk/Aspire.AppHost.Tasks/PickBestRid.cs new file mode 100644 index 00000000000..3dc7112607d --- /dev/null +++ b/src/Aspire.AppHost.Sdk/Aspire.AppHost.Tasks/PickBestRid.cs @@ -0,0 +1,63 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using Microsoft.Build.Framework; +using Aspire.Hosting.Sdk; +using NuGet.RuntimeModel; + +namespace Aspire.RuntimeIdentifier.Tool; + +/// +/// This task uses the given RID graph in a given SDK to pick the best match from among a set of supported RIDs for the current RID +/// +public sealed class PickBestRid : Microsoft.Build.Utilities.Task +{ + /// + /// The path to the RID graph to read + /// + [Required] + public string RuntimeGraphPath { get; set; } + + /// + /// The RID of the current process + /// + [Required] + public string CurrentRid { get; set; } + + /// + /// All of the RIDs that Aspire supports + /// + [Required] + public string[]? SupportedRids { get; set; } + + /// + /// The solution to the puzzle + /// + [Output] + public string MatchingRid { get; set; } + + /// + /// Computes the thing + /// + /// + public override bool Execute() + { + if (!File.Exists(RuntimeGraphPath)) + { + Log.LogError("File {0} does not exist. Please ensure the runtime graph path exists.", RuntimeGraphPath); + return !Log.HasLoggedErrors; + } + + RuntimeGraph graph = JsonRuntimeFormat.ReadRuntimeGraph(RuntimeGraphPath); + string? bestRidForPlatform = NuGetUtils.GetBestMatchingRid(graph, CurrentRid, SupportedRids, out bool wasInGraph); + + if (!wasInGraph) + { + Log.LogError("Unable to find a matching RID for {0} from among {1} in graph {2}", CurrentRid, string.Join(",", SupportedRids), RuntimeGraphPath); + return !Log.HasLoggedErrors; + } + + MatchingRid = bestRidForPlatform; + return true; + } +} diff --git a/src/Aspire.AppHost.Sdk/Aspire.RuntimeIdentifier.Tool/Program.cs b/src/Aspire.AppHost.Sdk/Aspire.RuntimeIdentifier.Tool/Program.cs deleted file mode 100644 index 61981bd9a53..00000000000 --- a/src/Aspire.AppHost.Sdk/Aspire.RuntimeIdentifier.Tool/Program.cs +++ /dev/null @@ -1,86 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.CommandLine; -using System.CommandLine.Parsing; -using System.Diagnostics; -using System.Reflection; -using Aspire.Hosting.Sdk; -using NuGet.RuntimeModel; - -namespace Aspire.RuntimeIdentifier.Tool; - -sealed class Program -{ - static int Main(string[] args) - { - RootCommand rootCommand = new("Aspire.RuntimeIdentifier.Tool v" + FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion) - { - TreatUnmatchedTokensAsErrors = true - }; - - Option runtimeGraphPathOption = new("--runtimeGraphPath") - { - Description = "Path to runtime graph path to use for RID mapping.", - Required = true - }; - - Option netcoreSdkRuntimeIdentifierOption = new("--netcoreSdkRuntimeIdentifier") - { - Description = "RID to use for finding the best applicable RID from mapping.", - Required = true - }; - - Option supportedRidsOption = new("--supportedRids") - { - Description = "List of RIDs that are supported. Comma-separated.", - Required = true, - Arity = ArgumentArity.OneOrMore, - CustomParser = ParseSupportedRidsArgument - }; - - rootCommand.Options.Add(runtimeGraphPathOption); - rootCommand.Options.Add(netcoreSdkRuntimeIdentifierOption); - rootCommand.Options.Add(supportedRidsOption); - rootCommand.SetAction((ParseResult parseResult) => - { - string rgp = parseResult.GetValue(runtimeGraphPathOption) ?? throw new InvalidOperationException("The --runtimeGraphPath argument is required."); - - if (!File.Exists(rgp)) - { - throw new FileNotFoundException("File {0} does not exist. Please ensure the runtime graph path exists.", rgp); - } - - RuntimeGraph graph = JsonRuntimeFormat.ReadRuntimeGraph(rgp); - - var ridToUse = parseResult.GetValue(netcoreSdkRuntimeIdentifierOption); - - var supportedRids = parseResult.GetValue(supportedRidsOption); - - string? bestRidForPlatform = NuGetUtils.GetBestMatchingRid(graph, ridToUse!, supportedRids!, out bool wasInGraph); - - if (!wasInGraph) - { - Console.WriteLine("Unable to find the best rid to use"); - return -1; - } - - Console.WriteLine(bestRidForPlatform); - return 0; - }); - - return rootCommand.Parse(args).Invoke(); - } - - private static string[]? ParseSupportedRidsArgument(ArgumentResult result) - { - List args = new(); - - foreach (var token in result.Tokens) - { - args.AddRange(token.Value.Split(',')); - } - - return args.ToArray(); - } -} diff --git a/src/Aspire.AppHost.Sdk/SDK/Sdk.in.targets b/src/Aspire.AppHost.Sdk/SDK/Sdk.in.targets index 391eb0b3135..f8511666dc9 100644 --- a/src/Aspire.AppHost.Sdk/SDK/Sdk.in.targets +++ b/src/Aspire.AppHost.Sdk/SDK/Sdk.in.targets @@ -49,6 +49,8 @@ $(AspireRidToolDirectory)Aspire.RuntimeIdentifier.Tool.dll + + - - - - - - - - - <_DashboardAndDcpRID>@(_AspireRidToolOutput) - + + + - From 64b95513c1797da9a8ecb81f3da8efb21a6e1908 Mon Sep 17 00:00:00 2001 From: Chet Husk Date: Wed, 9 Apr 2025 21:01:09 -0500 Subject: [PATCH 2/3] minor tweak to MSBuild logic removing dotnet_host logic since we're no longer spawning anything. consolidate RID patterns for easier viewing in binlogs --- .../Aspire.AppHost.Sdk.csproj | 10 +++---- src/Aspire.AppHost.Sdk/SDK/Sdk.in.targets | 30 +++---------------- 2 files changed, 8 insertions(+), 32 deletions(-) diff --git a/src/Aspire.AppHost.Sdk/Aspire.AppHost.Sdk.csproj b/src/Aspire.AppHost.Sdk/Aspire.AppHost.Sdk.csproj index e9819cd37a3..0827183ae70 100644 --- a/src/Aspire.AppHost.Sdk/Aspire.AppHost.Sdk.csproj +++ b/src/Aspire.AppHost.Sdk/Aspire.AppHost.Sdk.csproj @@ -5,7 +5,7 @@ aspire apphost sdk .NET Aspire AppHost SDK. Must be referenced by .NET Aspire AppHost projects. - _PublishAndPackRIDTool;$(TargetsForTfmSpecificContentInPackage) + _AddTaskToPackage;$(TargetsForTfmSpecificContentInPackage) false @@ -18,15 +18,13 @@ - - - + - + - + diff --git a/src/Aspire.AppHost.Sdk/SDK/Sdk.in.targets b/src/Aspire.AppHost.Sdk/SDK/Sdk.in.targets index f8511666dc9..6a48ec1f7b4 100644 --- a/src/Aspire.AppHost.Sdk/SDK/Sdk.in.targets +++ b/src/Aspire.AppHost.Sdk/SDK/Sdk.in.targets @@ -23,7 +23,7 @@ This defaulting needs to happen in the SDK targets so this metadata affects NuGet Restore. --> - + true @@ -68,7 +68,7 @@ <_AppHostVersion>%(_AppHostPackageReference.Version) - <_AppHostPackageReference /> @@ -93,36 +93,14 @@ - - <_DashboardAndDCPSupportedTargetingRIDs Include="win-x64" /> - <_DashboardAndDCPSupportedTargetingRIDs Include="win-x86" /> - <_DashboardAndDCPSupportedTargetingRIDs Include="win-arm64" /> - <_DashboardAndDCPSupportedTargetingRIDs Include="linux-x64" /> - <_DashboardAndDCPSupportedTargetingRIDs Include="linux-arm64" /> - <_DashboardAndDCPSupportedTargetingRIDs Include="osx-x64" /> - <_DashboardAndDCPSupportedTargetingRIDs Include="osx-arm64" /> + <_DashboardAndDCPSupportedTargetingRIDs Include="win-x64;win-x86;win-arm64;linux-x64;linux-arm64;osx-x64;osx-arm64" /> - - - <_DotNetHostPath>$(DOTNET_HOST_PATH) - - - - - <_DotNetHostDirectory>$(NetCoreRoot) - <_DotNetHostFileName>dotnet - <_DotNetHostFileName Condition="'$(OS)' == 'Windows_NT'">dotnet.exe - - <_DotNetHostPath>$(_DotNetHostDirectory)\$(_DotNetHostFileName) - - From e200e87d20986a0963a74940b112943cd7fee75b Mon Sep 17 00:00:00 2001 From: Chet Husk Date: Wed, 9 Apr 2025 21:17:44 -0500 Subject: [PATCH 3/3] a bit more cleanup --- src/Aspire.AppHost.Sdk/SDK/Sdk.in.targets | 8 +------- .../Aspire.Hosting.Sdk.Tests.csproj | 2 +- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/Aspire.AppHost.Sdk/SDK/Sdk.in.targets b/src/Aspire.AppHost.Sdk/SDK/Sdk.in.targets index 6a48ec1f7b4..4afc1e01093 100644 --- a/src/Aspire.AppHost.Sdk/SDK/Sdk.in.targets +++ b/src/Aspire.AppHost.Sdk/SDK/Sdk.in.targets @@ -43,12 +43,6 @@ - - $(MSBuildThisFileDirectory)..\tools\@DefaultTargetFramework@\ - $([MSBuild]::NormalizePath('$(AspireRidToolRoot)\')) - $(AspireRidToolDirectory)Aspire.RuntimeIdentifier.Tool.dll - - + diff --git a/tests/Aspire.Hosting.Sdk.Tests/Aspire.Hosting.Sdk.Tests.csproj b/tests/Aspire.Hosting.Sdk.Tests/Aspire.Hosting.Sdk.Tests.csproj index 0317413cb9c..30fab414575 100644 --- a/tests/Aspire.Hosting.Sdk.Tests/Aspire.Hosting.Sdk.Tests.csproj +++ b/tests/Aspire.Hosting.Sdk.Tests/Aspire.Hosting.Sdk.Tests.csproj @@ -5,7 +5,7 @@ - +