33
44using System ;
55using System . Collections . Generic ;
6+ using System . Collections . Immutable ;
67using System . Drawing ;
78using System . Linq ;
89using System . Reflection ;
910using System . Text ;
10- using System . Threading ;
1111using Microsoft . CodeAnalysis ;
1212using Microsoft . CodeAnalysis . CSharp ;
1313using Microsoft . CodeAnalysis . Text ;
@@ -20,19 +20,33 @@ internal sealed class MagickColorsGenerator : IIncrementalGenerator
2020 public void Initialize ( IncrementalGeneratorInitializationContext context )
2121 {
2222 context . RegisterPostInitializationOutput ( context => context . AddAttributeSource < MagickColorsAttribute > ( ) ) ;
23- context . RegisterAttributeCodeGenerator < MagickColorsAttribute , bool > ( CheckForInterface , GenerateCode ) ;
23+
24+ var type = typeof ( Color ) ;
25+ var colors = context . CompilationProvider
26+ . Select ( ( compilation , _ ) => compilation
27+ . GetTypesByMetadataName ( type . FullName ) . Single ( )
28+ . GetMembers ( ) . OfType < IPropertySymbol > ( )
29+ . Where ( property => property . Type . Name == type . Name )
30+ . Select ( property => property . Name )
31+ . ToImmutableArray ( ) ) ;
32+
33+ var fullName = typeof ( MagickColorsAttribute ) . FullName ?? throw new InvalidOperationException ( ) ;
34+ var info = context . SyntaxProvider . ForAttributeWithMetadataName ( fullName , ( _ , _ ) => true , ( syntaxContext , _ ) => CheckForInterface ( syntaxContext ) ) ;
35+ var combinedInfo = info . Combine ( colors ) ;
36+
37+ context . RegisterSourceOutput ( combinedInfo , GenerateCode ) ;
2438 }
2539
2640 private static bool CheckForInterface ( GeneratorAttributeSyntaxContext context )
2741 => context . TargetNode . IsKind ( SyntaxKind . InterfaceDeclaration ) ;
2842
29- private static IReadOnlyCollection < SystemDrawingColor > GetColors ( )
43+ private static IReadOnlyCollection < SystemDrawingColor > GetColors ( ImmutableArray < string > allowedColors )
3044 {
3145 var type = typeof ( Color ) ;
3246
3347 var properties = type
3448 . GetProperties ( BindingFlags . Public | BindingFlags . Static )
35- . Where ( property => property . PropertyType == type ) ;
49+ . Where ( property => property . PropertyType == type && allowedColors . Contains ( property . Name ) ) ;
3650
3751 var colors = properties
3852 . Select ( property => new SystemDrawingColor ( property ) )
@@ -43,11 +57,12 @@ private static IReadOnlyCollection<SystemDrawingColor> GetColors()
4357 return colors ;
4458 }
4559
46- private void GenerateCode ( SourceProductionContext context , bool generateInterface )
60+ private void GenerateCode ( SourceProductionContext context , ( bool GenerateInterface , ImmutableArray < string > Colors ) info )
4761 {
4862 var codeBuilder = new CodeBuilder ( ) ;
4963 codeBuilder . AppendLine ( "#nullable enable" ) ;
50- if ( ! generateInterface )
64+
65+ if ( ! info . GenerateInterface )
5166 {
5267 codeBuilder . AppendLine ( ) ;
5368 codeBuilder . AppendQuantumType ( ) ;
@@ -57,26 +72,23 @@ private void GenerateCode(SourceProductionContext context, bool generateInterfac
5772 codeBuilder . AppendLine ( "namespace ImageMagick;" ) ;
5873 codeBuilder . AppendLine ( ) ;
5974 codeBuilder . Append ( "public partial " ) ;
60- codeBuilder . AppendLine ( generateInterface ? "interface IMagickColors<TQuantumType>" : "class MagickColors" ) ;
75+ codeBuilder . AppendLine ( info . GenerateInterface ? "interface IMagickColors<TQuantumType>" : "class MagickColors" ) ;
6176 codeBuilder . AppendLine ( "{" ) ;
6277 codeBuilder . Indent ++ ;
63- AppendSystemDrawingColors ( codeBuilder , generateInterface ) ;
78+ AppendSystemDrawingColors ( codeBuilder , info ) ;
6479 codeBuilder . Indent -- ;
6580 codeBuilder . AppendLine ( "}" ) ;
6681
67- var hintName = generateInterface ? "IMagickColors{TQuantumType}.g.cs" : "MagickColors.g.cs" ;
82+ var hintName = info . GenerateInterface ? "IMagickColors{TQuantumType}.g.cs" : "MagickColors.g.cs" ;
6883 context . AddSource ( hintName , SourceText . From ( codeBuilder . ToString ( ) , Encoding . UTF8 ) ) ;
6984 }
7085
71- private void AppendSystemDrawingColors ( CodeBuilder codeBuilder , bool generateInterface )
86+ private void AppendSystemDrawingColors ( CodeBuilder codeBuilder , ( bool GenerateInterface , ImmutableArray < string > AllowedColors ) info )
7287 {
73- foreach ( var color in GetColors ( ) )
88+ foreach ( var color in GetColors ( info . AllowedColors ) )
7489 {
75- if ( color . Name == "RebeccaPurple" )
76- continue ;
77-
7890 codeBuilder . AppendComment ( color . Comment ) ;
79- if ( generateInterface )
91+ if ( info . GenerateInterface )
8092 {
8193 codeBuilder . Append ( "IMagickColor<TQuantumType> " ) ;
8294 codeBuilder . Append ( color . Name ) ;
0 commit comments