diff --git a/src/libs/AutoSDK.CLI/Commands/GenerateCommand.cs b/src/libs/AutoSDK.CLI/Commands/GenerateCommand.cs index 428fa5a541..99702b5bda 100644 --- a/src/libs/AutoSDK.CLI/Commands/GenerateCommand.cs +++ b/src/libs/AutoSDK.CLI/Commands/GenerateCommand.cs @@ -58,53 +58,48 @@ public class GenerateCommand : Command this.SetHandler( HandleAsync, - inputOption, - outputOption, - targetFrameworkOption, - namespaceOption, - clientClassNameOption, - singleFileOption, - excludeDeprecatedOption, - clsCompliantEnumPrefixOption); + new GenerateSettingsBinder( + inputOption, + outputOption, + targetFrameworkOption, + namespaceOption, + clientClassNameOption, + methodNamingConventionOption, + singleFileOption, + excludeDeprecatedOption, + clsCompliantEnumPrefixOption)); } private static async Task HandleAsync( - string inputPath, - string outputPath, - string targetFramework, - string @namespace, - string clientClassName, - bool generateAsSingleFile, - bool excludeDeprecatedOperations, - string clsCompliantEnumPrefix) + GenerateSettings arguments) { - Console.WriteLine($"Loading {inputPath}..."); + Console.WriteLine($"Loading {arguments.Input}..."); using var client = new HttpClient(); - var yaml = inputPath.StartsWith("http", StringComparison.OrdinalIgnoreCase) - ? await client.GetStringAsync(new Uri(inputPath)).ConfigureAwait(false) - : await File.ReadAllTextAsync(inputPath).ConfigureAwait(false); + var yaml = arguments.Input.StartsWith("http", StringComparison.OrdinalIgnoreCase) + ? await client.GetStringAsync(new Uri(arguments.Input)).ConfigureAwait(false) + : await File.ReadAllTextAsync(arguments.Input).ConfigureAwait(false); Console.WriteLine("Generating..."); - var name = Path.GetFileNameWithoutExtension(inputPath); + var name = Path.GetFileNameWithoutExtension(arguments.Input); - if (string.IsNullOrWhiteSpace(@namespace)) + if (string.IsNullOrWhiteSpace(arguments.Namespace)) { - @namespace = name.ToPropertyName() + arguments.Namespace = name.ToPropertyName() .UseWordSeparator('\\', '-', '.', '_', '/'); } - if (string.IsNullOrWhiteSpace(clientClassName)) + if (string.IsNullOrWhiteSpace(arguments.ClientClassName)) { - clientClassName = $"{name.ToPropertyName() + arguments.ClientClassName = $"{name.ToPropertyName() .UseWordSeparator('\\', '-', '.', '_', '/')}Api"; } var settings = new Settings( - TargetFramework: targetFramework, - Namespace: @namespace, - ClassName: clientClassName, - ClsCompliantEnumPrefix: clsCompliantEnumPrefix, + TargetFramework: arguments.TargetFramework, + Namespace: arguments.Namespace, + ClassName: arguments.ClientClassName, + ClsCompliantEnumPrefix: arguments.ClsCompliantEnumPrefix, NamingConvention: default, JsonSerializerType: default, UseRequiredKeyword: default, @@ -119,8 +114,8 @@ private static async Task HandleAsync( ExcludeOperationIds: [], IncludeTags: [], ExcludeTags: [], - ExcludeDeprecatedOperations: excludeDeprecatedOperations, - JsonSerializerContext: $"{@namespace}.SourceGenerationContext", + ExcludeDeprecatedOperations: arguments.ExcludeDeprecatedOperations, + JsonSerializerContext: $"{arguments.Namespace}.SourceGenerationContext", GenerateJsonSerializerContextTypes: true, GenerateModels: false, ValidateAnyOfs: false, @@ -151,18 +146,18 @@ private static async Task HandleAsync( .Where(x => !x.IsEmpty) .ToArray(); - Directory.CreateDirectory(outputPath); + Directory.CreateDirectory(arguments.Output); - if (generateAsSingleFile) + if (arguments.SingleFile) { var text = string.Join(Environment.NewLine, files.Select(x => x.Text)); - await File.WriteAllTextAsync(Path.Combine(outputPath, $"{name}.cs"), text).ConfigureAwait(false); + await File.WriteAllTextAsync(Path.Combine(arguments.Output, $"{name}.cs"), text).ConfigureAwait(false); return; } foreach (var file in files) { - await File.WriteAllTextAsync(Path.Combine(outputPath, file.Name), file.Text).ConfigureAwait(false); + await File.WriteAllTextAsync(Path.Combine(arguments.Output, file.Name), file.Text).ConfigureAwait(false); } Console.WriteLine("Done."); diff --git a/src/libs/AutoSDK.CLI/Commands/GenerateSettings.cs b/src/libs/AutoSDK.CLI/Commands/GenerateSettings.cs new file mode 100644 index 0000000000..44236a80b6 --- /dev/null +++ b/src/libs/AutoSDK.CLI/Commands/GenerateSettings.cs @@ -0,0 +1,16 @@ +using AutoSDK.Naming.Methods; + +namespace AutoSDK.CLI.Commands; + +public class GenerateSettings +{ + public required string Input { get; set; } + public required string Output { get; set; } + public required string TargetFramework { get; set; } + public required string Namespace { get; set; } + public required string ClientClassName { get; set; } + public required MethodNamingConvention MethodNamingConvention { get; set; } + public required bool SingleFile { get; set; } + public required bool ExcludeDeprecatedOperations { get; set; } + public required string ClsCompliantEnumPrefix { get; set; } +} \ No newline at end of file diff --git a/src/libs/AutoSDK.CLI/Commands/GenerateSettingsBinder.cs b/src/libs/AutoSDK.CLI/Commands/GenerateSettingsBinder.cs new file mode 100644 index 0000000000..934fc2d3c6 --- /dev/null +++ b/src/libs/AutoSDK.CLI/Commands/GenerateSettingsBinder.cs @@ -0,0 +1,37 @@ +using System.CommandLine; +using System.CommandLine.Binding; +using AutoSDK.Naming.Methods; + +namespace AutoSDK.CLI.Commands; + +public class GenerateSettingsBinder( + Argument input, + Option output, + Option targetFramework, + Option @namespace, + Option clientClassName, + Option methodNamingConvention, + Option singleFile, + Option excludeDeprecatedOperations, + Option clsCompliantEnumPrefix + ) + : BinderBase +{ + protected override GenerateSettings GetBoundValue(BindingContext bindingContext) + { + bindingContext = bindingContext ?? throw new ArgumentNullException(nameof(bindingContext)); + + return new GenerateSettings + { + Input = bindingContext.ParseResult.GetValueForArgument(input), + Output = bindingContext.ParseResult.GetValueForOption(output)!, + TargetFramework = bindingContext.ParseResult.GetValueForOption(targetFramework)!, + Namespace = bindingContext.ParseResult.GetValueForOption(@namespace)!, + ClientClassName = bindingContext.ParseResult.GetValueForOption(clientClassName)!, + MethodNamingConvention = bindingContext.ParseResult.GetValueForOption(methodNamingConvention), + SingleFile = bindingContext.ParseResult.GetValueForOption(singleFile), + ExcludeDeprecatedOperations = bindingContext.ParseResult.GetValueForOption(excludeDeprecatedOperations), + ClsCompliantEnumPrefix = bindingContext.ParseResult.GetValueForOption(clsCompliantEnumPrefix)!, + }; + } +} \ No newline at end of file