Skip to content

Commit 49de0fd

Browse files
authored
Source generators for command handlers (#1362)
1 parent c6263a4 commit 49de0fd

33 files changed

+1490
-63
lines changed

System.CommandLine.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.CommandLine.Hosting.
5858
EndProject
5959
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HostingPlayground", "samples\HostingPlayground\HostingPlayground.csproj", "{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}"
6060
EndProject
61+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.CommandLine.CommandGenerator", "src\System.CommandLine.CommandGenerator\System.CommandLine.CommandGenerator.csproj", "{B0D00128-E41B-4648-9D22-9B91F8F6BF0C}"
62+
EndProject
6163
Global
6264
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6365
Debug|Any CPU = Debug|Any CPU
@@ -236,6 +238,18 @@ Global
236238
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|x64.Build.0 = Release|Any CPU
237239
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|x86.ActiveCfg = Release|Any CPU
238240
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|x86.Build.0 = Release|Any CPU
241+
{B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
242+
{B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Debug|Any CPU.Build.0 = Debug|Any CPU
243+
{B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Debug|x64.ActiveCfg = Debug|Any CPU
244+
{B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Debug|x64.Build.0 = Debug|Any CPU
245+
{B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Debug|x86.ActiveCfg = Debug|Any CPU
246+
{B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Debug|x86.Build.0 = Debug|Any CPU
247+
{B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Release|Any CPU.ActiveCfg = Release|Any CPU
248+
{B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Release|Any CPU.Build.0 = Release|Any CPU
249+
{B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Release|x64.ActiveCfg = Release|Any CPU
250+
{B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Release|x64.Build.0 = Release|Any CPU
251+
{B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Release|x86.ActiveCfg = Release|Any CPU
252+
{B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Release|x86.Build.0 = Release|Any CPU
239253
EndGlobalSection
240254
GlobalSection(SolutionProperties) = preSolution
241255
HideSolutionNode = FALSE
@@ -255,6 +269,7 @@ Global
255269
{644C4B4A-4A32-4307-9F71-C3BF901FFB66} = {E5B1EC71-0FC4-4FAA-9C65-32D5016FBC45}
256270
{39483140-BC26-4CAD-BBAE-3DC76C2F16CF} = {E5B1EC71-0FC4-4FAA-9C65-32D5016FBC45}
257271
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906} = {6749FB3E-39DE-4321-A39E-525278E9408D}
272+
{B0D00128-E41B-4648-9D22-9B91F8F6BF0C} = {E5B1EC71-0FC4-4FAA-9C65-32D5016FBC45}
258273
EndGlobalSection
259274
GlobalSection(ExtensibilityGlobals) = postSolution
260275
SolutionGuid = {5C159F93-800B-49E7-9905-EE09F8B8434A}

eng/source-build-patches/0001-Exclude-dotnet-suggest-from-source-build.patch

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ Subject: [PATCH 1/3] Exclude dotnet-suggest from source-build.
66
This is not used by any downstream repos in source-build and adds a
77
bunch of prebuilts, so we can leave this out for now.
88
---
9-
System.CommandLine.sln | 134 ------------------
10-
.../dotnet-suggest.csproj | 1 +
11-
2 files changed, 1 insertion(+), 134 deletions(-)
9+
System.CommandLine.sln | 156 +------------------------------------------------
10+
1 file changed, 3 insertions(+), 153 deletions(-)
1211

1312
diff --git a/System.CommandLine.sln b/System.CommandLine.sln
14-
index ec97010..102aefe 100644
13+
index 7441d0ef..1519e16c 100644
1514
--- a/System.CommandLine.sln
1615
+++ b/System.CommandLine.sln
17-
@@ -30,31 +30,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E5B1EC71-0FC
16+
@@ -30,35 +30,15 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E5B1EC71-0FC
1817
EndProject
1918
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.CommandLine", "src\System.CommandLine\System.CommandLine.csproj", "{0BE8E56E-7580-4526-BE24-D304E1779724}"
2019
EndProject
@@ -43,10 +42,15 @@ index ec97010..102aefe 100644
4342
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.CommandLine.Hosting", "src\System.CommandLine.Hosting\System.CommandLine.Hosting.csproj", "{644C4B4A-4A32-4307-9F71-C3BF901FFB66}"
4443
EndProject
4544
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.CommandLine.Hosting.Tests", "src\System.CommandLine.Hosting.Tests\System.CommandLine.Hosting.Tests.csproj", "{39483140-BC26-4CAD-BBAE-3DC76C2F16CF}"
45+
-EndProject
46+
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HostingPlayground", "samples\HostingPlayground\HostingPlayground.csproj", "{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}"
47+
-EndProject
48+
-Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.CommandLine.CommandGenerator", "src\System.CommandLine.CommandGenerator\System.CommandLine.CommandGenerator.csproj", "{B0D00128-E41B-4648-9D22-9B91F8F6BF0C}"
49+
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HostingPlayground", "samples\HostingPlayground\HostingPlayground.csproj", "{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}"
4650
EndProject
47-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HostingPlayground", "samples\HostingPlayground\HostingPlayground.csproj", "{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}"
48-
EndProject
49-
@@ -78,30 +61,6 @@ Global
51+
Global
52+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
53+
@@ -82,30 +62,6 @@ Global
5054
{0BE8E56E-7580-4526-BE24-D304E1779724}.Release|x64.Build.0 = Release|Any CPU
5155
{0BE8E56E-7580-4526-BE24-D304E1779724}.Release|x86.ActiveCfg = Release|Any CPU
5256
{0BE8E56E-7580-4526-BE24-D304E1779724}.Release|x86.Build.0 = Release|Any CPU
@@ -77,7 +81,7 @@ index ec97010..102aefe 100644
7781
{EEC30462-078F-45EB-AA70-12E3170CD51E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
7882
{EEC30462-078F-45EB-AA70-12E3170CD51E}.Debug|Any CPU.Build.0 = Debug|Any CPU
7983
{EEC30462-078F-45EB-AA70-12E3170CD51E}.Debug|x64.ActiveCfg = Debug|Any CPU
80-
@@ -114,54 +73,6 @@ Global
84+
@@ -118,54 +74,6 @@ Global
8185
{EEC30462-078F-45EB-AA70-12E3170CD51E}.Release|x64.Build.0 = Release|Any CPU
8286
{EEC30462-078F-45EB-AA70-12E3170CD51E}.Release|x86.ActiveCfg = Release|Any CPU
8387
{EEC30462-078F-45EB-AA70-12E3170CD51E}.Release|x86.Build.0 = Release|Any CPU
@@ -132,7 +136,7 @@ index ec97010..102aefe 100644
132136
{27E3BFFC-4412-4E4C-A656-B9D35B8A0F3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
133137
{27E3BFFC-4412-4E4C-A656-B9D35B8A0F3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
134138
{27E3BFFC-4412-4E4C-A656-B9D35B8A0F3E}.Debug|x64.ActiveCfg = Debug|Any CPU
135-
@@ -174,30 +85,6 @@ Global
139+
@@ -178,30 +86,6 @@ Global
136140
{27E3BFFC-4412-4E4C-A656-B9D35B8A0F3E}.Release|x64.Build.0 = Release|Any CPU
137141
{27E3BFFC-4412-4E4C-A656-B9D35B8A0F3E}.Release|x86.ActiveCfg = Release|Any CPU
138142
{27E3BFFC-4412-4E4C-A656-B9D35B8A0F3E}.Release|x86.Build.0 = Release|Any CPU
@@ -163,7 +167,7 @@ index ec97010..102aefe 100644
163167
{644C4B4A-4A32-4307-9F71-C3BF901FFB66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
164168
{644C4B4A-4A32-4307-9F71-C3BF901FFB66}.Debug|Any CPU.Build.0 = Debug|Any CPU
165169
{644C4B4A-4A32-4307-9F71-C3BF901FFB66}.Debug|x64.ActiveCfg = Debug|Any CPU
166-
@@ -210,48 +97,27 @@ Global
170+
@@ -214,18 +98,6 @@ Global
167171
{644C4B4A-4A32-4307-9F71-C3BF901FFB66}.Release|x64.Build.0 = Release|Any CPU
168172
{644C4B4A-4A32-4307-9F71-C3BF901FFB66}.Release|x86.ActiveCfg = Release|Any CPU
169173
{644C4B4A-4A32-4307-9F71-C3BF901FFB66}.Release|x86.Build.0 = Release|Any CPU
@@ -182,15 +186,22 @@ index ec97010..102aefe 100644
182186
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
183187
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Debug|Any CPU.Build.0 = Debug|Any CPU
184188
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Debug|x64.ActiveCfg = Debug|Any CPU
185-
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Debug|x64.Build.0 = Debug|Any CPU
186-
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Debug|x86.ActiveCfg = Debug|Any CPU
187-
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Debug|x86.Build.0 = Debug|Any CPU
188-
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|Any CPU.ActiveCfg = Release|Any CPU
189-
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|Any CPU.Build.0 = Release|Any CPU
190-
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|x64.ActiveCfg = Release|Any CPU
189+
@@ -238,38 +110,16 @@ Global
191190
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|x64.Build.0 = Release|Any CPU
192191
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|x86.ActiveCfg = Release|Any CPU
193192
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|x86.Build.0 = Release|Any CPU
193+
- {B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
194+
- {B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Debug|Any CPU.Build.0 = Debug|Any CPU
195+
- {B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Debug|x64.ActiveCfg = Debug|Any CPU
196+
- {B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Debug|x64.Build.0 = Debug|Any CPU
197+
- {B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Debug|x86.ActiveCfg = Debug|Any CPU
198+
- {B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Debug|x86.Build.0 = Debug|Any CPU
199+
- {B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Release|Any CPU.ActiveCfg = Release|Any CPU
200+
- {B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Release|Any CPU.Build.0 = Release|Any CPU
201+
- {B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Release|x64.ActiveCfg = Release|Any CPU
202+
- {B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Release|x64.Build.0 = Release|Any CPU
203+
- {B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Release|x86.ActiveCfg = Release|Any CPU
204+
- {B0D00128-E41B-4648-9D22-9B91F8F6BF0C}.Release|x86.Build.0 = Release|Any CPU
194205
EndGlobalSection
195206
GlobalSection(SolutionProperties) = preSolution
196207
HideSolutionNode = FALSE
@@ -210,20 +221,7 @@ index ec97010..102aefe 100644
210221
{644C4B4A-4A32-4307-9F71-C3BF901FFB66} = {E5B1EC71-0FC4-4FAA-9C65-32D5016FBC45}
211222
- {39483140-BC26-4CAD-BBAE-3DC76C2F16CF} = {E5B1EC71-0FC4-4FAA-9C65-32D5016FBC45}
212223
{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906} = {6749FB3E-39DE-4321-A39E-525278E9408D}
224+
- {B0D00128-E41B-4648-9D22-9B91F8F6BF0C} = {E5B1EC71-0FC4-4FAA-9C65-32D5016FBC45}
213225
EndGlobalSection
214226
GlobalSection(ExtensibilityGlobals) = postSolution
215-
diff --git a/src/System.CommandLine.Suggest/dotnet-suggest.csproj b/src/System.CommandLine.Suggest/dotnet-suggest.csproj
216-
index f529484..ef9c3bb 100644
217-
--- a/src/System.CommandLine.Suggest/dotnet-suggest.csproj
218-
+++ b/src/System.CommandLine.Suggest/dotnet-suggest.csproj
219-
@@ -13,6 +13,7 @@
220-
<DotnetSuggestBuildNumber Condition="'$(VersionSuffixDateStamp)' != ''">.$(VersionSuffixDateStamp).$(VersionSuffixBuildOfTheDay)</DotnetSuggestBuildNumber>
221-
<VersionPrefix>1.1$(DotnetSuggestBuildNumber)</VersionPrefix>
222-
<VersionSuffix></VersionSuffix>
223-
+ <ExcludeFromSourceBuild>true</ExcludeFromSourceBuild>
224-
</PropertyGroup>
225-
226-
<PropertyGroup>
227-
--
228-
2.18.0
229-
227+
SolutionGuid = {5C159F93-800B-49E7-9905-EE09F8B8434A}

eng/source-build-patches/0003-Update-to-netcoreapp3.1-to-avoid-prebuilts.patch

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,30 @@ rest of source-build: runtime and roslyn.
88

99
Includes a code fix for ref nullability with the new framework.
1010
---
11-
.../System.CommandLine.DragonFruit.csproj | 2 +-
12-
.../System.CommandLine.Hosting.csproj | 2 +-
13-
.../System.CommandLine.Rendering.csproj | 2 +-
14-
src/System.CommandLine/Binding/BindingContext.cs | 2 +-
15-
src/System.CommandLine/System.CommandLine.csproj | 7 +------
16-
5 files changed, 5 insertions(+), 10 deletions(-)
11+
.../System.CommandLine.CommandGenerator.csproj | 4 ++--
12+
.../System.CommandLine.DragonFruit.csproj | 2 +-
13+
src/System.CommandLine.Hosting/System.CommandLine.Hosting.csproj | 2 +-
14+
.../System.CommandLine.Rendering.csproj | 2 +-
15+
src/System.CommandLine/Binding/BindingContext.cs | 2 +-
16+
src/System.CommandLine/System.CommandLine.csproj | 7 +------
17+
6 files changed, 7 insertions(+), 12 deletions(-)
1718

19+
diff --git a/src/System.CommandLine.CommandGenerator/System.CommandLine.CommandGenerator.csproj b/src/System.CommandLine.CommandGenerator/System.CommandLine.CommandGenerator.csproj
20+
index d419906e..8644194a 100644
21+
--- a/src/System.CommandLine.CommandGenerator/System.CommandLine.CommandGenerator.csproj
22+
+++ b/src/System.CommandLine.CommandGenerator/System.CommandLine.CommandGenerator.csproj
23+
@@ -1,7 +1,7 @@
24+
-<Project Sdk="Microsoft.NET.Sdk">
25+
+<Project Sdk="Microsoft.NET.Sdk">
26+
27+
<PropertyGroup>
28+
- <TargetFramework>netstandard2.0</TargetFramework>
29+
+ <TargetFramework>net5.0</TargetFramework>
30+
<Nullable>enable</Nullable>
31+
<LangVersion>9.0</LangVersion>
32+
<IsPackable>true</IsPackable>
1833
diff --git a/src/System.CommandLine.DragonFruit/System.CommandLine.DragonFruit.csproj b/src/System.CommandLine.DragonFruit/System.CommandLine.DragonFruit.csproj
19-
index b3a542fd..06167997 100644
34+
index b3a542fd..0e49d818 100644
2035
--- a/src/System.CommandLine.DragonFruit/System.CommandLine.DragonFruit.csproj
2136
+++ b/src/System.CommandLine.DragonFruit/System.CommandLine.DragonFruit.csproj
2237
@@ -1,7 +1,7 @@
@@ -29,7 +44,7 @@ index b3a542fd..06167997 100644
2944
</Description>
3045
</PropertyGroup>
3146
diff --git a/src/System.CommandLine.Hosting/System.CommandLine.Hosting.csproj b/src/System.CommandLine.Hosting/System.CommandLine.Hosting.csproj
32-
index 1d00cff2..7342c1c5 100644
47+
index bed1b893..913edb72 100644
3348
--- a/src/System.CommandLine.Hosting/System.CommandLine.Hosting.csproj
3449
+++ b/src/System.CommandLine.Hosting/System.CommandLine.Hosting.csproj
3550
@@ -2,7 +2,7 @@
@@ -42,7 +57,7 @@ index 1d00cff2..7342c1c5 100644
4257
<Description>This package provides support for using System.CommandLine with Microsoft.Extensions.Hosting.</Description>
4358
</PropertyGroup>
4459
diff --git a/src/System.CommandLine.Rendering/System.CommandLine.Rendering.csproj b/src/System.CommandLine.Rendering/System.CommandLine.Rendering.csproj
45-
index d552286e..84026ebe 100644
60+
index bd5ce1d7..762481d9 100644
4661
--- a/src/System.CommandLine.Rendering/System.CommandLine.Rendering.csproj
4762
+++ b/src/System.CommandLine.Rendering/System.CommandLine.Rendering.csproj
4863
@@ -2,7 +2,7 @@
@@ -55,10 +70,10 @@ index d552286e..84026ebe 100644
5570
<Description>This package provides support for structured command line output rendering. Write code once that renders correctly in multiple output modes, including System.Console, virtual terminal (using ANSI escape sequences), and plain text.
5671
</Description>
5772
diff --git a/src/System.CommandLine/Binding/BindingContext.cs b/src/System.CommandLine/Binding/BindingContext.cs
58-
index b942ba6a..bbb83891 100644
73+
index a80a4172..305a95ab 100644
5974
--- a/src/System.CommandLine/Binding/BindingContext.cs
6075
+++ b/src/System.CommandLine/Binding/BindingContext.cs
61-
@@ -55,7 +55,7 @@ namespace System.CommandLine.Binding
76+
@@ -56,7 +56,7 @@ namespace System.CommandLine.Binding
6277

6378
public ModelBinder GetModelBinder(IValueDescriptor valueDescriptor)
6479
{
@@ -68,7 +83,7 @@ index b942ba6a..bbb83891 100644
6883
return binder;
6984
}
7085
diff --git a/src/System.CommandLine/System.CommandLine.csproj b/src/System.CommandLine/System.CommandLine.csproj
71-
index aaa2c4a3..5e875a73 100644
86+
index 125e9c78..3a10c14d 100644
7287
--- a/src/System.CommandLine/System.CommandLine.csproj
7388
+++ b/src/System.CommandLine/System.CommandLine.csproj
7489
@@ -3,7 +3,7 @@
@@ -79,8 +94,8 @@ index aaa2c4a3..5e875a73 100644
7994
+ <TargetFramework>net5.0</TargetFramework>
8095
<LangVersion>9</LangVersion>
8196
<Nullable>enable</Nullable>
82-
<Description>This package includes a powerful command line parser and other tools for building command line applications, including:
83-
@@ -18,9 +18,4 @@
97+
98+
@@ -26,11 +26,6 @@
8499
<Compile Include="..\System.Diagnostics.CodeAnalysis.cs" Link="System.Diagnostics.CodeAnalysis.cs" />
85100
</ItemGroup>
86101

@@ -90,6 +105,5 @@ index aaa2c4a3..5e875a73 100644
90105
- </ItemGroup>
91106
-
92107
<ItemGroup>
93-
--
94-
2.25.2
95-
108+
<Compile Update="Properties\Resources.Designer.cs">
109+
<DesignTime>True</DesignTime>

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"tools": {
3-
"dotnet": "5.0.100",
3+
"dotnet": "5.0.300",
44
"vs": {
55
"version": "16.8"
66
},
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using Microsoft.CodeAnalysis;
2+
using System.Linq;
3+
using System.Text;
4+
5+
namespace System.CommandLine.CommandGenerator
6+
{
7+
[Generator]
8+
public class CommandHandlerGenerator : ISourceGenerator
9+
{
10+
private const string ICommandHandlerType = "System.CommandLine.Invocation.ICommandHandler";
11+
12+
public void Execute(GeneratorExecutionContext context)
13+
{
14+
SyntaxReceiver rx = (SyntaxReceiver)context.SyntaxContextReceiver!;
15+
16+
StringBuilder builder = new();
17+
builder.AppendLine(@"
18+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
19+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
20+
21+
#nullable disable
22+
using System.CommandLine.Binding;
23+
using System.Reflection;
24+
using System.Threading.Tasks;
25+
26+
namespace System.CommandLine.Invocation
27+
{
28+
public static partial class CommandHandlerGeneratorExtensions_Generated
29+
{
30+
");
31+
int count = 1;
32+
foreach (var invocation in rx.Invocations)
33+
{
34+
var methodParamters = invocation.Parameters
35+
.Select(x => x.GetMethodParameter())
36+
.Where(x => !string.IsNullOrWhiteSpace(x.Name))
37+
.ToList();
38+
39+
builder.AppendLine(@$"public static {ICommandHandlerType} Generate<{string.Join(", ", Enumerable.Range(1, invocation.NumberOfGenerericParameters).Select(x => $"Unused{x}"))}>(this CommandHandlerGenerator handler,");
40+
builder.AppendLine($"{invocation.DelegateType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)} method");
41+
if (methodParamters.Count > 0)
42+
{
43+
builder.AppendLine(",");
44+
builder.AppendLine(string.Join($", ", methodParamters.Select(x => $"{x.Type} {x.Name}")));
45+
}
46+
builder.AppendLine(")");
47+
builder.AppendLine("{");
48+
builder.Append($"return new GeneratedHandler_{count}(method");
49+
if (methodParamters.Count > 0)
50+
{
51+
builder.Append(", ");
52+
builder.Append(string.Join(", ", methodParamters.Select(x => x.Name)));
53+
}
54+
builder.AppendLine(");");
55+
56+
builder.AppendLine("}");
57+
58+
59+
//TODO: fully qualify type names
60+
builder.AppendLine($@"
61+
private class GeneratedHandler_{count} : {ICommandHandlerType}
62+
{{
63+
public GeneratedHandler_{count}({invocation.DelegateType} method");
64+
65+
if (methodParamters.Count > 0)
66+
{
67+
builder.AppendLine(",");
68+
builder.AppendLine(string.Join($", ", methodParamters.Select(x => $"{x.Type} {x.Name}")));
69+
}
70+
71+
builder.AppendLine($@")
72+
{{
73+
Method = method;");
74+
foreach (var propertyAssignment in invocation.Parameters
75+
.Select(x => x.GetPropertyAssignment())
76+
.Where(x => !string.IsNullOrWhiteSpace(x)))
77+
{
78+
builder.AppendLine(propertyAssignment);
79+
}
80+
builder.AppendLine($@"
81+
}}
82+
83+
public {invocation.DelegateType} Method {{ get; }}");
84+
85+
foreach (var propertyDeclaration in invocation.Parameters
86+
.Select(x => x.GetPropertyDeclaration())
87+
.Where(x => !string.IsNullOrWhiteSpace(x)))
88+
{
89+
builder.AppendLine(propertyDeclaration);
90+
}
91+
92+
builder.AppendLine($@"
93+
public async Task<int> InvokeAsync(InvocationContext context)
94+
{{");
95+
builder.AppendLine(invocation.InvokeContents());
96+
builder.AppendLine($@"
97+
}}
98+
}}");
99+
count++;
100+
}
101+
102+
builder.AppendLine(@"
103+
}
104+
}
105+
#nullable restore");
106+
107+
context.AddSource("CommandHandlerGeneratorExtensions_Generated.g.cs", builder.ToString());
108+
}
109+
110+
public void Initialize(GeneratorInitializationContext context)
111+
{
112+
#if DEBUG
113+
if (!System.Diagnostics.Debugger.IsAttached)
114+
{
115+
//System.Diagnostics.Debugger.Launch();
116+
}
117+
#endif
118+
119+
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)