@@ -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+
198223internal 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+ }
0 commit comments