Skip to content

Commit 12fbe72

Browse files
Merge pull request #141 from ItWorksOnMyMachine/master
Make ProjectEngineBuilderAction public and invoke it when compiling template.
2 parents 6045d53 + cf7d466 commit 12fbe72

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

RazorEngineCore.Tests/TestCompileAndRun.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,27 @@ public void TestCompileAndRun_IncludeDebuggingForTypedAnonymous_EnabledDebugging
908908
Assert.AreEqual("<h1>Hello Alex</h1>", actual);
909909
}
910910

911+
[TestMethod]
912+
public void TestCompileAndRun_ProjectEngineBuilderAction_IsInvoked()
913+
{
914+
var builderActionIsInvoked = false;
915+
RazorEngine razorEngine = new RazorEngine();
916+
IRazorEngineCompiledTemplate template = razorEngine.Compile("<h1>Hello @Model.Name</h1>", builder =>
917+
{
918+
builder.IncludeDebuggingInfo();
919+
builder.Options.ProjectEngineBuilderAction = (x) => builderActionIsInvoked = true;
920+
});
921+
922+
template.EnableDebugging();
923+
924+
string actual = template.Run(new
925+
{
926+
Name = "Alex"
927+
});
928+
929+
Assert.IsTrue(builderActionIsInvoked);
930+
}
931+
911932
[TestMethod]
912933
public void TestCompileAndRun_Typed_EnabledDebuggingThrowsException()
913934
{

RazorEngineCore/RazorEngine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ protected virtual RazorEngineCompiledTemplateMeta CreateAndCompileToStream(strin
6767
(builder) =>
6868
{
6969
builder.SetNamespace(options.TemplateNamespace);
70+
options.ProjectEngineBuilderAction?.Invoke(builder);
7071
});
7172

7273
RazorSourceDocument document = RazorSourceDocument.Create(templateSource, fileName);

RazorEngineCore/RazorEngineCompilationOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Runtime.InteropServices;
44
using Microsoft.CodeAnalysis;
55
using System;
6+
using Microsoft.AspNetCore.Razor.Language;
67

78
namespace RazorEngineCore
89
{
@@ -22,6 +23,7 @@ public class RazorEngineCompilationOptions
2223
"System.Collections",
2324
"System.Collections.Generic"
2425
};
26+
public Action<RazorProjectEngineBuilder> ProjectEngineBuilderAction { get; set; }
2527

2628
public RazorEngineCompilationOptions()
2729
{

0 commit comments

Comments
 (0)