|
| 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.Buffers; |
| 5 | +using System.CommandLine; |
| 6 | +using System.Text.Encodings.Web; |
| 7 | +using System.Text.Json; |
| 8 | +using System.Text.Json.Schema; |
| 9 | +using System.Text.Json.Serialization; |
| 10 | +using System.Text.Json.Serialization.Metadata; |
| 11 | +using Microsoft.DotNet.Cli.Telemetry; |
| 12 | +using Microsoft.DotNet.Cli.Utils; |
| 13 | +using Microsoft.DotNet.Cli.Utils.Extensions; |
| 14 | +using Command = System.CommandLine.Command; |
| 15 | +using CommandResult = System.CommandLine.Parsing.CommandResult; |
| 16 | + |
| 17 | +namespace Microsoft.DotNet.Cli; |
| 18 | + |
| 19 | +internal static class CliSchema |
| 20 | +{ |
| 21 | + // Using UnsafeRelaxedJsonEscaping because this JSON is not transmitted over the web. Therefore, HTML-sensitive characters are not encoded. |
| 22 | + // See: https://learn.microsoft.com/dotnet/api/system.text.encodings.web.javascriptencoder.unsaferelaxedjsonescaping |
| 23 | + // Force the newline to be "\n" instead of the default "\r\n" for consistency across platforms (and for testing) |
| 24 | + private static readonly JsonSerializerOptions s_jsonSerializerOptions = new() |
| 25 | + { |
| 26 | + WriteIndented = true, |
| 27 | + NewLine = "\n", |
| 28 | + Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, |
| 29 | + RespectNullableAnnotations = true, |
| 30 | + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
| 31 | + // needed to workaround https://github.com/dotnet/aspnetcore/issues/55692, but will need to be removed when |
| 32 | + // we tackle AOT in favor of the source-generated JsonTypeInfo stuff |
| 33 | + TypeInfoResolver = new DefaultJsonTypeInfoResolver() |
| 34 | + }; |
| 35 | + |
| 36 | + public record ArgumentDetails(string? description, int order, bool hidden, string? helpName, string valueType, bool hasDefaultValue, object? defaultValue, ArityDetails arity); |
| 37 | + public record ArityDetails(int minimum, int? maximum); |
| 38 | + public record OptionDetails( |
| 39 | + string? description, |
| 40 | + bool hidden, |
| 41 | + string[]? aliases, |
| 42 | + string? helpName, |
| 43 | + string valueType, |
| 44 | + bool hasDefaultValue, |
| 45 | + object? defaultValue, |
| 46 | + ArityDetails arity, |
| 47 | + bool required, |
| 48 | + bool recursive |
| 49 | + ); |
| 50 | + public record CommandDetails( |
| 51 | + string? description, |
| 52 | + bool hidden, |
| 53 | + string[]? aliases, |
| 54 | + Dictionary<string, ArgumentDetails>? arguments, |
| 55 | + Dictionary<string, OptionDetails>? options, |
| 56 | + Dictionary<string, CommandDetails>? subcommands); |
| 57 | + public record RootCommandDetails( |
| 58 | + string name, |
| 59 | + string version, |
| 60 | + string? description, |
| 61 | + bool hidden, |
| 62 | + string[]? aliases, |
| 63 | + Dictionary<string, ArgumentDetails>? arguments, |
| 64 | + Dictionary<string, OptionDetails>? options, |
| 65 | + Dictionary<string, CommandDetails>? subcommands |
| 66 | + ) : CommandDetails(description, hidden, aliases, arguments, options, subcommands); |
| 67 | + |
| 68 | + |
| 69 | + public static void PrintCliSchema(CommandResult commandResult, TextWriter outputWriter, ITelemetry? telemetryClient) |
| 70 | + { |
| 71 | + var command = commandResult.Command; |
| 72 | + RootCommandDetails transportStructure = CreateRootCommandDetails(command); |
| 73 | + var result = JsonSerializer.Serialize(transportStructure, s_jsonSerializerOptions); |
| 74 | + outputWriter.Write(result.AsSpan()); |
| 75 | + outputWriter.Flush(); |
| 76 | + var commandString = CommandHierarchyAsString(commandResult); |
| 77 | + var telemetryProperties = new Dictionary<string, string> { { "command", commandString } }; |
| 78 | + telemetryClient?.TrackEvent("schema", telemetryProperties, null); |
| 79 | + } |
| 80 | + |
| 81 | + public static object GetJsonSchema() |
| 82 | + { |
| 83 | + var node = s_jsonSerializerOptions.GetJsonSchemaAsNode(typeof(RootCommandDetails), new JsonSchemaExporterOptions()); |
| 84 | + return node.ToJsonString(s_jsonSerializerOptions); |
| 85 | + } |
| 86 | + |
| 87 | + private static ArityDetails CreateArityDetails(ArgumentArity arity) |
| 88 | + { |
| 89 | + return new ArityDetails( |
| 90 | + minimum: arity.MinimumNumberOfValues, |
| 91 | + maximum: arity.MaximumNumberOfValues == ArgumentArity.ZeroOrMore.MaximumNumberOfValues ? null : arity.MaximumNumberOfValues |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + private static RootCommandDetails CreateRootCommandDetails(Command command) |
| 96 | + { |
| 97 | + var arguments = CreateArgumentsDictionary(command.Arguments); |
| 98 | + var options = CreateOptionsDictionary(command.Options); |
| 99 | + var subcommands = CreateSubcommandsDictionary(command.Subcommands); |
| 100 | + |
| 101 | + return new RootCommandDetails( |
| 102 | + name: command.Name, |
| 103 | + version: Product.Version, |
| 104 | + description: command.Description?.ReplaceLineEndings("\n"), |
| 105 | + hidden: command.Hidden, |
| 106 | + aliases: DetermineAliases(command.Aliases), |
| 107 | + arguments: arguments, |
| 108 | + options: options, |
| 109 | + subcommands: subcommands |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + private static Dictionary<string, ArgumentDetails>? CreateArgumentsDictionary(IList<Argument> arguments) |
| 114 | + { |
| 115 | + if (arguments.Count == 0) |
| 116 | + { |
| 117 | + return null; |
| 118 | + } |
| 119 | + var dict = new Dictionary<string, ArgumentDetails>(); |
| 120 | + foreach ((var index, var argument) in arguments.Index()) |
| 121 | + { |
| 122 | + dict[argument.Name] = CreateArgumentDetails(index, argument); |
| 123 | + } |
| 124 | + return dict; |
| 125 | + } |
| 126 | + |
| 127 | + private static Dictionary<string, OptionDetails>? CreateOptionsDictionary(IList<Option> options) |
| 128 | + { |
| 129 | + if (options.Count == 0) |
| 130 | + { |
| 131 | + return null; |
| 132 | + } |
| 133 | + var dict = new Dictionary<string, OptionDetails>(); |
| 134 | + foreach (var option in options.OrderBy(o => o.Name, StringComparer.OrdinalIgnoreCase)) |
| 135 | + { |
| 136 | + dict[option.Name] = CreateOptionDetails(option); |
| 137 | + } |
| 138 | + return dict; |
| 139 | + } |
| 140 | + |
| 141 | + private static Dictionary<string, CommandDetails>? CreateSubcommandsDictionary(IList<Command> subcommands) |
| 142 | + { |
| 143 | + if (subcommands.Count == 0) |
| 144 | + { |
| 145 | + return null; |
| 146 | + } |
| 147 | + var dict = new Dictionary<string, CommandDetails>(); |
| 148 | + foreach (var subcommand in subcommands.OrderBy(c => c.Name, StringComparer.OrdinalIgnoreCase)) |
| 149 | + { |
| 150 | + dict[subcommand.Name] = CreateCommandDetails(subcommand); |
| 151 | + } |
| 152 | + return dict; |
| 153 | + } |
| 154 | + |
| 155 | + private static string[]? DetermineAliases(ICollection<string> aliases) |
| 156 | + { |
| 157 | + if (aliases.Count == 0) |
| 158 | + { |
| 159 | + return null; |
| 160 | + } |
| 161 | + |
| 162 | + // Order the aliases to ensure consistent output. |
| 163 | + return aliases.Order().ToArray(); |
| 164 | + } |
| 165 | + |
| 166 | + private static CommandDetails CreateCommandDetails(Command subCommand) => new CommandDetails( |
| 167 | + subCommand.Description?.ReplaceLineEndings("\n"), |
| 168 | + subCommand.Hidden, |
| 169 | + DetermineAliases(subCommand.Aliases), |
| 170 | + CreateArgumentsDictionary(subCommand.Arguments), |
| 171 | + CreateOptionsDictionary(subCommand.Options), |
| 172 | + CreateSubcommandsDictionary(subCommand.Subcommands) |
| 173 | + ); |
| 174 | + |
| 175 | + private static OptionDetails CreateOptionDetails(Option option) => new OptionDetails( |
| 176 | + option.Description?.ReplaceLineEndings("\n"), |
| 177 | + option.Hidden, |
| 178 | + DetermineAliases(option.Aliases), |
| 179 | + option.HelpName, |
| 180 | + option.ValueType.ToCliTypeString(), |
| 181 | + option.HasDefaultValue, |
| 182 | + option.HasDefaultValue ? option.GetDefaultValue() : null, |
| 183 | + CreateArityDetails(option.Arity), |
| 184 | + option.Required, |
| 185 | + option.Recursive |
| 186 | + ); |
| 187 | + |
| 188 | + private static ArgumentDetails CreateArgumentDetails(int index, Argument argument) => new ArgumentDetails( |
| 189 | + argument.Description?.ReplaceLineEndings("\n"), |
| 190 | + index, |
| 191 | + argument.Hidden, |
| 192 | + argument.HelpName, |
| 193 | + argument.ValueType.ToCliTypeString(), |
| 194 | + argument.HasDefaultValue, |
| 195 | + argument.HasDefaultValue ? argument.GetDefaultValue() : null, |
| 196 | + CreateArityDetails(argument.Arity) |
| 197 | + ); |
| 198 | + |
| 199 | + // Produces a string that represents the command call. |
| 200 | + // For example, calling the workload install command produces `dotnet workload install`. |
| 201 | + private static string CommandHierarchyAsString(CommandResult commandResult) |
| 202 | + { |
| 203 | + var commands = new List<string>(); |
| 204 | + var currentResult = commandResult; |
| 205 | + while (currentResult is not null) |
| 206 | + { |
| 207 | + commands.Add(currentResult.Command.Name); |
| 208 | + currentResult = currentResult.Parent as CommandResult; |
| 209 | + } |
| 210 | + |
| 211 | + return string.Join(" ", commands.AsEnumerable().Reverse()); |
| 212 | + } |
| 213 | +} |
0 commit comments