-
Notifications
You must be signed in to change notification settings - Fork 401
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"tools": { | ||
"dotnet": "5.0.100", | ||
"dotnet": "5.0.300", | ||
"vs": { | ||
"version": "16.8" | ||
}, | ||
|
122 changes: 122 additions & 0 deletions
122
src/System.CommandLine.CommandGenerator/CommandHandlerGenerator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | F42DDiff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
using Microsoft.CodeAnalysis; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace System.CommandLine.CommandGenerator | ||
{ | ||
[Generator] | ||
public class CommandHandlerGenerator : ISourceGenerator | ||
{ | ||
private const string ICommandHandlerType = "System.CommandLine.Invocation.ICommandHandler"; | ||
|
||
public void Execute(GeneratorExecutionContext context) | ||
{ | ||
SyntaxReceiver rx = (SyntaxReceiver)context.SyntaxContextReceiver!; | ||
|
||
StringBuilder builder = new(); | ||
builder.AppendLine(@" | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
#nullable disable | ||
using System.CommandLine.Binding; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
|
||
namespace System.CommandLine.Invocation | ||
{ | ||
public static partial class CommandHandlerGeneratorExtensions_Generated | ||
{ | ||
"); | ||
int count = 1; | ||
foreach (var invocation in rx.Invocations) | ||
{ | ||
var methodParamters = invocation.Parameters | ||
.Select(x => x.GetMethodParameter()) | ||
.Where(x => !string.IsNullOrWhiteSpace(x.Name)) | ||
.ToList(); | ||
|
||
builder.AppendLine(@$"public static {ICommandHandlerType} Generate<{string.Join(", ", Enumerable.Range(1, invocation.NumberOfGenerericParameters).Select(x => $"Unused{x}"))}>(this CommandHandlerGenerator handler,"); | ||
builder.AppendLine($"{invocation.DelegateType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)} method"); | ||
if (methodParamters.Count > 0) | ||
{ | ||
builder.AppendLine(","); | ||
builder.AppendLine(string.Join($", ", methodParamters.Select(x => $"{x.Type} {x.Name}"))); | ||
} | ||
builder.AppendLine(")"); | ||
builder.AppendLine("{"); | ||
builder.Append($"return new GeneratedHandler_{count}(method"); | ||
if (methodParamters.Count > 0) | ||
{ | ||
builder.Append(", "); | ||
builder.Append(string.Join(", ", methodParamters.Select(x => x.Name))); | ||
} | ||
builder.AppendLine(");"); | ||
|
||
builder.AppendLine("}"); | ||
|
||
|
||
//TODO: fully qualify type names | ||
builder.AppendLine($@" | ||
private class GeneratedHandler_{count} : {ICommandHandlerType} | ||
{{ | ||
public GeneratedHandler_{count}({invocation.DelegateType} method"); | ||
|
||
if (methodParamters.Count > 0) | ||
{ | ||
builder.AppendLine(","); | ||
builder.AppendLine(string.Join($", ", methodParamters.Select(x => $"{x.Type} {x.Name}"))); | ||
} | ||
|
||
builder.AppendLine($@") | ||
{{ | ||
Method = method;"); | ||
foreach (var propertyAssignment in invocation.Parameters | ||
.Select(x => x.GetPropertyAssignment()) | ||
.Where(x => !string.IsNullOrWhiteSpace(x))) | ||
{ | ||
builder.AppendLine(propertyAssignment); | ||
} | ||
builder.AppendLine($@" | ||
}} | ||
|
||
public {invocation.DelegateType} Method {{ get; }}"); | ||
|
||
foreach (var propertyDeclaration in invocation.Parameters | ||
.Select(x => x.GetPropertyDeclaration()) | ||
.Where(x => !string.IsNullOrWhiteSpace(x))) | ||
{ | ||
builder.AppendLine(propertyDeclaration); | ||
} | ||
|
||
builder.AppendLine($@" | ||
public async Task<int> InvokeAsync(InvocationContext context) | ||
{{"); | ||
builder.AppendLine(invocation.InvokeContents()); | ||
builder.AppendLine($@" | ||
}} | ||
}}"); | ||
count++; | ||
} | ||
|
||
builder.AppendLine(@" | ||
} | ||
} | ||
#nullable restore"); | ||
|
||
context.AddSource("CommandHandlerGeneratorExtensions_Generated.g.cs", builder.ToString()); | ||
} | ||
|
||
public void Initialize(GeneratorInitializationContext context) | ||
{ | ||
#if DEBUG | ||
if (!System.Diagnostics.Debugger.IsAttached) | ||
{ | ||
//System.Diagnostics.Debugger.Launch(); | ||
} | ||
#endif | ||
|
||
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver()); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Source generators for command handlers #1362
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
Uh oh!
There was an error while loading. Please reload this page.
Source generators for command handlers #1362
Changes from all commits
1bbd9c9
d183cfd
eb6ab4a
f3c3a60
eb126af
972f80f
936c6a5
6328220
6e50644
6ade863
0d4ad3f
< 8000 div class="text-emphasized css-truncate css-truncate-target"> Adding support for arguments716060d
f080f7e
aa7bd60
ef0e2f1
ef89d1e
2056cf1
ddf0688
e7b091b
c20e6b0
27e1621
2c84e10
2d9662c
9f8b4cf
98c689f
50cf35c
268ff16
98e9eb0
916b174
d58949f
cfacc7a
ab415c7
198be27
92c95fc
568a4da
fa46cf6
0155e9d
6f6c06f
11839db
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.