-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySourceGenerator.cs
96 lines (94 loc) · 3.83 KB
/
MySourceGenerator.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
using Microsoft.CodeAnalysis;
using System;
using System.Text;
namespace GenerateHelperLibraries;
[Generator]
public class MySourceGenerator : ISourceGenerator
{
private void AppendToBuilder(ref StringBuilder builder, SyntaxTree tree)
{
string text = tree.ToString();
string fileName = tree.GetFileNameForCopy();
string spaces = " ";
string content = text.GetCSharpString();
content = content.RemoveAttribute("IncludeCode");
content = content.RemoveAttribute("IgnoreCode");
string others = "#nullable enable";
if (content.StartsWith(others) == false)
{
content = $"{others}{Environment.NewLine}{content}";
}
builder.AppendLine($@"{spaces}text = @""{content}"";");
builder.AppendLine($@"{spaces}context.AddSource(""{fileName}"", text);");
}
public void Execute(GeneratorExecutionContext context)
{
Compilation compilation = context.Compilation;
bool includeglobal = compilation.DidIncludeCodeAtLeastOnce();
StringBuilder builder = new();
builder.AppendLine("global using SourceCodeHelpers.Utilities;");
builder.AppendLine("using Microsoft.CodeAnalysis;");
builder.AppendLine("namespace SourceCodeHelpers.Utilities;");
builder.AppendLine("internal static class BuilderExtensions");
builder.AppendLine("{");
builder.AppendLine(" internal static void BuildSourceCode(this IAddSource context)"); //a breaking change. so this can be supported from incremental source generators which means increasing version (because of breaking change)
builder.AppendLine(" {");
builder.AppendLine(" string text;");
bool hadOne = false;
foreach (var item in compilation.SyntaxTrees)
{
if (item.ToString().Contains("namespace ") == false)
{
continue; //because there was none.
}
if (item.ToString().Contains("IIncrementalGenerator"))
{
continue; //you cannot generate source code for itself obviously //has to be t
}
if (item.ToString().Contains("ISourceGenerator"))
{
continue; //2 situations. this means you can now support the new iincrementalgenerator.
}
if (item.ToString().Contains("ISyntaxReceiver"))
{
continue; //you cannot generate source code for syntax receivers
}
bool includSingle = item.DidIncludeCode();
bool ignoreSingle = item.DidIgnoreCode();
if (ignoreSingle && includSingle)
{
string error = "Cannot include and ignore code at the same time";
//not reporting errors (has to test that next).
context.ReportError(error, "IgnoreIncludeConflict");
return;
}
if (item.DidIgnoreCode() && includeglobal == true)
{
string error = "Unable to generate source code because you ignored code even though you manually marked some to include";
context.ReportError(error, "IgnoreIncludeConflict");
return;
}
if (ignoreSingle)
{
continue; //because its being ignored.
}
if (includeglobal == true && includSingle == false)
{
continue;
}
hadOne = true;
AppendToBuilder(ref builder, item);
}
if (hadOne == false)
{
return;
}
builder.AppendLine(" }");
builder.AppendLine("}");
string results = builder.ToString();
context.AddSource("importlibrary.g", results);
}
public void Initialize(GeneratorInitializationContext context)
{
}
}