|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Didot.Core.TemplateEngines; |
| 7 | +using HandlebarsDotNet; |
| 8 | + |
| 9 | +namespace Didot.Core; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Builder class for creating template engine instances with a fluent API. |
| 13 | +/// </summary> |
| 14 | +public class TemplateEngineBuilder : ITemplateEngineConfigurabable |
| 15 | +{ |
| 16 | + private Type? _templateEngineType; |
| 17 | + private ITemplateEngineOptionsBuilder? _optionsBuilder; |
| 18 | + private TemplateConfigurationBuilder? _configBuilder; |
| 19 | + |
| 20 | + public ITemplateEngineConfigurabable UseDotLiquid() |
| 21 | + { |
| 22 | + _templateEngineType = typeof(DotLiquidWrapper); |
| 23 | + return this; |
| 24 | + } |
| 25 | + |
| 26 | + public ITemplateEngineConfigurabable UseFluid() |
| 27 | + { |
| 28 | + _templateEngineType = typeof(FluidWrapper); |
| 29 | + return this; |
| 30 | + } |
| 31 | + |
| 32 | + public ITemplateEngineConfigurabable UseHandlebars() |
| 33 | + { |
| 34 | + _templateEngineType = typeof(HandlebarsWrapper); |
| 35 | + return this; |
| 36 | + } |
| 37 | + |
| 38 | + public ITemplateEngineConfigurabable UseMorestachio() |
| 39 | + { |
| 40 | + _templateEngineType = typeof(MorestachioWrapper); |
| 41 | + return this; |
| 42 | + } |
| 43 | + |
| 44 | + public ITemplateEngineConfigurabable UseScriban() |
| 45 | + { |
| 46 | + _templateEngineType = typeof(ScribanWrapper); |
| 47 | + return this; |
| 48 | + } |
| 49 | + |
| 50 | + public ITemplateEngineConfigurabable UseSmartFormat() |
| 51 | + { |
| 52 | + _templateEngineType = typeof(SmartFormatWrapper); |
| 53 | + return this; |
| 54 | + } |
| 55 | + |
| 56 | + public ITemplateEngineConfigurabable UseStringTemplate() |
| 57 | + { |
| 58 | + _templateEngineType = typeof(StringTemplateWrapper); |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + public ITemplateEngineConfigurabable UseStringTemplate(Func<StringTemplateOptionsBuilder, StringTemplateOptionsBuilder> builder) |
| 63 | + { |
| 64 | + _templateEngineType = typeof(StringTemplateWrapper); |
| 65 | + _optionsBuilder = builder(new()); |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + ITemplateEngineBuildable ITemplateEngineConfigurabable.WithConfiguration(Func<TemplateConfigurationBuilder, TemplateConfigurationBuilder> builder) |
| 70 | + { |
| 71 | + _configBuilder = builder(new()); |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + ITemplateEngine ITemplateEngineBuildable.Build() |
| 76 | + { |
| 77 | + if (_templateEngineType is null) |
| 78 | + throw new InvalidOperationException("Template engine type is not set. Call one of the Use* methods first (e.g., UseHandlebars(), UseStringTemplate(), etc.)."); |
| 79 | + |
| 80 | + var parameters = new List<object>(); |
| 81 | + if (_optionsBuilder is not null) |
| 82 | + parameters.Add(_optionsBuilder.Build()); |
| 83 | + |
| 84 | + if (_configBuilder is not null) |
| 85 | + parameters.Add(_configBuilder.Build()); |
| 86 | + |
| 87 | + if (parameters.Count == 0) |
| 88 | + try |
| 89 | + { |
| 90 | + return (ITemplateEngine)Activator.CreateInstance(_templateEngineType)!; |
| 91 | + } |
| 92 | + catch (Exception ex) when (ex is not InvalidOperationException) |
| 93 | + { |
| 94 | + throw new InvalidOperationException($"Failed to create instance of {_templateEngineType.Name}.", ex); |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + var constructor = _templateEngineType.GetConstructor([.. parameters.Select(p => p.GetType())]); |
| 99 | + return constructor is null |
| 100 | + ? throw new InvalidOperationException($"No suitable constructor found for type {_templateEngineType.Name}.") |
| 101 | + : (ITemplateEngine)constructor.Invoke([.. parameters]); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +public interface ITemplateEngineBuildable |
| 107 | +{ |
| 108 | + ITemplateEngine Build(); |
| 109 | +} |
| 110 | + |
| 111 | +public interface ITemplateEngineConfigurabable : ITemplateEngineBuildable |
| 112 | +{ |
| 113 | + ITemplateEngineBuildable WithConfiguration(Func<TemplateConfigurationBuilder, TemplateConfigurationBuilder> builder); |
| 114 | +} |
0 commit comments