Skip to content

Commit

Permalink
asyncapi#196 Tool rewrite to make its components testable, removed Sw…
Browse files Browse the repository at this point in the history
…achbuckle copy/paste code
  • Loading branch information
Senn Geerts authored and Senn Geerts committed Jul 9, 2024
1 parent a746e4d commit 8e7a01f
Show file tree
Hide file tree
Showing 19 changed files with 290 additions and 928 deletions.
10 changes: 0 additions & 10 deletions src/AsyncAPI.Saunter.Generator.Cli/Args.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<TargetFrameworks>net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>12</LangVersion>
<RootNamespace>AsyncAPI.Saunter.Generator.Cli</RootNamespace>
Expand All @@ -11,6 +11,7 @@
<Authors>AsyncAPI Initiative</Authors>
<PackAsTool>true</PackAsTool>
<PackageId>AsyncAPI.Saunter.Generator.Cli</PackageId>
<PackageType>DotnetTool</PackageType>
<ToolCommandName>dotnet-asyncapi</ToolCommandName>
<PackageTags>asyncapi;aspnetcore;openapi;documentation;amqp;generator;cli;tool</PackageTags>
<PackageReadmeFile>readme.md</PackageReadmeFile>
Expand All @@ -34,18 +35,21 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AsyncAPI.NET.Readers" Version="5.2.1" />

<!-- Development Dependencies -->
<PackageReference Include="AsyncAPI.NET.Readers" Version="5.2.1" PrivateAssets="All" />
<PackageReference Include="ConsoleAppFramework" Version="5.2.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="NSwag.Commands" Version="14.0.8" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Saunter\Saunter.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="readme.md" Pack="true" PackagePath="\"/>
<None Include="readme.md" Pack="true" PackagePath="\" />
<None Include="../../assets/logo.png" Pack="true" PackagePath="/" />
</ItemGroup>

Expand Down
42 changes: 0 additions & 42 deletions src/AsyncAPI.Saunter.Generator.Cli/Commands/Tofile.cs

This file was deleted.

195 changes: 0 additions & 195 deletions src/AsyncAPI.Saunter.Generator.Cli/Commands/TofileInternal.cs

This file was deleted.

24 changes: 0 additions & 24 deletions src/AsyncAPI.Saunter.Generator.Cli/Internal/DependencyResolver.cs

This file was deleted.

50 changes: 14 additions & 36 deletions src/AsyncAPI.Saunter.Generator.Cli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,17 @@
using AsyncApi.Saunter.Generator.Cli.Commands;
using AsyncApi.Saunter.Generator.Cli.SwashbuckleImport;
using AsyncAPI.Saunter.Generator.Cli.Internal;
using AsyncAPI.Saunter.Generator.Cli.ToFile;
using ConsoleAppFramework;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

DependencyResolver.Init();
var services = new ServiceCollection();
services.AddLogging(builder => builder.AddSimpleConsole(x => x.SingleLine = true).SetMinimumLevel(LogLevel.Trace));
services.AddToFileCommand();

// Helper to simplify command line parsing etc.
var runner = new CommandRunner("dotnet asyncapi", "AsyncAPI Command Line Tools", Console.Out);
using var serviceProvider = services.BuildServiceProvider();
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
ConsoleApp.LogError = msg => logger.LogError(msg);
ConsoleApp.ServiceProvider = serviceProvider;

// NOTE: The "dotnet asyncapi tofile" command does not serve the request directly. Instead, it invokes a corresponding
// command (called _tofile) via "dotnet exec" so that the runtime configuration (*.runtimeconfig & *.deps.json) of the
// provided startupassembly can be used instead of the tool's. This is neccessary to successfully load the
// startupassembly and it's transitive dependencies. See https://github.com/dotnet/coreclr/issues/13277 for more.

// > dotnet asyncapi tofile ...
runner.SubCommand("tofile", "retrieves AsyncAPI from a startup assembly, and writes to file ", c =>
{
c.Argument(StartupAssemblyArgument, "relative path to the application's startup assembly");
c.Option(DocOption, "name(s) of the AsyncAPI documents you want to retrieve, as configured in your startup class [defaults to all documents]");
c.Option(OutputOption, "relative path where the AsyncAPI will be output [defaults to stdout]");
c.Option(FileNameOption, "defines the file name template, {document} and {extension} template variables can be used [defaults to \"{document}_asyncapi.{extension}\"]");
c.Option(FormatOption, "exports AsyncAPI in json and/or yml format [defaults to json]");
c.Option(EnvOption, "define environment variable(s) for the application during generation of the AsyncAPI files [defaults to empty, can be used to define for example ASPNETCORE_ENVIRONMENT]");
c.OnRun(Tofile.Run(args));
});

// > dotnet asyncapi _tofile ... (* should only be invoked via "dotnet exec")
runner.SubCommand("_tofile", "", c =>
{
c.Argument(StartupAssemblyArgument, "");
c.Option(DocOption, "");
c.Option(OutputOption, "");
c.Option(FileNameOption, "");
c.Option(FormatOption, "");
c.Option(EnvOption, "");
c.OnRun(TofileInternal.Run);
});

return runner.Run(args);
var app = ConsoleApp.Create();
app.Add<ToFileCommand>();
app.Run(args);
Loading

0 comments on commit 8e7a01f

Please sign in to comment.