Skip to content

Commit

Permalink
fix: Fixed settings binding in cli.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Sep 11, 2024
1 parent 517e735 commit 0688cb0
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 105 deletions.
81 changes: 29 additions & 52 deletions src/libs/AutoSDK.CLI/Commands/GenerateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,74 +58,51 @@ public class GenerateCommand : Command

this.SetHandler(
HandleAsync,
new GenerateSettingsBinder(
inputOption,
outputOption,
inputOption,
outputOption,
singleFileOption,
new SettingsBinder(
targetFrameworkOption,
namespaceOption,
clientClassNameOption,
methodNamingConventionOption,
singleFileOption,
excludeDeprecatedOption,
clsCompliantEnumPrefixOption));
}

private static async Task HandleAsync(
GenerateSettings arguments)
string input,
string output,
bool singleFile,
Settings settings)
{
Console.WriteLine($"Loading {arguments.Input}...");
Console.WriteLine($"Loading {input}...");

using var client = new HttpClient();
var yaml = arguments.Input.StartsWith("http", StringComparison.OrdinalIgnoreCase)
? await client.GetStringAsync(new Uri(arguments.Input)).ConfigureAwait(false)
: await File.ReadAllTextAsync(arguments.Input).ConfigureAwait(false);
var yaml = input.StartsWith("http", StringComparison.OrdinalIgnoreCase)
? await client.GetStringAsync(new Uri(input)).ConfigureAwait(false)
: await File.ReadAllTextAsync(input).ConfigureAwait(false);

Console.WriteLine("Generating...");

var name = Path.GetFileNameWithoutExtension(arguments.Input);
var name = Path.GetFileNameWithoutExtension(input);

if (string.IsNullOrWhiteSpace(arguments.Namespace))
if (string.IsNullOrWhiteSpace(settings.Namespace))
{
arguments.Namespace = name.ToPropertyName()
.UseWordSeparator('\\', '-', '.', '_', '/');
settings = settings with
{
Namespace = name.ToPropertyName()
.UseWordSeparator('\\', '-', '.', '_', '/'),
};
}
if (string.IsNullOrWhiteSpace(arguments.ClientClassName))
if (string.IsNullOrWhiteSpace(settings.ClassName))
{
arguments.ClientClassName = $"{name.ToPropertyName()
.UseWordSeparator('\\', '-', '.', '_', '/')}Api";
settings = settings with
{
ClassName = $"{name.ToPropertyName()
.UseWordSeparator('\\', '-', '.', '_', '/')}Api",
};
}

var settings = new Settings(
TargetFramework: arguments.TargetFramework,
Namespace: arguments.Namespace,
ClassName: arguments.ClientClassName,
ClsCompliantEnumPrefix: arguments.ClsCompliantEnumPrefix,
NamingConvention: default,
JsonSerializerType: default,
UseRequiredKeyword: default,
GenerateConstructors: false,
GroupByTags: true,
GenerateMethods: false,
MethodNamingConvention: default,
MethodNamingConventionFallback: MethodNamingConvention.MethodAndPath,
GenerateMethodsAsHttpClientExtensions: false,
GenerateMethodsUsingSystemNetHttpJson: false,
IncludeOperationIds: [],
ExcludeOperationIds: [],
IncludeTags: [],
ExcludeTags: [],
ExcludeDeprecatedOperations: arguments.ExcludeDeprecatedOperations,
JsonSerializerContext: $"{arguments.Namespace}.SourceGenerationContext",
GenerateJsonSerializerContextTypes: true,
GenerateModels: false,
ValidateAnyOfs: false,
ModelStyle: default,
IncludeModels: [],
ExcludeModels: [],
GeneratePolyfills: true,
GenerateSdk: true,
FromCli: true
);

var data = Generation.Data.Prepare((yaml, settings));
var files = data.Enums
Expand All @@ -146,18 +123,18 @@ private static async Task HandleAsync(
.Where(x => !x.IsEmpty)
.ToArray();

Directory.CreateDirectory(arguments.Output);
Directory.CreateDirectory(output);

if (arguments.SingleFile)
if (singleFile)
{
var text = string.Join(Environment.NewLine, files.Select(x => x.Text));
await File.WriteAllTextAsync(Path.Combine(arguments.Output, $"{name}.cs"), text).ConfigureAwait(false);
await File.WriteAllTextAsync(Path.Combine(output, $"{name}.cs"), text).ConfigureAwait(false);
return;
}

foreach (var file in files)
{
await File.WriteAllTextAsync(Path.Combine(arguments.Output, file.Name), file.Text).ConfigureAwait(false);
await File.WriteAllTextAsync(Path.Combine(output, file.Name), file.Text).ConfigureAwait(false);
}

Console.WriteLine("Done.");
Expand Down
16 changes: 0 additions & 16 deletions src/libs/AutoSDK.CLI/Commands/GenerateSettings.cs

This file was deleted.

37 changes: 0 additions & 37 deletions src/libs/AutoSDK.CLI/Commands/GenerateSettingsBinder.cs

This file was deleted.

56 changes: 56 additions & 0 deletions src/libs/AutoSDK.CLI/Commands/SettingsBinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.CommandLine;
using System.CommandLine.Binding;
using AutoSDK.Models;
using AutoSDK.Naming.Methods;

namespace AutoSDK.CLI.Commands;

public class SettingsBinder(
Option<string> targetFramework,
Option<string> @namespace,
Option<string> clientClassName,
Option<MethodNamingConvention> methodNamingConvention,
Option<bool> excludeDeprecatedOperations,
Option<string> clsCompliantEnumPrefix
)
: BinderBase<Settings>
{
protected override Settings GetBoundValue(BindingContext bindingContext)
{
bindingContext = bindingContext ?? throw new ArgumentNullException(nameof(bindingContext));

var namespaceValue = bindingContext.ParseResult.GetValueForOption(@namespace);

return new Settings(
TargetFramework: bindingContext.ParseResult.GetValueForOption(targetFramework)!,
Namespace: namespaceValue!,
ClassName: bindingContext.ParseResult.GetValueForOption(clientClassName)!,
ClsCompliantEnumPrefix: bindingContext.ParseResult.GetValueForOption(clsCompliantEnumPrefix)!,
NamingConvention: default,
JsonSerializerType: default,
UseRequiredKeyword: default,
GenerateConstructors: false,
GroupByTags: true,
GenerateMethods: false,
MethodNamingConvention: bindingContext.ParseResult.GetValueForOption(methodNamingConvention),
MethodNamingConventionFallback: MethodNamingConvention.MethodAndPath,
GenerateMethodsAsHttpClientExtensions: false,
GenerateMethodsUsingSystemNetHttpJson: false,
IncludeOperationIds: [],
ExcludeOperationIds: [],
IncludeTags: [],
ExcludeTags: [],
ExcludeDeprecatedOperations: bindingContext.ParseResult.GetValueForOption(excludeDeprecatedOperations),
JsonSerializerContext: $"{namespaceValue}.SourceGenerationContext",
GenerateJsonSerializerContextTypes: true,
GenerateModels: false,
ValidateAnyOfs: false,
ModelStyle: default,
IncludeModels: [],
ExcludeModels: [],
GeneratePolyfills: true,
GenerateSdk: true,
FromCli: true
);
}
}

0 comments on commit 0688cb0

Please sign in to comment.