-
Notifications
You must be signed in to change notification settings - Fork 16
/
Runner.cs
362 lines (311 loc) · 13.8 KB
/
Runner.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
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using MonoTouch.Dialog.Utilities;
#nullable enable
namespace SourceGeneratorPlayground
{
internal class Runner : IRunner
{
private static readonly Dictionary<string, MetadataReference>? s_references = new();
private static readonly LRUCache<string, ImmutableArray<ISourceGenerator>> s_sourceGeneratorCache = new(entryLimit: 10);
private readonly string _baseUri;
public string ErrorText { get; private set; } = "";
public string? GeneratorOutput { get; private set; } = "";
public string ProgramOutput { get; private set; } = "";
public Runner(NavigationManager navigationManager)
{
_baseUri = navigationManager.BaseUri;
}
public async Task RunAsync(string code, string generator, CancellationToken cancellationToken)
{
await UpdateReferences(_baseUri);
this.ProgramOutput = "";
this.GeneratorOutput = "";
this.ErrorText = "";
if (!TryCompileGenerator(generator, out var errorCompilingGenerator, out var generatorInstances, cancellationToken))
{
this.ErrorText = errorCompilingGenerator;
return;
}
if (!TryCompileUserCode(code, generatorInstances, out var errorCompilingUserCode, out var programAssembly, out var generatorOutput, cancellationToken))
{
this.ErrorText = errorCompilingUserCode;
this.GeneratorOutput = generatorOutput;
return;
}
this.GeneratorOutput = generatorOutput;
var (success, output) = await TryExecuteProgramAsync(programAssembly);
if (!success)
{
this.ErrorText = output;
}
else
{
this.ProgramOutput = output;
}
}
private static bool TryCompileGenerator(
string code,
[NotNullWhen(false)] out string? error,
[NotNullWhen(true)] out ImmutableArray<ISourceGenerator> generators,
CancellationToken cancellationToken)
{
error = default;
generators = default;
if (string.IsNullOrWhiteSpace(code))
{
error = "Need more input for the generator code!";
return false;
}
var normalizedCode = CSharpSyntaxTree.ParseText(code).GetRoot().NormalizeWhitespace().ToFullString();
var cacheHit = s_sourceGeneratorCache[normalizedCode];
if (cacheHit != null)
{
generators = cacheHit;
return true;
}
var generatorTree = CSharpSyntaxTree.ParseText(code, new CSharpParseOptions(kind: SourceCodeKind.Regular), "Generator.cs", cancellationToken: cancellationToken);
var generatorCompilation = CSharpCompilation.Create("Generator", new[] { generatorTree }, s_references.Values, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
error = GetErrors("Error(s) compiling generator:", generatorCompilation.GetDiagnostics(cancellationToken: cancellationToken));
if (error != null)
{
return false;
}
var generatorAssembly = GetAssembly(generatorCompilation, "generator", out error, cancellationToken: cancellationToken);
if (error != null)
{
return false;
}
if (generatorAssembly == null)
{
error = "Unknown error emitting generator.";
return false;
}
generators = generatorAssembly.GetTypes()
.Where(t => !t.GetTypeInfo().IsInterface && !t.GetTypeInfo().IsAbstract && !t.GetTypeInfo().ContainsGenericParameters)
.Where(t => typeof(ISourceGenerator).IsAssignableFrom(t))
.Select(t => Activator.CreateInstance(t))
.OfType<ISourceGenerator>()
.ToImmutableArray();
if (generators.Length == 0)
{
error = "Could not instantiate source generator. Types in assembly:" + Environment.NewLine + Environment.NewLine + string.Join(Environment.NewLine, (object)generatorAssembly.GetTypes());
return false;
}
s_sourceGeneratorCache[normalizedCode] = generators;
return true;
}
private static bool TryCompileUserCode
(string code,
ImmutableArray<ISourceGenerator> generators,
[NotNullWhen(false)] out string? error,
[NotNullWhen(true)] out Assembly? programAssembly,
out string? generatorOutput, CancellationToken cancellationToken)
{
error = default;
programAssembly = default;
generatorOutput = default;
if (string.IsNullOrWhiteSpace(code))
{
error = "Need more input for the user code!";
return false;
}
var codeTree = CSharpSyntaxTree.ParseText(code, new CSharpParseOptions(kind: SourceCodeKind.Regular), "Program.cs", cancellationToken: cancellationToken);
var codeCompilation = CSharpCompilation.Create("Program", new SyntaxTree[] { codeTree }, s_references.Values, new CSharpCompilationOptions(OutputKind.ConsoleApplication));
var driver = CSharpGeneratorDriver.Create(generators);
driver.RunGeneratorsAndUpdateCompilation(codeCompilation, out Compilation? outputCompilation, out ImmutableArray<Diagnostic> diagnostics, cancellationToken: cancellationToken);
error = GetErrors("Error(s) running generator:", diagnostics, false);
if (error != null)
{
return false;
}
var output = new StringBuilder();
var trees = outputCompilation.SyntaxTrees.Where(t => t != codeTree).OrderBy(t => t.FilePath).ToArray();
foreach (var tree in trees)
{
if (output.Length > 0)
{
output.AppendLine().AppendLine();
}
if (trees.Length > 1)
{
output.AppendLine(tree.FilePath);
output.AppendLine(new string('-', 50));
}
output.AppendLine(tree.WithRootAndOptions(tree.GetRoot(cancellationToken: cancellationToken).NormalizeWhitespace(), tree.Options).ToString());
}
if (output.Length == 0)
{
output.AppendLine("< No source generated >");
}
generatorOutput = output.ToString();
error = GetErrors("Error(s) compiling program:", outputCompilation.GetDiagnostics(cancellationToken: cancellationToken));
if (error != null)
{
return false;
}
programAssembly = GetAssembly(outputCompilation, "program", out error, cancellationToken: cancellationToken);
if (error != null)
{
return false;
}
if (programAssembly == null)
{
error = "Unknown error emitting program.";
return false;
}
return true;
}
private static async Task<(bool Success, string Output)> TryExecuteProgramAsync(Assembly programAssembly)
{
string? error = default;
string? output = default;
var program = programAssembly.GetTypes().FirstOrDefault(t => t.Name == "Program");
if (program == null)
{
error = "Error executing program:" + Environment.NewLine + Environment.NewLine + "Could not find type \"Program\" in program.";
return (Success: false, Output: error);
}
var main = program.GetMethod("Main", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (main == null)
{
error = "Error executing program:" + Environment.NewLine + Environment.NewLine + "Could not find static method \"Main\" in program.";
return (Success: false, Output: error);
}
using var writer = new StringWriter();
try
{
Console.SetOut(writer);
int paramCount = main.GetParameters().Length;
if (main.ReturnType == typeof(void))
{
if (paramCount == 1)
{
main.Invoke(null, new object?[] { null });
}
else if (paramCount == 0)
{
main.Invoke(null, null);
}
else
{
error = "Error executing program:" + Environment.NewLine + Environment.NewLine + "Method \"Main\" must have 0 or 1 parameters.";
return (Success: false, Output: error);
}
}
else if (main.ReturnType == typeof(Task))
{
if (paramCount == 1)
{
var task = main.Invoke(null, new object?[] { null }) as Task;
await task!;
}
else if (paramCount == 0)
{
var task = main.Invoke(null, null) as Task;
await task!;
}
else
{
error = "Error executing program:" + Environment.NewLine + Environment.NewLine + "Method \"Main\" must have 0 or 1 parameters.";
return (Success: false, Output: error);
}
}
else
{
error = "Error executing program:" + Environment.NewLine + Environment.NewLine + "Method \"Main\" must have either void or Task return type.";
return (Success: false, Output: error);
}
output = writer.ToString();
if (string.IsNullOrEmpty(output))
{
output = "< No program output >";
}
}
catch (Exception ex)
{
error = writer.ToString() + "\n\nError executing program:" + Environment.NewLine + Environment.NewLine + ex.ToString();
return (Success: false, Output: error);
}
return (Success: true, Output: output);
}
private static Assembly? GetAssembly(Compilation generatorCompilation, string name, out string? errors, CancellationToken cancellationToken)
{
try
{
using var generatorStream = new MemoryStream();
var result = generatorCompilation.Emit(generatorStream, cancellationToken: cancellationToken);
if (result == null)
{
errors = "Failed to compile with unknown error";
return null;
}
if (!result.Success)
{
errors = GetErrors($"Error emitting {name}:", result.Diagnostics, false);
return null;
}
generatorStream.Seek(0, SeekOrigin.Begin);
errors = null;
return Assembly.Load(generatorStream.ToArray());
}
catch (Exception ex)
{
errors = ex.ToString();
return null;
}
}
private static string? GetErrors(string header, IEnumerable<Diagnostic> diagnostics, bool errorsOnly = true)
{
IEnumerable<Diagnostic>? errors = diagnostics.Where(d => !errorsOnly || d.Severity == DiagnosticSeverity.Error);
if (!errors.Any())
{
return null;
}
return header + Environment.NewLine + Environment.NewLine + string.Join(Environment.NewLine, errors);
}
private static async Task<List<MetadataReference>> GetReferences(string baseUri)
{
Assembly[]? refs = AppDomain.CurrentDomain.GetAssemblies();
var client = new HttpClient
{
BaseAddress = new Uri(baseUri)
};
var references = new List<MetadataReference>();
foreach (Assembly? reference in refs.Where(x => !x.IsDynamic && !string.IsNullOrWhiteSpace(x.Location)))
{
Stream? stream = await client.GetStreamAsync($"_framework/_bin/{reference.Location}");
references.Add(MetadataReference.CreateFromStream(stream));
}
return references;
}
private async Task UpdateReferences(string baseUri)
{
Assembly[]? refs = AppDomain.CurrentDomain.GetAssemblies();
var client = new Lazy<HttpClient>(() => new HttpClient
{
BaseAddress = new Uri(baseUri)
});
foreach (Assembly? reference in refs.Where(x => !x.IsDynamic && !string.IsNullOrWhiteSpace(x.Location)))
{
if (!s_references.ContainsKey(reference.Location))
{
Stream? stream = await client.Value.GetStreamAsync($"_framework/_bin/{reference.Location}");
s_references.Add(reference.Location, MetadataReference.CreateFromStream(stream));
}
}
}
}
}