-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathRazorEngineCompilationOptionsBuilder.cs
More file actions
147 lines (126 loc) · 4.95 KB
/
RazorEngineCompilationOptionsBuilder.cs
File metadata and controls
147 lines (126 loc) · 4.95 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis;
namespace RazorEngineCore
{
public class RazorEngineCompilationOptionsBuilder : IRazorEngineCompilationOptionsBuilder
{
public RazorEngineCompilationOptions Options { get; set; }
public RazorEngineCompilationOptionsBuilder(RazorEngineCompilationOptions options = null)
{
this.Options = options ?? new RazorEngineCompilationOptions();
}
/// <summary>
/// Loads the Assembly by name and adds it to the Engine's Assembly Reference list
/// </summary>
/// <param name="assemblyName">Full Name of the Assembly to add to the assembly list</param>
public void AddAssemblyReferenceByName(string assemblyName)
{
Assembly assembly = Assembly.Load(new AssemblyName(assemblyName));
this.AddAssemblyReference(assembly);
}
/// <summary>
/// Adds a loaded assembly to the Engine's Assembly Reference list
/// </summary>
/// <param name="assembly">Assembly to add to the assembly list</param>
public void AddAssemblyReference(Assembly assembly)
{
this.Options.ReferencedAssemblies.Add(assembly);
}
/// <summary>
/// <para>Adds a type's assembly to the Engine's Assembly Reference list</para>
/// <para>Also adds the type's GenericTypeArguments to the Reference list as well</para>
/// </summary>
/// <param name="type">The type who's assembly should be added to the assembly list</param>
public void AddAssemblyReference(Type type)
{
this.AddAssemblyReference(type.Assembly);
foreach (Type argumentType in type.GenericTypeArguments)
{
this.AddAssemblyReference(argumentType);
}
}
/// <summary>
/// Adds a MetadataReference for use in the Engine's Assembly Reference generation
/// </summary>
/// <param name="reference">Metadata Reference to add to the Engine's Referenced Assemblies</param>
public void AddMetadataReference(MetadataReference reference)
{
this.Options.MetadataReferences.Add(reference);
}
/// <summary>
/// <para>Adds a default <c>using</c> to the compiled view. This is equivalent to adding <c>@using [NAMESPACE]</c> to the template rendered by the engine'</para>
/// Current Defaults:
/// <list type="bullet">
/// <listheader></listheader>
/// <item>System.Linq</item>
/// <item>System.Collections</item>
/// <item>System.Collections.Generic</item>
/// </list>
/// </summary>
/// <param name="namespaceName">Namespace to add to default usings</param>
public void AddUsing(string namespaceName)
{
this.Options.DefaultUsings.Add(namespaceName);
}
/// <summary>
/// Adds <c>@inherits</c> directive to the compiled template
/// </summary>
/// <param name="type">Type to <c>@inherits</c> from</param>
public void Inherits(Type type)
{
this.Options.Inherits = this.RenderTypeName(type);
this.AddAssemblyReference(type);
}
private string RenderTypeName(Type type)
{
IList<string> elements = new List<string>()
{
type.Namespace,
RenderDeclaringType(type.DeclaringType),
type.Name
};
string result = string.Join(".", elements.Where(e => !string.IsNullOrWhiteSpace(e)));
int tildeLocation = result.IndexOf('`');
if (tildeLocation > -1)
{
result = result.Substring(0, tildeLocation);
}
if (type.GenericTypeArguments.Length == 0)
{
return result;
}
return result + "<" + string.Join(",", type.GenericTypeArguments.Select(this.RenderTypeName)) + ">";
}
private string RenderDeclaringType(Type type)
{
if (type == null)
{
return null;
}
string parent = RenderDeclaringType(type.DeclaringType);
if (string.IsNullOrWhiteSpace(parent))
{
return type.Name;
}
return parent + "." + type.Name;
}
/// <summary>
/// Enables debug info
/// </summary>
public void IncludeDebuggingInfo()
{
this.Options.IncludeDebuggingInfo = true;
}
/// <summary>
/// Enables debug info
/// </summary>
public void ConfigureRazorEngineProject(Action<RazorProjectEngineBuilder> configure)
{
this.Options.ProjectEngineBuilder = configure;
}
}
}