|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.CommandLine; |
| 5 | +using System.CommandLine.Parsing; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.Reflection; |
| 8 | +using Aspire.Hosting.Sdk; |
| 9 | +using NuGet.RuntimeModel; |
| 10 | + |
| 11 | +namespace Aspire.RuntimeIdentifier.Tool; |
| 12 | + |
| 13 | +sealed class Program |
| 14 | +{ |
| 15 | + static int Main(string[] args) |
| 16 | + { |
| 17 | + CliRootCommand rootCommand = new("Aspire.RuntimeIdentifier.Tool v" + FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion) |
| 18 | + { |
| 19 | + TreatUnmatchedTokensAsErrors = true |
| 20 | + }; |
| 21 | + |
| 22 | + CliOption<string?> runtimeGraphPathOption = new("--runtimeGraphPath") |
| 23 | + { |
| 24 | + Description = "Path to runtime graph path to use for RID mapping.", |
| 25 | + Required = true |
| 26 | + }; |
| 27 | + |
| 28 | + CliOption<string?> netcoreSdkRuntimeIdentifierOption = new("--netcoreSdkRuntimeIdentifier") |
| 29 | + { |
| 30 | + Description = "RID to use for finding the best applicable RID from mapping.", |
| 31 | + Required = true |
| 32 | + }; |
| 33 | + |
| 34 | + CliOption<string[]> supportedRidsOption = new("--supportedRids") |
| 35 | + { |
| 36 | + Description = "List of RIDs that are supported. Comma-separated.", |
| 37 | + Required = true, |
| 38 | + Arity = ArgumentArity.OneOrMore, |
| 39 | + CustomParser = ParseSupportedRidsArgument |
| 40 | + }; |
| 41 | + |
| 42 | + rootCommand.Options.Add(runtimeGraphPathOption); |
| 43 | + rootCommand.Options.Add(netcoreSdkRuntimeIdentifierOption); |
| 44 | + rootCommand.Options.Add(supportedRidsOption); |
| 45 | + rootCommand.SetAction((ParseResult parseResult) => |
| 46 | + { |
| 47 | + string rgp = parseResult.GetValue(runtimeGraphPathOption) ?? throw new InvalidOperationException("The --runtimeGraphPath argument is required."); |
| 48 | + |
| 49 | + if (!File.Exists(rgp)) |
| 50 | + { |
| 51 | + throw new FileNotFoundException("File {0} does not exist. Please ensure the runtime graph path exists.", rgp); |
| 52 | + } |
| 53 | + |
| 54 | + RuntimeGraph graph = JsonRuntimeFormat.ReadRuntimeGraph(rgp); |
| 55 | + |
| 56 | + var ridToUse = parseResult.GetValue(netcoreSdkRuntimeIdentifierOption); |
| 57 | + |
| 58 | + var supportedRids = parseResult.GetValue(supportedRidsOption); |
| 59 | + |
| 60 | + string? bestRidForPlatform = NuGetUtils.GetBestMatchingRid(graph, ridToUse!, supportedRids!, out bool wasInGraph); |
| 61 | + |
| 62 | + if (!wasInGraph) |
| 63 | + { |
| 64 | + Console.WriteLine("Unable to find the best rid to use"); |
| 65 | + return -1; |
| 66 | + } |
| 67 | + |
| 68 | + Console.WriteLine(bestRidForPlatform); |
| 69 | + return 0; |
| 70 | + }); |
| 71 | + |
| 72 | + return rootCommand.Parse(args).Invoke(); |
| 73 | + } |
| 74 | + |
| 75 | + private static string[]? ParseSupportedRidsArgument(ArgumentResult result) |
| 76 | + { |
| 77 | + List<string> args = new(); |
| 78 | + |
| 79 | + foreach (var token in result.Tokens) |
| 80 | + { |
| 81 | + args.AddRange(token.Value.Split(',')); |
| 82 | + } |
| 83 | + |
| 84 | + return args.ToArray(); |
| 85 | + } |
| 86 | +} |
0 commit comments