Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename source generator anchor to SetHandler #1411

Merged
merged 2 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,3 @@ index 3048701..16a0eb7 100644
+ <SystemMemoryPackageVersion Condition="'$(SystemMemoryPackageVersion)' == ''">4.5.4</SystemMemoryPackageVersion>
+ </PropertyGroup>
</Project>
diff --git a/src/System.CommandLine/System.CommandLine.csproj b/src/System.CommandLine/System.CommandLine.csproj
index 1ae8dd3..aaa2c4a 100644
--- a/src/System.CommandLine/System.CommandLine.csproj
+++ b/src/System.CommandLine/System.CommandLine.csproj
@@ -19,8 +19,8 @@
</ItemGroup>

<ItemGroup>
- <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
- <PackageReference Include="system.memory" Version="4.5.4" />
+ <PackageReference Include="Microsoft.CSharp" Version="$(MicrosoftCSharpPackageVersion)" />
+ <PackageReference Include="system.memory" Version="$(SystemMemoryPackageVersion)" />
</ItemGroup>

<ItemGroup>
--
2.18.0

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

diff --git a/src/System.CommandLine.Generator/System.CommandLine.Generator.csproj b/src/System.CommandLine.Generator/System.CommandLine.Generator.csproj
index d419906e..8644194a 100644
--- a/src/System.CommandLine.Generator/System.CommandLine.Generator.csproj
+++ b/src/System.CommandLine.Generator/System.CommandLine.Generator.csproj
@@ -1,7 +1,7 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
- <TargetFramework>netstandard2.0</TargetFramework>
+ <TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>9.0</LangVersion>
<IsPackable>true</IsPackable>
diff --git a/src/System.CommandLine.DragonFruit/System.CommandLine.DragonFruit.csproj b/src/System.CommandLine.DragonFruit/System.CommandLine.DragonFruit.csproj
index b3a542fd..0e49d818 100644
--- a/src/System.CommandLine.DragonFruit/System.CommandLine.DragonFruit.csproj
Expand Down Expand Up @@ -100,8 +86,8 @@ index 125e9c78..3a10c14d 100644
</ItemGroup>

- <ItemGroup>
- <PackageReference Include="Microsoft.CSharp" Version="$(MicrosoftCSharpPackageVersion)" />
- <PackageReference Include="system.memory" Version="$(SystemMemoryPackageVersion)" />
- <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
- <PackageReference Include="system.memory" Version="4.5.4" />
- </ItemGroup>
-
<ItemGroup>
Expand Down
11 changes: 11 additions & 0 deletions repack.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

# clean up the previously-cached NuGet packages
Remove-Item -Recurse ~\.nuget\packages\System.CommandLine* -Force

# build and pack dotnet-interactive
dotnet clean
dotnet pack /p:PackageVersion=2.0.0-dev

# copy the dotnet-interactive packages to the temp directory
Get-ChildItem -Recurse -Filter *.nupkg | Copy-Item -Destination c:\temp -Force

107 changes: 53 additions & 54 deletions src/System.CommandLine.Generator.Tests/GeneratedCommandHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;
using static System.CommandLine.Invocation.CommandHandlerGenerator;

#nullable enable
namespace System.CommandLine.Generator.Tests
{
public class CommandHandlerTests
Expand All @@ -32,54 +30,58 @@ void Execute(string fullnameOrNickname, IConsole console, int age)
boundAge = age;
}

var command = new Command("command");
var nameArgument = new Argument<string>();
command.AddArgument(nameArgument);
var ageOption = new Option<int>("--age");
command.AddOption(ageOption);

command.Handler = GeneratedHandler.Create<Action<string, IConsole, int>>
var command = new Command("command")
{
nameArgument,
ageOption
};

command.SetHandler<Action<string, IConsole, int>>
(Execute, nameArgument, ageOption);

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

boundName.Should().Be("Gandalf");
boundAge.Should().Be(425);
boundConsole.Should().NotBeNull();
}

}
[Fact]
public async Task Can_generate_handler_for_method_with_model()
public async Task Can_generate_handler_for_void_returning_delegate()
{
string? boundName = default;
int boundAge = default;
IConsole? boundConsole = null;

void Execute(Character character, IConsole console)
{
boundName = character.FullName;
boundConsole = console;
boundAge = character.Age;
}

var command = new Command("command");
var nameOption = new Option<string>("--name");
command.AddOption(nameOption);
var nameArgument = new Argument<string>();
var ageOption = new Option<int>("--age");
command.AddOption(ageOption);

command.Handler = GeneratedHandler.Create<Action<Character, IConsole>>
(Execute, nameOption, ageOption);
var command = new Command("command")
{
nameArgument,
ageOption
};

await command.InvokeAsync("command --age 425 --name Gandalf", _console);
command.SetHandler<Action<string, IConsole, int>>
((fullnameOrNickname, console, age) =>
{
boundName = fullnameOrNickname;
boundConsole = console;
boundAge = age;
}, nameArgument, ageOption);

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

boundName.Should().Be("Gandalf");
boundAge.Should().Be(425);
boundConsole.Should().NotBeNull();
}

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

command.Handler = GeneratedHandler.Create<Action<Character, IConsole>, Character>
(Execute, context => new Character
{
FullName = context.ParseResult.ValueForOption(nameOption),
Age = context.ParseResult.ValueForOption(ageOption),
});
command.SetHandler<Action<Character, IConsole>>(Execute, nameOption, ageOption);

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

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

command.Handler = GeneratedHandler.Create<Func<int, int, int>>
(Execute, firstArgument, secondArgument);
command.SetHandler<Func<int, int, int>>(Execute, firstArgument, secondArgument);

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

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

void Execute(
InvocationContext invocationContext,
IConsole console,
IConsole console,
ParseResult parseResult,
IHelpBuilder helpBuilder,
BindingContext bindingContext)
Expand All @@ -159,7 +155,7 @@ void Execute(

var command = new Command("command");

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

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

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

async Task ExecuteAsync(string fullnameOrNickname, IConsole console, int age)
{
//Just long enough to make sure the taks is be awaited
await Task.Delay(100);
await Task.Yield();
boundName = fullnameOrNickname;
boundConsole = console;
boundAge = age;
}

var command = new Command("command");
var nameArgument = new Argument<string>();
command.AddArgument(nameArgument);
var ageOption = new Option<int>("--age");
command.AddOption(ageOption);

command.Handler = GeneratedHandler.Create<Func<string, IConsole, int, Task>>
var command = new Command("command")
{
nameArgument,
ageOption
};

command.SetHandler<Func<string, IConsole, int, Task>>
(ExecuteAsync, nameArgument, ageOption);

await command.InvokeAsync("command Gandalf --age 425", _console);
Expand All @@ -207,50 +205,52 @@ public async Task Can_generate_handler_for_async_task_of_int_returning_method()
{
async Task<int> Execute(int first, int second)
{
await Task.Delay(100);
await Task.Yield();
return first + second;
}

var command = new Command("add");
var firstArgument = new Argument<int>("first");
command.AddArgument(firstArgument);
var secondArgument = new Argument<int>("second");
command.AddArgument(secondArgument);
var command = new Command("add")
{
firstArgument,
secondArgument
};

command.Handler = GeneratedHandler.Create<Func<int, int, Task<int>>>
command.SetHandler<Func<int, int, Task<int>>>
(Execute, firstArgument, secondArgument);

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

result.Should().Be(3);
}
}

[Fact]
public async Task Can_generate_handler_for_multiple_commands_with_the_same_signature()
{
string firstValue = "";

void Execute1(string value)
{
firstValue = value;
}

string secondValue = "";

void Execute2(string value)
{
secondValue = value;
}


var command1 = new Command("first");
var argument1 = new Argument<string>("first-value");
command1.AddArgument(argument1);
command1.Handler = GeneratedHandler.Create<Action<string>>
(Execute1, argument1);
command1.SetHandler<Action<string>>(Execute1, argument1);

var command2 = new Command("second");
var argument2 = new Argument<string>("second-value");
command2.AddArgument(argument2);
command2.Handler = GeneratedHandler.Create<Action<string>>
(Execute2, argument2);
command2.SetHandler<Action<string>>(Execute2, argument2);

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

public Character()
{ }
{
}

public string? FullName { get; set; }
public int Age { get; set; }
}

}
}
#nullable restore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<LangVersion>9</LangVersion>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<ExcludeFromSourceBuild>true</ExcludeFromSourceBuild>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
14 changes: 0 additions & 14 deletions src/System.CommandLine.Generator/CommandHandlerGenerator.cs

This file was deleted.

This file was deleted.

Loading