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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user