Skip to content

Commit 1996c08

Browse files
committed
disable naming conversion
1 parent 0a53f8f commit 1996c08

File tree

4 files changed

+56
-29
lines changed

4 files changed

+56
-29
lines changed

sandbox/GeneratorSandbox/Program.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
using ConsoleAppFramework;
2-
using FilterShareProject;
3-
using System.Reflection;
42

5-
//using Microsoft.Extensions.DependencyInjection;
6-
using System.Runtime.CompilerServices;
3+
[assembly: ConsoleAppFrameworkGeneratorOptions(DisableNamingConversion = true)]
74

8-
args = ["--version"];
9-
Console.WriteLine(args.Length);
10-
Console.WriteLine(args.AsSpan(0).Length);
115

12-
// ConsoleApp.ServiceProvider
13-
// ConsoleApp.Create(
14-
15-
16-
// ConsoleApp.Run(args, (int x, int y) => { });
6+
args = ["HelloWorld", "--help"];
177
var app = ConsoleApp.Create();
18-
app.Add<MyCommands>("zzz");
19-
8+
//app.Add<MyCommands>();
9+
app.Add<Tacommands>();
2010
app.Run(args);
2111

2212

2313

14+
2415
public class MyProjectCommand
2516
{
2617
public void Execute(int x)
@@ -53,10 +44,11 @@ public void Output(string msg = @"\\")
5344
}
5445
}
5546

56-
[HogeHoge.Batch2Attribute]
5747
public class Tacommands
5848
{
59-
49+
public void HelloWorld(int hogeMoge)
50+
{
51+
}
6052
}
6153

6254
namespace ConsoleAppFramework
@@ -101,9 +93,14 @@ internal static partial class ConsoleApp
10193

10294
}
10395

96+
97+
98+
10499
namespace HogeHoge
105100
{
106101

102+
103+
107104
public class BatchAttribute : Attribute
108105
{
109106
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace ConsoleAppFramework;
2+
3+
readonly record struct ConsoleAppFrameworkGeneratorOptions(bool DisableNamingConversion);

src/ConsoleAppFramework/ConsoleAppGenerator.cs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
1313
{
1414
context.RegisterPostInitializationOutput(EmitConsoleAppTemplateSource);
1515

16-
// ConsoleApp.Create(Action<IServiceCollection> configure)
16+
// TODO: modify this. ConsoleApp.Create(Action<IServiceCollection> configure)
1717
var hasDependencyInjection = context.MetadataReferencesProvider
1818
.Where(x =>
1919
{
@@ -24,6 +24,21 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2424

2525
context.RegisterSourceOutput(hasDependencyInjection, EmitConsoleAppCreateConfigure);
2626

27+
var generatorOptions = context.CompilationProvider.Select((compilation, token) =>
28+
{
29+
foreach (var attr in compilation.Assembly.GetAttributes())
30+
{
31+
if (attr.AttributeClass?.Name == "ConsoleAppFrameworkGeneratorOptionsAttribute")
32+
{
33+
var args = attr.NamedArguments;
34+
var disableNamingConversion = args.FirstOrDefault(x => x.Key == "DisableNamingConversion").Value.Value as bool? ?? false;
35+
return new ConsoleAppFrameworkGeneratorOptions(disableNamingConversion);
36+
}
37+
}
38+
39+
return new ConsoleAppFrameworkGeneratorOptions(DisableNamingConversion: false);
40+
});
41+
2742
// ConsoleApp.Run
2843
var runSource = context.SyntaxProvider
2944
.CreateSyntaxProvider((node, ct) =>
@@ -47,12 +62,15 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
4762
}
4863

4964
return false;
50-
}, (context, ct) =>
65+
}, (context, ct) => context)
66+
.Combine(generatorOptions)
67+
.Select((t, ct) =>
5168
{
69+
var (context, options) = t;
5270
var reporter = new DiagnosticReporter();
5371
var node = (InvocationExpressionSyntax)context.Node;
5472
var wellknownTypes = new WellKnownTypes(context.SemanticModel.Compilation);
55-
var parser = new Parser(reporter, node, context.SemanticModel, wellknownTypes, DelegateBuildType.MakeCustomDelegateWhenHasDefaultValueOrTooLarge, []);
73+
var parser = new Parser(options, reporter, node, context.SemanticModel, wellknownTypes, DelegateBuildType.MakeCustomDelegateWhenHasDefaultValueOrTooLarge, []);
5674
var isRunAsync = (node.Expression as MemberAccessExpressionSyntax)?.Name.Identifier.Text == "RunAsync";
5775

5876
var command = parser.ParseAndValidateForRun();
@@ -96,7 +114,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
96114
})
97115
.WithTrackingName("ConsoleApp.Builder.1_Where")
98116
.Collect()
99-
.Select((x, ct) => new CollectBuilderContext(x, ct))
117+
.Combine(generatorOptions)
118+
.Select((x, ct) => new CollectBuilderContext(x.Right, x.Left, ct))
100119
.WithTrackingName("ConsoleApp.Builder.2_Collect");
101120

102121
var registerCommands = context.SyntaxProvider.ForAttributeWithMetadataName("ConsoleAppFramework.RegisterCommandsAttribute",
@@ -195,6 +214,12 @@ public RegisterCommandsAttribute(string commandPath)
195214
}
196215
}
197216
217+
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false, Inherited = false)]
218+
public class ConsoleAppFrameworkGeneratorOptionsAttribute : Attribute
219+
{
220+
public bool DisableNamingConversion { get; set; }
221+
}
222+
198223
internal static partial class ConsoleApp
199224
{
200225
public static IServiceProvider? ServiceProvider { get; set; }
@@ -773,11 +798,13 @@ class CollectBuilderContext : IEquatable<CollectBuilderContext>
773798
public bool HasRunAsync { get; }
774799

775800
FilterInfo[]? globalFilters { get; }
801+
ConsoleAppFrameworkGeneratorOptions generatorOptions { get; }
776802

777-
public CollectBuilderContext(ImmutableArray<BuilderContext> contexts, CancellationToken cancellationToken)
803+
public CollectBuilderContext(ConsoleAppFrameworkGeneratorOptions generatorOptions, ImmutableArray<BuilderContext> contexts, CancellationToken cancellationToken)
778804
{
779805
this.DiagnosticReporter = new DiagnosticReporter();
780806
this.CancellationToken = cancellationToken;
807+
this.generatorOptions = generatorOptions;
781808

782809
// validation, invoke in loop is not allowed.
783810
foreach (var item in contexts)
@@ -836,7 +863,7 @@ public CollectBuilderContext(ImmutableArray<BuilderContext> contexts, Cancellati
836863
.Select(x =>
837864
{
838865
var wellKnownTypes = new WellKnownTypes(x.Model.Compilation);
839-
var parser = new Parser(DiagnosticReporter, x.Node, x.Model, wellKnownTypes, DelegateBuildType.MakeCustomDelegateWhenHasDefaultValueOrTooLarge, globalFilters);
866+
var parser = new Parser(generatorOptions, DiagnosticReporter, x.Node, x.Model, wellKnownTypes, DelegateBuildType.MakeCustomDelegateWhenHasDefaultValueOrTooLarge, globalFilters);
840867
var command = parser.ParseAndValidateForBuilderDelegateRegistration();
841868

842869
// validation command name duplicate
@@ -855,7 +882,7 @@ public CollectBuilderContext(ImmutableArray<BuilderContext> contexts, Cancellati
855882
.SelectMany(x =>
856883
{
857884
var wellKnownTypes = new WellKnownTypes(x.Model.Compilation);
858-
var parser = new Parser(DiagnosticReporter, x.Node, x.Model, wellKnownTypes, DelegateBuildType.None, globalFilters);
885+
var parser = new Parser(generatorOptions, DiagnosticReporter, x.Node, x.Model, wellKnownTypes, DelegateBuildType.None, globalFilters);
859886
var commands = parser.ParseAndValidateForBuilderClassRegistration();
860887

861888
// validation command name duplicate
@@ -903,7 +930,7 @@ public void AddRegisterAttributes(ImmutableArray<GeneratorAttributeSyntaxContext
903930
}
904931

905932
var wellKnownTypes = new WellKnownTypes(ctx.SemanticModel.Compilation);
906-
var parser = new Parser(DiagnosticReporter, ctx.TargetNode, ctx.SemanticModel, wellKnownTypes, DelegateBuildType.None, globalFilters ?? []);
933+
var parser = new Parser(generatorOptions, DiagnosticReporter, ctx.TargetNode, ctx.SemanticModel, wellKnownTypes, DelegateBuildType.None, globalFilters ?? []);
907934

908935
var commands = parser.CreateCommandsFromType((ITypeSymbol)ctx.TargetSymbol, commandPath);
909936

@@ -950,4 +977,4 @@ public bool Equals(BuilderContext other)
950977
return Node == other.Node; // no means.
951978
}
952979
}
953-
}
980+
}

src/ConsoleAppFramework/Parser.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace ConsoleAppFramework;
66

7-
internal class Parser(DiagnosticReporter context, SyntaxNode node, SemanticModel model, WellKnownTypes wellKnownTypes, DelegateBuildType delegateBuildType, FilterInfo[] globalFilters)
7+
internal class Parser(ConsoleAppFrameworkGeneratorOptions generatorOptions, DiagnosticReporter context, SyntaxNode node, SemanticModel model, WellKnownTypes wellKnownTypes, DelegateBuildType delegateBuildType, FilterInfo[] globalFilters)
88
{
99
public Command? ParseAndValidateForRun() // for ConsoleApp.Run, lambda or method or &method
1010
{
@@ -160,7 +160,7 @@ internal class Parser(DiagnosticReporter context, SyntaxNode node, SemanticModel
160160
}
161161
else
162162
{
163-
commandName = NameConverter.ToKebabCase(x.Name);
163+
commandName = generatorOptions.DisableNamingConversion ? x.Name : NameConverter.ToKebabCase(x.Name);
164164
}
165165

166166
var command = ParseFromMethodSymbol(x, false, (commandPath == null) ? commandName : $"{commandPath.Trim()} {commandName}", typeFilters);
@@ -354,7 +354,7 @@ internal class Parser(DiagnosticReporter context, SyntaxNode node, SemanticModel
354354

355355
return new CommandParameter
356356
{
357-
Name = NameConverter.ToKebabCase(x.Identifier.Text),
357+
Name = generatorOptions.DisableNamingConversion ? x.Identifier.Text : NameConverter.ToKebabCase(x.Identifier.Text),
358358
WellKnownTypes = wellKnownTypes,
359359
OriginalParameterName = x.Identifier.Text,
360360
IsNullableReference = isNullableReference,
@@ -529,7 +529,7 @@ internal class Parser(DiagnosticReporter context, SyntaxNode node, SemanticModel
529529

530530
return new CommandParameter
531531
{
532-
Name = NameConverter.ToKebabCase(x.Name),
532+
Name = generatorOptions.DisableNamingConversion ? x.Name : NameConverter.ToKebabCase(x.Name),
533533
WellKnownTypes = wellKnownTypes,
534534
OriginalParameterName = x.Name,
535535
IsNullableReference = isNullableReference,

0 commit comments

Comments
 (0)