Import from internal git
This commit is contained in:
91
Generator/Infrastructure/CommandExecutor.cs
Normal file
91
Generator/Infrastructure/CommandExecutor.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
using System.Diagnostics;
|
||||
using Core.Events;
|
||||
using Core.Exceptions;
|
||||
|
||||
namespace Generator.Infrastructure;
|
||||
|
||||
public class CommandExecutor
|
||||
{
|
||||
|
||||
private readonly Queue<string> _commands;
|
||||
private string _invite;
|
||||
private DisplayEmitter _emitter;
|
||||
|
||||
public CommandExecutor(string invite, DisplayEmitter emitter)
|
||||
{
|
||||
_commands = new Queue<string>();
|
||||
_invite = invite;
|
||||
_emitter = emitter;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs a single asynchronous command
|
||||
/// </summary>
|
||||
/// <param name="command">Command to run</param>
|
||||
/// <exception cref="CommandExecutionException">When the command hasn't been terminated successfully</exception>
|
||||
public async Task RunAsync(string command)
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
using var process = new Process();
|
||||
process.StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = _invite,
|
||||
Arguments = $"{command}",
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
WindowStyle = ProcessWindowStyle.Normal
|
||||
};
|
||||
process.Start();
|
||||
ReadOutput(process);
|
||||
ReadError(process);
|
||||
process.WaitForExit();
|
||||
|
||||
if(process.ExitCode == 1)
|
||||
throw new CommandExecutionException($"The following task has failed : \n '{command}'");
|
||||
});
|
||||
}
|
||||
|
||||
private void ReadError(Process process)
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
using var output = process.StandardError;
|
||||
while (!output.EndOfStream)
|
||||
{
|
||||
var line = await output.ReadLineAsync();
|
||||
_emitter.Warn(this, line);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void ReadOutput(Process process)
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
using var output = process.StandardOutput;
|
||||
while (!output.EndOfStream)
|
||||
{
|
||||
var line = await output.ReadLineAsync();
|
||||
_emitter.Say(this, line);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers a command to execute later
|
||||
/// </summary>
|
||||
/// <param name="command"></param>
|
||||
public void Register(string command) => _commands.Enqueue(command);
|
||||
|
||||
/// <summary>
|
||||
/// Runs all previously registered command
|
||||
/// </summary>
|
||||
public async Task RunRegistered()
|
||||
{
|
||||
while (_commands.TryDequeue(out var c))
|
||||
{
|
||||
await RunAsync(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
60
Generator/Infrastructure/Export/Exporter.cs
Normal file
60
Generator/Infrastructure/Export/Exporter.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using Core;
|
||||
using Core.Actions;
|
||||
using Core.Helpers;
|
||||
using Core.Templates;
|
||||
using Core.Yaml;
|
||||
using Generator.Infrastructure.TemplateFiller;
|
||||
|
||||
namespace Generator.Infrastructure.Export;
|
||||
|
||||
/// <summary>
|
||||
/// Provides multiple methods to specification references
|
||||
/// </summary>
|
||||
public class Exporter
|
||||
{
|
||||
private readonly ITemplateFactory _templateFactory;
|
||||
|
||||
public Exporter(ITemplateFactory factory)
|
||||
{
|
||||
_templateFactory = factory;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Export file references as plant uml diagram
|
||||
/// </summary>
|
||||
/// <param name="export">plantuml data needed for exportation</param>
|
||||
/// <param name="file">File to export</param>
|
||||
public void PlantUml(PlantUmlExport export, ISpecFile file)
|
||||
{
|
||||
var yaml = TypeHelper.SafeCast<ISpecFile, OpenApiYaml>(file);
|
||||
var input = export.LocalRoot.ConcatenateWith(export.Template);
|
||||
var output = export.LocalRoot.ConcatenateWith(export.Output);
|
||||
|
||||
var references = new List<(string Package, string Reference)>();
|
||||
var schemas = yaml.ReferencedSchemas.ToList();
|
||||
|
||||
for (var i = -1; i < schemas.Count; i++)
|
||||
{
|
||||
var baseFile = i == -1 ? yaml : schemas[i];
|
||||
if(!baseFile.Location.IsInSameFolder(yaml.Location)) continue;
|
||||
|
||||
foreach (var reference in baseFile.ReferencedSchemas)
|
||||
{
|
||||
references.Add((baseFile.NugetPackage, reference.NugetPackage));
|
||||
}
|
||||
}
|
||||
|
||||
var data = new Dictionary<string, object>()
|
||||
{
|
||||
{"specName", file.Folder},
|
||||
{"packages", references.Select(t => t.Package).ToHashSet()},
|
||||
{"references", references}
|
||||
};
|
||||
|
||||
var template = _templateFactory.GetTemplate(input.ToString(), data);
|
||||
var f = new MustacheFiller(template);
|
||||
f.Fill();
|
||||
f.Write(output.ToString());
|
||||
}
|
||||
|
||||
}
|
||||
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();*/
|
||||
}
|
||||
}
|
||||
90
Generator/Infrastructure/Publication/Publisher.cs
Normal file
90
Generator/Infrastructure/Publication/Publisher.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using System.Text;
|
||||
using Core;
|
||||
using Core.Actions;
|
||||
using Core.Events;
|
||||
|
||||
namespace Generator.Infrastructure.Publication;
|
||||
|
||||
public class Publisher
|
||||
{
|
||||
private readonly CommandExecutor _executor;
|
||||
|
||||
public Publisher(string invite, DisplayEmitter emitter)
|
||||
{
|
||||
_executor = new CommandExecutor(invite, emitter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Published nuget package
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
public async Task PublishNugget(DotnetPublish data)
|
||||
{
|
||||
|
||||
var packageFolder = new Location([data.DockerRoot, data.PackageFolder]);
|
||||
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder
|
||||
.Append($"docker run --rm -it -v {data.LocalRoot}:{data.DockerRoot} {data.Image} /bin/sh -c '")
|
||||
.Append($"cd {packageFolder} && ")
|
||||
.Append($"dotnet nuget push out/{data.PackageFile}.{data.PackageVersion}.nupkg -k {data.AuthorizationToken} -s {data.Registry}'");
|
||||
|
||||
await _executor.RunAsync(stringBuilder.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Publishes npm packages
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
public async Task PublishNpm(JavascriptPublish data)
|
||||
{
|
||||
var specFile = new Location([data.DockerRoot, data.SpecFile]).ToString().Replace("/api/", "");
|
||||
var frontFolder = new Location([data.DockerRoot, data.FrontFolder]);
|
||||
var creds = data.DockerRoot.ConcatenateWith(".npmrc");
|
||||
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append($"docker run --rm -it -v {data.LocalRoot}:{data.DockerRoot} {data.Image} /bin/sh -c '");
|
||||
stringBuilder.Append("cd front && ");
|
||||
stringBuilder.Append($"npm run prepare-workspace -- --apiName={data.PackageName} --apiVersion={data.Version} ");
|
||||
stringBuilder.Append($"--apiFile={specFile} --registry={data.Registry} && ");
|
||||
stringBuilder.Append("npm run package && ");
|
||||
stringBuilder.Append($"cp {creds} ./ && ");
|
||||
stringBuilder.Append($"mkdir -p {frontFolder} && ");
|
||||
stringBuilder.Append($"cp -r dist/* {frontFolder} && ");
|
||||
stringBuilder.Append("npm run publish'");
|
||||
|
||||
await _executor.RunAsync(stringBuilder.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Publishes maven package
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
public async Task PublishMaven(JavaPublish data)
|
||||
{
|
||||
var templateFolder = new Location([data.DockerRoot, data.TemplateFolder]);
|
||||
var outputFolder = new Location([data.DockerRoot, data.OutputFolder]);
|
||||
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append($"docker run --rm -it -v {data.LocalRoot}:{data.DockerRoot} ");
|
||||
stringBuilder.Append($"-v {data.LocalRoot}maven-generated:/root/.m2/repository {data.Image} /bin/sh -c '");
|
||||
stringBuilder.Append($"cp {templateFolder}/settings.xml /root/.m2 && ");
|
||||
stringBuilder.Append($"cd {outputFolder} && ");
|
||||
stringBuilder.Append("mvn -e deploy:deploy-file ");
|
||||
stringBuilder.Append($"-D file=target/{data.Artifact}-{data.Version}.jar ");
|
||||
stringBuilder.Append($"-D groupId={data.Group} ");
|
||||
stringBuilder.Append($"-D artifactId={data.Artifact} ");
|
||||
stringBuilder.Append($"-D version={data.Version} ");
|
||||
stringBuilder.Append("-D packaging=jar ");
|
||||
stringBuilder.Append("-D generatePom=true ");
|
||||
stringBuilder.Append("-D repositoryId=fcsd ");
|
||||
stringBuilder.Append("-D skipTests ");
|
||||
stringBuilder.Append($"-D url={data.Registry} ");
|
||||
stringBuilder.Append("-D repo.id=fcsd ");
|
||||
stringBuilder.Append($"-D repo.username={data.Username} ");
|
||||
stringBuilder.Append($"-D repo.password={data.Password}'");
|
||||
|
||||
await _executor.RunAsync(stringBuilder.ToString());
|
||||
}
|
||||
|
||||
}
|
||||
20
Generator/Infrastructure/TemplateFiller/AbstractFiller.cs
Normal file
20
Generator/Infrastructure/TemplateFiller/AbstractFiller.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Core.Templates;
|
||||
|
||||
namespace Generator.Infrastructure.TemplateFiller;
|
||||
|
||||
public abstract class AbstractFiller
|
||||
{
|
||||
protected string Render;
|
||||
protected ITemplate Template;
|
||||
|
||||
/// <summary>
|
||||
/// Fills the text extracted from the template file
|
||||
/// </summary>
|
||||
public abstract void Fill();
|
||||
|
||||
/// <summary>
|
||||
/// Output the filled text in a file
|
||||
/// </summary>
|
||||
/// <param name="outputPath"></param>
|
||||
public abstract void Write(string outputPath);
|
||||
}
|
||||
35
Generator/Infrastructure/TemplateFiller/MustacheFiller.cs
Normal file
35
Generator/Infrastructure/TemplateFiller/MustacheFiller.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using Core.Exceptions;
|
||||
using Core.Templates;
|
||||
using Mustache;
|
||||
|
||||
namespace Generator.Infrastructure.TemplateFiller;
|
||||
|
||||
public class MustacheFiller : AbstractFiller
|
||||
{
|
||||
public MustacheFiller(ITemplate template)
|
||||
{
|
||||
Template = template;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Fill()
|
||||
{
|
||||
var compiler = new FormatCompiler
|
||||
{
|
||||
RemoveNewLines = false
|
||||
};
|
||||
var generator = compiler.Compile(Template?.GetText());
|
||||
|
||||
Render = generator.Render(Template?.GetData());
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Write(string outputPath)
|
||||
{
|
||||
if (Render == null) throw new FillerException(
|
||||
"Filler Exception : No text can be written. Have you forgot to call the Fill method ?");
|
||||
|
||||
using StreamWriter file = new StreamWriter(outputPath);
|
||||
file.Write(Render);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user