-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextTransformProcessor.cs
More file actions
289 lines (242 loc) · 9.13 KB
/
Copy pathTextTransformProcessor.cs
File metadata and controls
289 lines (242 loc) · 9.13 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
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.CodeDom.Compiler;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Build.Utilities;
using Microsoft.VisualStudio.TextTemplating;
using Mono.TextTemplating.CodeCompilation;
namespace Mono.TextTemplating.Build
{
static class TextTransformProcessor
{
public static bool Process (TaskLoggingHelper taskLog, TemplateBuildState previousBuildState, TemplateBuildState buildState, bool preprocessOnly, ICodeCompilationContext context)
{
(var transforms, var preprocessed) = buildState.GetStaleAndNewTemplates (previousBuildState, preprocessOnly, new WriteTimeCache ().GetWriteTime, taskLog);
if ((transforms == null || transforms.Count == 0) && (preprocessed == null || preprocessed.Count == 0)) {
return true;
}
IEnumerable<(string templateFile, Action<MSBuildTemplateGenerator> generate)> GetTransformActions ()
{
if (transforms == null) {
yield break;
}
var parameterMap = buildState.Parameters?.ToDictionary (p => p.Name, p => p.Value);
foreach (var transform in transforms) {
yield return (transform.InputFile, (generator) => {
string inputFile = transform.InputFile;
string outputFile = Path.ChangeExtension (inputFile, ".txt");
var pt = LoadTemplate (generator, inputFile, out var inputContent);
TemplateSettings settings = TemplatingEngine.GetSettings (generator, pt);
if (parameterMap != null) {
AddCoercedSessionParameters (generator, pt, parameterMap);
}
generator.Errors.AddRange (pt.Errors);
if (generator.Errors.HasErrors) {
return;
}
string outputContent;
(outputFile, outputContent) = generator.ProcessTemplateAsync (pt, inputFile, inputContent, outputFile, settings, context).Result;
if (generator.Errors.HasErrors) {
return;
}
transform.OutputFile = outputFile;
transform.Dependencies = new List<string> (generator.IncludedFiles);
transform.Dependencies.AddRange (generator.CapturedReferences);
WriteOutput (generator, outputFile, outputContent, settings.Encoding);
}
);
};
}
IEnumerable<(string templateFile, Action<MSBuildTemplateGenerator> generate)> GetPreprocessedActions ()
{
if (preprocessed == null) {
yield break;
}
foreach (var preprocess in preprocessed) {
yield return (preprocess.InputFile, (generator) => {
string inputFile = preprocess.InputFile;
var pt = LoadTemplate (generator, inputFile, out var inputContent);
TemplateSettings settings = TemplatingEngine.GetSettings (generator, pt);
settings.RelativeLinePragmas = true;
settings.RelativeLinePragmasBaseDirectory = Path.GetDirectoryName (preprocess.OutputFile);
settings.CodeGenerationOptions.UseRemotingCallContext = buildState.PreprocessTargetRuntimeIdentifier == ".NETFramework";
// FIXME: make these configurable, take relative path into account
settings.Namespace = buildState.DefaultNamespace;
settings.Name = Path.GetFileNameWithoutExtension (preprocess.InputFile);
generator.Errors.AddRange (pt.Errors);
if (generator.Errors.HasErrors) {
return;
}
//FIXME: escaping
//FIXME: namespace name based on relative path and link metadata
string preprocessClassName = Path.GetFileNameWithoutExtension (inputFile);
settings.Name = preprocessClassName;
var outputContent = generator.PreprocessTemplate (pt, inputFile, inputContent, settings, out var references);
if (generator.Errors.HasErrors) {
return;
}
preprocess.Dependencies = new List<string> (generator.IncludedFiles);
preprocess.References = new List<string> (references);
WriteOutput (generator, preprocess.OutputFile, outputContent, settings.Encoding);
}
);
}
}
var templateErrorSets = new ConcurrentQueue<(string filename, CompilerErrorCollection errors)> ();
Parallel.ForEach (
GetTransformActions ().Concat (GetPreprocessedActions ()),
() => CreateGenerator (buildState),
(action, state, generator) => {
try {
action.generate (generator);
}
catch (Exception ex) {
generator.Errors.Add (new CompilerError (null, -1, -1, null, $"Internal error: {ex}"));
}
templateErrorSets.Enqueue ((action.templateFile, new CompilerErrorCollection (generator.Errors)));
generator.Reset ();
return generator;
},
(generator) => { }
);
bool hasErrors = false;
foreach (var templateErrorGroup in templateErrorSets) {
hasErrors |= LogErrors (taskLog, templateErrorGroup.filename, templateErrorGroup.errors);
}
return !hasErrors;
}
static bool LogErrors (TaskLoggingHelper taskLog, string filename, CompilerErrorCollection errors)
{
bool hasErrors = false;
foreach (CompilerError err in errors) {
if (err.IsWarning) {
taskLog.LogWarning (null, err.ErrorNumber, null, err.FileName ?? filename, err.Line, err.Column, 0, 0, err.ErrorText);
} else {
hasErrors = true;
taskLog.LogError (null, err.ErrorNumber, null, err.FileName ?? filename, err.Line, err.Column, 0, 0, err.ErrorText);
}
}
return hasErrors;
}
static ParsedTemplate LoadTemplate (MSBuildTemplateGenerator generator, string filename, out string inputContent)
{
if (!File.Exists (filename)) {
generator.Errors.Add (new CompilerError (filename, -1, -1, null, $"Template file '{filename}' does not exist"));
inputContent = null;
return null;
}
try {
inputContent = File.ReadAllText (filename);
}
catch (IOException ex) {
generator.Errors.Add (new CompilerError (filename, -1, -1, null, $"Internal error: {ex}"));
inputContent = null;
return null;
}
return generator.ParseTemplate (filename, inputContent);
}
static void WriteOutput (MSBuildTemplateGenerator generator, string outputFile, string outputContent, Encoding encoding)
{
try {
File.WriteAllText (outputFile, outputContent, encoding ?? new UTF8Encoding (encoderShouldEmitUTF8Identifier: false));
}
catch (IOException ex) {
generator.Errors.Add (new CompilerError (outputFile, -1, -1, null, $"Internal error: {ex}"));
}
}
static MSBuildTemplateGenerator CreateGenerator (TemplateBuildState buildState)
{
var generator = new MSBuildTemplateGenerator ();
if (buildState.ReferencePaths != null) {
generator.ReferencePaths.AddRange (buildState.ReferencePaths);
}
if (buildState.AssemblyReferences != null) {
generator.Refs.AddRange (buildState.AssemblyReferences);
}
if (buildState.IncludePaths != null) {
generator.IncludePaths.AddRange (buildState.IncludePaths);
}
if (buildState.DirectiveProcessors != null) {
foreach (var dp in buildState.DirectiveProcessors) {
generator.AddDirectiveProcessor (dp.Name, dp.Class, dp.Assembly);
}
}
if (buildState.Parameters != null) {
foreach (var par in buildState.Parameters) {
generator.AddParameter (par.Processor, par.Directive, par.Name, par.Value);
}
}
return generator;
}
sealed class WriteTimeCache
{
public DateTime? GetWriteTime (string filepath)
{
if (!writeTimeCache.TryGetValue (filepath, out var value)) {
writeTimeCache.Add (filepath, value = File.Exists(filepath)? File.GetLastWriteTime (filepath) : null);
}
return value;
}
readonly Dictionary<string, DateTime?> writeTimeCache = new ();
}
static void AddCoercedSessionParameters (MSBuildTemplateGenerator generator, ParsedTemplate pt, Dictionary<string, string> properties)
{
if (properties.Count == 0) {
return;
}
var session = generator.GetOrCreateSession ();
foreach (var p in properties) {
var directive = pt.Directives.FirstOrDefault (d =>
d.Name == "parameter" &&
d.Attributes.TryGetValue ("name", out string attVal) &&
attVal == p.Key);
if (directive != null) {
directive.Attributes.TryGetValue ("type", out string typeName);
var mappedType = ParameterDirectiveProcessor.MapTypeName (typeName);
if (mappedType != "System.String") {
if (ConvertType (mappedType, p.Value, out object converted)) {
session[p.Key] = converted;
continue;
}
pt.Errors.Add (
new CompilerError (
null, 0, 0, null,
$"Could not convert property '{p.Key}'='{p.Value}' to parameter type '{typeName}'"
)
);
}
}
session[p.Key] = p.Value;
}
}
static bool ConvertType (string typeName, string value, out object converted)
{
converted = null;
try {
var type = Type.GetType (typeName);
if (type == null) {
return false;
}
Type stringType = typeof (string);
if (type == stringType) {
return true;
}
var converter = System.ComponentModel.TypeDescriptor.GetConverter (type);
if (converter == null || !converter.CanConvertFrom (stringType)) {
return false;
}
converted = converter.ConvertFromString (value);
return true;
}
catch {
}
return false;
}
}
}