Skip to content

Commit 313d87d

Browse files
authored
rename source generator anchor to SetHandler (#1411)
1 parent 4832a65 commit 313d87d

15 files changed

+240
-377
lines changed

eng/source-build-patches/0002-Use-package-version-properties-for-reference-version.patch

-18
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,3 @@ index 3048701..16a0eb7 100644
2727
+ <SystemMemoryPackageVersion Condition="'$(SystemMemoryPackageVersion)' == ''">4.5.4</SystemMemoryPackageVersion>
2828
+ </PropertyGroup>
2929
</Project>
30-
diff --git a/src/System.CommandLine/System.CommandLine.csproj b/src/System.CommandLine/System.CommandLine.csproj
31-
index 1ae8dd3..aaa2c4a 100644
32-
--- a/src/System.CommandLine/System.CommandLine.csproj
33-
+++ b/src/System.CommandLine/System.CommandLine.csproj
34-
@@ -19,8 +19,8 @@
35-
</ItemGroup>
36-
37-
<ItemGroup>
38-
- <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
39-
- <PackageReference Include="system.memory" Version="4.5.4" />
40-
+ <PackageReference Include="Microsoft.CSharp" Version="$(MicrosoftCSharpPackageVersion)" />
41-
+ <PackageReference Include="system.memory" Version="$(SystemMemoryPackageVersion)" />
42-
</ItemGroup>
43-
44-
<ItemGroup>
45-
--
46-
2.18.0
47-

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

+2-16
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@ Includes a code fix for ref nullability with the new framework.
1616
src/System.CommandLine/System.CommandLine.csproj | 7 +------
1717
6 files changed, 7 insertions(+), 12 deletions(-)
1818

19-
diff --git a/src/System.CommandLine.Generator/System.CommandLine.Generator.csproj b/src/System.CommandLine.Generator/System.CommandLine.Generator.csproj
20-
index d419906e..8644194a 100644
21-
--- a/src/System.CommandLine.Generator/System.CommandLine.Generator.csproj
22-
+++ b/src/System.CommandLine.Generator/System.CommandLine.Generator.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>
3319
diff --git a/src/System.CommandLine.DragonFruit/System.CommandLine.DragonFruit.csproj b/src/System.CommandLine.DragonFruit/System.CommandLine.DragonFruit.csproj
3420
index b3a542fd..0e49d818 100644
3521
--- a/src/System.CommandLine.DragonFruit/System.CommandLine.DragonFruit.csproj
@@ -100,8 +86,8 @@ index 125e9c78..3a10c14d 100644
10086
</ItemGroup>
10187

10288
- <ItemGroup>
103-
- <PackageReference Include="Microsoft.CSharp" Version="$(MicrosoftCSharpPackageVersion)" />
104-
- <PackageReference Include="system.memory" Version="$(SystemMemoryPackageVersion)" />
89+
- <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
90+
- <PackageReference Include="system.memory" Version="4.5.4" />
10591
- </ItemGroup>
10692
-
10793
<ItemGroup>

repack.ps1

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
# clean up the previously-cached NuGet packages
3+
Remove-Item -Recurse ~\.nuget\packages\System.CommandLine* -Force
4+
5+
# build and pack dotnet-interactive
6+
dotnet clean
7+
dotnet pack /p:PackageVersion=2.0.0-dev
8+
9+
# copy the dotnet-interactive packages to the temp directory
10+
Get-ChildItem -Recurse -Filter *.nupkg | Copy-Item -Destination c:\temp -Force
11+

src/System.CommandLine.Generator.Tests/GeneratedCommandHandlerTests.cs

+53-54
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
using System.Threading.Tasks;
1010
using FluentAssertions;
1111
using Xunit;
12-
using static System.CommandLine.Invocation.CommandHandlerGenerator;
1312

14-
#nullable enable
1513
namespace System.CommandLine.Generator.Tests
1614
{
1715
public class CommandHandlerTests
@@ -32,54 +30,58 @@ void Execute(string fullnameOrNickname, IConsole console, int age)
3230
boundAge = age;
3331
}
3432

35-
var command = new Command("command");
3633
var nameArgument = new Argument<string>();
37-
command.AddArgument(nameArgument);
3834
var ageOption = new Option<int>("--age");
39-
command.AddOption(ageOption);
4035

41-
command.Handler = GeneratedHandler.Create<Action<string, IConsole, int>>
36+
var command = new Command("command")
37+
{
38+
nameArgument,
39+
ageOption
40+
};
41+
42+
command.SetHandler<Action<string, IConsole, int>>
4243
(Execute, nameArgument, ageOption);
4344

4445
await command.InvokeAsync("command Gandalf --age 425", _console);
4546

4647
boundName.Should().Be("Gandalf");
4748
boundAge.Should().Be(425);
4849
boundConsole.Should().NotBeNull();
49-
}
50-
50+
}
51+
5152
[Fact]
52-
public async Task Can_generate_handler_for_method_with_model()
53+
public async Task Can_generate_handler_for_void_returning_delegate()
5354
{
5455
string? boundName = default;
5556
int boundAge = default;
5657
IConsole? boundConsole = null;
5758

58-
void Execute(Character character, IConsole console)
59-
{
60-
boundName = character.FullName;
61-
boundConsole = console;
62-
boundAge = character.Age;
63-
}
64-
65-
var command = new Command("command");
66-
var nameOption = new Option<string>("--name");
67-
command.AddOption(nameOption);
59+
var nameArgument = new Argument<string>();
6860
var ageOption = new Option<int>("--age");
69-
command.AddOption(ageOption);
7061

71-
command.Handler = GeneratedHandler.Create<Action<Character, IConsole>>
72-
(Execute, nameOption, ageOption);
62+
var command = new Command("command")
63+
{
64+
nameArgument,
65+
ageOption
66+
};
7367

74-
await command.InvokeAsync("command --age 425 --name Gandalf", _console);
68+
command.SetHandler<Action<string, IConsole, int>>
69+
((fullnameOrNickname, console, age) =>
70+
{
71+
boundName = fullnameOrNickname;
72+
boundConsole = console;
73+
boundAge = age;
74+
}, nameArgument, ageOption);
75+
76+
await command.InvokeAsync("command Gandalf --age 425", _console);
7577

7678
boundName.Should().Be("Gandalf");
7779
boundAge.Should().Be(425);
7880
boundConsole.Should().NotBeNull();
7981
}
8082

8183
[Fact]
82-
public async Task Can_generate_handler_for_method_with_model_property_binding()
84+
public async Task Can_generate_handler_for_method_with_model()
8385
{
8486
string? boundName = default;
8587
int boundAge = default;
@@ -98,12 +100,7 @@ void Execute(Character character, IConsole console)
98100
var ageOption = new Option<int>("--age");
99101
command.AddOption(ageOption);
100102

101-
command.Handler = GeneratedHandler.Create<Action<Character, IConsole>, Character>
102-
(Execute, context => new Character
103-
{
104-
FullName = context.ParseResult.ValueForOption(nameOption),
105-
Age = context.ParseResult.ValueForOption(ageOption),
106-
});
103+
command.SetHandler<Action<Character, IConsole>>(Execute, nameOption, ageOption);
107104

108105
await command.InvokeAsync("command --age 425 --name Gandalf", _console);
109106

@@ -126,8 +123,7 @@ int Execute(int first, int second)
126123
var secondArgument = new Argument<int>("second");
127124
command.AddArgument(secondArgument);
128125

129-
command.Handler = GeneratedHandler.Create<Func<int, int, int>>
130-
(Execute, firstArgument, secondArgument);
126+
command.SetHandler<Func<int, int, int>>(Execute, firstArgument, secondArgument);
131127

132128
int result = await command.InvokeAsync("add 1 2", _console);
133129

@@ -145,7 +141,7 @@ public async Task Can_generate_handler_with_well_know_parameters_types()
145141

146142
void Execute(
147143
InvocationContext invocationContext,
148-
IConsole console,
144+
IConsole console,
149145
ParseResult parseResult,
150146
IHelpBuilder helpBuilder,
151147
BindingContext bindingContext)
@@ -159,7 +155,7 @@ void Execute(
159155

160156
var command = new Command("command");
161157

162-
command.Handler = GeneratedHandler.Create<Action<InvocationContext, IConsole, ParseResult, IHelpBuilder, BindingContext>>(Execute);
158+
command.SetHandler<Action<InvocationContext, IConsole, ParseResult, IHelpBuilder, BindingContext>>(Execute);
163159

164160
await command.InvokeAsync("command", _console);
165161

@@ -179,20 +175,22 @@ public async Task Can_generate_handler_for_async_method()
179175

180176
async Task ExecuteAsync(string fullnameOrNickname, IConsole console, int age)
181177
{
182-
//Just long enough to make sure the taks is be awaited
183-
await Task.Delay(100);
178+
await Task.Yield();
184179
boundName = fullnameOrNickname;
185180
boundConsole = console;
186181
boundAge = age;
187182
}
188183

189-
var command = new Command("command");
190184
var nameArgument = new Argument<string>();
191-
command.AddArgument(nameArgument);
192185
var ageOption = new Option<int>("--age");
193-
command.AddOption(ageOption);
194186

195-
command.Handler = GeneratedHandler.Create<Func<string, IConsole, int, Task>>
187+
var command = new Command("command")
188+
{
189+
nameArgument,
190+
ageOption
191+
};
192+
193+
command.SetHandler<Func<string, IConsole, int, Task>>
196194
(ExecuteAsync, nameArgument, ageOption);
197195

198196
await command.InvokeAsync("command Gandalf --age 425", _console);
@@ -207,50 +205,52 @@ public async Task Can_generate_handler_for_async_task_of_int_returning_method()
207205
{
208206
async Task<int> Execute(int first, int second)
209207
{
210-
await Task.Delay(100);
208+
await Task.Yield();
211209
return first + second;
212210
}
213211

214-
var command = new Command("add");
215212
var firstArgument = new Argument<int>("first");
216-
command.AddArgument(firstArgument);
217213
var secondArgument = new Argument<int>("second");
218-
command.AddArgument(secondArgument);
214+
var command = new Command("add")
215+
{
216+
firstArgument,
217+
secondArgument
218+
};
219219

220-
command.Handler = GeneratedHandler.Create<Func<int, int, Task<int>>>
220+
command.SetHandler<Func<int, int, Task<int>>>
221221
(Execute, firstArgument, secondArgument);
222222

223223
int result = await command.InvokeAsync("add 1 2", _console);
224224

225225
result.Should().Be(3);
226-
}
226+
}
227227

228228
[Fact]
229229
public async Task Can_generate_handler_for_multiple_commands_with_the_same_signature()
230230
{
231231
string firstValue = "";
232+
232233
void Execute1(string value)
233234
{
234235
firstValue = value;
235236
}
237+
236238
string secondValue = "";
239+
237240
void Execute2(string value)
238241
{
239242
secondValue = value;
240243
}
241244

242-
243245
var command1 = new Command("first");
244246
var argument1 = new Argument<string>("first-value");
245247
command1.AddArgument(argument1);
246-
command1.Handler = GeneratedHandler.Create<Action<string>>
247-
(Execute1, argument1);
248+
command1.SetHandler<Action<string>>(Execute1, argument1);
248249

249250
var command2 = new Command("second");
250251
var argument2 = new Argument<string>("second-value");
251252
command2.AddArgument(argument2);
252-
command2.Handler = GeneratedHandler.Create<Action<string>>
253-
(Execute2, argument2);
253+
command2.SetHandler<Action<string>>(Execute2, argument2);
254254

255255
await command1.InvokeAsync("first v1", _console);
256256
await command2.InvokeAsync("second v2", _console);
@@ -268,12 +268,11 @@ public Character(string? fullName, int age)
268268
}
269269

270270
public Character()
271-
{ }
271+
{
272+
}
272273

273274
public string? FullName { get; set; }
274275
public int Age { get; set; }
275276
}
276-
277277
}
278278
}
279-
#nullable restore

src/System.CommandLine.Generator.Tests/System.CommandLine.Generator.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<LangVersion>9</LangVersion>
66
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
77
<ExcludeFromSourceBuild>true</ExcludeFromSourceBuild>
8+
<Nullable>enable</Nullable>
89
</PropertyGroup>
910

1011
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

src/System.CommandLine.Generator/CommandHandlerGenerator.cs

-14
This file was deleted.

src/System.CommandLine.Generator/CommandHandlerGeneratorExtensions.cs

-24
This file was deleted.

0 commit comments

Comments
 (0)