forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompilerServiceBase.cs
More file actions
214 lines (181 loc) · 8.61 KB
/
CompilerServiceBase.cs
File metadata and controls
214 lines (181 loc) · 8.61 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
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
using System.Web.Razor.Parser.SyntaxTree;
using ServiceStack.RazorEngine.ServiceStack;
using ServiceStack.RazorEngine.Templating;
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web.Razor;
using System.Web.Razor.Generator;
using System.Web.Razor.Parser;
namespace ServiceStack.RazorEngine.Compilation
{
/// <summary>
/// Provides a base implementation of a compiler service.
/// </summary>
public abstract class CompilerServiceBase : ICompilerService
{
#region Constructor
/// <summary>
/// Initialises a new instance of <see cref="CompilerServiceBase"/>
/// </summary>
/// <param name="codeLanguage">The code language.</param>
/// <param name="markupParser">The markup parser.</param>
protected CompilerServiceBase(RazorCodeLanguage codeLanguage, MarkupParser markupParser)
{
if (codeLanguage == null)
throw new ArgumentNullException("codeLanguage");
CodeLanguage = codeLanguage;
MarkupParser = markupParser ?? new HtmlMarkupParser();
}
#endregion
#region Properties
/// <summary>
/// Gets the code language.
/// </summary>
public RazorCodeLanguage CodeLanguage { get; private set; }
/// <summary>
/// Gets the markup parser.
/// </summary>
public MarkupParser MarkupParser { get; private set; }
#endregion
#region Methods
/// <summary>
/// Builds a type name for the specified template type and model type.
/// </summary>
/// <param name="templateType">The template type.</param>
/// <param name="modelType">The model type.</param>
/// <returns>The string type name (including namespace).</returns>
public virtual string BuildTypeName(Type templateType, Type modelType)
{
if (templateType == null)
throw new ArgumentNullException("templateType");
if (!templateType.IsGenericTypeDefinition && !templateType.IsGenericType)
return templateType.FullName;
if (modelType == null)
throw new ArgumentException("The template type is a generic defintion, and no model type has been supplied.");
bool @dynamic = CompilerServices.IsDynamicType(modelType);
Type genericType = templateType.MakeGenericType(modelType);
return BuildTypeNameInternal(genericType, @dynamic);
}
/// <summary>
/// Builds a type name for the specified generic type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="isDynamic">Is the model type dynamic?</param>
/// <returns>The string typename (including namespace and generic type parameters).</returns>
public abstract string BuildTypeNameInternal(Type type, bool isDynamic);
/// <summary>
/// Compiles the type defined in the specified type context.
/// </summary>
/// <param name="context">The type context which defines the type to compile.</param>
/// <returns>The compiled type.</returns>
public abstract Type CompileType(TypeContext context);
/// <summary>
/// Generates any required contructors for the specified type.
/// </summary>
/// <param name="constructors">The set of constructors.</param>
/// <param name="codeType">The code type declaration.</param>
private static void GenerateConstructors(IEnumerable<ConstructorInfo> constructors, CodeTypeDeclaration codeType)
{
if (constructors == null || !constructors.Any())
return;
var existingConstructors = codeType.Members.OfType<CodeConstructor>().ToArray();
foreach (var existingConstructor in existingConstructors)
codeType.Members.Remove(existingConstructor);
foreach (var constructor in constructors)
{
var ctor = new CodeConstructor();
ctor.Attributes = MemberAttributes.Public;
foreach (var param in constructor.GetParameters())
{
ctor.Parameters.Add(new CodeParameterDeclarationExpression(param.ParameterType, param.Name));
ctor.BaseConstructorArgs.Add(new CodeSnippetExpression(param.Name));
}
codeType.Members.Add(ctor);
}
}
/// <summary>
/// Gets the code compile unit used to compile a type.
/// </summary>
/// <param name="className">The class name.</param>
/// <param name="template">The template to compile.</param>
/// <param name="namespaceImports">The set of namespace imports.</param>
/// <param name="templateType">The template type.</param>
/// <param name="modelType">The model type.</param>
/// <returns>A <see cref="CodeCompileUnit"/> used to compile a type.</returns>
public CodeCompileUnit GetCodeCompileUnit(string className, string template, ISet<string> namespaceImports, Type templateType, Type modelType)
{
if (string.IsNullOrEmpty(className))
throw new ArgumentException("Class name is required.");
if (string.IsNullOrEmpty(template))
throw new ArgumentException("Template is required.");
templateType = templateType
?? ((modelType == null)
? typeof(TemplateBase)
: typeof(TemplateBase<>));
var host = new MvcWebPageRazorHost(CodeLanguage, () => MarkupParser)
{
DefaultBaseClass = BuildTypeName(templateType, modelType),
DefaultClassName = className,
DefaultNamespace = "CompiledRazorTemplates.Dynamic",
GeneratedClassContext = new GeneratedClassContext("Execute", "Write", "WriteLiteral",
"WriteTo", "WriteLiteralTo",
"RazorEngine.Templating.TemplateWriter",
"WriteSection")
};
host.NamespaceImports.Add("ServiceStack.Markdown.Html");
var templateNamespaces = templateType.GetCustomAttributes(typeof (RequireNamespacesAttribute), true)
.Cast<RequireNamespacesAttribute>()
.SelectMany(att => att.Namespaces);
foreach (string ns in templateNamespaces)
namespaceImports.Add(ns);
foreach (string @namespace in namespaceImports)
host.NamespaceImports.Add(@namespace);
var engine = new RazorTemplateEngine(host);
GeneratorResults result;
using (var reader = new StringReader(template))
{
result = engine.GenerateCode(reader);
}
var type = result.GeneratedCode.Namespaces[0].Types[0];
if (modelType != null)
{
if (CompilerServices.IsAnonymousType(modelType))
{
type.CustomAttributes.Add(new CodeAttributeDeclaration(new CodeTypeReference(typeof(HasDynamicModelAttribute))));
}
}
GenerateConstructors(CompilerServices.GetConstructors(templateType), type);
var statement = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "Clear");
foreach (CodeTypeMember member in type.Members)
{
if (member.Name.Equals("Execute"))
{
((CodeMemberMethod)member).Statements.Insert(0, new CodeExpressionStatement(statement));
break;
}
}
return result.GeneratedCode;
}
#endregion
public IEnumerable<T> AllNodesOfType<T>(Block block)
{
if (block is T)
yield return (T)(object)block;
foreach (var syntaxTreeNode in block.Children)
{
if (syntaxTreeNode is T)
yield return (T)(object)syntaxTreeNode;
var childBlock = syntaxTreeNode as Block;
if (childBlock == null) continue;
foreach (var variable in AllNodesOfType<T>(childBlock))
{
yield return variable;
}
}
}
}
}