-
Notifications
You must be signed in to change notification settings - Fork 4
/
IncrementalGenerator.cs
82 lines (61 loc) · 3.3 KB
/
IncrementalGenerator.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
using Nemesis.TextParsers.CodeGen.Utils;
namespace Nemesis.TextParsers.CodeGen;
public abstract class IncrementalGenerator : IIncrementalGenerator
{
protected const string INPUTS = "Inputs";
protected abstract string GetAttributeName();
public abstract void Initialize(IncrementalGeneratorInitializationContext context);
public GeneratorRunResult RunIncrementalGenerator(Compilation compilation)
{
GeneratorDriver driver = CSharpGeneratorDriver.Create(
generators: new ISourceGenerator[] { this.AsSourceGenerator() },
parseOptions: (CSharpParseOptions)compilation.SyntaxTrees.First().Options,
driverOptions: new GeneratorDriverOptions(default, trackIncrementalGeneratorSteps: true));
driver = driver.RunGenerators(compilation);
return driver.GetRunResult().Results.Single();
}
public IReadOnlyList<string> RunIncrementalGeneratorAndGetGeneratedSources(Compilation compilation, int requiredCardinality = 1)
{
var result = RunIncrementalGenerator(compilation);
return GetGeneratedOutput(result, requiredCardinality);
}
public (IReadOnlyList<string> Sources, IReadOnlyList<TInput> Inputs) RunIncrementalGeneratorAndCaptureInputs<TInput>(Compilation compilation, int requiredCardinality = 1)
{
var result = RunIncrementalGenerator(compilation);
var generatedSources = GetGeneratedOutput(result, requiredCardinality);
if (result.TrackedSteps.TryGetValue(INPUTS, out var metaValue))
{
var stepResults = metaValue.Single().Outputs
.Select(o => (
Result: (Result<TInput, Diagnostic>)o.Value,
o.Reason
))
.ToList();
if (stepResults.Any(r => r.Reason != IncrementalStepRunReason.New))
throw new NotSupportedException($"All generation steps are expected to be new");
if (stepResults.Any(r => !r.Result.IsSuccess))
throw new NotSupportedException($"All generation steps are expected to be succesful");
var meta = new List<TInput>(stepResults.Count);
stepResults.ForEach(r => r.Result.Invoke(meta.Add));
return (generatedSources, meta);
}
return (generatedSources, []);
}
private IReadOnlyList<string> GetGeneratedOutput(GeneratorRunResult result, int requiredCardinality)
{
string attributeNameToRemove = GetAttributeName();
if (result.Diagnostics.Length > 0)
throw new NotSupportedException($"Expected no diagnostics but was {result.Diagnostics.Length}");
if (result.Exception is not null)
throw new NotSupportedException($"Not expected exception: {result.Exception}");
if (result.GeneratedSources.Length < 1)
throw new NotSupportedException("No generated sources");
var generatedSources = result.GeneratedSources
.Where(gen => !gen.HintName.Equals($"{attributeNameToRemove}.g.cs"))
.Select(gen => gen.SourceText.ToString())
.ToList();
if (generatedSources.Count != requiredCardinality)
throw new NotSupportedException($"Expected cardinality of generated sources is {requiredCardinality} but was {generatedSources.Count}");
return generatedSources;
}
}