|
5 | 5 | namespace EasyNetQ.Management.Client.ExtensionsGenerator;
|
6 | 6 |
|
7 | 7 | [Generator]
|
8 |
| -public class ExtensionsGenerator : ISourceGenerator |
| 8 | +public class ExtensionsGenerator : IIncrementalGenerator |
9 | 9 | {
|
10 |
| - public void Execute(GeneratorExecutionContext context) |
| 10 | + public void Initialize(IncrementalGeneratorInitializationContext context) |
11 | 11 | {
|
12 |
| - var compilation = context.Compilation; |
| 12 | + // if (!System.Diagnostics.Debugger.IsAttached) |
| 13 | + // { |
| 14 | + // System.Diagnostics.Debugger.Launch(); |
| 15 | + // } |
13 | 16 |
|
14 |
| - string typeName = "EasyNetQ.Management.Client.IManagementClient"; |
15 |
| - string typeExtensionsName = $"{typeName}Extensions"; |
| 17 | + context.RegisterSourceOutput(context.CompilationProvider, (context, compilation) => |
| 18 | + { |
| 19 | + string typeName = "EasyNetQ.Management.Client.IManagementClient"; |
| 20 | + string typeExtensionsName = $"{typeName}Extensions"; |
16 | 21 |
|
17 |
| - var typeNames = new List<string> { typeName, typeExtensionsName }; |
18 |
| - var types = typeNames.Select(tn => compilation.GetTypeByMetadataName(tn) ?? throw new KeyNotFoundException(tn)); |
| 22 | + var typeNames = new List<string> { typeName, typeExtensionsName }; |
| 23 | + var types = typeNames.Select(tn => compilation.GetTypeByMetadataName(tn) ?? throw new KeyNotFoundException(tn)); |
19 | 24 |
|
20 |
| - Dictionary<string, CompilationUnitSyntax> compilationUnits = new(); |
| 25 | + Dictionary<string, CompilationUnitSyntax> compilationUnits = new(); |
21 | 26 |
|
22 |
| - var thisParameter = "this IManagementClient client".GetParameterSyntax(); |
23 |
| - QualifiedNameSyntax extensionsClassName = (SyntaxFactory.ParseTypeName(typeExtensionsName) as QualifiedNameSyntax)!; |
| 27 | + var thisParameter = "this IManagementClient client".GetParameterSyntax(); |
| 28 | + QualifiedNameSyntax extensionsClassName = (SyntaxFactory.ParseTypeName(typeExtensionsName) as QualifiedNameSyntax)!; |
24 | 29 |
|
25 |
| - { |
26 |
| - var fileScopedNamespaceDeclaration = SyntaxFactory.FileScopedNamespaceDeclaration(extensionsClassName.Left) |
27 |
| - .WithLeadingTrivia(SyntaxFactory.Trivia(SyntaxFactory.NullableDirectiveTrivia(SyntaxFactory.Token(SyntaxKind.EnableKeyword), true))); |
28 |
| - |
29 |
| - foreach (var t in types) |
30 | 30 | {
|
31 |
| - fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
32 |
| - .AddReplacementExtensionsClass(extensionsClassName, t, thisParameter, |
33 |
| - ("string vhostName", "Vhost vhost", "vhost.Name") |
34 |
| - ); |
35 |
| - fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
36 |
| - .AddReplacementExtensionsClass(extensionsClassName, t, thisParameter, |
37 |
| - ("string vhostName", null, "exchange.Vhost"), |
38 |
| - ("string exchangeName", "ExchangeName exchange", "exchange.Name") |
39 |
| - ); |
40 |
| - fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
41 |
| - .AddReplacementExtensionsClass(extensionsClassName, t, thisParameter, |
42 |
| - ("string vhostName", null, "queue.Vhost"), |
43 |
| - ("string queueName", "QueueName queue", "queue.Name") |
44 |
| - ); |
45 |
| - fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
46 |
| - .AddReplacementExtensionsClass(extensionsClassName, t, thisParameter, |
47 |
| - ("string vhostName", null, "exchange.Vhost"), |
48 |
| - ("string exchangeName", "ExchangeName exchange", "exchange.Name"), |
49 |
| - ("string queueName", "QueueName queue", "queue.Name") |
50 |
| - ); |
51 |
| - fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
52 |
| - .AddReplacementExtensionsClass(extensionsClassName, t, thisParameter, |
53 |
| - ("string vhostName", null, "sourceExchange.Vhost"), |
54 |
| - ("string sourceExchangeName", "ExchangeName sourceExchange", "sourceExchange.Name"), |
55 |
| - ("string destinationExchangeName", "ExchangeName destinationExchange", "destinationExchange.Name") |
56 |
| - ); |
57 |
| - fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
58 |
| - .AddReplacementExtensionsClass(extensionsClassName, t, thisParameter, |
59 |
| - ("string connectionName", "Connection connection", "connection.Name") |
60 |
| - ); |
61 |
| - fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
62 |
| - .AddReplacementExtensionsClass(extensionsClassName, t, thisParameter, |
63 |
| - ("string userName", "User user", "user.Name") |
64 |
| - ); |
65 |
| - fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
66 |
| - .AddReplacementExtensionsClass(extensionsClassName, t, thisParameter, |
67 |
| - ("string vhostName", "Vhost vhost", "vhost.Name"), |
68 |
| - ("string userName", "User user", "user.Name") |
69 |
| - ); |
70 |
| - fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
71 |
| - .AddReplacementExtensionsClass(extensionsClassName, t, thisParameter, |
72 |
| - ("string componentName", null, "parameter.Component"), |
73 |
| - ("string vhostName", null, "parameter.Vhost"), |
74 |
| - ("string parameterName", "Parameter parameter", "parameter.Name") |
75 |
| - ); |
76 |
| - fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
77 |
| - .AddReplacementExtensionsClass(extensionsClassName, t, thisParameter, |
78 |
| - ("string componentName", null, "parameter.Component"), |
79 |
| - ("string vhostName", null, "parameter.Vhost"), |
80 |
| - ("string parameterName", null, "parameter.Name"), |
81 |
| - ("object parameterValue", "Parameter parameter", "parameter.Value") |
82 |
| - ); |
| 31 | + var fileScopedNamespaceDeclaration = SyntaxFactory.FileScopedNamespaceDeclaration(extensionsClassName.Left) |
| 32 | + .WithLeadingTrivia(SyntaxFactory.Trivia(SyntaxFactory.NullableDirectiveTrivia(SyntaxFactory.Token(SyntaxKind.EnableKeyword), true))); |
| 33 | + |
| 34 | + foreach (var t in types) |
| 35 | + { |
| 36 | + fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
| 37 | + .AddReplacementExtensionsClass(extensionsClassName, t, thisParameter, |
| 38 | + ("string vhostName", "Vhost vhost", "vhost.Name") |
| 39 | + ); |
| 40 | + fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
| 41 | + .AddReplacementExtensionsClass(extensionsClassName, t, thisParameter, |
| 42 | + ("string vhostName", null, "exchange.Vhost"), |
| 43 | + ("string exchangeName", "ExchangeName exchange", "exchange.Name") |
| 44 | + ); |
| 45 | + fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
| 46 | + .AddReplacementExtensionsClass(extensionsClassName, t, thisParameter, |
| 47 | + ("string vhostName", null, "queue.Vhost"), |
| 48 | + ("string queueName", "QueueName queue", "queue.Name") |
| 49 | + ); |
| 50 | + fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
| 51 | + .AddReplacementExtensionsClass(extensionsClassName, t, thisParameter, |
| 52 | + ("string vhostName", null, "exchange.Vhost"), |
| 53 | + ("string exchangeName", "ExchangeName exchange", "exchange.Name"), |
| 54 | + ("string queueName", "QueueName queue", "queue.Name") |
| 55 | + ); |
| 56 | + fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
| 57 | + .AddReplacementExtensionsClass(extensionsClassName, t, thisParameter, |
| 58 | + ("string vhostName", null, "sourceExchange.Vhost"), |
| 59 | + ("string sourceExchangeName", "ExchangeName sourceExchange", "sourceExchange.Name"), |
| 60 | + ("string destinationExchangeName", "ExchangeName destinationExchange", "destinationExchange.Name") |
| 61 | + ); |
| 62 | + fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
| 63 | + .AddReplacementExtensionsClass(extensionsClassName, t, thisParameter, |
| 64 | + ("string connectionName", "Connection connection", "connection.Name") |
| 65 | + ); |
| 66 | + fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
| 67 | + .AddReplacementExtensionsClass(extensionsClassName, t, thisParameter, |
| 68 | + ("string userName", "User user", "user.Name") |
| 69 | + ); |
| 70 | + fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
| 71 | + .AddReplacementExtensionsClass(extensionsClassName, t, thisParameter, |
| 72 | + ("string vhostName", "Vhost vhost", "vhost.Name"), |
| 73 | + ("string userName", "User user", "user.Name") |
| 74 | + ); |
| 75 | + fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
| 76 | + .AddReplacementExtensionsClass(extensionsClassName, t, thisParameter, |
| 77 | + ("string componentName", null, "parameter.Component"), |
| 78 | + ("string vhostName", null, "parameter.Vhost"), |
| 79 | + ("string parameterName", "Parameter parameter", "parameter.Name") |
| 80 | + ); |
| 81 | + fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
| 82 | + .AddReplacementExtensionsClass(extensionsClassName, t, thisParameter, |
| 83 | + ("string componentName", null, "parameter.Component"), |
| 84 | + ("string vhostName", null, "parameter.Vhost"), |
| 85 | + ("string parameterName", null, "parameter.Name"), |
| 86 | + ("object parameterValue", "Parameter parameter", "parameter.Value") |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + var compilationUnit = SyntaxFactory.CompilationUnit() |
| 91 | + .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("EasyNetQ.Management.Client.Model"))) |
| 92 | + .AddMembers(fileScopedNamespaceDeclaration) |
| 93 | + .NormalizeWhitespace(eol: "\n"); |
| 94 | + |
| 95 | + compilationUnits[$"{extensionsClassName.Right}Replacement"] = compilationUnit; |
| 96 | + |
| 97 | + compilation = compilation.AddSyntaxTrees(compilationUnit.SyntaxTree); |
83 | 98 | }
|
84 | 99 |
|
85 |
| - var compilationUnit = SyntaxFactory.CompilationUnit() |
86 |
| - .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("EasyNetQ.Management.Client.Model"))) |
87 |
| - .AddMembers(fileScopedNamespaceDeclaration) |
88 |
| - .NormalizeWhitespace(eol: "\n"); |
| 100 | + { |
| 101 | + var fileScopedNamespaceDeclaration = SyntaxFactory.FileScopedNamespaceDeclaration(extensionsClassName.Left) |
| 102 | + .WithLeadingTrivia(SyntaxFactory.Trivia(SyntaxFactory.NullableDirectiveTrivia(SyntaxFactory.Token(SyntaxKind.EnableKeyword), true))); |
89 | 103 |
|
90 |
| - compilationUnits[$"{extensionsClassName.Right}Replacement"] = compilationUnit; |
| 104 | + foreach (var t in types) |
| 105 | + { |
| 106 | + fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
| 107 | + .AddSyncExtensionsClass(extensionsClassName, t, thisParameter); |
| 108 | + } |
91 | 109 |
|
92 |
| - compilation = compilation.AddSyntaxTrees(compilationUnit.SyntaxTree); |
93 |
| - } |
| 110 | + var compilationUnit = SyntaxFactory.CompilationUnit() |
| 111 | + .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("EasyNetQ.Management.Client.Model"))) |
| 112 | + .AddMembers(fileScopedNamespaceDeclaration) |
| 113 | + .NormalizeWhitespace(eol: "\n"); |
94 | 114 |
|
95 |
| - { |
96 |
| - var fileScopedNamespaceDeclaration = SyntaxFactory.FileScopedNamespaceDeclaration(extensionsClassName.Left) |
97 |
| - .WithLeadingTrivia(SyntaxFactory.Trivia(SyntaxFactory.NullableDirectiveTrivia(SyntaxFactory.Token(SyntaxKind.EnableKeyword), true))); |
| 115 | + compilationUnits[$"{extensionsClassName.Right}Sync"] = compilationUnit; |
| 116 | + } |
98 | 117 |
|
99 |
| - foreach (var t in types) |
| 118 | + foreach (var kvpair in compilationUnits) |
100 | 119 | {
|
101 |
| - fileScopedNamespaceDeclaration = fileScopedNamespaceDeclaration |
102 |
| - .AddSyncExtensionsClass(extensionsClassName, t, thisParameter); |
| 120 | + context.AddSource($"{kvpair.Key}.g.cs", kvpair.Value.ToString()); |
103 | 121 | }
|
104 |
| - |
105 |
| - var compilationUnit = SyntaxFactory.CompilationUnit() |
106 |
| - .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("EasyNetQ.Management.Client.Model"))) |
107 |
| - .AddMembers(fileScopedNamespaceDeclaration) |
108 |
| - .NormalizeWhitespace(eol: "\n"); |
109 |
| - |
110 |
| - compilationUnits[$"{extensionsClassName.Right}Sync"] = compilationUnit; |
111 |
| - } |
112 |
| - |
113 |
| - foreach (var kvpair in compilationUnits) |
114 |
| - { |
115 |
| - context.AddSource($"{kvpair.Key}.g.cs", kvpair.Value.ToString()); |
116 |
| - } |
117 |
| - } |
118 |
| - |
119 |
| - public void Initialize(GeneratorInitializationContext context) |
120 |
| - { |
121 |
| - // No initialization required for this one |
122 |
| - |
123 |
| - // if (!System.Diagnostics.Debugger.IsAttached) |
124 |
| - // { |
125 |
| - // System.Diagnostics.Debugger.Launch(); |
126 |
| - // } |
| 122 | + }); |
127 | 123 | }
|
128 | 124 | }
|
0 commit comments