From ed7b42fcc2101df640a5b101630523f528c56e3c Mon Sep 17 00:00:00 2001 From: "Eric Sibly [chullybun]" Date: Mon, 20 Mar 2023 14:38:12 -0700 Subject: [PATCH] CodeGenerator fixed. (#16) --- CHANGELOG.md | 4 ++ src/OnRamp/CodeGenStatistics.cs | 7 +++ src/OnRamp/CodeGenerator.cs | 17 +++++++- src/OnRamp/Console/CodeGenConsole.cs | 50 +++++++++++----------- src/OnRamp/OnRamp.csproj | 2 +- src/OnRamp/Scripts/CodeGenScriptItem.cs | 6 +-- tests/OnRamp.Test/CodeGeneratorTest.cs | 2 +- tests/OnRamp.Test/HandlebarsHelpersTest.cs | 7 +++ 8 files changed, 63 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 648585e..c0426f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ Represents the **NuGet** versions. +## v1.0.7 +- *Fixed:* `CodeGenerator` now supports inheritance as intended; new `CreateAsync` added to enable. +- *Fixed:* The `CodeGeneratorArgsBase.ConfigFileName` per script `IRootConfig` instance included within `CodeGenStatistics` per script item execution to enable access from `CodeGenerator`. + ## v1.0.6 - *Enhancement:* Added new `set-value` function to allow a variable to be updated from the template when executed. - *Enhancement:* Added new `add-value` function to allow a variable to be added to from the template when executed. diff --git a/src/OnRamp/CodeGenStatistics.cs b/src/OnRamp/CodeGenStatistics.cs index 0907ba3..5754103 100644 --- a/src/OnRamp/CodeGenStatistics.cs +++ b/src/OnRamp/CodeGenStatistics.cs @@ -1,5 +1,7 @@ // Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/OnRamp +using OnRamp.Config; + namespace OnRamp { /// @@ -33,6 +35,11 @@ public class CodeGenStatistics /// A null indicates that the value has not been set. public long? ElapsedMilliseconds { get; set; } + /// + /// Gets or sets the instance. + /// + public IRootConfig? RootConfig { get; set; } + /// /// Adds other to this instance. /// diff --git a/src/OnRamp/CodeGenerator.cs b/src/OnRamp/CodeGenerator.cs index e843e0a..b1b3505 100644 --- a/src/OnRamp/CodeGenerator.cs +++ b/src/OnRamp/CodeGenerator.cs @@ -26,6 +26,16 @@ public class CodeGenerator public static async Task CreateAsync(ICodeGeneratorArgs args) => new CodeGenerator(args, await LoadScriptsAsync(args).ConfigureAwait(false)); + /// + /// Create a new instance of the class. + /// + /// The . + /// The . + /// The instance. + /// The constructor must be the same as the . + public static async Task CreateAsync(ICodeGeneratorArgs args) where TCodeGenerator : CodeGenerator + => (TCodeGenerator)Activator.CreateInstance(typeof(TCodeGenerator), new object[] { args, await LoadScriptsAsync(args).ConfigureAwait(false) }); + /// /// Load the Scripts. /// @@ -99,7 +109,10 @@ private static async Task LoadScriptStreamAsync(ICodeGeneratorArg /// /// Initializes a new instance of the class. /// - private CodeGenerator(ICodeGeneratorArgs args, CodeGenScript scripts) + /// The . + /// The . + /// The class should only be instantiated by or to ensure conrrectly set up for execution. + protected CodeGenerator(ICodeGeneratorArgs args, CodeGenScript scripts) { CodeGenArgs = args ?? throw new ArgumentNullException(nameof(args)); CodeGenArgs.OutputDirectory ??= new DirectoryInfo(Environment.CurrentDirectory); @@ -205,7 +218,7 @@ private async Task GenerateAsync(string configFileName, TextR rootConfig.MergeRuntimeParameters(script.RuntimeParameters); rootConfig.MergeRuntimeParameters(Scripts.RuntimeParameters); - var scriptStats = new CodeGenStatistics(); + var scriptStats = new CodeGenStatistics { RootConfig = rootConfig }; OnBeforeScript(script, scriptStats); script.GetGenerator().Generate(script, config, (oa) => OnCodeGenerated(oa, scriptStats)); diff --git a/src/OnRamp/Console/CodeGenConsole.cs b/src/OnRamp/Console/CodeGenConsole.cs index c0a6822..4c8ddbe 100644 --- a/src/OnRamp/Console/CodeGenConsole.cs +++ b/src/OnRamp/Console/CodeGenConsole.cs @@ -185,14 +185,14 @@ public async Task RunAsync(string[] args) } catch (CommandParsingException cpex) { - Args.Logger?.LogError(cpex.Message); - Args.Logger?.LogError(string.Empty); + Args.Logger?.LogError("{Content}", cpex.Message); + Args.Logger?.LogError("{Content}", string.Empty); return 1; } catch (CodeGenException cgex) { - Args.Logger?.LogError(cgex.Message); - Args.Logger?.LogError(string.Empty); + Args.Logger?.LogError("{Content}", cgex.Message); + Args.Logger?.LogError("{Content}", string.Empty); return 2; } } @@ -282,16 +282,16 @@ private async Task RunRunawayAsync() /* Method name inspired by: Slade - Ru { if (cgex.Message != null) { - Args.Logger?.LogError(cgex.Message); - Args.Logger?.LogError(string.Empty); + Args.Logger?.LogError("{Content}", cgex.Message); + Args.Logger?.LogError("{Content}", string.Empty); } return 2; } catch (CodeGenChangesFoundException cgcfex) { - Args.Logger?.LogError(cgcfex.Message); - Args.Logger?.LogError(string.Empty); + Args.Logger?.LogError("{Content}", cgcfex.Message); + Args.Logger?.LogError("{Content}", string.Empty); return 3; } } @@ -302,7 +302,7 @@ private async Task RunRunawayAsync() /* Method name inspired by: Slade - Ru protected virtual void OnWriteMasthead() { if (MastheadText != null) - Logger?.LogInformation(MastheadText); + Logger?.LogInformation("{Content}", MastheadText); } /// @@ -322,8 +322,8 @@ protected virtual async Task OnCodeGenerationAsync() /// Writes the . protected virtual void OnWriteHeader() { - Logger?.LogInformation(AppTitle); - Logger?.LogInformation(string.Empty); + Logger?.LogInformation("{Content}", AppTitle); + Logger?.LogInformation("{Content}", string.Empty); } /// @@ -341,26 +341,26 @@ public static void WriteStandardizedArgs(ICodeGeneratorArgs args) if (args == null || args.Logger == null) return; - args.Logger.LogInformation($"Config = {args.ConfigFileName}"); - args.Logger.LogInformation($"Script = {args.ScriptFileName}"); - args.Logger.LogInformation($"OutDir = {args.OutputDirectory?.FullName}"); - args.Logger.LogInformation($"ExpectNoChanges = {args.ExpectNoChanges}"); - args.Logger.LogInformation($"IsSimulation = {args.IsSimulation}"); + args.Logger.LogInformation("{Content}", $"Config = {args.ConfigFileName}"); + args.Logger.LogInformation("{Content}", $"Script = {args.ScriptFileName}"); + args.Logger.LogInformation("{Content}", $"OutDir = {args.OutputDirectory?.FullName}"); + args.Logger.LogInformation("{Content}", $"ExpectNoChanges = {args.ExpectNoChanges}"); + args.Logger.LogInformation("{Content}", $"IsSimulation = {args.IsSimulation}"); - args.Logger.LogInformation($"Parameters{(args.Parameters.Count == 0 ? " = none" : ":")}"); + args.Logger.LogInformation("{Content}", $"Parameters{(args.Parameters.Count == 0 ? " = none" : ":")}"); foreach (var p in args.Parameters) { - args.Logger.LogInformation($" {p.Key} = {p.Value}"); + args.Logger.LogInformation("{Content}", $" {p.Key} = {p.Value}"); } - args.Logger.LogInformation($"Assemblies{(args.Assemblies.Count == 0 ? " = none" : ":")}"); + args.Logger.LogInformation("{Content}", $"Assemblies{(args.Assemblies.Count == 0 ? " = none" : ":")}"); foreach (var a in args.Assemblies) { - args.Logger.LogInformation($" {a.FullName}"); + args.Logger.LogInformation("{Content}", $" {a.FullName}"); } - args.Logger.LogInformation(string.Empty); - args.Logger.LogInformation("Scripts:"); + args.Logger.LogInformation("{Content}", string.Empty); + args.Logger.LogInformation("{Content}", "Scripts:"); } /// @@ -369,9 +369,9 @@ public static void WriteStandardizedArgs(ICodeGeneratorArgs args) /// The information. protected virtual void OnWriteFooter(CodeGenStatistics stats) { - Logger?.LogInformation(string.Empty); - Logger?.LogInformation($"{AppName} Complete. {stats.ToSummaryString()}"); - Logger?.LogInformation(string.Empty); + Logger?.LogInformation("{Content}", string.Empty); + Logger?.LogInformation("{Content}", $"{AppName} Complete. {stats.ToSummaryString()}"); + Logger?.LogInformation("{Content}", string.Empty); } } } \ No newline at end of file diff --git a/src/OnRamp/OnRamp.csproj b/src/OnRamp/OnRamp.csproj index 72f9ec3..9a82d54 100644 --- a/src/OnRamp/OnRamp.csproj +++ b/src/OnRamp/OnRamp.csproj @@ -4,7 +4,7 @@ Exe netstandard2.1 OnRamp - 1.0.6 + 1.0.7 true Avanade Avanade diff --git a/src/OnRamp/Scripts/CodeGenScriptItem.cs b/src/OnRamp/Scripts/CodeGenScriptItem.cs index b9866bb..811482c 100644 --- a/src/OnRamp/Scripts/CodeGenScriptItem.cs +++ b/src/OnRamp/Scripts/CodeGenScriptItem.cs @@ -111,11 +111,11 @@ protected override Task PrepareAsync() catch (Exception ex) { throw new CodeGenException(this, nameof(Type), $"Type '{Type}' is invalid: {ex.Message}"); } // Make sure the template exists. - var r = StreamLocator.HasTemplateStream(Template!, Root!.CodeGenArgs!.Assemblies.ToArray(), StreamLocator.HandlebarsExtensions); - if (!r.Exists) + var (Exists, FileName) = StreamLocator.HasTemplateStream(Template!, Root!.CodeGenArgs!.Assemblies.ToArray(), StreamLocator.HandlebarsExtensions); + if (!Exists) throw new CodeGenException(this, nameof(Template), $"Template '{Template}' does not exist."); - Template = r.FileName; + Template = FileName; // Add special runtime parameters. RuntimeParameters.Add(nameof(IsGenOnce), IsGenOnce); diff --git a/tests/OnRamp.Test/CodeGeneratorTest.cs b/tests/OnRamp.Test/CodeGeneratorTest.cs index 3adee48..405873f 100644 --- a/tests/OnRamp.Test/CodeGeneratorTest.cs +++ b/tests/OnRamp.Test/CodeGeneratorTest.cs @@ -12,7 +12,7 @@ public class CodeGeneratorTest [Test] public void A100_Script_DoesNotExist() { - var ex = Assert.ThrowsAsync(() => CodeGenerator.CreateAsync(new CodeGeneratorArgs("DoesNotExist.yaml").AddAssembly(typeof(CodeGeneratorTest).Assembly))); + var ex = Assert.ThrowsAsync(() => CodeGenerator.CreateAsync(new CodeGeneratorArgs("DoesNotExist.yaml").AddAssembly(typeof(CodeGeneratorTest).Assembly))); Assert.AreEqual("Script 'DoesNotExist.yaml' does not exist.", ex.Message); } diff --git a/tests/OnRamp.Test/HandlebarsHelpersTest.cs b/tests/OnRamp.Test/HandlebarsHelpersTest.cs index ca67c38..a7f29c1 100644 --- a/tests/OnRamp.Test/HandlebarsHelpersTest.cs +++ b/tests/OnRamp.Test/HandlebarsHelpersTest.cs @@ -195,6 +195,13 @@ public void Add() Assert.AreEqual("12", g.Generate(new { Count = -2 })); } + [Test] + public void Add_Index() + { + var g = new HandlebarsCodeGenerator("{{#each .}}{{.}}{{add @index 1}}{{/each}}"); + Assert.AreEqual("a1b2", g.Generate(new System.Collections.Generic.List { "a", "b" })); + } + public class SetData { public int Count { get; set; } internal bool Check { get; set; } public decimal Sum { get; set; } } [Test]