Skip to content

Commit 7f8d002

Browse files
committed
feat: Improve CLI generation.
1 parent 3957e22 commit 7f8d002

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

src/libs/AutoSDK/Sources/Sources.CLI.Command.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public static string GenerateCommand(
1818

1919
var clientType = $"{endPoint.GlobalSettings.Namespace}.I{endPoint.GlobalSettings.ClassName}";
2020
var canReturn = endPoint.SuccessResponse.Type != TypeData.Default;
21+
22+
var newModifierIfRequired = (MethodParameter parameter) =>
23+
parameter.Name.ToPropertyName() is "Name" or "Description" or "Action"
24+
? "new "
25+
: "";
2126

2227
return $@"
2328
#nullable enable
@@ -28,25 +33,28 @@ internal sealed partial class {endPoint.NotAsyncMethodName}Command : global::Sys
2833
{{
2934
private readonly {clientType} _client;
3035
31-
partial void BeforeRequest(
36+
partial void Initialize();
37+
partial void Validate(
38+
global::System.CommandLine.ParseResult parseResult,
3239
{endPoint.Parameters.Select((x, i) => @$"
3340
{x.Type.CSharpType} {x.ParameterName},").Inject()}
34-
CancellationToken cancellationToken);
35-
partial void AfterRequest(
41+
global::System.Threading.CancellationToken cancellationToken);
42+
partial void Complete(
43+
global::System.CommandLine.ParseResult parseResult,
3644
{(canReturn ? $@"
3745
{endPoint.SuccessResponse.Type.CSharpType} response," : " ")}
38-
CancellationToken cancellationToken);
46+
global::System.Threading.CancellationToken cancellationToken);
3947
4048
{endPoint.Parameters.Where(x => x.IsRequired).Select((x, i) => @$"
41-
private global::System.CommandLine.Argument<{x.Type.CSharpType}> {x.Name.ToPropertyName()} {{ get; }} = new(
49+
private {newModifierIfRequired(x)}global::System.CommandLine.Argument<{x.Type.CSharpType}> {x.Name.ToPropertyName()} {{ get; }} = new(
4250
name: ""{x.ParameterName}"")
4351
{{
4452
Description = """",
4553
}};
4654
").Inject()}
4755
4856
{endPoint.Parameters.Where(x => !x.IsRequired).Select((x, i) => @$"
49-
private global::System.CommandLine.Option<{x.Type.CSharpType}> {x.Name.ToPropertyName()} {{ get; }} = new(
57+
private {newModifierIfRequired(x)}global::System.CommandLine.Option<{x.Type.CSharpType}> {x.Name.ToPropertyName()} {{ get; }} = new(
5058
name: ""{x.ParameterName}"")
5159
{{
5260
Description = """",
@@ -55,7 +63,7 @@ partial void AfterRequest(
5563
5664
public {endPoint.NotAsyncMethodName}Command({clientType} client) : base(
5765
name: ""{endPoint.CliAction}"",
58-
description: ""{endPoint.Description}"")
66+
description: @""{endPoint.Description.Replace("\"", "\"\"")}"")
5967
{{
6068
_client = client;
6169
@@ -64,30 +72,36 @@ partial void AfterRequest(
6472
{endPoint.Parameters.Where(x => !x.IsRequired).Select((x, i) => @$"
6573
Options.Add({x.Name.ToPropertyName()});").Inject()}
6674
75+
Initialize();
76+
6777
SetAction(HandleAsync);
6878
}}
6979
70-
private async Task HandleAsync(
80+
private async global::System.Threading.Tasks.Task HandleAsync(
7181
global::System.CommandLine.ParseResult parseResult,
72-
CancellationToken cancellationToken = default)
82+
global::System.Threading.CancellationToken cancellationToken = default)
7383
{{
7484
{endPoint.Parameters.Select((x, i) => @$"
7585
var {x.ParameterName} = parseResult.GetRequiredValue({x.Name.ToPropertyName()});").Inject()}
7686
77-
BeforeRequest(
87+
Validate(
88+
parseResult: parseResult,
7889
{endPoint.Parameters.Select((x, i) => @$"
7990
{x.ParameterName}: {x.ParameterName},").Inject()}
8091
cancellationToken: cancellationToken);
8192
8293
// ReSharper disable once RedundantAssignment
8394
{(!canReturn
8495
? string.Empty
85-
: "var response = ")}await _client.{endPoint.Tag.SafeName}.{endPoint.MethodName}(
96+
: "var response = ")}await _client.{(!string.IsNullOrWhiteSpace(endPoint.Tag.SafeName)
97+
? $"{endPoint.Tag.SafeName}."
98+
: "")}{endPoint.MethodName}(
8699
{endPoint.Parameters.Select((x, i) => @$"
87100
{x.ParameterName}: {x.ParameterName},").Inject()}
88101
cancellationToken: cancellationToken);
89102
90-
AfterRequest(
103+
Complete(
104+
parseResult: parseResult,
91105
{(canReturn ? @"
92106
response: response," : " ")}
93107
cancellationToken: cancellationToken);

0 commit comments

Comments
 (0)