about summary refs log tree commit diff
path: root/Utilities/LibMatrix.JsonSerializerContextGenerator/Program.cs
blob: 95af553a27c98ea9894eeda719fc6cfd68d74c54 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using ArcaneLibs;
using ArcaneLibs.Extensions.Streams;
using LibMatrix.LegacyEvents.EventTypes;

// string binary = args.Length > 1 ? args[0] : Console.ReadLine()!;

// var asm = Assembly.LoadFrom(binary);
File.Delete("EventSerializerContexts.g.cs");
var stream = File.OpenWrite("EventSerializerContexts.g.cs");
var eventContentTypes = new ClassCollector<LegacyEventContent>().ResolveFromAllAccessibleAssemblies();

stream.WriteString("using System.Text.Json.Serialization;\n");

stream.WriteString(string.Join('\n', eventContentTypes.DistinctBy(x => x.Namespace)
    .Select(x => $"using {x.Namespace};")));
stream.WriteString("\n\nnamespace LibMatrix.Generated;\n\n[JsonSourceGenerationOptions(WriteIndented = true)]\n");

// stream.WriteString(string.Join('\n', eventContentTypes//.DistinctBy(x => x.Namespace)
//     .Select(x => $$"""
//                   [JsonSourceGenerationOptions(WriteIndented = true)]
//                   [JsonSerializable(typeof({{x.Name}}))]
//                   internal partial class {{x.Name}}SerializerContext : JsonSerializerContext { }
//
//                   """)));

stream.WriteString(string.Join('\n', eventContentTypes //.DistinctBy(x => x.Namespace)
    .Select(x => $"[JsonSerializable(typeof({x.Name}))]")));

stream.WriteString("\ninternal partial class EventTypeSerializerContext : JsonSerializerContext { }");

await stream.FlushAsync();
stream.Close();