-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathTemplateProcessor.cs
408 lines (344 loc) · 18.7 KB
/
TemplateProcessor.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
namespace Microsoft.Graph.ODataTemplateWriter.TemplateProcessor
{
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.CSharp;
using Microsoft.Graph.ODataTemplateWriter.Extensions;
using Microsoft.Graph.ODataTemplateWriter.PathWriters;
using Microsoft.Graph.ODataTemplateWriter.TemplateProcessor.Enums;
using Microsoft.VisualStudio.TextTemplating;
using Vipr.Core;
using Vipr.Core.CodeModel;
using NLog;
using Microsoft.Graph.ODataTemplateWriter.Settings;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Emit;
using System.Reflection;
using Mono.TextTemplating;
public class TemplateProcessor : ITemplateProcessor
{
private static CustomT4Host _host;
private static Logger logger = LogManager.GetCurrentClassLogger();
protected static CustomT4Host Host(ITemplateInfo templateInfo, string templatesDirectory, OdcmObject odcmObject, OdcmModel odcmModel)
{
// Need to always set the host. Typically, this is run against a single platform when generating codefiels.
// In test cases, we need to target multiple platforms. Since much of this code is static, we need to make sure
// reset the information provided to the template processor. This change fixes a bug when targeting
// multiple platforms in a test.
_host = new CustomT4Host(templateInfo, templatesDirectory, odcmObject, odcmModel);
return _host;
}
private Dictionary<SubProcessor, Func<ITemplateInfo, IEnumerable<TextFile>>> subProcessors;
private readonly Dictionary<String, Func<ITextTemplatingEngineHost, string>> preProcessedTemplates;
protected Dictionary<SubProcessor, Func<ITemplateInfo, IEnumerable<TextFile>>> SubProcessors
{
get
{
if (this.subProcessors == null)
{
this.InitializeSubprocessors();
}
return this.subProcessors;
}
}
protected void InitializeSubprocessors()
{
this.subProcessors = new Dictionary<SubProcessor, Func<ITemplateInfo, IEnumerable<TextFile>>>() {
{SubProcessor.EntityType, this.ProcessEntityTypes},
{SubProcessor.ComplexType, this.ProcessComplexTypes},
{SubProcessor.EnumType, this.ProcessEnumTypes},
{SubProcessor.EntityContainer, this.ProcessEntityContainerType},
{SubProcessor.MediaEntityType, this.ProcessMediaEntityTypes},
{SubProcessor.Property, this.ProcessProperties},
{SubProcessor.StreamProperty, this.ProcessStreamProperties},
{SubProcessor.CollectionProperty, this.ProcessCollections},
{SubProcessor.NavigationCollectionProperty, this.ProcessNavigationCollections},
{SubProcessor.CollectionReferenceProperty, this.ProcessCollectionReferences},
{SubProcessor.EntityReferenceType, this.ProcessEntityReferenceProperties},
{SubProcessor.Method, this.ProcessMethods},
{SubProcessor.NonCollectionMethod, this.ProcessNonCollectionMethods},
{SubProcessor.CollectionMethod, this.ProcessCollectionMethods},
{SubProcessor.MethodWithBody, this.ProcessMethodsWithBody},
{SubProcessor.Other, this.ProcessTemplate},
};
}
protected OdcmModel CurrentModel { get; set; }
protected TemplatingEngine T4Engine { get; set; }
protected IPathWriter PathWriter { get; set; }
protected string TemplatesDirectory { get; set; }
public TemplateProcessor(IPathWriter pathWriter, OdcmModel odcmModel, string templatesDirectory)
{
this.T4Engine = new TemplatingEngine();
this.CurrentModel = odcmModel;
this.PathWriter = pathWriter;
this.PathWriter.Model = odcmModel;
this.TemplatesDirectory = templatesDirectory;
this.preProcessedTemplates = new Dictionary<string, Func<ITextTemplatingEngineHost, string>>();
}
public string GetProcessorVerboseOutput()
{
//TODO:Check NullReferenceException due no templates loaded.
return _host.TemplateHostStats.ToString();
}
public IEnumerable<TextFile> Process(ITemplateInfo templateInfo)
{
Func<ITemplateInfo, IEnumerable<TextFile>> subProcessor = this.ProcessTemplate;
this.SubProcessors.TryGetValue(templateInfo.SubprocessorType, out subProcessor);
logger.Info("Current subprocessor type: {0}", templateInfo.SubprocessorType);
return subProcessor(templateInfo);
}
protected virtual IEnumerable<TextFile> ProcessEntityContainerType(ITemplateInfo templateInfo)
{
yield return this.ProcessTemplate(templateInfo, this.CurrentModel.EntityContainer);
}
protected virtual IEnumerable<TextFile> ProcessEnumTypes(ITemplateInfo templateInfo)
{
return this.ProcessTypes(templateInfo, this.CurrentModel.GetEnumTypes);
}
protected virtual IEnumerable<TextFile> ProcessComplexTypes(ITemplateInfo templateInfo)
{
return this.ProcessTypes(templateInfo, this.CurrentModel.GetComplexTypes);
}
protected virtual IEnumerable<TextFile> ProcessEntityTypes(ITemplateInfo templateInfo)
{
return this.ProcessTypes(templateInfo, this.CurrentModel.GetEntityTypes);
}
protected virtual IEnumerable<TextFile> ProcessCollections(ITemplateInfo templateInfo)
{
return this.ProcessProperties(templateInfo, this.CollectionProperties);
}
protected virtual IEnumerable<TextFile> ProcessMediaEntityTypes(ITemplateInfo templateInfo)
{
return this.ProcessTypes(templateInfo, this.CurrentModel.GetMediaEntityTypes);
}
protected virtual IEnumerable<TextFile> ProcessNavigationCollections(ITemplateInfo templateInfo)
{
return this.ProcessProperties(templateInfo, this.NavigationCollectionProperties);
}
protected virtual IEnumerable<TextFile> ProcessCollectionReferences(ITemplateInfo templateInfo)
{
return this.ProcessProperties(templateInfo, this.CollectionReferenceProperties);
}
protected virtual IEnumerable<TextFile> ProcessProperties(ITemplateInfo templateInfo)
{
return this.ProcessProperties(templateInfo, this.CurrentModel.GetProperties);
}
protected virtual IEnumerable<TextFile> ProcessEntityReferenceProperties(ITemplateInfo templateInfo)
{
return this.ProcessTypes(templateInfo, this.CurrentModel.GetEntityReferenceTypes);
}
protected virtual IEnumerable<TextFile> ProcessStreamProperties(ITemplateInfo templateInfo)
{
return this.ProcessProperties(templateInfo, this.CurrentModel.GetStreamProperties);
}
protected virtual IEnumerable<TextFile> ProcessMethods(ITemplateInfo templateInfo)
{
return this.ProcessMethods(templateInfo, this.CurrentModel.GetMethods);
}
protected virtual IEnumerable<TextFile> ProcessMethodsWithBody(ITemplateInfo templateInfo)
{
return this.ProcessMethods(templateInfo, this.MethodsWithBody);
}
protected virtual IEnumerable<TextFile> ProcessCollectionMethods(ITemplateInfo templateInfo)
{
return this.ProcessMethods(templateInfo, this.CollectionMethods);
}
protected virtual IEnumerable<TextFile> ProcessNonCollectionMethods(ITemplateInfo templateInfo)
{
return this.ProcessMethods(templateInfo, this.NonCollectionMethods);
}
private IEnumerable<TextFile> ProcessTypes(ITemplateInfo templateInfo, Func<IEnumerable<OdcmObject>> modelMethod)
{
return this.FilterOdcmEnumerable(templateInfo, modelMethod)
.Select(odcmType => this.ProcessTemplate(templateInfo, odcmType, className: odcmType.Name));
}
private IEnumerable<TextFile> ProcessProperties(ITemplateInfo templateInfo, Func<IEnumerable<OdcmObject>> modelMethod)
{
return this.FilterOdcmEnumerable(templateInfo, modelMethod)
.Select(property => property as OdcmProperty)
.Select(odcmProperty => this.ProcessTemplate(templateInfo, odcmProperty,
className: odcmProperty.Class.Name,
propertyName: odcmProperty.Name,
propertyType: odcmProperty.Projection.Type.Name));
}
protected virtual IEnumerable<TextFile> ProcessMethods(ITemplateInfo templateInfo, Func<IEnumerable<OdcmMethod>> methods)
{
return this.FilterOdcmEnumerable(templateInfo, methods)
.Select(method => method as OdcmMethod)
.Select(odcmMethod => this.ProcessTemplate(templateInfo, odcmMethod,
className: odcmMethod.Class.Name,
methodName: odcmMethod.Name));
}
protected virtual IEnumerable<OdcmMethod> NonCollectionMethods()
{
return this.CurrentModel.GetMethods().Where(method => !method.IsCollection);
}
protected virtual IEnumerable<OdcmMethod> MethodsWithBody()
{
return this.CurrentModel.GetMethods().Where(method => method.Parameters != null && method.Parameters.Any() && method.IsAction());
}
protected virtual IEnumerable<OdcmMethod> CollectionMethods()
{
var methods = this.CurrentModel.GetMethods().Where(method => method.IsCollection && method.ReturnType != null).ToList();
return methods;
}
protected virtual IEnumerable<OdcmProperty> NavigationCollectionProperties()
{
return this.CurrentModel.GetProperties().Where(prop => prop.IsCollection && prop.IsNavigation() && !prop.IsReference());
}
protected virtual IEnumerable<OdcmProperty> CollectionReferenceProperties()
{
return this.CurrentModel.GetProperties().Where(prop => prop.IsReference() && prop.IsCollection);
}
protected virtual IEnumerable<OdcmProperty> CollectionProperties()
{
return this.CurrentModel.GetProperties().Where(prop => prop.IsCollection && !(prop.Projection.Type is OdcmPrimitiveType) && !prop.IsReference());
}
protected virtual IEnumerable<OdcmObject> FilterOdcmEnumerable(ITemplateInfo templateInfo, Func<IEnumerable<OdcmObject>> modelMethod)
{
var filteredEnum = modelMethod().Where(o => templateInfo.ShouldIncludeObject(o));
if (_host != null && !filteredEnum.Any())
{
_host.TemplateHostStats.RecordProcessedNothing(templateInfo);
}
return filteredEnum;
}
protected IEnumerable<TextFile> ProcessTemplate(ITemplateInfo templateInfo)
{
yield return this.ProcessTemplate(templateInfo, null, templateInfo.BaseFileName());
}
private const string RuntimeTemplatesNamespace = "RuntimeTemplates";
private Func<ITextTemplatingEngineHost, string> PreProcessTemplate(ITemplateInfo templateInfo)
{
var templateContent = File.ReadAllText(templateInfo.FullPath);
var className = templateInfo.TemplateName.Replace(".", "_");
var assembly = CompileAndLoadTemplateAssembly(templateInfo, templateContent, className);
var templateClassType = assembly.GetType(RuntimeTemplatesNamespace + "." + className);
dynamic templateClassInstance = Activator.CreateInstance(templateClassType);
return (ITextTemplatingEngineHost host) =>
{
templateClassInstance.Host = host;
return templateClassInstance.TransformText();
};
}
private static Dictionary<string, Assembly> cachedAssemblies = new Dictionary<string, Assembly>();
private Assembly CompileTemplateOrRetrieveFromCache(ITemplateInfo templateInfo, string templateContent, string className)
{
if (!cachedAssemblies.ContainsKey(className))
{
var assembly = CompileAndLoadTemplateAssembly(templateInfo, templateContent, className);
cachedAssemblies.Add(className, assembly);
return assembly;
}
return cachedAssemblies[className];
}
private Assembly CompileAndLoadTemplateAssembly(ITemplateInfo templateInfo, string templateContent, string className)
{
var dummyHost = new CustomT4Host(templateInfo, this.TemplatesDirectory, null, null);
var generatedCode = this.T4Engine.PreprocessTemplate(templateContent, dummyHost, className, RuntimeTemplatesNamespace, out var language, out var references);
var syntaxTree = CSharpSyntaxTree.ParseText(generatedCode);
var refs = new List<PortableExecutableReference>();
foreach (var reference in ((string)AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES")).Split(Path.PathSeparator))
{
refs.Add(MetadataReference.CreateFromFile(reference));
}
var compilation = CSharpCompilation.Create($"{templateInfo.TemplateName}.dll",
syntaxTrees: new[] { syntaxTree },
references: refs,
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
var messages = new List<string>();
using (var ms = new MemoryStream())
{
EmitResult result = compilation.Emit(ms);
if (!result.Success)
{
var failures = result.Diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError ||
diagnostic.Severity == DiagnosticSeverity.Error);
foreach (Diagnostic diagnostic in failures.OrderBy(o => o.Location.GetLineSpan().StartLinePosition.Line))
{
messages.Add($"({diagnostic.Location.GetLineSpan().StartLinePosition.Line}) {diagnostic.Id}: {diagnostic.GetMessage()}");
}
throw new InvalidOperationException("Template error.");
}
else
{
ms.Seek(0, SeekOrigin.Begin);
var byteAssembly = ms.ToArray();
var assembly = Assembly.Load(byteAssembly);
messages.Add("Compilation successful");
return assembly;
}
}
}
private TextFile ProcessTemplate(ITemplateInfo templateInfo, OdcmObject odcmObject,
string className = "", string propertyName = "", string methodName = "", string propertyType = "")
{
return this.ProcessTemplate(templateInfo, odcmObject,
templateInfo.BaseFileName(this.CurrentModel.EntityContainer.Name,
className,
propertyName,
methodName,
propertyType));
}
protected TextFile ProcessTemplate(ITemplateInfo templateInfo, OdcmObject odcmObject, string fileName)
{
logger.Trace($"Generating file {fileName}");
var host = TemplateProcessor.Host(templateInfo, this.TemplatesDirectory, odcmObject, this.CurrentModel);
Func<ITextTemplatingEngineHost, string> preProcessedTemplate;
if (!this.preProcessedTemplates.TryGetValue(templateInfo.FullPath, out preProcessedTemplate))
{
preProcessedTemplate = this.PreProcessTemplate(templateInfo);
this.preProcessedTemplates.Add(templateInfo.FullPath, preProcessedTemplate);
}
var output = preProcessedTemplate(host);
if (!string.IsNullOrEmpty(host.TemplateName))
{
fileName = host.TemplateName;
}
if (host.Errors != null && host.Errors.HasErrors)
{
var errors = this.LogErrors(host, templateInfo);
logger.Error(errors);
throw new InvalidOperationException(errors);
}
string @namespace = ConfigurationService.Settings.PrimaryNamespaceName;
switch (odcmObject)
{
case OdcmType t:
@namespace = t.Namespace.GetNamespaceName();
break;
case OdcmProperty p:
@namespace = p?.Class.Namespace.GetNamespaceName();
break;
default:
break;
}
var path = this.PathWriter.WritePath(templateInfo, @namespace, fileName);
logger.Debug("Wrote template to path {0}", path);
host.TemplateHostStats.RecordProcessed(templateInfo, odcmObject != null ? odcmObject.Name : string.Empty, path);
return new TextFile(path, output);
}
public string LogErrors(CustomT4Host host, ITemplateInfo templateInfo)
{
var sb = new StringBuilder();
if (host.Errors == null || host.Errors.Count <= 0) return sb.ToString();
foreach (CompilerError error in host.Errors)
{
sb.AppendLine("TemplateProcessor ERROR").
AppendFormat(@"Name: {0}{1}", templateInfo.TemplateName, Environment.NewLine).
AppendFormat(@"Line: {0}{1}", error.Line, Environment.NewLine).
AppendFormat(@"Details: {0}{1}", error.ErrorText, Environment.NewLine).
AppendLine().
AppendLine();
}
return sb.ToString();
}
}
}