Skip to content

Commit f2da550

Browse files
shehrozeeeTheAtomicOption
authored andcommitted
Updated Builder intreface with GeneratePdbSteram
function
1 parent 5f832ed commit f2da550

File tree

7 files changed

+44
-29
lines changed

7 files changed

+44
-29
lines changed

Pack.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
dotnet build -c Release
22
dotnet test
33
dotnet pack -c Release -o artifacts RazorEngineCore\RazorEngineCore.csproj -p:symbolPackageFormat=snupkg --include-symbols
4-
dotnet nuget push artifacts\RazorEngineCore.2022.8.1.nupkg --source https://www.nuget.org/api/v2/package -k KEY
4+
dotnet nuget push artifacts\RazorEngineCore.2022.8.1.nupkg --source https://www.nuget.org/api/v2/package -k KEY

README.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -209,22 +209,20 @@ string result = compiledTemplate.Run(new { name = "Hello" });
209209
```
210210

211211
#### Debugging templates
212-
Add the following line in your template source and your debugger(vs code/vs studio) break on it.
213-
```cs
214-
System.Diagnostics.Debugger.Break();
215-
```
216-
Use this overload for the Compile function and pass true for the addPdb argument
212+
In the builder options, set GeneratePdbStream to true, and set the TemplateFilename.
217213
```cs
218214
razorEngine.Compile(templateSource, builder =>
219215
{
220-
builder.AddAssemblyReferenceByName("System.Security"); // by name
221-
builder.AddAssemblyReference(typeof(System.IO.File)); // by type
222-
builder.AddAssemblyReference(Assembly.Load("source")); // by reference
223-
},
224-
true); //This 'true' will enable debugging.
216+
builder.Options.GeneratePdbStream = true;
217+
builder.Options.TemplateFilename = "TemplateFilename.cshtml"
218+
});
225219
```
226-
Your debbuger will ask you to provide the path to the source file, by defult it is set to be generated in %temp% (point your there when asked for the file).
220+
Your debugger will popup a window asking you to find the source file, after which you can step through as normal.
227221

222+
To set a breakpoint add this line in a code block in the template.
223+
```cs
224+
System.Diagnostics.Debugger.Break();
225+
```
228226

229227
#### Credits
230228
This package is inspired by [Simon Mourier SO post](https://stackoverflow.com/a/47756437/267736)

RazorEngineCore/IRazorEngineCompilationOptionsBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ public interface IRazorEngineCompilationOptionsBuilder
1414
void AddMetadataReference(MetadataReference reference);
1515
void AddUsing(string namespaceName);
1616
void Inherits(Type type);
17+
void GeneratePdbSteram(bool flag);
1718
}
1819
}

RazorEngineCore/RazorEngine.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,37 @@ namespace RazorEngineCore
1414
{
1515
public class RazorEngine : IRazorEngine
1616
{
17-
public IRazorEngineCompiledTemplate<T> Compile<T>(string content, Action<IRazorEngineCompilationOptionsBuilder> builderAction = null) where T : IRazorEngineTemplate
17+
public IRazorEngineCompiledTemplate<T> Compile<T>(string content, Action<IRazorEngineCompilationOptionsBuilder> builderAction = null, bool addPdb = false) where T : IRazorEngineTemplate
1818
{
1919
IRazorEngineCompilationOptionsBuilder compilationOptionsBuilder = new RazorEngineCompilationOptionsBuilder();
2020
compilationOptionsBuilder.AddAssemblyReference(typeof(T).Assembly);
2121
compilationOptionsBuilder.Inherits(typeof(T));
2222

2323
builderAction?.Invoke(compilationOptionsBuilder);
24-
25-
CompiledStreams streams = this.CreateAndCompileToStream(content, compilationOptionsBuilder.Options);
26-
return new RazorEngineCompiledTemplate<T>(streams.assembly, compilationOptionsBuilder.Options.TemplateNamespace);
24+
if (compilationOptionsBuilder.Options.GeneratePdbStream)
25+
{
26+
CompiledStreams streams = this.CreateAndCompileToStream(content, compilationOptionsBuilder.Options);
27+
return new RazorEngineCompiledTemplate<T>(streams.assembly, compilationOptionsBuilder.Options.TemplateNamespace, streams.pdb);
28+
}
29+
else
30+
{
31+
CompiledStreams streams = this.CreateAndCompileToStream(content, compilationOptionsBuilder.Options);
32+
return new RazorEngineCompiledTemplate<T>(streams.assembly, compilationOptionsBuilder.Options.TemplateNamespace);
33+
}
2734
}
2835

2936
public Task<IRazorEngineCompiledTemplate<T>> CompileAsync<T>(string content, Action<IRazorEngineCompilationOptionsBuilder> builderAction = null) where T : IRazorEngineTemplate
3037
{
3138
return Task.Factory.StartNew(() => this.Compile<T>(content: content, builderAction: builderAction));
3239
}
33-
public IRazorEngineCompiledTemplate Compile(string content, Action<IRazorEngineCompilationOptionsBuilder> builderAction = null)
34-
{
35-
return Compile(content, builderAction, false);
36-
}
40+
3741
public IRazorEngineCompiledTemplate Compile(string content, Action<IRazorEngineCompilationOptionsBuilder> builderAction = null, bool addPdb = false)
3842
{
3943
IRazorEngineCompilationOptionsBuilder compilationOptionsBuilder = new RazorEngineCompilationOptionsBuilder();
4044
compilationOptionsBuilder.Inherits(typeof(RazorEngineTemplateBase));
4145

4246
builderAction?.Invoke(compilationOptionsBuilder);
43-
if (compilationOptionsBuilder.Options.GeneratePdbSteram)
47+
if (compilationOptionsBuilder.Options.GeneratePdbStream)
4448
{
4549
CompiledStreams streams = this.CreateAndCompileToStream(content, compilationOptionsBuilder.Options);
4650
return new RazorEngineCompiledTemplate(streams.assembly, compilationOptionsBuilder.Options.TemplateNamespace, streams.pdb);
@@ -62,12 +66,12 @@ protected virtual CompiledStreams CreateAndCompileToStream(string templateSource
6266
{
6367
templateSource = this.WriteDirectives(templateSource, options);
6468
string projectPath = @".";
65-
string fileName = Path.GetRandomFileName()+".cshtml";
66-
if (options.GeneratePdbSteram)
69+
string pdbfileName = Path.GetRandomFileName() + ".cshtml";
70+
if (options.GeneratePdbStream)
6771
{
6872
projectPath = Path.GetTempPath();
6973
Directory.CreateDirectory(projectPath);
70-
File.WriteAllText(Path.Combine(projectPath, fileName), templateSource);
74+
File.WriteAllText(Path.Combine(projectPath, pdbfileName), templateSource);
7175
}
7276

7377
RazorProjectEngine engine = RazorProjectEngine.Create(
@@ -119,7 +123,7 @@ protected virtual CompiledStreams CreateAndCompileToStream(string templateSource
119123
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
120124
CompiledStreams streams = new CompiledStreams();
121125
EmitResult emitResult;
122-
if (options.GeneratePdbSteram)
126+
if (options.GeneratePdbStream)
123127
emitResult = compilation.Emit(streams.assembly, streams.pdb);
124128
else
125129
emitResult = compilation.Emit(streams.assembly);
@@ -136,7 +140,7 @@ protected virtual CompiledStreams CreateAndCompileToStream(string templateSource
136140
}
137141

138142
streams.assembly.Position = 0;
139-
if(options.GeneratePdbSteram)
143+
if(options.GeneratePdbStream)
140144
streams.pdb.Position = 0;
141145
return streams;
142146
}
@@ -156,7 +160,7 @@ protected virtual string WriteDirectives(string content, RazorEngineCompilationO
156160
return stringBuilder.ToString();
157161
}
158162

159-
private class CompiledStreams
163+
protected class CompiledStreams
160164
{
161165
public CompiledStreams()
162166
{

RazorEngineCore/RazorEngineCompilationOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class RazorEngineCompilationOptions
1515
public string TemplateFilename { get; set; } = "";
1616
public string Inherits { get; set; } = "RazorEngineCore.RazorEngineTemplateBase";
1717
///Set to true to generate PDB symbols information along with the assembly for debugging support
18-
public bool GeneratePdbSteram {get;set;} = false;
18+
public bool GeneratePdbStream {get;set;} = false;
1919
public HashSet<string> DefaultUsings { get; set; } = new HashSet<string>()
2020
{
2121
"System.Linq",

RazorEngineCore/RazorEngineCompilationOptionsBuilder.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,10 @@ private string RenderDeclaringType(Type type)
9292

9393
return parent + "." + type.Name;
9494
}
95+
96+
public void GeneratePdbSteram(bool flag)
97+
{
98+
this.Options.GeneratePdbStream = flag;
99+
}
95100
}
96-
}
101+
}

RazorEngineCore/RazorEngineCompiledTemplateT.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ internal RazorEngineCompiledTemplate(MemoryStream assemblyByteCode, string templ
1616

1717
Assembly assembly = Assembly.Load(assemblyByteCode.ToArray());
1818
this.templateType = assembly.GetType($"{templateNamespace}.Template");
19+
}
20+
internal RazorEngineCompiledTemplate(MemoryStream assemblyByteCode, string templateNamespace, MemoryStream pdbByteCode)
21+
{
22+
this.assemblyByteCode = assemblyByteCode;
23+
24+
Assembly assembly = Assembly.Load(assemblyByteCode.ToArray(), pdbByteCode.ToArray());
25+
this.templateType = assembly.GetType(templateNamespace + ".Template");
1926
}
2027

2128
public static IRazorEngineCompiledTemplate<T> LoadFromFile(string fileName, string templateNamespace = "TemplateNamespace")

0 commit comments

Comments
 (0)