/
RazorLightEngineBuilder.cs
369 lines (293 loc) · 10.1 KB
/
RazorLightEngineBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
using Microsoft.CodeAnalysis;
using RazorLight.Caching;
using RazorLight.Compilation;
using RazorLight.Generation;
using RazorLight.Razor;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reflection;
namespace RazorLight
{
public class RazorLightEngineBuilder
{
protected Assembly operatingAssembly;
protected HashSet<string> namespaces;
protected ConcurrentDictionary<string, string> dynamicTemplates;
protected HashSet<MetadataReference> metadataReferences;
protected HashSet<string> excludedAssemblies;
protected List<Action<ITemplatePage>> prerenderCallbacks;
protected RazorLightProject project;
protected ICachingProvider cachingProvider;
private bool? disableEncoding;
private bool? enableDebugMode;
private RazorLightOptions options;
/// <summary>
/// Configures RazorLight to use a project.
/// </summary>
/// <remarks>
/// Use this if implementing a custom <see cref="RazorLightProject"/>.
/// </remarks>
/// <param name="razorLightProject"></param>
/// <returns></returns>
public virtual RazorLightEngineBuilder UseProject(RazorLightProject razorLightProject)
{
project = razorLightProject ?? throw new ArgumentNullException(nameof(razorLightProject), $"Use {nameof(NoRazorProject)} instead of null. See also {nameof(UseNoProject)}.");
return this;
}
/// <summary>
/// Configures RazorLight to use a project whose persistent store is a "null device".
/// </summary>
public RazorLightEngineBuilder UseNoProject()
{
project = new NoRazorProject();
return this;
}
/// <summary>
/// Configures RazorLight to use a project whose persistent store is the file system.
/// </summary>
/// <param name="root"></param>
/// <returns></returns>
public RazorLightEngineBuilder UseFileSystemProject(string root)
{
project = new FileSystemRazorProject(root);
return this;
}
/// <summary>
/// Configures RazorLight to use a project whose persistent store is the file system.
/// </summary>
/// <param name="root">Directory path to the root folder containing your Razor markup files.</param>
/// <param name="extension">If you wish, you can use a different extension than .cshtml.</param>
/// <returns><see cref="RazorLightEngineBuilder"/></returns>
public RazorLightEngineBuilder UseFileSystemProject(string root, string extension)
{
project = new FileSystemRazorProject(root, extension);
return this;
}
/// <summary>
/// Configures RazorLight to use a project whose persistent store an assembly manifest resource stream.
/// </summary>
/// <param name="rootType">Any type in the root namespace (prefix) for your assembly manifest resource stream.</param>
/// <returns><see cref="EmbeddedRazorProject"/></returns>
public RazorLightEngineBuilder UseEmbeddedResourcesProject(Type rootType)
{
if (rootType == null) throw new ArgumentNullException(nameof(rootType));
project = new EmbeddedRazorProject(rootType);
return this;
}
/// <summary>
/// Configures RazorLight to use a project whose persistent store an assembly manifest resource stream.
/// </summary>
/// <param name="assembly">Assembly containing embedded resources</param>
/// <param name="rootNamespace">The root namespace (prefix) for your assembly manifest resource stream.</param>
/// <returns></returns>
public RazorLightEngineBuilder UseEmbeddedResourcesProject(Assembly assembly, string rootNamespace = null)
{
project = new EmbeddedRazorProject(assembly, rootNamespace);
return this;
}
public RazorLightEngineBuilder UseOptions(RazorLightOptions razorLightOptions)
{
options = razorLightOptions;
return this;
}
/// <summary>
/// Disables encoding of HTML entities in variables.
/// </summary>
/// <example>
/// The model contains a property with value ">hello<".
///
/// In the rendered template this will be:
///
/// <code>
/// >hello<
/// </code>
/// </example>
/// <returns>A <see cref="RazorLightEngineBuilder"/></returns>
public RazorLightEngineBuilder DisableEncoding()
{
if (disableEncoding.HasValue)
throw new RazorLightException($"{nameof(disableEncoding)} has already been set");
disableEncoding = true;
return this;
}
/// <summary>
/// Enables encoding of HTML entities in variables.
/// </summary>
/// <example>
/// The model contains a property with value ">hello<".
///
/// In the rendered template this will be:
///
/// <code>
/// &gt;hello&lt;
/// </code>
/// </example>
/// <returns>A <see cref="RazorLightEngineBuilder"/></returns>
public RazorLightEngineBuilder EnableEncoding()
{
if (disableEncoding.HasValue)
throw new RazorLightException($"{nameof(disableEncoding)} has already been set");
disableEncoding = false;
return this;
}
public virtual RazorLightEngineBuilder UseMemoryCachingProvider()
{
cachingProvider = new MemoryCachingProvider();
return this;
}
public virtual RazorLightEngineBuilder UseCachingProvider(ICachingProvider provider)
{
if (provider == null)
{
throw new ArgumentNullException(nameof(provider));
}
cachingProvider = provider;
return this;
}
public virtual RazorLightEngineBuilder AddDefaultNamespaces(params string[] namespaces)
{
if (namespaces == null)
{
throw new ArgumentNullException(nameof(namespaces));
}
this.namespaces = new HashSet<string>();
foreach (string @namespace in namespaces)
{
this.namespaces.Add(@namespace);
}
return this;
}
public virtual RazorLightEngineBuilder AddMetadataReferences(params MetadataReference[] metadata)
{
if (metadata == null)
{
throw new ArgumentNullException(nameof(metadata));
}
metadataReferences = new HashSet<MetadataReference>();
foreach (var reference in metadata)
{
metadataReferences.Add(reference);
}
return this;
}
public virtual RazorLightEngineBuilder ExcludeAssemblies(params string[] assemblyNames)
{
if (assemblyNames == null)
{
throw new ArgumentNullException(nameof(assemblyNames));
}
excludedAssemblies = new HashSet<string>();
foreach (var assemblyName in assemblyNames)
{
excludedAssemblies.Add(assemblyName);
}
return this;
}
public virtual RazorLightEngineBuilder AddPrerenderCallbacks(params Action<ITemplatePage>[] callbacks)
{
if (callbacks == null)
{
throw new ArgumentNullException(nameof(callbacks));
}
prerenderCallbacks = new List<Action<ITemplatePage>>();
prerenderCallbacks.AddRange(callbacks);
return this;
}
public virtual RazorLightEngineBuilder AddDynamicTemplates(IDictionary<string, string> dynamicTemplates)
{
if (dynamicTemplates == null)
{
throw new ArgumentNullException(nameof(dynamicTemplates));
}
this.dynamicTemplates = new ConcurrentDictionary<string, string>(dynamicTemplates);
return this;
}
public virtual RazorLightEngineBuilder SetOperatingAssembly(Assembly assembly)
{
if (assembly == null)
{
throw new ArgumentNullException(nameof(assembly));
}
operatingAssembly = assembly;
return this;
}
public virtual RazorLightEngineBuilder EnableDebugMode(bool enableDebugMode = true)
{
this.enableDebugMode = enableDebugMode;
return this;
}
public virtual RazorLightEngine Build()
{
options = options ?? new RazorLightOptions();
project = project ?? new NoRazorProject();
if (namespaces != null)
{
if(namespaces.Count > 0 && options.Namespaces.Count > 0)
ThrowIfHasBeenSetExplicitly(nameof(namespaces));
options.Namespaces = namespaces;
}
if (dynamicTemplates != null)
{
if(dynamicTemplates.Count > 0 && options.DynamicTemplates.Count > 0)
ThrowIfHasBeenSetExplicitly(nameof(dynamicTemplates));
options.DynamicTemplates = dynamicTemplates;
}
if (metadataReferences != null)
{
if (metadataReferences.Count > 0 && options.AdditionalMetadataReferences.Count > 0)
ThrowIfHasBeenSetExplicitly(nameof(metadataReferences));
options.AdditionalMetadataReferences = metadataReferences;
}
if (excludedAssemblies != null)
{
if(excludedAssemblies.Count > 0 && options.ExcludedAssemblies.Count > 0)
ThrowIfHasBeenSetExplicitly(nameof(excludedAssemblies));
options.ExcludedAssemblies = excludedAssemblies;
}
if (prerenderCallbacks != null)
{
if(prerenderCallbacks.Count > 0 && options.PreRenderCallbacks.Count > 0)
ThrowIfHasBeenSetExplicitly(nameof(prerenderCallbacks));
options.PreRenderCallbacks = prerenderCallbacks;
}
if (cachingProvider != null)
{
if(options.CachingProvider != null)
ThrowIfHasBeenSetExplicitly(nameof(cachingProvider));
options.CachingProvider = cachingProvider;
}
if (disableEncoding.HasValue)
{
if(options.DisableEncoding != null)
ThrowIfHasBeenSetExplicitly(nameof(disableEncoding));
options.DisableEncoding = options.DisableEncoding ?? disableEncoding ?? false;
}
else
{
if (!options.DisableEncoding.HasValue)
options.DisableEncoding = false;
}
if (enableDebugMode.HasValue && options.EnableDebugMode.HasValue)
{
ThrowIfHasBeenSetExplicitly(nameof(enableDebugMode));
}
else
{
options.EnableDebugMode = options.EnableDebugMode ?? enableDebugMode ?? false;
}
var metadataReferenceManager = new DefaultMetadataReferenceManager(options.AdditionalMetadataReferences, options.ExcludedAssemblies);
var assembly = operatingAssembly ?? Assembly.GetEntryAssembly();
var compiler = new RoslynCompilationService(metadataReferenceManager, assembly);
var sourceGenerator = new RazorSourceGenerator(DefaultRazorEngine.Instance, project, options.Namespaces);
var templateCompiler = new RazorTemplateCompiler(sourceGenerator, compiler, project, options);
var templateFactoryProvider = new TemplateFactoryProvider();
var engineHandler = new EngineHandler(options, templateCompiler, templateFactoryProvider, cachingProvider);
return new RazorLightEngine(engineHandler);
}
private void ThrowIfHasBeenSetExplicitly(string option)
{
throw new RazorLightException($"{option} has conflicting settings, configure using either fluent configuration or setting an Options object.");
}
}
}