Skip to content

Commit

Permalink
cli: Added init command.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Sep 7, 2024
1 parent 185df50 commit 02d484a
Show file tree
Hide file tree
Showing 33 changed files with 1,057 additions and 0 deletions.
132 changes: 132 additions & 0 deletions src/libs/OpenApiGenerator.Cli/Commands/InitializeCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System.CommandLine;
using System.Globalization;

namespace OpenApiGenerator.Cli.Commands;

public class InitializeCommand : Command
{
public InitializeCommand() : base(name: "init", description: "Creates a new solution with GitHub Actions.")
{
var solutionName = new Argument<string>(
name: "solution-name",
getDefaultValue: () => string.Empty,
description: "Solution name");
var apiName = new Argument<string>(
name: "api-name",
getDefaultValue: () => string.Empty,
description: "API client name");
var openApiSpec = new Argument<string>(
name: "open-api-spec",
getDefaultValue: () => string.Empty,
description: "OpenAPI spec url");
var company = new Argument<string>(
name: "company",
getDefaultValue: () => string.Empty,
description: "Company name");
var output = new Option<string>(
aliases: ["--output", "-o"],
getDefaultValue: () => string.Empty,
description: "Output directory");
var addMkDocs = new Option<bool>(
aliases: ["--add-mkdocs", "-m"],
getDefaultValue: () => true,
description: "Adds MkDocs to the solution");
AddArgument(solutionName);
AddArgument(apiName);
AddArgument(openApiSpec);
AddArgument(company);
AddOption(output);
AddOption(addMkDocs);

this.SetHandler(
HandleAsync,
solutionName,
apiName,
openApiSpec,
company,
output,
addMkDocs);
}

private static async Task HandleAsync(
string solutionName,
string apiName,
string openApiSpec,
string company,
string outputPath,
bool addMkDocs)
{
Console.WriteLine("Initializing...");

if (string.IsNullOrWhiteSpace(outputPath))
{
outputPath = Path.Combine(Directory.GetCurrentDirectory(), solutionName);
}
else
{
Directory.CreateDirectory(outputPath);
}

var resources = new List<H.Resource>
{
H.Resources.__SolutionName__sln,
H.Resources.assets_nuget_icon_png,
H.Resources.github_dependabot_yml,
H.Resources.github_workflows_auto_merge_yml,
H.Resources.github_workflows_auto_update_yml,
H.Resources.github_workflows_dotnet_yml,
H.Resources.github_workflows_pull_request_yml,
H.Resources.global_json,
H.Resources.LICENSE,
H.Resources.README_md,
H.Resources.src_Directory_Build_props,
H.Resources.src_helpers_FixOpenApiSpec_FixOpenApiSpec_csproj,
H.Resources.src_helpers_FixOpenApiSpec_Program_cs,
H.Resources.src_helpers_FixOpenApiSpec_Properties_launchSettings_json,
H.Resources.src_helpers_TrimmingHelper_Program_cs,
H.Resources.src_helpers_TrimmingHelper_TrimmingHelper_csproj,
H.Resources.src_key_snk,
H.Resources.src_libs__SolutionName___SolutionName__csproj,
H.Resources.src_libs__SolutionName__generate_sh,
H.Resources.src_libs_Directory_Build_props,
H.Resources.src_tests_IntegrationTests__SolutionName__IntegrationTests_csproj,
H.Resources.src_tests_IntegrationTests_Tests_cs,
H.Resources.src_tests_IntegrationTests_Tests_Test_cs,
};
if (addMkDocs)
{
resources.AddRange(new []
{
H.Resources.docs_css_extra_css,
H.Resources.docs_media_icon128_png,
H.Resources.github_workflows_mkdocs_yml,
H.Resources.mkdocs_yml,
H.Resources.src_helpers_GenerateDocs_GenerateDocs_csproj,
H.Resources.src_helpers_GenerateDocs_Program_cs,
});
}

foreach (var resource in resources)
{
await File.WriteAllTextAsync(
Path.Combine(outputPath, Replace(resource.FileName)
.Replace("_", Path.PathSeparator.ToString(), StringComparison.OrdinalIgnoreCase)),
Replace(resource.AsString())).ConfigureAwait(false);
}

Console.WriteLine("Done.");
return;

string Replace(string content)
{
return content
.Replace("$SolutionName$", solutionName, StringComparison.OrdinalIgnoreCase)
.Replace("$ApiName$", apiName, StringComparison.OrdinalIgnoreCase)
.Replace("$OpenApiSpec$", openApiSpec, StringComparison.OrdinalIgnoreCase)
.Replace("$CurrentYear$", DateTime.Now.Year.ToString(CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase)
.Replace("$CompanyName$", company, StringComparison.OrdinalIgnoreCase)
.Replace("$SolutionNameUppercase$", solutionName.ToUpperInvariant(), StringComparison.OrdinalIgnoreCase)
;
}
}
}
5 changes: 5 additions & 0 deletions src/libs/OpenApiGenerator.Cli/OpenApiGenerator.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="H.Resources.Generator" Version="1.6.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<Compile Remove="Resources\*.cs" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup>

Expand Down
1 change: 1 addition & 0 deletions src/libs/OpenApiGenerator.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
description: "CLI tool to use OpenApiGenerator");
rootCommand.AddCommand(new GenerateCommand());
rootCommand.AddCommand(new SimplifyCommand());
rootCommand.AddCommand(new InitializeCommand());

return await rootCommand.InvokeAsync(args).ConfigureAwait(false);
84 changes: 84 additions & 0 deletions src/libs/OpenApiGenerator.Cli/Resources/$SolutionName$.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30204.135
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{E793AF18-4371-4EBD-96FC-195EB1798855}"
ProjectSection(SolutionItems) = preProject
.gitignore = .gitignore
src\Directory.Build.props = src\Directory.Build.props
LICENSE = LICENSE
README.md = README.md
.github\dependabot.yml = .github\dependabot.yml
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "libs", "libs", "{61E7E11E-4558-434C-ACE8-06316A3097B3}"
ProjectSection(SolutionItems) = preProject
src\libs\Directory.Build.props = src\libs\Directory.Build.props
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{AAA11B78-2764-4520-A97E-46AA7089A588}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "$SolutionName$", "src\libs\$SolutionName$\$SolutionName$.csproj", "{0028BC85-0064-4CE8-A21A-C1F5E922BD59}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "$SolutionName$.IntegrationTests", "src\tests\$SolutionName$.IntegrationTests\$SolutionName$.IntegrationTests.csproj", "{592ADBC9-C951-4AF7-A163-B6C63B970B19}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "helpers", "helpers", "{B761B212-7CAB-46A7-BB98-B76EDE56530D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FixOpenApiSpec", "src\helpers\FixOpenApiSpec\FixOpenApiSpec.csproj", "{67BDEAD9-10A3-4D84-8D71-ED3FCF8C30B8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GenerateDocs", "src\helpers\GenerateDocs\GenerateDocs.csproj", "{2E33DFAF-4E36-4549-832E-202AAB6D1B52}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrimmingHelper", "src\helpers\TrimmingHelper\TrimmingHelper.csproj", "{4BB0367D-803F-430B-AE3B-853811C48A94}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{02762835-D98C-4E26-B6AC-992CC318DCEC}"
ProjectSection(SolutionItems) = preProject
.github\workflows\auto-merge.yml = .github\workflows\auto-merge.yml
.github\workflows\auto-update.yml = .github\workflows\auto-update.yml
.github\workflows\mkdocs.yml = .github\workflows\mkdocs.yml
.github\workflows\dotnet.yml = .github\workflows\dotnet.yml
.github\workflows\pull-request.yml = .github\workflows\pull-request.yml
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0028BC85-0064-4CE8-A21A-C1F5E922BD59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0028BC85-0064-4CE8-A21A-C1F5E922BD59}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0028BC85-0064-4CE8-A21A-C1F5E922BD59}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0028BC85-0064-4CE8-A21A-C1F5E922BD59}.Release|Any CPU.Build.0 = Release|Any CPU
{592ADBC9-C951-4AF7-A163-B6C63B970B19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{592ADBC9-C951-4AF7-A163-B6C63B970B19}.Debug|Any CPU.Build.0 = Debug|Any CPU
{592ADBC9-C951-4AF7-A163-B6C63B970B19}.Release|Any CPU.ActiveCfg = Release|Any CPU
{592ADBC9-C951-4AF7-A163-B6C63B970B19}.Release|Any CPU.Build.0 = Release|Any CPU
{67BDEAD9-10A3-4D84-8D71-ED3FCF8C30B8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{67BDEAD9-10A3-4D84-8D71-ED3FCF8C30B8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{67BDEAD9-10A3-4D84-8D71-ED3FCF8C30B8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{67BDEAD9-10A3-4D84-8D71-ED3FCF8C30B8}.Release|Any CPU.Build.0 = Release|Any CPU
{2E33DFAF-4E36-4549-832E-202AAB6D1B52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2E33DFAF-4E36-4549-832E-202AAB6D1B52}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2E33DFAF-4E36-4549-832E-202AAB6D1B52}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2E33DFAF-4E36-4549-832E-202AAB6D1B52}.Release|Any CPU.Build.0 = Release|Any CPU
{4BB0367D-803F-430B-AE3B-853811C48A94}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4BB0367D-803F-430B-AE3B-853811C48A94}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4BB0367D-803F-430B-AE3B-853811C48A94}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4BB0367D-803F-430B-AE3B-853811C48A94}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{0028BC85-0064-4CE8-A21A-C1F5E922BD59} = {61E7E11E-4558-434C-ACE8-06316A3097B3}
{592ADBC9-C951-4AF7-A163-B6C63B970B19} = {AAA11B78-2764-4520-A97E-46AA7089A588}
{67BDEAD9-10A3-4D84-8D71-ED3FCF8C30B8} = {B761B212-7CAB-46A7-BB98-B76EDE56530D}
{2E33DFAF-4E36-4549-832E-202AAB6D1B52} = {B761B212-7CAB-46A7-BB98-B76EDE56530D}
{4BB0367D-803F-430B-AE3B-853811C48A94} = {B761B212-7CAB-46A7-BB98-B76EDE56530D}
{02762835-D98C-4E26-B6AC-992CC318DCEC} = {E793AF18-4371-4EBD-96FC-195EB1798855}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CED9A020-DBA5-4BE6-8096-75E528648EC1}
EndGlobalSection
EndGlobal
21 changes: 21 additions & 0 deletions src/libs/OpenApiGenerator.Cli/Resources/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) $CurrentYear$ $CompanyName$

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
36 changes: 36 additions & 0 deletions src/libs/OpenApiGenerator.Cli/Resources/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# $SolutionName$

[![Nuget package](https://img.shields.io/nuget/vpre/$SolutionName$)](https://www.nuget.org/packages/$SolutionName$/)
[![dotnet](https://github.com/$CompanyName$/$SolutionName$/actions/workflows/dotnet.yml/badge.svg?branch=main)](https://github.com/$CompanyName$/$SolutionName$/actions/workflows/dotnet.yml)
[![License: MIT](https://img.shields.io/github/license/$CompanyName$/$SolutionName$)](https://github.com/$CompanyName$/$SolutionName$/blob/main/LICENSE.txt)
[![Discord](https://img.shields.io/discord/1115206893015662663?label=Discord&logo=discord&logoColor=white&color=d82679)](https://discord.gg/Ca2xhfBf3v)

## Features 🔥
- Fully generated C# SDK based on [official $SolutionName$ OpenAPI specification](https://raw.githubusercontent.com/$SolutionName$/assemblyai-api-spec/main/openapi.yml) using [OpenApiGenerator](https://github.com/HavenDV/OpenApiGenerator)
- Same day update to support new features
- Updated and supported automatically if there are no breaking changes
- All modern .NET features - nullability, trimming, NativeAOT, etc.
- Support .Net Framework/.Net Standard 2.0

### Usage
```csharp
using $SolutionName$;

using var api = new $ApiName$(apiKey);
```

## Support

Priority place for bugs: https://github.com/$CompanyName$/$SolutionName$/issues
Priority place for ideas and general questions: https://github.com/$CompanyName$/$SolutionName$/discussions
Discord: https://discord.gg/Ca2xhfBf3v

## Acknowledgments

![JetBrains logo](https://resources.jetbrains.com/storage/products/company/brand/logos/jetbrains.png)

This project is supported by JetBrains through the [Open Source Support Program](https://jb.gg/OpenSourceSupport).

![CodeRabbit logo](https://opengraph.githubassets.com/1c51002d7d0bbe0c4fd72ff8f2e58192702f73a7037102f77e4dbb98ac00ea8f/marketplace/coderabbitai)

This project is supported by CodeRabbit through the [Open Source Support Program](https://github.com/marketplace/coderabbitai).
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 02d484a

Please sign in to comment.