11namespace Schema . NET . Tool ;
22
33using System ;
4+ using System . Collections . Immutable ;
45using System . Linq ;
5- using System . Reflection ;
66using Microsoft . CodeAnalysis ;
7+ using Microsoft . CodeAnalysis . Diagnostics ;
78using Schema . NET . Tool . CustomOverrides ;
89using Schema . NET . Tool . GeneratorModels ;
910using Schema . NET . Tool . Repositories ;
1011using Schema . NET . Tool . Services ;
1112
1213[ Generator ]
13- public class SchemaSourceGenerator : ISourceGenerator
14+ public class SchemaSourceGenerator : IIncrementalGenerator
1415{
15- private const string SchemaDataName = "Schema.NET.Tool.Data. schemaorg-all-https.jsonld" ;
16+ private const string SchemaDataName = "schemaorg-all-https.jsonld" ;
1617
17- public void Initialize ( GeneratorInitializationContext context )
18+ public void Initialize ( IncrementalGeneratorInitializationContext context )
1819 {
20+ var schemaJsonLdDataFile = context . AdditionalTextsProvider
21+ . Where ( static text => text . Path . EndsWith ( SchemaDataName , StringComparison . OrdinalIgnoreCase ) )
22+ . Collect ( ) ;
23+
24+ var configuration = context . AnalyzerConfigOptionsProvider
25+ . Select ( ( provider , _ ) => provider . GlobalOptions ) ;
26+
27+ context . RegisterSourceOutput ( configuration . Combine ( schemaJsonLdDataFile ) , Generate ) ;
1928 }
2029
21- public void Execute ( GeneratorExecutionContext context )
30+ private static void Generate ( SourceProductionContext context , ( AnalyzerConfigOptions Options , ImmutableArray < AdditionalText > AdditionalText ) data )
2231 {
23- var schemaDataStream = Assembly
24- . GetExecutingAssembly ( )
25- . GetManifestResourceStream ( SchemaDataName ) ??
26- throw new InvalidOperationException ( $ "Schema data file '{ SchemaDataName } ' not found.") ;
32+ var schemaJsonLdDataFile = data . AdditionalText . SingleOrDefault ( ) ??
33+ throw new InvalidOperationException ( $ "Schema data file '{ SchemaDataName } ' not configured.") ;
34+
35+ var schemaJsonLdData = schemaJsonLdDataFile . GetText ( context . CancellationToken ) ??
36+ throw new InvalidOperationException ( $ "Unable to read schema data file '{ SchemaDataName } '.") ;
2737
28- var schemaRepository = new SchemaRepository ( schemaDataStream ) ;
38+ var schemaRepository = new SchemaRepository ( schemaJsonLdData ) ;
2939 var schemaService = new SchemaService (
3040 new IClassOverride [ ]
3141 {
@@ -35,12 +45,9 @@ public void Execute(GeneratorExecutionContext context)
3545 } ,
3646 Array . Empty < IEnumerationOverride > ( ) ,
3747 schemaRepository ,
38- IncludePendingSchemaObjects ( context ) ) ;
39-
40- #pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
41- var schemaObjects = schemaService . GetObjectsAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
42- #pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
48+ IncludePendingSchemaObjects ( data . Options ) ) ;
4349
50+ var schemaObjects = schemaService . GetObjects ( ) ;
4451 if ( schemaObjects is not null )
4552 {
4653 foreach ( var schemaObject in schemaObjects )
@@ -60,12 +67,9 @@ public void Execute(GeneratorExecutionContext context)
6067 }
6168 }
6269
63- private static bool IncludePendingSchemaObjects ( GeneratorExecutionContext context )
64- {
65- var configuration = context . AnalyzerConfigOptions . GlobalOptions ;
66- return configuration . TryGetValue ( $ "build_property.IncludePendingSchemaObjects", out var value ) &&
70+ private static bool IncludePendingSchemaObjects ( AnalyzerConfigOptions options ) =>
71+ options . TryGetValue ( $ "build_property.IncludePendingSchemaObjects", out var value ) &&
6772 value . Equals ( "true" , StringComparison . OrdinalIgnoreCase ) ;
68- }
6973
7074 private static string RenderClass ( GeneratorSchemaClass schemaClass )
7175 {
0 commit comments