From 1e90a34d3637cd57814014cadbbf4287f944b88b Mon Sep 17 00:00:00 2001
From: "Dear.Va" <1328886154@qq.com>
Date: Fri, 8 Dec 2023 19:28:22 +0800
Subject: [PATCH] fix bug if class is non-static
---
...t.Parameterization.SourceGenerators.csproj | 4 ++--
.../Extensions/StringExtension.cs | 7 +++----
.../Generators/ParameterizationGenerator.cs | 20 ++++++++++++-------
.../Antelcat.Parameterization.csproj | 6 +++---
4 files changed, 21 insertions(+), 16 deletions(-)
diff --git a/src/Antelcat.Parameterization.SourceGenerators/Antelcat.Parameterization.SourceGenerators.csproj b/src/Antelcat.Parameterization.SourceGenerators/Antelcat.Parameterization.SourceGenerators.csproj
index b110f14..ba9a3d2 100644
--- a/src/Antelcat.Parameterization.SourceGenerators/Antelcat.Parameterization.SourceGenerators.csproj
+++ b/src/Antelcat.Parameterization.SourceGenerators/Antelcat.Parameterization.SourceGenerators.csproj
@@ -6,8 +6,8 @@
enable
true
Antelcat
- 1.2.0
- 1.2.0
+ 1.2.1
+ 1.2.1
Recommended
diff --git a/src/Antelcat.Parameterization.SourceGenerators/Extensions/StringExtension.cs b/src/Antelcat.Parameterization.SourceGenerators/Extensions/StringExtension.cs
index 1813c28..3ad7e90 100644
--- a/src/Antelcat.Parameterization.SourceGenerators/Extensions/StringExtension.cs
+++ b/src/Antelcat.Parameterization.SourceGenerators/Extensions/StringExtension.cs
@@ -2,8 +2,7 @@
public static class StringExtension
{
- public static string Escape(this string? s)
- {
- return s == null ? "null" : $"\"{s.Replace("\"", "\\\"")}\"";
- }
+ public static string Escape(this string? s) => s == null ? "null" : $"\"{s.Replace("\"", "\\\"")}\"";
+
+ public static string If(this string s, bool condition) => condition ? s : string.Empty;
}
\ No newline at end of file
diff --git a/src/Antelcat.Parameterization.SourceGenerators/Generators/ParameterizationGenerator.cs b/src/Antelcat.Parameterization.SourceGenerators/Generators/ParameterizationGenerator.cs
index 3cd27e1..c6d5e3f 100644
--- a/src/Antelcat.Parameterization.SourceGenerators/Generators/ParameterizationGenerator.cs
+++ b/src/Antelcat.Parameterization.SourceGenerators/Generators/ParameterizationGenerator.cs
@@ -37,14 +37,14 @@ protected override void GenerateCode(SourceProductionContext context, ImmutableA
modifier.IsKind(SyntaxKind.PublicKeyword) || modifier.IsKind(SyntaxKind.InternalKeyword))
.Text ??
"internal"; // 默认为 internal
- var isClassStatic = classDeclarationSyntax.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.StaticKeyword));
+ var isStaticClass = classDeclarationSyntax.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.StaticKeyword));
IEnumerable<(MethodDeclarationSyntax, AttributeProxy)> GetCandidateMethods()
{
foreach (var method in classDeclarationSyntax.Members.OfType())
{
// 如果类是static,那么方法也必须是static
- if (method.Modifiers.All(modifier => !modifier.IsKind(SyntaxKind.StaticKeyword)) && isClassStatic) continue;
+ if (method.Modifiers.All(modifier => !modifier.IsKind(SyntaxKind.StaticKeyword)) && isStaticClass) continue;
if (method.GetSpecifiedAttributes(syntaxContext.SemanticModel, context.CancellationToken)
.FirstOrDefault() is not { } commandAttribute) continue;
yield return (method, commandAttribute);
@@ -131,10 +131,16 @@ symbol is not IMethodSymbol
$"new {converterType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}()");
}
+ var isStatic = true;
var isAsync = false;
var caseBuilder = new SourceStringBuilder(initialIndentCount: 3);
foreach (var (method, commandAttribute) in candidateMethods)
{
+ if (method.Modifiers.All(modifier => !modifier.IsKind(SyntaxKind.StaticKeyword)))
+ {
+ isStatic = false;
+ }
+
{
var fullName = commandAttribute[nameof(CommandAttribute.FullName)] as string ?? method.Identifier.ValueText;
caseBuilder.AppendLine($"case \"{(caseSensitive ? fullName : fullName.ToLower())}\":");
@@ -269,13 +275,13 @@ symbol is not IMethodSymbol
sourceBuilder.Append(
$$"""
- {{classAccessModifier}} {{(isClassStatic ? "static" : string.Empty)}} partial class {{className}}
+ {{classAccessModifier}} {{(isStaticClass ? "static" : string.Empty)}} partial class {{className}}
{
- private static {{(isAsync ? Global.ValueTask : "void")}} ExecuteInput{{(isAsync ? "Async" : string.Empty)}}({{Global.String}}? input)
+ private {{"static ".If(isStatic)}}{{(isAsync ? Global.ValueTask : "void")}} ExecuteInput{{"Async".If(isAsync)}}({{Global.String}}? input)
{
if ({{Global.String}}.IsNullOrEmpty(input))
{
- return{{(isAsync ? " default" : string.Empty)}};
+ return{{" default".If(isAsync)}};
}
var arguments = new {{Global.GenericList}}<{{Global.String}}>();
@@ -286,10 +292,10 @@ symbol is not IMethodSymbol
arguments.Add(part);
}
- {{(isAsync ? "return " : string.Empty)}}ExecuteArguments{{(isAsync ? "Async" : string.Empty)}}(arguments);
+ {{"return ".If(isAsync)}}ExecuteArguments{{"Async".If(isAsync)}}(arguments);
}
- private static {{(isAsync ? $"async {Global.ValueTask}" : "void")}} ExecuteArguments{{(isAsync ? "Async" : string.Empty)}}({{Global.GenericIReadonlyList}}<{{Global.String}}> arguments)
+ private {{"static ".If(isStatic)}}{{(isAsync ? $"async {Global.ValueTask}" : "void")}} ExecuteArguments{{"Async".If(isAsync)}}({{Global.GenericIReadonlyList}}<{{Global.String}}> arguments)
{
if (arguments.Count == 0)
{
diff --git a/src/Antelcat.Parameterization/Antelcat.Parameterization.csproj b/src/Antelcat.Parameterization/Antelcat.Parameterization.csproj
index ea14459..450030a 100644
--- a/src/Antelcat.Parameterization/Antelcat.Parameterization.csproj
+++ b/src/Antelcat.Parameterization/Antelcat.Parameterization.csproj
@@ -4,10 +4,10 @@
netstandard2.0
preview
enable
- 1.2.0
+ 1.2.1
Antelcat
- 1.2.0
- 1.2.0
+ 1.2.1
+ 1.2.1
Effortless Command-Line Application Builder
A powerful source generator designed to revolutionize the way you create command-line applications. This tool simplifies the process of building CLI applications by automatically generating parsing methods with just attribute marking on classes and methods.
https://github.com/Antelcat/Antelcat.Parameterization