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
|
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace I2R.Endpoints;
[Generator]
public class EndpointGenerator : ISourceGenerator
{
private const string SourceGenereatedComment = "// Generated, probably smart to leave it be";
public void Initialize(GeneratorInitializationContext context) {
context.RegisterForSyntaxNotifications(() => new EndpointFinder());
}
private Diagnostic CreateDebugDiagnostic(string message) {
var descriptor = new DiagnosticDescriptor("debug", "Debug", message, "debug", DiagnosticSeverity.Warning, true);
return Diagnostic.Create(descriptor, null, "");
}
public class NeededInfo
{
public string BaseEndpointClassName { get; set; }
public string BaseEnpointNamespaceName { get; set; }
}
private NeededInfo ExtractNeededInfo(ClassDeclarationSyntax classDeclarationSyntax) {
var baseType = classDeclarationSyntax.BaseList?.Types.FirstOrDefault();
if (baseType == default) {
return default;
}
var fullTypeName = baseType?.Type.ToString();
var typeNamespace = GetNamespace(baseType);
var className = Regex.Match(fullTypeName, "(!?<).+?(?=>)").Value.Replace("<", "");
return new NeededInfo() {
BaseEndpointClassName = className,
BaseEnpointNamespaceName = typeNamespace
};
}
public void Execute(GeneratorExecutionContext context) {
var asyncEndpoints = ((EndpointFinder) context.SyntaxReceiver)?.AsyncEndpoints;
var syncEndpoints = ((EndpointFinder) context.SyntaxReceiver)?.SyncEndpoints;
if (asyncEndpoints == null) {
context.ReportDiagnostic(CreateDebugDiagnostic("no endpoints were found"));
return;
}
foreach (var endpoint in syncEndpoints) {
var info = ExtractNeededInfo(endpoint);
context.AddSource(info.BaseEndpointClassName + "s.g.cs", GetSyncSource(info.BaseEndpointClassName, info.BaseEnpointNamespaceName));
}
foreach (var endpoint in asyncEndpoints) {
var info = ExtractNeededInfo(endpoint);
context.AddSource(info.BaseEndpointClassName + "s.g.cs", GetAsyncSource(info.BaseEndpointClassName, info.BaseEnpointNamespaceName));
}
}
private string GetSyncSource(string className, string namespaceName) {
return $@"
{SourceGenereatedComment}
namespace {namespaceName};
public static partial class SyncEndpoint<T{className}>
{{
public static class Req<TRequest>
{{
public abstract class Res<TResponse> : {className}
{{
public abstract TResponse Handle(
TRequest request
);
}}
public abstract class NoRes : {className}
{{
public abstract void Handle(
TRequest request
);
}}
}}
public static class NoReq
{{
public abstract class Res<TResponse> : {className}
{{
public abstract TResponse Handle();
}}
public abstract class NoRes : {className}
{{
public abstract void Handle();
}}
}}
}}
";
}
private string GetAsyncSource(string className, string namespaceName) {
return $@"
{SourceGenereatedComment}
namespace {namespaceName};
public static partial class AsyncEndpoint<T>
{{
public static class Req<TRequest>
{{
public abstract class Res<TResponse> : {className}
{{
public abstract Task<TResponse> HandleAsync(
TRequest request,
CancellationToken cancellationToken = default
);
}}
public abstract class NoRes : {className}
{{
public abstract Task HandleAsync(
TRequest request,
CancellationToken cancellationToken = default
);
}}
}}
public static class NoReq
{{
public abstract class Res<TResponse> : {className}
{{
public abstract Task<TResponse> HandleAsync(
CancellationToken cancellationToken = default
);
}}
public abstract class NoRes : {className}
{{
public abstract Task HandleAsync(
CancellationToken cancellationToken = default
);
}}
}}
}}
";
}
// determine the namespace the class/enum/struct is declared in, if any
static string GetNamespace(BaseTypeSyntax syntax) {
// If we don't have a namespace at all we'll return an empty string
// This accounts for the "default namespace" case
var nameSpace = string.Empty;
// Get the containing syntax node for the type declaration
// (could be a nested type, for example)
var potentialNamespaceParent = syntax.Parent;
// Keep moving "out" of nested classes etc until we get to a namespace
// or until we run out of parents
while (potentialNamespaceParent != null &&
potentialNamespaceParent is not NamespaceDeclarationSyntax
&& potentialNamespaceParent is not FileScopedNamespaceDeclarationSyntax) {
potentialNamespaceParent = potentialNamespaceParent.Parent;
}
// Build up the final namespace by looping until we no longer have a namespace declaration
if (potentialNamespaceParent is BaseNamespaceDeclarationSyntax namespaceParent) {
// We have a namespace. Use that as the type
nameSpace = namespaceParent.Name.ToString();
// Keep moving "out" of the namespace declarations until we
// run out of nested namespace declarations
while (true) {
if (namespaceParent.Parent is not NamespaceDeclarationSyntax parent) {
break;
}
// Add the outer namespace as a prefix to the final namespace
nameSpace = $"{namespaceParent.Name}.{nameSpace}";
namespaceParent = parent;
}
}
// return the final namespace
return nameSpace;
}
}
|