-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathManagedCrdtServiceGenerator.cs
211 lines (182 loc) · 9.76 KB
/
ManagedCrdtServiceGenerator.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
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using Nyris.Crdt.Distributed.SourceGenerators.Model;
using Scriban;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
namespace Nyris.Crdt.Distributed.SourceGenerators
{
/// <summary>
/// Some useful resources:
/// - https://www.meziantou.net/working-with-types-in-a-roslyn-analyzer.htm
/// </summary>
[Generator]
public class ManagedCrdtServiceGenerator : IIncrementalGenerator
{
// TODO: currently it does not seem possible to reference another project from source generator project, fix when possible
private const string ManagedCrdtTypeName = "ManagedCRDT";
private const string PartiallyReplicatedCrdtRegistryTypeName = "PartiallyReplicatedCRDTRegistry";
private static Lazy<IEnumerable<(Template, string)>> Templates { get; } = new(EnumerateTemplates);
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var classDeclarationsProvider =
context.SyntaxProvider.CreateSyntaxProvider((node, _) => node is ClassDeclarationSyntax { BaseList: { } },
(syntaxContext, _) => (ClassDeclarationSyntax) syntaxContext.Node);
var recordDeclarationsProvider =
context.SyntaxProvider.CreateSyntaxProvider((node, _) => node is RecordDeclarationSyntax { BaseList: { } },
(syntaxContext, _) => (RecordDeclarationSyntax) syntaxContext.Node);
var classAndCompilationProvider =
context.CompilationProvider.Combine(classDeclarationsProvider.Collect().Combine(recordDeclarationsProvider.Collect()));
context.RegisterSourceOutput(classAndCompilationProvider, Execute);
}
private void Execute(
SourceProductionContext context,
(Compilation compilation, (ImmutableArray<ClassDeclarationSyntax> syntaxClasses, ImmutableArray<RecordDeclarationSyntax>
syntaxRecords) syntaxes)
syntaxTuple
)
{
var (compilation, syntaxes) = syntaxTuple;
var (syntaxClasses, syntaxRecords) = syntaxes;
AnalyzeCandidatesForManagedCrdts(compilation,
syntaxClasses,
syntaxRecords,
out var crdtInfos,
out var operationInfos);
foreach (var (template, templateFileName) in Templates.Value)
{
var text = template.Render(new
{
DtoInfos = crdtInfos
.GroupBy(i => i.DtoTypeName)
.Select(group => new DtoInfo(group.Key,
group.Select(i => new TypeWithArguments(i.CrdtTypeName, i.AllArgumentsString))
.ToList()))
.ToList(),
OperationInfos = operationInfos
}, member => member.Name);
var source = SourceText.From(text, Encoding.UTF8);
context.AddSource(templateFileName.Replace("Template.sbntxt", ".generated.cs"), source);
}
}
private static IEnumerable<(Template, string)> EnumerateTemplates() => new[]
{
"ManagedCrdtServiceTemplate.sbntxt",
"IManagedCrdtServiceTemplate.sbntxt",
"ServiceCollectionExtensionsTemplate.sbntxt"
}.Select(templateFileName => (Template.Parse(EmbeddedResource.GetContent(templateFileName), templateFileName),
templateFileName));
private void AnalyzeCandidatesForManagedCrdts(
Compilation compilation,
IEnumerable<ClassDeclarationSyntax> candidates,
// NOTE: Should eventually support Records for CRDT Contaxes
IEnumerable<RecordDeclarationSyntax> operationCandidates,
out List<CrdtInfo> crdtInfos,
out HashSet<RoutedOperationInfo> operationInfos
)
{
crdtInfos = new List<CrdtInfo>();
operationInfos = new HashSet<RoutedOperationInfo>();
// process predefined internal crdts
var nodeSet = compilation.GetTypeByMetadataName("Nyris.Crdt.Distributed.Crdts.NodeSet");
if (nodeSet is null || !TryGetCrdtInfo(nodeSet, out var crdtInfo, out _))
{
throw new MissingMemberException(
"Something went wrong - could not get crdtInfo of a known class Nyris.Crdt.Distributed.Crdts.NodeSet");
}
if (crdtInfo is not null)
{
crdtInfos.Add(crdtInfo);
}
// process user-defined crdts
foreach (var candidateClass in candidates)
{
// _log.AppendLine("Analyzing class: " + candidateClass.Identifier.ToFullString());
var namedTypeSymbol = compilation.GetSemanticModel(candidateClass.SyntaxTree)
.GetDeclaredSymbol(candidateClass);
if (namedTypeSymbol == null)
{
// _log.AppendLine("Something went wrong - semantic model did not produce an INamedTypeSymbol");
continue;
}
if (namedTypeSymbol.IsGenericType)
{
// _log.AppendLine("Class is generic - skipping");
continue;
}
if (!TryGetCrdtInfo(namedTypeSymbol, out crdtInfo, out var operations)) continue;
foreach (var operation in operations)
{
operationInfos.Add(operation);
}
if (crdtInfo is not null)
{
crdtInfos.Add(crdtInfo);
}
}
}
/// <summary>
/// Checks symbols inheritance chain and return required info if it is a descendent of ManagedCrdt
/// </summary>
/// <param name="symbol"></param>
/// <param name="crdtInfo"></param>
/// <param name="operations"></param>
/// <returns>True if symbol is a managedCrdt, false otherwise</returns>
private static bool TryGetCrdtInfo(
ITypeSymbol symbol,
out CrdtInfo? crdtInfo,
out ImmutableArray<RoutedOperationInfo> operations
)
{
var current = symbol.BaseType;
var operationInfos = new List<RoutedOperationInfo>();
while (current != null && current.ToDisplayString() != "object")
{
if (current.Name == PartiallyReplicatedCrdtRegistryTypeName)
{
var keyType = current.TypeArguments[0];
// _log.AppendLine(
// $"Class {symbol.Name} determined to be a {PartiallyReplicatedCRDTRegistryTypeName}. " +
// "Generated gRPC service will include methods for applying its operations.");
var crdtTypeParams = string.Join(", ", current.TypeArguments.Select(s => s.ToDisplayString()));
// get attributes of symbol
// get type arguments of constructor
operationInfos = symbol.GetAttributes()
.Where(ad => ad.AttributeClass?.Name == "RequireOperationAttribute")
.Select(attr =>
{
var operationConcreteType = attr.ConstructorArguments[0].Value as INamedTypeSymbol;
var operationResponseConcreteType = attr.ConstructorArguments[1].Value as INamedTypeSymbol;
return new RoutedOperationInfo(operationConcreteType?.ToDisplayString(),
operationResponseConcreteType?.ToDisplayString(),
keyType.ToDisplayString(),
$"{symbol.ToDisplayString()}, {crdtTypeParams}");
}).ToList();
}
if (current.Name == ManagedCrdtTypeName)
{
// _log.AppendLine($"Class {symbol.Name} determined to be a ManagedCRDT. " +
// "Generated gRPC service will include transport operations for it's dto");
var allArgumentsString = string.Join(", ",
current.TypeArguments.Select(typeSymbol => typeSymbol.ToDisplayString()));
var dtoString = current.TypeArguments.Last().ToDisplayString();
crdtInfo = new CrdtInfo(
CrdtTypeName: symbol.ToDisplayString(),
AllArgumentsString: allArgumentsString,
DtoTypeName: dtoString);
operations = operationInfos.ToImmutableArray();
return true;
}
current = current.BaseType;
}
crdtInfo = null;
operations = operationInfos.ToImmutableArray();
return false;
}
}
}