Skip to content

Commit 521bd58

Browse files
committed
refactor2
1 parent af0c123 commit 521bd58

File tree

4 files changed

+31
-26
lines changed

4 files changed

+31
-26
lines changed

sandbox/GeneratorSandbox/Program.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ public static class Program
88
{
99
public static void Main(string[] args)
1010
{
11-
var app = ConsoleApp.Create();
12-
13-
app.Add("", (CancellationToken cancellationToken, ConsoleAppContext ctx) => { });
14-
15-
app.Run(args, CancellationToken.None);
11+
var cmd = new Commands();
12+
ConsoleApp.Run(args, cmd.SomeCommand);
1613

1714
}
1815
}

src/ConsoleAppFramework/Command.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public record class Command
3131
public required EquatableArray<FilterInfo> Filters { get; init; }
3232
public IgnoreEquality<ISymbol> Symbol { get; init; }
3333
public bool HasFilter => Filters.Length != 0;
34+
public bool IsRequireDynamicDependencyAttribute { get; set; } // can set...!
3435

3536
// return is delegateType(Name).
3637
public string? BuildDelegateSignature(string customDelegateTypeName, out string? customDelegateDefinition)
@@ -145,6 +146,23 @@ public string BuildDelegateTypeDefinition(string delegateName)
145146
var parameters = string.Join(", ", Parameters.Select(x => x.ToString()));
146147
return $"delegate {retType} {delegateName}({parameters});";
147148
}
149+
150+
public string BuildDynamicDependencyAttribute()
151+
{
152+
if (!IsRequireDynamicDependencyAttribute) return "";
153+
154+
var dynamicDependencyMethod = Symbol.Value as IMethodSymbol;
155+
if (dynamicDependencyMethod == null) return "";
156+
157+
var docCommentId = dynamicDependencyMethod.GetDocumentationCommentId();
158+
var parameterPartIndex = docCommentId?.IndexOf('(') ?? -1;
159+
var memberSignature = parameterPartIndex >= 0
160+
? dynamicDependencyMethod.Name + docCommentId!.Substring(parameterPartIndex)
161+
: dynamicDependencyMethod.Name;
162+
163+
var containingType = dynamicDependencyMethod.ContainingType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
164+
return $"[global::System.Diagnostics.CodeAnalysis.DynamicDependency(\"{memberSignature}\", typeof({containingType}))]";
165+
}
148166
}
149167

150168
public record class CommandParameter

src/ConsoleAppFramework/Emitter.cs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,6 @@ public void EmitRun(SourceBuilder sb, CommandWithId commandWithId, bool isRunAsy
4646
var cancellationTokenName = (emitForBuilder) ? "cancellationToken" : null;
4747
var cancellationTokenParameter = cancellationTokenName != null ? "CancellationToken cancellationToken" : null;
4848

49-
// TODO: to parser
50-
string? dynamicDependencyAttribute = null;
51-
if (command.CommandMethodInfo == null &&
52-
command.Symbol.Value is IMethodSymbol dynamicDependencyMethod &&
53-
dynamicDependencyMethod.ContainingType != null)
54-
{
55-
var docCommentId = dynamicDependencyMethod.GetDocumentationCommentId();
56-
var parameterPartIndex = docCommentId?.IndexOf('(') ?? -1;
57-
var memberSignature = parameterPartIndex >= 0
58-
? dynamicDependencyMethod.Name + docCommentId!.Substring(parameterPartIndex)
59-
: dynamicDependencyMethod.Name;
60-
61-
var containingType = dynamicDependencyMethod.ContainingType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
62-
dynamicDependencyAttribute = $"[global::System.Diagnostics.CodeAnalysis.DynamicDependency(\"{memberSignature}\", typeof({containingType}))]";
63-
}
64-
6549
if (!emitForBuilder)
6650
{
6751
sb.AppendLine("/// <summary>");
@@ -75,10 +59,9 @@ command.Symbol.Value is IMethodSymbol dynamicDependencyMethod &&
7559
sb.AppendLine("/// </summary>");
7660
}
7761

78-
// method signature
79-
if (dynamicDependencyAttribute != null)
62+
if (emitForBuilder && command.IsRequireDynamicDependencyAttribute)
8063
{
81-
sb.AppendLine(dynamicDependencyAttribute);
64+
sb.AppendLine(command.BuildDynamicDependencyAttribute());
8265
}
8366

8467
var methodArgument = command.HasFilter

src/ConsoleAppFramework/Parser.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.CodeAnalysis.CSharp.Syntax;
44
using System.Collections.Immutable;
55
using System.Runtime.InteropServices.ComTypes;
6+
using System.Security.Cryptography;
67
using System.Xml.Linq;
78

89
namespace ConsoleAppFramework;
@@ -356,7 +357,12 @@ bool IsParsableType(ITypeSymbol type)
356357
var methodSymbols = model.GetMemberGroup(expression);
357358
if (methodSymbols.Length > 0 && methodSymbols[0] is IMethodSymbol methodSymbol)
358359
{
359-
return ParseFromMethodSymbol(methodSymbol, addressOf: false, commandName, []);
360+
var cmd = ParseFromMethodSymbol(methodSymbol, addressOf: false, commandName, []);
361+
if (cmd != null)
362+
{
363+
cmd.IsRequireDynamicDependencyAttribute = true;
364+
}
365+
return cmd;
360366
}
361367
}
362368
}
@@ -596,7 +602,8 @@ bool IsParsableType(ITypeSymbol type)
596602
MethodKind = MethodKind.Lambda,
597603
Description = "",
598604
DelegateBuildType = delegateBuildType,
599-
Filters = globalFilters
605+
Filters = globalFilters,
606+
IsRequireDynamicDependencyAttribute = false,
600607
};
601608

602609
return cmd;

0 commit comments

Comments
 (0)