using Core;
using Core.Events;
using Core.Helpers;
using Core.Templates;
using Generator.Infrastructure.TemplateFiller;
using Microsoft.OpenApi.Exceptions;
namespace Generator.Infrastructure.OpenApi.Builders;
///
/// Provides base behavior for generation purpose
///
/// Derived type from ISpecFile
public abstract class AbstractBuilder 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;
}
///
/// Registers file used for generation
///
///
public void Load(TFile specFile) => SpecFile = TypeHelper.SafeCast(specFile);
///
/// Launches generation
///
public async Task ExecuteAllAsync() => await Executor.RunRegistered();
///
/// Registers generation process
///
public abstract void Generate();
///
/// Registers build process
///
public abstract void Build();
///
/// Check if the spec file has been loaded
///
///
protected void CheckForNullSpec()
{
if (SpecFile == null) throw new OpenApiException("Spec file hasn't been loaded yet.");
}
protected void MakeTemplate(string inputPath, string outputPath, IDictionary data)
{
var template = _templateFactory.GetTemplate(inputPath, data);
var f = new MustacheFiller(template);
f.Fill();
f.Write(outputPath);
}
}