|
1 | 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved.
|
2 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
3 | 3 |
|
| 4 | +using System.CommandLine.Generator.Invocations; |
4 | 5 | using System.Linq;
|
5 | 6 | using System.Text;
|
6 | 7 | using Microsoft.CodeAnalysis;
|
@@ -45,109 +46,144 @@ internal static class GeneratedCommandHandlers
|
45 | 46 |
|
46 | 47 | foreach (var invocation in rx.Invocations)
|
47 | 48 | {
|
48 |
| - var methodParameters = invocation.Parameters |
49 |
| - .Select(x => x.GetMethodParameter()) |
50 |
| - .Where(x => !string.IsNullOrWhiteSpace(x.Name)) |
51 |
| - .ToArray(); |
52 |
| - |
53 |
| - builder.Append( |
54 |
| - @$" |
55 |
| - public static void SetHandler<{string.Join(", ", Enumerable.Range(1, invocation.NumberOfGenerericParameters).Select(x => $@"T{x}"))}>( |
56 |
| - this Command command,"); |
57 |
| - builder.Append($@" |
58 |
| - {invocation.DelegateType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)} method"); |
| 49 | + var methodParameters = GetMethodParameters(invocation); |
59 | 50 |
|
60 |
| - if (methodParameters.Length > 0) |
61 |
| - { |
62 |
| - builder.Append(","); |
63 |
| - builder.AppendLine(string.Join(", ", methodParameters.Select(x => $@" |
64 |
| - {x.Type} {x.Name}")) + ")"); |
65 |
| - } |
66 |
| - else |
67 |
| - { |
68 |
| - builder.Append(")"); |
69 |
| - } |
| 51 | + GenerateSetHandler(builder, invocation, methodParameters, handlerCount, true); |
| 52 | + //The non-geric overload is to support C# 10 natural type lambdas |
| 53 | + GenerateSetHandler(builder, invocation, methodParameters, handlerCount, false); |
70 | 54 |
|
71 |
| - builder.Append(@" |
72 |
| - {"); |
73 |
| - builder.Append($@" |
74 |
| - command.Handler = new GeneratedHandler_{handlerCount}(method"); |
| 55 | + GenerateHandlerClass(builder, invocation, methodParameters, handlerCount); |
75 | 56 |
|
76 |
| - if (methodParameters.Length > 0) |
77 |
| - { |
78 |
| - builder.Append(", "); |
79 |
| - builder.Append(string.Join(", ", methodParameters.Select(x => x.Name))); |
80 |
| - } |
| 57 | + //TODO: fully qualify type names |
| 58 | + |
| 59 | + handlerCount++; |
| 60 | + } |
81 | 61 |
|
82 |
| - builder.Append(");"); |
| 62 | + builder.Append(@" |
| 63 | + } |
| 64 | +} |
| 65 | +"); |
83 | 66 |
|
84 |
| - builder.AppendLine(@" |
85 |
| - }"); |
| 67 | + context.AddSource("CommandHandlerGeneratorExtensions_Generated.g.cs", builder.ToString()); |
| 68 | + } |
86 | 69 |
|
87 |
| - //TODO: fully qualify type names |
88 |
| - builder.Append($@" |
| 70 | + private static void GenerateHandlerClass( |
| 71 | + StringBuilder builder, |
| 72 | + DelegateInvocation invocation, |
| 73 | + (string Type, string Name)[] methodParameters, |
| 74 | + int handlerCount) |
| 75 | + { |
| 76 | + builder.Append($@" |
89 | 77 | private class GeneratedHandler_{handlerCount} : {ICommandHandlerType}
|
90 | 78 | {{
|
91 | 79 | public GeneratedHandler_{handlerCount}(
|
92 |
| - {invocation.DelegateType} method"); |
| 80 | + {invocation.DelegateType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)} method"); |
93 | 81 |
|
94 |
| - if (methodParameters.Length > 0) |
95 |
| - { |
96 |
| - builder.Append(","); |
97 |
| - builder.Append(string.Join($", ", methodParameters.Select(x => $@" |
| 82 | + if (methodParameters.Length > 0) |
| 83 | + { |
| 84 | + builder.Append(","); |
| 85 | + builder.Append(string.Join($", ", methodParameters.Select(x => $@" |
98 | 86 | {x.Type} {x.Name}")) + ")");
|
99 |
| - } |
100 |
| - else |
101 |
| - { |
102 |
| - builder.Append(")"); |
103 |
| - } |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + builder.Append(")"); |
| 91 | + } |
104 | 92 |
|
105 |
| - builder.Append($@" |
| 93 | + builder.Append($@" |
106 | 94 | {{
|
107 | 95 | Method = method;");
|
108 |
| - foreach (var propertyAssignment in invocation.Parameters |
109 |
| - .Select(x => x.GetPropertyAssignment()) |
110 |
| - .Where(x => !string.IsNullOrWhiteSpace(x))) |
111 |
| - { |
112 |
| - builder.Append($@" |
| 96 | + foreach (var propertyAssignment in invocation.Parameters |
| 97 | + .Select(x => x.GetPropertyAssignment()) |
| 98 | + .Where(x => !string.IsNullOrWhiteSpace(x))) |
| 99 | + { |
| 100 | + builder.Append($@" |
113 | 101 | {propertyAssignment}");
|
114 |
| - } |
| 102 | + } |
115 | 103 |
|
116 |
| - builder.AppendLine($@" |
| 104 | + builder.AppendLine($@" |
117 | 105 | }}
|
118 | 106 |
|
119 |
| - public {invocation.DelegateType} Method {{ get; }}"); |
| 107 | + public {invocation.DelegateType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)} Method {{ get; }}"); |
120 | 108 |
|
121 |
| - foreach (var propertyDeclaration in invocation.Parameters |
122 |
| - .Select(x => x.GetPropertyDeclaration()) |
123 |
| - .Where(x => !string.IsNullOrWhiteSpace(x))) |
124 |
| - { |
125 |
| - builder.Append($@" |
| 109 | + foreach (var propertyDeclaration in invocation.Parameters |
| 110 | + .Select(x => x.GetPropertyDeclaration()) |
| 111 | + .Where(x => !string.IsNullOrWhiteSpace(x))) |
| 112 | + { |
| 113 | + builder.Append($@" |
126 | 114 | {propertyDeclaration}");
|
127 |
| - } |
| 115 | + } |
128 | 116 |
|
129 |
| - builder.Append($@" |
130 |
| - public async Task<int> InvokeAsync(InvocationContext context) |
| 117 | + builder.Append($@" |
| 118 | + public async global::System.Threading.Tasks.Task<int> InvokeAsync(global::System.CommandLine.Invocation.InvocationContext context) |
131 | 119 | {{");
|
132 |
| - builder.Append($@" |
| 120 | + builder.Append($@" |
133 | 121 | {invocation.InvokeContents()}");
|
134 |
| - builder.Append($@" |
| 122 | + builder.Append($@" |
135 | 123 | }}
|
136 | 124 | }}");
|
137 |
| - handlerCount++; |
| 125 | + } |
| 126 | + |
| 127 | + private static (string Type, string Name)[] GetMethodParameters(DelegateInvocation invocation) |
| 128 | + { |
| 129 | + return invocation.Parameters |
| 130 | + .Select(x => x.GetMethodParameter()) |
| 131 | + .Where(x => !string.IsNullOrWhiteSpace(x.Name)) |
| 132 | + .ToArray(); |
| 133 | + } |
| 134 | + |
| 135 | + private static void GenerateSetHandler( |
| 136 | + StringBuilder builder, |
| 137 | + DelegateInvocation invocation, |
| 138 | + (string Type, string Name)[] methodParameters, |
| 139 | + int handlerCount, |
| 140 | + bool isGeneric) |
| 141 | + { |
| 142 | + builder.Append( |
| 143 | + @$" |
| 144 | + public static void SetHandler"); |
| 145 | + |
| 146 | + if (isGeneric) |
| 147 | + { |
| 148 | + builder.Append($"<{string.Join(", ", Enumerable.Range(1, invocation.NumberOfGenerericParameters).Select(x => $@"T{x}"))}>"); |
| 149 | + } |
| 150 | + builder.Append(@$"( |
| 151 | + this global::System.CommandLine.Command command,"); |
| 152 | + |
| 153 | + builder.Append($@" |
| 154 | + {invocation.DelegateType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)} method"); |
| 155 | + |
| 156 | + if (methodParameters.Length > 0) |
| 157 | + { |
| 158 | + builder.Append(","); |
| 159 | + builder.AppendLine(string.Join(", ", methodParameters.Select(x => $@" |
| 160 | + {x.Type} {x.Name}")) + ")"); |
| 161 | + } |
| 162 | + else |
| 163 | + { |
| 164 | + builder.Append(")"); |
138 | 165 | }
|
139 | 166 |
|
140 | 167 | builder.Append(@"
|
141 |
| - } |
142 |
| -} |
143 |
| -"); |
| 168 | + {"); |
| 169 | + builder.Append($@" |
| 170 | + command.Handler = new GeneratedHandler_{handlerCount}(method"); |
144 | 171 |
|
145 |
| - context.AddSource("CommandHandlerGeneratorExtensions_Generated.g.cs", builder.ToString()); |
| 172 | + if (methodParameters.Length > 0) |
| 173 | + { |
| 174 | + builder.Append(", "); |
| 175 | + builder.Append(string.Join(", ", methodParameters.Select(x => x.Name))); |
| 176 | + } |
| 177 | + |
| 178 | + builder.Append(");"); |
| 179 | + |
| 180 | + builder.AppendLine(@" |
| 181 | + }"); |
146 | 182 | }
|
147 | 183 |
|
148 | 184 | public void Initialize(GeneratorInitializationContext context)
|
149 | 185 | {
|
150 |
| - // Debugger.Launch(); |
| 186 | + //System.Diagnostics.Debugger.Launch(); |
151 | 187 |
|
152 | 188 | context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
|
153 | 189 | }
|
|
0 commit comments