118 lines
4.1 KiB
C#
118 lines
4.1 KiB
C#
using System.Text;
|
|
using Core;
|
|
using Core.Events;
|
|
using Core.Helpers;
|
|
using Core.Settings;
|
|
using Core.Yaml;
|
|
using YamlDotNet.Core;
|
|
|
|
namespace Generator.Infrastructure.OpenApi.Builders;
|
|
|
|
public class OpenApiDotnetClientBuilder : AbstractBuilder<OpenApiYaml>
|
|
{
|
|
private readonly DotnetConfig _config;
|
|
public OpenApiDotnetClientBuilder(DotnetConfig config, DisplayEmitter emitter)
|
|
: base(config.Invite, emitter)
|
|
{
|
|
_config = config;
|
|
}
|
|
|
|
///<inheritdoc/>
|
|
public override void Generate()
|
|
{
|
|
CheckForNullSpec();
|
|
|
|
CreateIgnore();
|
|
GenerateOpenApi();
|
|
|
|
var inputFile = new Location([_config.DockerRoot, _config.OpenApiConfigFile()]);
|
|
|
|
var command = $"docker run --rm -it -v {_config.LocalRoot}:{_config.DockerRoot} {_config.GenerationImage} batch --clean {inputFile}";
|
|
Executor.Register(command);
|
|
}
|
|
|
|
///<inheritdoc/>
|
|
public override void Build()
|
|
{
|
|
CheckForNullSpec();
|
|
|
|
var templateFolder = new Location([_config.DockerRoot, _config.TemplateFolder]);
|
|
var packageFolder = new Location([_config.DockerRoot, _config.PackageFolderPath()]);
|
|
|
|
var stringBuilder = new StringBuilder();
|
|
stringBuilder
|
|
.Append($"docker run --rm -it -v {_config.LocalRoot}:{_config.DockerRoot} {_config.BuildImage} /bin/sh -c '")
|
|
.Append($"cp {templateFolder}/NuGet.config {packageFolder} && ")
|
|
.Append($"cd {packageFolder} && ")
|
|
.Append($"dotnet pack -c Release -o out -p:PackageVersion={SpecFile.Version}'");
|
|
|
|
var command = stringBuilder.ToString();
|
|
Executor.Register(command);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates config file used by openapi generator
|
|
/// </summary>
|
|
public void CreateGeneratorConfig()
|
|
{
|
|
CheckForNullSpec();
|
|
|
|
var inputPath = new Location([_config.LocalRoot, _config.ClientConfigTemplate]);
|
|
var outputPath = new Location([_config.LocalRoot, _config.ConfigFilePath()]);
|
|
|
|
PathHelper.CreateFileIfNotExists(outputPath.ToString());
|
|
|
|
var data = new Dictionary<string, object>
|
|
{
|
|
{"specPath", new Location([_config.DockerRoot, _config.OpenApiSpecFile()]).ToString()},
|
|
{"templateFolder", new Location([_config.DockerRoot, _config.TemplateFolder]).ToString()},
|
|
{"generatorVersion", "7.3.0"},
|
|
{"outputFolder", new Location([_config.DockerRoot, _config.OutputFolder()]).ToString()},
|
|
{"removeModelPackage", false},
|
|
{"modelSuffix", SpecFile.Config.ModelSuffix},
|
|
{"packageName", SpecFile.Config.NugetPackage},
|
|
{"packageVersion", SpecFile.Info["version"]},
|
|
{"refs", SpecFile.ReferencedSchemas},
|
|
{"modelNameSpace", SpecFile.ReferencedSchemas},
|
|
};
|
|
|
|
MakeTemplate(inputPath.ToString(), outputPath.ToString(), data);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Registers the generation of the openapi spec file
|
|
/// </summary>
|
|
private void GenerateOpenApi()
|
|
{
|
|
CheckForNullSpec();
|
|
|
|
var specFile = new Location([_config.DockerRoot, _config.SpecFile]);
|
|
var output = new Location([_config.DockerRoot, _config.OpenApiFolder()]);
|
|
|
|
var command = $"docker run --rm -it -v {_config.LocalRoot}:{_config.DockerRoot} {_config.GenerationImage} " +
|
|
$"generate -g openapi-yaml -i {specFile} -o {output}";
|
|
Executor.Register(command);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generates the ignore file used by openapi generator
|
|
/// </summary>
|
|
private void CreateIgnore()
|
|
{
|
|
CheckForNullSpec();
|
|
|
|
var inputPath = new Location([_config.LocalRoot, _config.ClientIgnoreTemplate]).ToString();
|
|
var outputPath = new Location([_config.LocalRoot, _config.IgnoreFilePath()]).ToString();
|
|
|
|
PathHelper.CreateFileIfNotExists(outputPath);
|
|
|
|
var data = new Dictionary<string, object>
|
|
{
|
|
{"modelSuffix", SpecFile!.Config.ModelSuffix},
|
|
{"keepModels", SpecFile.Config.KeepModels},
|
|
};
|
|
|
|
MakeTemplate(inputPath, outputPath, data);
|
|
|
|
}
|
|
} |