@@ -17,12 +17,24 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
1717 . Combine ( context . AnalyzerConfigOptionsProvider )
1818 . Select ( ( tuple , _ ) => ( file : tuple . Left , path : tuple . Left . Path , options : tuple . Right . GetOptions ( tuple . Left ) ) )
1919 . Where ( x => x . options . TryGetValue ( "build_metadata.EmbeddedResource.Value" , out _ ) )
20- . Select ( ( x , ct ) =>
20+ . Combine ( context . CompilationProvider )
21+ . Select ( ( a , ct ) =>
2122 {
23+ var x = a . Left ;
24+ var c = a . Right ;
2225 x . options . TryGetValue ( "build_metadata.EmbeddedResource.Value" , out var resourceName ) ;
2326 x . options . TryGetValue ( "build_metadata.EmbeddedResource.Area" , out var area ) ;
2427 x . options . TryGetValue ( "build_metadata.EmbeddedResource.Comment" , out var comment ) ;
25- return ( x . path , resourceName : resourceName ! , area : string . IsNullOrWhiteSpace ( area ) ? null : area , comment : string . IsNullOrWhiteSpace ( comment ) ? null : comment ) ;
28+ x . options . TryGetValue ( "build_metadata.EmbeddedResource.LogicalName" , out var logicalName ) ;
29+ resourceName = string . Join ( "." , ( resourceName ?? "" ) . Split ( [ '/' , '\\ ' , '.' , '-' ] , StringSplitOptions . RemoveEmptyEntries ) ) ;
30+ area = string . IsNullOrWhiteSpace ( area ) ? null : string . Join ( "/" , area ! . Split ( [ '/' , '\\ ' ] , StringSplitOptions . RemoveEmptyEntries ) ) ;
31+
32+ return (
33+ logicalName ?? $ "{ c . AssemblyName } .{ resourceName } ",
34+ resourceName ,
35+ area ,
36+ comment : string . IsNullOrWhiteSpace ( comment ) ? null : comment
37+ ) ;
2638 }
2739 ) ;
2840
@@ -43,14 +55,12 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
4355
4456 static void GenerateSource (
4557 SourceProductionContext spc ,
46- ( ( string path , string resourceName , string area , string comment ) resource , ( string , string ) Right ) valueTuple
58+ ( ( string logicalName , string resourceName , string ? area , string ? comment ) resource , ( string ? , string ? ) Right ) valueTuple
4759 )
4860 {
49- var ( ( path , resourceName , area , comment ) , ( ns , visibility ) ) = valueTuple ;
50-
51- var rest = area . Split ( '.' , StringSplitOptions . RemoveEmptyEntries ) ;
61+ var ( ( logicalName , resourceName , area , comment ) , ( ns , visibility ) ) = valueTuple ;
5262
53- var property = MethodDeclaration ( ParseTypeName ( "System.IO.Stream" ) , Identifier ( resourceName . Replace ( ". " , "" ) . Replace ( "- " , "_" ) ) )
63+ var property = MethodDeclaration ( ParseTypeName ( "System.IO.Stream" ) , Identifier ( $ " { area } / { resourceName } " . Replace ( "/ " , "_ " ) . Replace ( ". " , "_" ) ) )
5464 . WithModifiers ( TokenList ( Token ( SyntaxKind . InternalKeyword ) , Token ( SyntaxKind . StaticKeyword ) ) )
5565 . WithExpressionBody (
5666 ArrowExpressionClause (
@@ -71,30 +81,20 @@ static void GenerateSource(
7181 Argument (
7282 LiteralExpression (
7383 SyntaxKind . StringLiteralExpression ,
74- Literal ( path )
84+ Literal ( logicalName )
7585 )
7686 )
7787 )
7888 )
7989 )
8090 )
8191 )
82- . WithSemicolonToken ( Token ( SyntaxKind . SemicolonToken ) )
83- . AddSummary ( comment ) ;
92+ . WithSemicolonToken ( Token ( SyntaxKind . SemicolonToken ) ) ;
93+ if ( comment is { Length : > 0 } ) property = property . AddSummary ( comment ) ;
8494
85- var classDefinition = rest
86- . AsEnumerable ( )
87- . Reverse ( )
88- . Aggregate < string , MemberDeclarationSyntax > (
89- property ,
90- ( definition , parent ) => ClassDeclaration ( parent )
91- . WithModifiers ( TokenList ( Token ( SyntaxKind . InternalKeyword ) , Token ( SyntaxKind . StaticKeyword ) , Token ( SyntaxKind . PartialKeyword ) ) )
92- . AddMembers ( definition )
93- . AddSummary ( $ "Provides access to embedded resources under { parent } ")
94- ) ;
95- classDefinition = ClassDeclaration ( "Resources" )
95+ var classDefinition = ClassDeclaration ( "Resources" )
9696 . WithModifiers ( TokenList ( Token ( SyntaxKind . InternalKeyword ) , Token ( SyntaxKind . StaticKeyword ) , Token ( SyntaxKind . PartialKeyword ) ) )
97- . AddMembers ( classDefinition )
97+ . AddMembers ( property )
9898 . AddSummary ( "Provides access to embedded resources" ) ;
9999
100100 classDefinition = ClassDeclaration ( "MyAssembly" )
@@ -104,6 +104,6 @@ static void GenerateSource(
104104 var cu = CompilationUnit ( ) ;
105105 cu = ns is { Length : > 0 } ? cu . AddMembers ( NamespaceDeclaration ( ParseName ( ns ) ) . AddMembers ( classDefinition ) ) : cu . AddMembers ( classDefinition ) ;
106106
107- spc . AddSource ( $ "{ area } . { resourceName } .g.cs", SourceText . From ( cu . NormalizeWhitespace ( ) . GetText ( ) . ToString ( ) , Encoding . UTF8 ) ) ;
107+ spc . AddSource ( $ "{ resourceName } .g.cs", SourceText . From ( cu . NormalizeWhitespace ( ) . GetText ( ) . ToString ( ) , Encoding . UTF8 ) ) ;
108108 }
109109}
0 commit comments