From cf7d46683012b6f5b32f393a7708618d1cebd3db Mon Sep 17 00:00:00 2001 From: Brian Adams Date: Sun, 14 Apr 2024 09:36:00 -0400 Subject: [PATCH] Add support for custom extensions like @section directive processing by making ProjectEngineBuilderAction public and invoking it upon compilation. --- RazorEngineCore.Tests/TestCompileAndRun.cs | 21 +++++++++++++++++++ RazorEngineCore/RazorEngine.cs | 1 + .../RazorEngineCompilationOptions.cs | 2 ++ 3 files changed, 24 insertions(+) diff --git a/RazorEngineCore.Tests/TestCompileAndRun.cs b/RazorEngineCore.Tests/TestCompileAndRun.cs index cab85a3..f269369 100644 --- a/RazorEngineCore.Tests/TestCompileAndRun.cs +++ b/RazorEngineCore.Tests/TestCompileAndRun.cs @@ -908,6 +908,27 @@ public void TestCompileAndRun_IncludeDebuggingForTypedAnonymous_EnabledDebugging Assert.AreEqual("

Hello Alex

", actual); } + [TestMethod] + public void TestCompileAndRun_ProjectEngineBuilderAction_IsInvoked() + { + var builderActionIsInvoked = false; + RazorEngine razorEngine = new RazorEngine(); + IRazorEngineCompiledTemplate template = razorEngine.Compile("

Hello @Model.Name

", builder => + { + builder.IncludeDebuggingInfo(); + builder.Options.ProjectEngineBuilderAction = (x) => builderActionIsInvoked = true; + }); + + template.EnableDebugging(); + + string actual = template.Run(new + { + Name = "Alex" + }); + + Assert.IsTrue(builderActionIsInvoked); + } + [TestMethod] public void TestCompileAndRun_Typed_EnabledDebuggingThrowsException() { diff --git a/RazorEngineCore/RazorEngine.cs b/RazorEngineCore/RazorEngine.cs index 89838d2..0bfe950 100644 --- a/RazorEngineCore/RazorEngine.cs +++ b/RazorEngineCore/RazorEngine.cs @@ -67,6 +67,7 @@ protected virtual RazorEngineCompiledTemplateMeta CreateAndCompileToStream(strin (builder) => { builder.SetNamespace(options.TemplateNamespace); + options.ProjectEngineBuilderAction?.Invoke(builder); }); RazorSourceDocument document = RazorSourceDocument.Create(templateSource, fileName); diff --git a/RazorEngineCore/RazorEngineCompilationOptions.cs b/RazorEngineCore/RazorEngineCompilationOptions.cs index fd19928..4d2282f 100644 --- a/RazorEngineCore/RazorEngineCompilationOptions.cs +++ b/RazorEngineCore/RazorEngineCompilationOptions.cs @@ -3,6 +3,7 @@ using System.Runtime.InteropServices; using Microsoft.CodeAnalysis; using System; +using Microsoft.AspNetCore.Razor.Language; namespace RazorEngineCore { @@ -22,6 +23,7 @@ public class RazorEngineCompilationOptions "System.Collections", "System.Collections.Generic" }; + public Action ProjectEngineBuilderAction { get; set; } public RazorEngineCompilationOptions() {