about summary refs log tree commit diff
path: root/Utilities/LibMatrix.JsonSerializerContextGenerator/Program.cs
blob: 0c0277b4fb11366aa64ff22936397fc721176f5f (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.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<EventContent>().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();