Skip to content

Commit

Permalink
Update client
Browse files Browse the repository at this point in the history
  • Loading branch information
pomianowski committed May 30, 2024
1 parent 73b4b42 commit b6acf7b
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 9 deletions.
26 changes: 23 additions & 3 deletions src/OpenApi.Client.Cli/Commands/GenerateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,40 @@
// All Rights Reserved.

using OpenApi.Client.Cli.Settings;
using OpenApi.Client.SourceGenerators.Schema;
using OpenApi.Client.SourceGenerators.Serialization;

namespace OpenApi.Client.Cli.Commands;

/// <summary>
/// Represents the Generate command in the Open API Client CLI.
/// </summary>
/// <remarks>
/// This command reads an Open API file, deserializes it into an IApiDocument object using the OpenApiSerializer,
/// and then creates a new client class.
/// </remarks>
// ReSharper disable once ClassNeverInstantiated.Global
public sealed class GenerateCommand : AsyncCommand<GenerateCommandSettings>
{
/// <inheritdoc />
public override async Task<int> ExecuteAsync(CommandContext context, GenerateCommandSettings settings)
public override async Task<int> ExecuteAsync(
CommandContext context,
GenerateCommandSettings settings
)
{
await Task.CompletedTask;
string contents = await File.ReadAllTextAsync(settings.File);

SerializationResult<IApiDocument>? serializationResult =
new OpenApiSerializer().Deserialize(settings.File, contents);

return 0;
}

public override ValidationResult Validate(CommandContext context, GenerateCommandSettings settings)
/// <inheritdoc />
public override ValidationResult Validate(
CommandContext context,
GenerateCommandSettings settings
)
{
if (!Path.Exists(settings.File))
{
Expand Down
4 changes: 2 additions & 2 deletions src/OpenApi.Client.Cli/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// Copyright (C) Leszek Pomianowski and OpenAPI Client Contributors.
// All Rights Reserved.

global using System.ComponentModel;
global using System.Threading.Tasks;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Hosting;
global using Spectre.Console;
global using Spectre.Console.Cli;
global using System.ComponentModel;
global using System.Threading.Tasks;
4 changes: 4 additions & 0 deletions src/OpenApi.Client.Cli/OpenApi.Client.Cli.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>openapiclient</AssemblyName>
<PackageId>OpenApiClient.Cli</PackageId>
<TargetFramework>net8.0</TargetFramework>
<OutputType>exe</OutputType>
<PackAsTool>true</PackAsTool>
<ToolCommandName>openapiclient</ToolCommandName>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

<Import Project="..\OpenApi.Client.SourceGenerators\OpenApi.Client.SourceGenerators.projitems" Label="Shared" />
Expand Down
9 changes: 8 additions & 1 deletion src/OpenApi.Client.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@

app.Configure(config =>
{
config.AddCommand<GenerateCommand>("generate");
config.Settings.ApplicationName = "openapiclient";
config.Settings.CaseSensitivity = CaseSensitivity.None;

config
.AddCommand<GenerateCommand>("generate")
.WithDescription("Generates the Open API Client from the provided json.")
.WithExample(["generate", "my-open-api.json", "--output", "MyOpenApiClient.cs"]);
;
});

await app.RunAsync(args);
29 changes: 26 additions & 3 deletions src/OpenApi.Client.Cli/Settings/GenerateCommandSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,36 @@

namespace OpenApi.Client.Cli.Settings;

/// <summary>
/// Settings for the Generate command in the Open API Client CLI.
/// </summary>
/// <remarks>
/// This class represents the command line options and arguments for the Generate command.
/// The File property is a required argument that specifies the Open API file to parse.
/// The Output property is an optional option that specifies the output directory. If not provided, the current directory is used.
/// The Serializer property is an optional option that specifies the JSON serializer to use. If not provided, System.Text.Json is used.
/// </remarks>
// ReSharper disable once ClassNeverInstantiated.Global
public sealed class GenerateCommandSettings : CommandSettings
{
[CommandOption("-f|--file <PATH>")]
/// <summary>
/// Gets or sets the path to the Open API file to parse.
/// </summary>
[CommandArgument(0, "<PATH>")]
[Description("The Open API file to parse.")]
public string File { get; set; }
public string File { get; set; } = default!;

/// <summary>
/// Gets or sets the output directory. If not provided, the current directory is used.
/// </summary>
[CommandOption("-o|--output <PATH>")]
[Description("The output directory.")]
public string Output { get; set; }
public string Output { get; set; } = "./";

/// <summary>
/// Gets or sets the JSON serializer to use. If not provided, System.Text.Json is used.
/// </summary>
[CommandOption("-s|--serializer <TYPE>")]
[Description("The JSON serializer.")]
public JsonSerializerType Serializer { get; set; } = JsonSerializerType.SystemTextJson;
}
27 changes: 27 additions & 0 deletions src/OpenApi.Client.Cli/Settings/JsonSerializerType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and OpenAPI Client Contributors.
// All Rights Reserved.

namespace OpenApi.Client.Cli.Settings;

/// <summary>
/// Specifies the type of JSON serializer to use.
/// </summary>
/// <remarks>
/// This enum is used in the GenerateCommandSettings to allow the user to choose the JSON serializer.
/// SystemTextJson represents the System.Text.Json serializer.
/// NewtonsoftJson represents the Newtonsoft.Json serializer.
/// </remarks>
public enum JsonSerializerType
{
/// <summary>
/// Represents the System.Text.Json serializer.
/// </summary>
SystemTextJson,

/// <summary>
/// Represents the Newtonsoft.Json serializer.
/// </summary>
NewtonsoftJson
}

0 comments on commit b6acf7b

Please sign in to comment.