-
Notifications
You must be signed in to change notification settings - Fork 1.1k
dotnet CLI: Add --cli-schema option for CLI structure JSON #49118
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
Draft
MiYanni
wants to merge
3
commits into
dotnet:main
Choose a base branch
from
MiYanni:CliSchemaOutput
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
src/Cli/Microsoft.DotNet.Cli.Utils/Extensions/ArgumentExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// 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.Reflection; | ||
|
||
namespace Microsoft.DotNet.Cli.Utils.Extensions; | ||
|
||
public static class ArgumentExtensions | ||
{ | ||
private static readonly PropertyInfo[] s_nonPublicProperties = typeof(Argument).GetProperties(BindingFlags.Instance | BindingFlags.NonPublic); | ||
|
||
public static bool? GetHasValidators(this Argument argument) => | ||
s_nonPublicProperties.First(pi => pi.Name == "HasValidators").GetValue(argument) as bool?; | ||
} | ||
|
27 changes: 27 additions & 0 deletions
27
src/Cli/Microsoft.DotNet.Cli.Utils/Extensions/CommandExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Reflection; | ||
|
||
namespace Microsoft.DotNet.Cli.Utils.Extensions; | ||
|
||
#pragma warning disable IDE0065 // Misplaced using directive | ||
using Command = System.CommandLine.Command; | ||
#pragma warning restore IDE0065 // Misplaced using directive | ||
|
||
public static class CommandExtensions | ||
{ | ||
private static readonly PropertyInfo[] s_nonPublicProperties = typeof(Command).GetProperties(BindingFlags.Instance | BindingFlags.NonPublic); | ||
|
||
public static bool? GetHasArguments(this Command command) => | ||
s_nonPublicProperties.First(pi => pi.Name == "HasArguments").GetValue(command) as bool?; | ||
|
||
public static bool? GetHasOptions(this Command command) => | ||
s_nonPublicProperties.First(pi => pi.Name == "HasOptions").GetValue(command) as bool?; | ||
|
||
public static bool? GetHasSubcommands(this Command command) => | ||
s_nonPublicProperties.First(pi => pi.Name == "HasSubcommands").GetValue(command) as bool?; | ||
|
||
public static bool? GetHasValidators(this Command command) => | ||
s_nonPublicProperties.First(pi => pi.Name == "HasValidators").GetValue(command) as bool?; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Cli/Microsoft.DotNet.Cli.Utils/Extensions/OptionExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// 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.Reflection; | ||
|
||
namespace Microsoft.DotNet.Cli.Utils.Extensions; | ||
|
||
public static class OptionExtensions | ||
{ | ||
private static readonly PropertyInfo[] s_nonPublicProperties = typeof(Option).GetProperties(BindingFlags.Instance | BindingFlags.NonPublic); | ||
|
||
public static Argument? GetArgument(this Option option) => | ||
s_nonPublicProperties.First(pi => pi.Name == "Argument").GetValue(option) as Argument; | ||
|
||
public static bool? GetHasValidators(this Option option) => | ||
s_nonPublicProperties.First(pi => pi.Name == "HasValidators").GetValue(option) as bool?; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/Cli/Microsoft.DotNet.Cli.Utils/Extensions/TypeExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.DotNet.Cli.Utils.Extensions; | ||
|
||
public static class TypeExtensions | ||
{ | ||
// This is used when outputting the Type information for the CLI schema JSON. | ||
public static string ToCliTypeString(this Type type) | ||
{ | ||
var typeName = type.FullName ?? string.Empty; | ||
if (!type.IsGenericType) | ||
{ | ||
return typeName; | ||
} | ||
|
||
var genericTypeName = typeName.Substring(0, typeName.IndexOf('`')); | ||
var genericTypes = string.Join(", ", type.GenericTypeArguments.Select(generic => generic.ToCliTypeString())); | ||
return $"{genericTypeName}<{genericTypes}>"; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// 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.Text.Encodings.Web; | ||
using System.Text.Json; | ||
using Microsoft.DotNet.Cli.Utils; | ||
using Microsoft.DotNet.Cli.Utils.Extensions; | ||
using Command = System.CommandLine.Command; | ||
|
||
namespace Microsoft.DotNet.Cli; | ||
|
||
internal static class CliSchema | ||
{ | ||
// Using UnsafeRelaxedJsonEscaping because this JSON is not transmitted over the web. Therefore, HTML-sensitive characters are not encoded. | ||
// See: https://learn.microsoft.com/dotnet/api/system.text.encodings.web.javascriptencoder.unsaferelaxedjsonescaping | ||
private static readonly JsonWriterOptions s_jsonWriterOptions = new() { Indented = true, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }; | ||
private static readonly JsonSerializerOptions s_jsonSerializerOptions = new() { WriteIndented = true, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }; | ||
|
||
public static void PrintCliSchema(Command command) | ||
{ | ||
using var writer = new Utf8JsonWriter(Console.OpenStandardOutput(), s_jsonWriterOptions); | ||
writer.WriteStartObject(); | ||
|
||
// Explicitly write "name" into the root JSON object as the name for any sub-commands are used as the key to the sub-command object. | ||
writer.WriteString("name", command.Name); | ||
writer.WriteString("version", Product.Version); | ||
WriteCommand(command, writer); | ||
|
||
writer.WriteEndObject(); | ||
writer.Flush(); | ||
} | ||
|
||
private static void WriteCommand(Command command, Utf8JsonWriter writer) | ||
{ | ||
writer.WriteString(nameof(command.Description).ToCamelCase(), command.Description); | ||
writer.WriteBoolean(nameof(command.Hidden).ToCamelCase(), command.Hidden); | ||
|
||
writer.WriteStartArray(nameof(command.Aliases).ToCamelCase()); | ||
foreach (var alias in command.Aliases.Order()) | ||
{ | ||
writer.WriteStringValue(alias); | ||
} | ||
writer.WriteEndArray(); | ||
|
||
writer.WriteStartObject(nameof(command.Arguments).ToCamelCase()); | ||
// Leave default ordering for arguments. Do not order by name. | ||
foreach (var argument in command.Arguments) | ||
{ | ||
WriteArgument(argument, writer); | ||
} | ||
writer.WriteEndObject(); | ||
|
||
writer.WriteStartObject(nameof(command.Options).ToCamelCase()); | ||
foreach (var option in command.Options.OrderBy(o => o.Name)) | ||
{ | ||
WriteOption(option, writer); | ||
} | ||
writer.WriteEndObject(); | ||
|
||
writer.WriteStartObject(nameof(command.Subcommands).ToCamelCase()); | ||
foreach (var subCommand in command.Subcommands.OrderBy(sc => sc.Name)) | ||
{ | ||
writer.WriteStartObject(subCommand.Name); | ||
WriteCommand(subCommand, writer); | ||
writer.WriteEndObject(); | ||
} | ||
writer.WriteEndObject(); | ||
} | ||
|
||
private static void WriteArgument(Argument argument, Utf8JsonWriter writer) | ||
{ | ||
writer.WriteStartObject(argument.Name); | ||
|
||
writer.WriteString(nameof(argument.Description).ToCamelCase(), argument.Description); | ||
writer.WriteBoolean(nameof(argument.Hidden).ToCamelCase(), argument.Hidden); | ||
writer.WriteString(nameof(argument.HelpName).ToCamelCase(), argument.HelpName); | ||
writer.WriteString(nameof(argument.ValueType).ToCamelCase(), argument.ValueType.ToCliTypeString()); | ||
|
||
WriteDefaultValue(argument, writer); | ||
WriteArity(argument.Arity, writer); | ||
|
||
writer.WriteEndObject(); | ||
} | ||
|
||
private static void WriteOption(Option option, Utf8JsonWriter writer) | ||
{ | ||
writer.WriteStartObject(option.Name); | ||
|
||
writer.WriteString(nameof(option.Description).ToCamelCase(), option.Description); | ||
writer.WriteBoolean(nameof(option.Hidden).ToCamelCase(), option.Hidden); | ||
|
||
writer.WriteStartArray(nameof(option.Aliases).ToCamelCase()); | ||
foreach (var alias in option.Aliases.Order()) | ||
{ | ||
writer.WriteStringValue(alias); | ||
} | ||
writer.WriteEndArray(); | ||
|
||
writer.WriteString(nameof(option.HelpName).ToCamelCase(), option.HelpName); | ||
writer.WriteString(nameof(option.ValueType).ToCamelCase(), option.ValueType.ToCliTypeString()); | ||
|
||
// GetArgument will only return null if System.CommandLine is changed to no longer contain an Argument property within Option. | ||
var internalArgument = option.GetArgument() ?? new DynamicArgument<string>(string.Empty); | ||
WriteDefaultValue(internalArgument, writer); | ||
WriteArity(option.Arity, writer); | ||
|
||
writer.WriteBoolean(nameof(option.Required).ToCamelCase(), option.Required); | ||
writer.WriteBoolean(nameof(option.Recursive).ToCamelCase(), option.Recursive); | ||
|
||
writer.WriteEndObject(); | ||
} | ||
|
||
private static void WriteDefaultValue(Argument argument, Utf8JsonWriter writer) | ||
{ | ||
writer.WriteBoolean(nameof(argument.HasDefaultValue).ToCamelCase(), argument.HasDefaultValue); | ||
writer.WritePropertyName("defaultValue"); | ||
if (!argument.HasDefaultValue) | ||
{ | ||
writer.WriteNullValue(); | ||
return; | ||
} | ||
|
||
// Encode the value automatically based on the System.Type of the argument. | ||
JsonSerializer.Serialize(writer, argument.GetDefaultValue(), argument.ValueType, s_jsonSerializerOptions); | ||
return; | ||
} | ||
|
||
private static void WriteArity(ArgumentArity arity, Utf8JsonWriter writer) | ||
{ | ||
writer.WriteStartObject(nameof(arity)); | ||
|
||
writer.WriteNumber("minimum", arity.MinimumNumberOfValues); | ||
writer.WritePropertyName("maximum"); | ||
// ArgumentArity.ZeroOrMore.MaximumNumberOfValues is required as MaximumArity in ArgumentArity is a private field. | ||
if (arity.MaximumNumberOfValues == ArgumentArity.ZeroOrMore.MaximumNumberOfValues) | ||
{ | ||
// When the "OrMore" arity is present, write the maximum as null (thus unbounded). | ||
// The literal max integer value is set to an arbitrary amount (ATTOW 100000), which is not necessary to know for an external consumer. | ||
writer.WriteNullValue(); | ||
} | ||
else | ||
{ | ||
writer.WriteNumberValue(arity.MaximumNumberOfValues); | ||
} | ||
|
||
writer.WriteEndObject(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is bizarre but necessary since declaring the
using
above the namespace means that anything in the namespace scope will override it. Meaning,System.DotNet.Cli.Utils.Command
will be used even if an alias is declared. So, you put the alias inside the namespace to have it be used properly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: We should just change that
Command
class to not be calledCommand
sinceCliCommand
from S.CL was changed back toCommand
, which causes situations like this to happen.