1
1
namespace Schema . NET . Tool ;
2
2
3
3
using System ;
4
+ using System . Collections . Immutable ;
4
5
using System . Linq ;
5
- using System . Reflection ;
6
6
using Microsoft . CodeAnalysis ;
7
+ using Microsoft . CodeAnalysis . Diagnostics ;
7
8
using Schema . NET . Tool . CustomOverrides ;
8
9
using Schema . NET . Tool . GeneratorModels ;
9
10
using Schema . NET . Tool . Repositories ;
10
11
using Schema . NET . Tool . Services ;
11
12
12
13
[ Generator ]
13
- public class SchemaSourceGenerator : ISourceGenerator
14
+ public class SchemaSourceGenerator : IIncrementalGenerator
14
15
{
15
- private const string SchemaDataName = "Schema.NET.Tool.Data. schemaorg-all-https.jsonld" ;
16
+ private const string SchemaDataName = "schemaorg-all-https.jsonld" ;
16
17
17
- public void Initialize ( GeneratorInitializationContext context )
18
+ public void Initialize ( IncrementalGeneratorInitializationContext context )
18
19
{
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 ) ;
19
28
}
20
29
21
- public void Execute ( GeneratorExecutionContext context )
30
+ private static void Generate ( SourceProductionContext context , ( AnalyzerConfigOptions Options , ImmutableArray < AdditionalText > AdditionalText ) data )
22
31
{
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 } '.") ;
27
37
28
- var schemaRepository = new SchemaRepository ( schemaDataStream ) ;
38
+ var schemaRepository = new SchemaRepository ( schemaJsonLdData ) ;
29
39
var schemaService = new SchemaService (
30
40
new IClassOverride [ ]
31
41
{
@@ -35,12 +45,9 @@ public void Execute(GeneratorExecutionContext context)
35
45
} ,
36
46
Array . Empty < IEnumerationOverride > ( ) ,
37
47
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 ) ) ;
43
49
50
+ var schemaObjects = schemaService . GetObjects ( ) ;
44
51
if ( schemaObjects is not null )
45
52
{
46
53
foreach ( var schemaObject in schemaObjects )
@@ -60,12 +67,9 @@ public void Execute(GeneratorExecutionContext context)
60
67
}
61
68
}
62
69
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 ) &&
67
72
value . Equals ( "true" , StringComparison . OrdinalIgnoreCase ) ;
68
- }
69
73
70
74
private static string RenderClass ( GeneratorSchemaClass schemaClass )
71
75
{
0 commit comments