Skip to content

Commit 4b0906f

Browse files
TurnerjRehanSaeed
authored andcommitted
Use Incremental Source Generators
1 parent 6d606c7 commit 4b0906f

File tree

7 files changed

+43
-42
lines changed

7 files changed

+43
-42
lines changed

Source/Schema.NET.Pending/Schema.NET.Pending.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@
2727
<Compile Include="../Common/*.*" />
2828
</ItemGroup>
2929

30+
<ItemGroup Label="Source Generation">
31+
<AdditionalFiles Include="../../Data/*.*" LinkBase="Data" />
32+
</ItemGroup>
33+
3034
</Project>

Source/Schema.NET/Schema.NET.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@
2727
<Compile Include="../Common/*.*" />
2828
</ItemGroup>
2929

30+
<ItemGroup Label="Source Generation">
31+
<AdditionalFiles Include="../../Data/*.*" LinkBase="Data" />
32+
</ItemGroup>
33+
3034
</Project>
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
namespace Schema.NET.Tool.Repositories;
22

33
using System.Collections.Generic;
4-
using System.Threading.Tasks;
54
using Schema.NET.Tool.Models;
65

76
public interface ISchemaRepository
87
{
9-
Task<(IEnumerable<SchemaClass> Classes, IEnumerable<SchemaProperty> Properties, IEnumerable<SchemaEnumerationValue> EnumerationValues)> GetObjectsAsync();
8+
(IEnumerable<SchemaClass> Classes, IEnumerable<SchemaProperty> Properties, IEnumerable<SchemaEnumerationValue> EnumerationValues) GetObjects();
109
}

Tools/Schema.NET.Tool/Repositories/SchemaRepository.cs

+7-14
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,23 @@ namespace Schema.NET.Tool.Repositories;
22

33
using System;
44
using System.Collections.Generic;
5-
using System.IO;
65
using System.Linq;
76
using System.Text.Json;
87
using System.Text.Json.Serialization;
9-
using System.Threading.Tasks;
8+
using Microsoft.CodeAnalysis.Text;
109
using Schema.NET.Tool.Models;
1110

1211
public class SchemaRepository : ISchemaRepository
1312
{
14-
private readonly Stream stream;
13+
private readonly SourceText jsonLd;
1514

16-
public SchemaRepository(Stream stream) => this.stream = stream;
15+
public SchemaRepository(SourceText jsonLd) => this.jsonLd = jsonLd;
1716

18-
public async Task<(IEnumerable<SchemaClass> Classes, IEnumerable<SchemaProperty> Properties, IEnumerable<SchemaEnumerationValue> EnumerationValues)> GetObjectsAsync()
17+
public (IEnumerable<SchemaClass> Classes, IEnumerable<SchemaProperty> Properties, IEnumerable<SchemaEnumerationValue> EnumerationValues) GetObjects()
1918
{
20-
var schemaObjects = await this.GetSchemaObjectsAsync().ConfigureAwait(false) ??
19+
var schemaObjects = Deserialize<List<SchemaObject>>(this.jsonLd.ToString(), new SchemaPropertyJsonConverter()) ??
2120
throw new InvalidOperationException("No schema objects found.");
2221

23-
schemaObjects = schemaObjects.ToArray();
24-
2522
var schemaClasses = schemaObjects.OfType<SchemaClass>().ToArray();
2623

2724
foreach (var schemaClass in schemaClasses)
@@ -34,17 +31,13 @@ public class SchemaRepository : ISchemaRepository
3431
schemaObjects.OfType<SchemaEnumerationValue>().ToArray());
3532
}
3633

37-
public async Task<IEnumerable<SchemaObject>?> GetSchemaObjectsAsync() =>
38-
await DeserializeAsync<List<SchemaObject>>(this.stream, new SchemaPropertyJsonConverter())
39-
.ConfigureAwait(false);
40-
41-
private static async Task<T?> DeserializeAsync<T>(Stream jsonStream, JsonConverter converter)
34+
private static T? Deserialize<T>(string json, JsonConverter converter)
4235
{
4336
var options = new JsonSerializerOptions
4437
{
4538
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
4639
};
4740
options.Converters.Add(converter);
48-
return await JsonSerializer.DeserializeAsync<T>(jsonStream, options).ConfigureAwait(false);
41+
return JsonSerializer.Deserialize<T>(json, options);
4942
}
5043
}

Tools/Schema.NET.Tool/Schema.NET.Tool.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
<Generator>ResXFileCodeGenerator</Generator>
6969
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
7070
</EmbeddedResource>
71-
<EmbeddedResource Include="../../Data/*.*" LinkBase="Data" />
7271
</ItemGroup>
7372

7473
</Project>

Tools/Schema.NET.Tool/SchemaSourceGenerator.cs

+24-20
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
11
namespace Schema.NET.Tool;
22

33
using System;
4+
using System.Collections.Immutable;
45
using System.Linq;
5-
using System.Reflection;
66
using Microsoft.CodeAnalysis;
7+
using Microsoft.CodeAnalysis.Diagnostics;
78
using Schema.NET.Tool.CustomOverrides;
89
using Schema.NET.Tool.GeneratorModels;
910
using Schema.NET.Tool.Repositories;
1011
using 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
{

Tools/Schema.NET.Tool/Services/SchemaService.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace Schema.NET.Tool.Services;
66
using System.Linq;
77
using System.Text;
88
using System.Text.RegularExpressions;
9-
using System.Threading.Tasks;
109
using Schema.NET.Tool.CustomOverrides;
1110
using Schema.NET.Tool.GeneratorModels;
1211
using Schema.NET.Tool.Repositories;
@@ -32,11 +31,10 @@ public SchemaService(
3231
this.includePending = includePending;
3332
}
3433

35-
public async Task<IEnumerable<GeneratorSchemaObject>> GetObjectsAsync()
34+
public IEnumerable<GeneratorSchemaObject> GetObjects()
3635
{
37-
var (schemaClasses, schemaProperties, schemaValues) = await this.schemaRepository
38-
.GetObjectsAsync()
39-
.ConfigureAwait(false);
36+
var (schemaClasses, schemaProperties, schemaValues) = this.schemaRepository
37+
.GetObjects();
4038

4139
var isEnumMap = new HashSet<string>(
4240
schemaClasses.Where(c => c.IsEnum).Select(c => c.Label),

0 commit comments

Comments
 (0)