Skip to content

Commit

Permalink
refactor: Added GenerateSettings to bypass limit in cli.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Sep 11, 2024
1 parent cdd293b commit 05f597b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 35 deletions.
65 changes: 30 additions & 35 deletions src/libs/AutoSDK.CLI/Commands/GenerateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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.");
Expand Down
16 changes: 16 additions & 0 deletions src/libs/AutoSDK.CLI/Commands/GenerateSettings.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
37 changes: 37 additions & 0 deletions src/libs/AutoSDK.CLI/Commands/GenerateSettingsBinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.CommandLine;
using System.CommandLine.Binding;
using AutoSDK.Naming.Methods;

namespace AutoSDK.CLI.Commands;

public class GenerateSettingsBinder(
Argument<string> input,
Option<string> output,
Option<string> targetFramework,
Option<string> @namespace,
Option<string> clientClassName,
Option<MethodNamingConvention> methodNamingConvention,
Option<bool> singleFile,
Option<bool> excludeDeprecatedOperations,
Option<string> clsCompliantEnumPrefix
)
: BinderBase<GenerateSettings>
{
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)!,
};
}
}

0 comments on commit 05f597b

Please sign in to comment.