Import from internal git
This commit is contained in:
64
Generator/Infrastructure/OpenApi/Builders/AbstractBuilder.cs
Normal file
64
Generator/Infrastructure/OpenApi/Builders/AbstractBuilder.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using Core;
|
||||
using Core.Events;
|
||||
using Core.Helpers;
|
||||
using Core.Templates;
|
||||
using Generator.Infrastructure.TemplateFiller;
|
||||
using Microsoft.OpenApi.Exceptions;
|
||||
|
||||
namespace Generator.Infrastructure.OpenApi.Builders;
|
||||
|
||||
/// <summary>
|
||||
/// Provides base behavior for generation purpose
|
||||
/// </summary>
|
||||
/// <typeparam name="TFile">Derived type from ISpecFile</typeparam>
|
||||
public abstract class AbstractBuilder<TFile> where TFile : ISpecFile
|
||||
{
|
||||
private readonly ITemplateFactory _templateFactory;
|
||||
protected readonly CommandExecutor Executor;
|
||||
protected TFile? SpecFile;
|
||||
|
||||
public AbstractBuilder(string invite, DisplayEmitter emitter)
|
||||
{
|
||||
Executor = new CommandExecutor(invite, emitter);
|
||||
_templateFactory = new MustacheTemplateFactory();
|
||||
SpecFile = default;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers file used for generation
|
||||
/// </summary>
|
||||
/// <param name="specFile"></param>
|
||||
public void Load(TFile specFile) => SpecFile = TypeHelper.SafeCast<ISpecFile, TFile>(specFile);
|
||||
|
||||
/// <summary>
|
||||
/// Launches generation
|
||||
/// </summary>
|
||||
public async Task ExecuteAllAsync() => await Executor.RunRegistered();
|
||||
|
||||
/// <summary>
|
||||
/// Registers generation process
|
||||
/// </summary>
|
||||
public abstract void Generate();
|
||||
|
||||
/// <summary>
|
||||
/// Registers build process
|
||||
/// </summary>
|
||||
public abstract void Build();
|
||||
|
||||
/// <summary>
|
||||
/// Check if the spec file has been loaded
|
||||
/// </summary>
|
||||
/// <exception cref="OpenApiException"></exception>
|
||||
protected void CheckForNullSpec()
|
||||
{
|
||||
if (SpecFile == null) throw new OpenApiException("Spec file hasn't been loaded yet.");
|
||||
}
|
||||
|
||||
protected void MakeTemplate(string inputPath, string outputPath, IDictionary<string, object> data)
|
||||
{
|
||||
var template = _templateFactory.GetTemplate(inputPath, data);
|
||||
var f = new MustacheFiller(template);
|
||||
f.Fill();
|
||||
f.Write(outputPath);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
using System.Text;
|
||||
using Core;
|
||||
using Core.Events;
|
||||
using Core.Helpers;
|
||||
using Core.Settings;
|
||||
using Core.SpecConfig;
|
||||
using Core.Yaml;
|
||||
|
||||
namespace Generator.Infrastructure.OpenApi.Builders;
|
||||
|
||||
public class OpenApiDotnetServerBuilder : AbstractBuilder<OpenApiYaml>
|
||||
{
|
||||
private readonly DotnetConfig _config;
|
||||
|
||||
public OpenApiDotnetServerBuilder(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.ServerConfigTemplate]);
|
||||
var outputPath = new Location([_config.LocalRoot, _config.ConfigFilePath()]);
|
||||
|
||||
PathHelper.CreateFileIfNotExists(outputPath.ToString());
|
||||
|
||||
var templateData = 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()},
|
||||
{"modelSuffix", SpecFile.Config.ModelSuffix},
|
||||
{"aspnetCoreVersion", "3.1"},
|
||||
{"packageName", SpecFile.Config.NugetPackage},
|
||||
{"packageVersion", SpecFile.Info!["version"]},
|
||||
{"refs", SpecFile.ReferencedSchemas},
|
||||
{"modelNameSpace", SpecFile.ReferencedSchemas},
|
||||
};
|
||||
|
||||
MakeTemplate(inputPath.ToString(), outputPath.ToString(), templateData);
|
||||
}
|
||||
|
||||
/// <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.ServerIgnoreTemplate]).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},
|
||||
{"ignoredModels", SpecFile.IgnoredModels},
|
||||
{"isCommon", SpecFile.SpecType == SpecType.Model}
|
||||
};
|
||||
|
||||
MakeTemplate(inputPath, outputPath, data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
using System.Text;
|
||||
using Core;
|
||||
using Core.Events;
|
||||
using Core.Helpers;
|
||||
using Core.Settings;
|
||||
using Core.Yaml;
|
||||
|
||||
namespace Generator.Infrastructure.OpenApi.Builders;
|
||||
|
||||
public class OpenApiJavaBuilder : AbstractBuilder<OpenApiYaml>
|
||||
{
|
||||
private readonly JavaConfig _config;
|
||||
|
||||
public OpenApiJavaBuilder(JavaConfig config, DisplayEmitter emitter)
|
||||
: base(config.Invite, emitter)
|
||||
{
|
||||
_config = config;
|
||||
}
|
||||
|
||||
///<inheritdoc/>
|
||||
public override void Generate()
|
||||
{
|
||||
CheckForNullSpec();
|
||||
|
||||
GenerateOpenApi();
|
||||
|
||||
var configFile = new Location([_config.DockerRoot, _config.ConfigFilePath()]);
|
||||
var command = $"docker run --rm -it -v {_config.LocalRoot}:{_config.DockerRoot} {_config.GenerationImage} " +
|
||||
$"batch --clean {configFile}";
|
||||
|
||||
Executor.Register(command);
|
||||
}
|
||||
|
||||
///<inheritdoc/>
|
||||
public override void Build()
|
||||
{
|
||||
CheckForNullSpec();
|
||||
|
||||
var templateFolder = new Location([_config.DockerRoot, _config.TemplateFolder]);
|
||||
var outputFolder = new Location([_config.DockerRoot, _config.OutputFolder()]);
|
||||
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append($"docker run --rm -it -v {_config.LocalRoot}:{_config.DockerRoot} {_config.BuildImage} /bin/sh -c '");
|
||||
stringBuilder.Append($"cp {templateFolder}/settings.xml /root/.m2 && ");
|
||||
stringBuilder.Append($"cd {outputFolder} && ");
|
||||
stringBuilder.Append("mvn package'");
|
||||
|
||||
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.ServerConfigTemplate]);
|
||||
var outputPath = new Location([_config.LocalRoot, _config.ConfigFilePath()]);
|
||||
|
||||
PathHelper.CreateFileIfNotExists(outputPath.ToString());
|
||||
|
||||
var artifact = $"{SpecFile.Name}-api";
|
||||
|
||||
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()},
|
||||
{"groupId", SpecFile.Config.JavaGroup},
|
||||
{"artifactId", artifact},
|
||||
{"artifactVersion", SpecFile.Info["version"]},
|
||||
};
|
||||
|
||||
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()]);
|
||||
|
||||
PathHelper.CreateFileIfNotExists(output.ToString());
|
||||
|
||||
var command = $"docker run --rm -it -v {_config.LocalRoot}:{_config.DockerRoot} {_config.GenerationImage} " +
|
||||
$"generate -g openapi-yaml -i {specFile} -o {output}";
|
||||
Executor.Register(command);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System.Text;
|
||||
using Core;
|
||||
using Core.Events;
|
||||
using Core.Settings;
|
||||
using Core.Yaml;
|
||||
|
||||
namespace Generator.Infrastructure.OpenApi.Builders;
|
||||
|
||||
public class OpenApiJavascriptBuilder : AbstractBuilder<OpenApiYaml>
|
||||
{
|
||||
|
||||
private readonly JavascriptConfig _config;
|
||||
|
||||
public OpenApiJavascriptBuilder(JavascriptConfig config, DisplayEmitter emitter)
|
||||
: base(config.Invite, emitter)
|
||||
{
|
||||
_config = config;
|
||||
}
|
||||
|
||||
///<inheritdoc/>
|
||||
public override void Generate()
|
||||
{
|
||||
CheckForNullSpec();
|
||||
|
||||
GenerateOpenApi();
|
||||
|
||||
var outputFolder = new Location([_config.DockerRoot, _config.ClientFolder]);
|
||||
var specFile = new Location([_config.DockerRoot, _config.OpenApiSpecFile()]).ToString();
|
||||
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder
|
||||
.Append($"docker run --rm -it -v {_config.LocalRoot}:{_config.DockerRoot} {_config.BuildImage} /bin/sh -c '")
|
||||
.Append($"npm run prepare-workspace -- --apiName={_config.PackageName} --apiVersion={_config.OpenApiVersion} ")
|
||||
.Append($"--apiFile={specFile} --registry={_config.Registry} && ")
|
||||
.Append("npm run package && ")
|
||||
.Append($"mkdir -p {outputFolder} && ")
|
||||
.Append($"cp -r dist/* {outputFolder}'");
|
||||
|
||||
var command = stringBuilder.ToString();
|
||||
|
||||
Executor.Register(command);
|
||||
}
|
||||
|
||||
///<inheritdoc/>
|
||||
public override void Build()
|
||||
=> throw new InvalidOperationException("Why the heck do you want to compile javascript code ?");
|
||||
|
||||
/// <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);
|
||||
}
|
||||
|
||||
}
|
||||
76
Generator/Infrastructure/OpenApi/OpenApiDirector.cs
Normal file
76
Generator/Infrastructure/OpenApi/OpenApiDirector.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using Core.Events;
|
||||
using Core.Interfaces;
|
||||
using Core.Settings;
|
||||
using Core.Yaml;
|
||||
using Generator.Infrastructure.OpenApi.Builders;
|
||||
|
||||
namespace Generator.Infrastructure.OpenApi;
|
||||
|
||||
public class OpenApiDirector : IGeneratorDirector<OpenApiYaml>
|
||||
{
|
||||
private DisplayEmitter _emitter;
|
||||
|
||||
public OpenApiDirector(DisplayEmitter emitter)
|
||||
{
|
||||
_emitter = emitter;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Launches dotnet server generation
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
/// <param name="config"></param>
|
||||
public async Task DotnetServer(OpenApiYaml file, DotnetConfig config)
|
||||
{
|
||||
var dotnetServerBuilder = new OpenApiDotnetServerBuilder(config, _emitter);
|
||||
dotnetServerBuilder.Load(file);
|
||||
dotnetServerBuilder.CreateGeneratorConfig();
|
||||
dotnetServerBuilder.Generate();
|
||||
dotnetServerBuilder.Build();
|
||||
await dotnetServerBuilder.ExecuteAllAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Launches dotnet client generation
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
/// <param name="config"></param>
|
||||
public async Task DotnetClient(OpenApiYaml file, DotnetConfig config)
|
||||
{
|
||||
var dotnetClientBuilder = new OpenApiDotnetClientBuilder(config, _emitter);
|
||||
dotnetClientBuilder.Load(file);
|
||||
dotnetClientBuilder.CreateGeneratorConfig();
|
||||
dotnetClientBuilder.Generate();
|
||||
dotnetClientBuilder.Build();
|
||||
await dotnetClientBuilder.ExecuteAllAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Launches java generation
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
/// <param name="config"></param>
|
||||
public async Task Java(OpenApiYaml file, JavaConfig config)
|
||||
{
|
||||
var javaBuilder = new OpenApiJavaBuilder(config, _emitter);
|
||||
javaBuilder.Load(file);
|
||||
javaBuilder.CreateGeneratorConfig();
|
||||
javaBuilder.Generate();
|
||||
javaBuilder.Build();
|
||||
await javaBuilder.ExecuteAllAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Launches javascript generation
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
/// <param name="config"></param>
|
||||
public async Task Javascript(OpenApiYaml file, JavascriptConfig config)
|
||||
{
|
||||
_emitter.Warn(this, "Javascript generation is temporally disabled");
|
||||
/*var jsBuilder = new OpenApiJavascriptBuilder(config, _emitter);
|
||||
jsBuilder.Load(file);
|
||||
jsBuilder.Generate();
|
||||
await jsBuilder.ExecuteAllAsync();*/
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user