-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
488 lines (423 loc) · 18.3 KB
/
Program.cs
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.Json.Serialization;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace JsonAotTool
{
class Program
{
public static int OPERATION = 0; // 0 is bad, 1 is SETUP, 2 is RUN
public static int WRITEMODE = 0; // 0 is append, 1 is overwrite
public static bool WRITETOPROGRAM = false; // write to Program.cs or not
public static readonly Dictionary<string, Action> RunParams = new Dictionary<string, Action>()
{
{"-a", SETWRITEMODE_APPEND },
{"-o", SETWRITEMODE_OVERWRITE },
{"-s", SETWRITETOPROGRAM },
};
public static readonly Dictionary<string, int> StartParam = new Dictionary<string, int>()
{
{"-run", 2},
{"-setup", 1}
};
static void Main(string[] args)
{
if (args.Length == 0)
{
ShowHelp();
return;
}
var command = args[0];
for(int i = 0; i < args.Length; i++)
{
if(i == 0)
{
bool goodval = StartParam.TryGetValue(args[0], out int OPERATIONCODE);
if(goodval == true)
{
OPERATION = OPERATIONCODE;
}
} else if (i != 0)
{
if (RunParams.ContainsKey(args[i]))
{
try
{
RunParams[args[i]].Invoke();
}
catch
{
// nothing here
}
} else
{
Console.WriteLine($"Invalid parameter {args[i]} detected. Terminating.");
ShowHelp();
Environment.Exit(0);
return;
}
}
}
switch (OPERATION)
{
case 1:
CommandHandlers.HandleSetup();
break;
case 2:
CommandHandlers.HandleRun();
break;
default:
Console.WriteLine($"Invalid operation parameter {command} detected. Terminating.");
ShowHelp();
Environment.Exit(0);
break;
}
}
#region RUNPARSE
public static void SETWRITEMODE_APPEND()
{
if (OPERATION == 2)
{
WRITEMODE = 0;
} else
{
Console.WriteLine("Parameter for --run detected. Terminating.");
}
}
public static void SETWRITEMODE_OVERWRITE()
{
if (OPERATION == 2)
{
WRITEMODE = 1;
} else
{
Console.WriteLine("Parameter for --run detected. Terminating.");
Environment.Exit(0);
return;
}
}
public static void BADRUNPARAM()
{
Console.WriteLine("Invalid parameter for --run detected.");
Environment.Exit(0);
return;
}
#endregion
#region RUNSETUP
public static void SETWRITETOPROGRAM()
{
if (OPERATION == 1)
{
WRITETOPROGRAM = true;
} else
{
Console.WriteLine("Parameter for --setup detected. Terminating.");
Environment.Exit(0);
return;
}
}
#endregion
private static void ShowHelp()
{
Console.Write(@"
JsonAot -- Quickly setup your project classes to use AOT compliant JSON serialization.
Add the attribute [JsonAot] to all classes slated for serialization or deserialization.
Run JsonAot in the root of your project solution and all *.cs class files will be scanned
and all classes slated for serialization willl automatically have their source gen code
generated. Use the built-in Serialize and Deserialize functions for seamless useage.
Parameter: Desc:
-setup Setups initial JSONHandler file, making features accessible.
-s Places '");
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("global using ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("JSONHandler;");
Console.ResetColor();
Console.WriteLine(@"' into local Program.cs
-run Scans project directory for all [JsonAot] classes, then
automatically generates source gen. code for tagged classes.
-a During run, append all findings to JSONHandler (can result in
duplicated classes/entries).
-o During run, completely overwrites JSONHandler (default).");
Console.ReadKey();
}
class CommandHandlers
{
public static void HandleSetup()
{
JSONHandlerGenerator.CreateJSONHandlerFile();
if (WRITETOPROGRAM == true)
{
JSONHandlerGenerator.ModifyProgramCS();
}
}
public static void HandleRun()
{
List<string> ClassNames = new List<string>();
ScanSolutionForAttribute();
UpdateJSONHandler(ClassNames);
void ScanSolutionForAttribute()
{
string solutionPath = Directory.GetCurrentDirectory();
var syntaxTrees = Directory.GetFiles(solutionPath, "*.cs", SearchOption.AllDirectories)
.Select(path => CSharpSyntaxTree.ParseText(File.ReadAllText(path)))
.ToList();
var compilation = CSharpCompilation.Create("MyCompilation", syntaxTrees)
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
var rootNamespace = syntaxTrees.Select(tree => compilation.GetSemanticModel(tree).Compilation.GlobalNamespace);
foreach (var ns in rootNamespace)
{
ProcessNamespace(ns);
#if DEBUG
Console.WriteLine($"Checking namespace {ns.Name}");
#endif
}
}
void ProcessNamespace(INamespaceSymbol ns, int x = 1)
{
string tabs_ = new string('\t', x);
foreach (var type in ns.GetTypeMembers())
{
ProcessType(type);
#if DEBBUG
Console.WriteLine($"{tabs_}Checking type {ns.Name}");
#endif
}
foreach (var nestedNs in ns.GetNamespaceMembers())
{
ProcessNamespace(nestedNs, x + 1);
#if DEBUG
Console.WriteLine($"{tabs_}Checking process {nestedNs.Name}");
#endif
}
}
void ProcessType(INamedTypeSymbol type)
{
foreach (var attribute in type.GetAttributes())
{
if (attribute.AttributeClass.Name.Contains("JsonA", StringComparison.CurrentCultureIgnoreCase))
{
ClassNames.Add(type.Name);
#if DEBUG
Console.WriteLine($"Added {type.Name} to ClassNames.");
#endif
break; // Stop processing attributes for this type
}
}
#if DEBUG
Console.WriteLine($"Checking type {type.ContainingNamespace}.{type.Name}");
#endif
}
void UpdateJSONHandler(List<string> taggedClasses)
{
taggedClasses = taggedClasses.Distinct(StringComparer.CurrentCultureIgnoreCase).ToList();
string jsonHandlerPath = "JSONHandler.cs";
List<string> AllPartialClasses = new List<string>();
List<string> AllDictionaryEntries = new List<string>();
foreach(string JSONAOTClass in taggedClasses)
{
string PartialClass = ($"\n [JsonSerializable(typeof({JSONAOTClass}))]\n public partial class {JSONAOTClass}Context : JsonSerializerContext {{ }}\n");
string DictionaryEntry = ($@" {{ typeof({JSONAOTClass}), new {JSONAOTClass}Context().GetTypeInfo(typeof({JSONAOTClass})) }},");
AllPartialClasses.Add(PartialClass);
AllDictionaryEntries.Add(DictionaryEntry);
}
if(WRITEMODE == 0)
{
AppendToJSONHandler();
} else if (WRITEMODE == 1)
{
OverwriteToJSONHandler();
}
void OverwriteToJSONHandler()
{
string jsonHandlerTop = @"
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
namespace JSONHandler
{
public static class JSONHandler
{
private static readonly Dictionary<Type, JsonTypeInfo> _serializerContexts = new Dictionary<Type, JsonTypeInfo>(){
";
string jsonHandlerMiddle = @"
};
public static string Serialize<T>(T obj)
{
if (!_serializerContexts.TryGetValue(typeof(T), out var context))
{
throw new ArgumentException($""No serializer context found for type { typeof(T)}"");
}
return System.Text.Json.JsonSerializer.Serialize(obj, context);
}
public static T Deserialize<T>(string json)
{
if (!_serializerContexts.TryGetValue(typeof(T), out var context))
{
throw new ArgumentException($""No serializer context found for type {typeof(T)}"");
}
return System.Text.Json.JsonSerializer.Deserialize<T>(json, (JsonTypeInfo<T>)context);
}
}
#region ContextualizationOfClasses
// space for adding context classes
";
string jsonHandlerBottom = (@"
#endregion
[AttributeUsage(AttributeTargets.Class)]
public class JsonAotAttribute : Attribute { }
}
");
StringBuilder entireJSONHandler = new StringBuilder(jsonHandlerTop);
foreach(string dictentry in AllDictionaryEntries)
{
entireJSONHandler.AppendLine($"\t{dictentry}");
}
entireJSONHandler.Append(jsonHandlerMiddle);
foreach(string partialclass in AllPartialClasses)
{
entireJSONHandler.AppendLine($"{partialclass}");
}
entireJSONHandler.Append(jsonHandlerBottom);
bool errorEncountered = false;
try
{
File.Delete(jsonHandlerPath);
} catch
{
errorEncountered = true;
Console.WriteLine("Encountered error trying to remove old JSONHandler.cs\nAttempting to rewrite.");
}
try
{
File.WriteAllText(jsonHandlerPath, entireJSONHandler.ToString());
}
catch
{
errorEncountered = true;
} finally {
if(errorEncountered == false)
{
Console.WriteLine("JSONHandler.cs updated successfully!");
} else
{
Console.WriteLine("Errors were encountered during write process to JSONHandler.cs");
}
}
}
void AppendToJSONHandler()
{
// Read the existing JSONHandler content
List<string> jsonHandlerContent = File.ReadAllLines(jsonHandlerPath).ToList();
//new Dictionary<Type, JsonTypeInfo> {
// Find insertion points and update the content
int contextRegionStart = jsonHandlerContent
.Select((line, index) => new { Line = line, Index = index })
.FirstOrDefault(item => item.Line.Contains("#region ContextualizationOfClasses", StringComparison.CurrentCultureIgnoreCase))?
.Index ?? -1;
int dictionaryStart = jsonHandlerContent
.Select((line, index) => new { Line = line, Index = index })
.FirstOrDefault(item => item.Line.Contains("static readonly Dictionary<Type, JsonTypeInfo>", StringComparison.CurrentCultureIgnoreCase))?
.Index ?? -1;
// Insert partial classes first, as inserting into the dictionary first could alter the index of
// the region.
foreach (string partialclass in AllPartialClasses)
{
jsonHandlerContent.Insert(contextRegionStart + 1, partialclass);
}
foreach (string dictionaryentry in AllDictionaryEntries)
{
jsonHandlerContent.Insert(dictionaryStart + 1, '\t' + dictionaryentry);
}
bool errorEncountered = false;
try
{
// Write the modified content back to the file
File.WriteAllLines(jsonHandlerPath, jsonHandlerContent);
}
catch
{
errorEncountered = true;
}
finally
{
if (errorEncountered == false)
{
Console.WriteLine("JSONHandler.cs updated successfully!");
}
else
{
Console.WriteLine("Errors were encountered during write process to JSONHandler.cs");
}
}
}
}
}
}
public static class JSONHandlerGenerator
{
public static void CreateJSONHandlerFile()
{
string jsonHandlerContent = @"
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
namespace JSONHandler
{
public static class JSONHandler
{
private static readonly Dictionary<Type, JsonTypeInfo> _serializerContexts = new Dictionary<Type, JsonTypeInfo>(){
};
public static string Serialize<T>(T obj)
{
if (!_serializerContexts.TryGetValue(typeof(T), out var context))
{
throw new ArgumentException($""No serializer context found for type { typeof(T)}"");
}
return System.Text.Json.JsonSerializer.Serialize(obj, context);
}
public static T Deserialize<T>(string json)
{
if (!_serializerContexts.TryGetValue(typeof(T), out var context))
{
throw new ArgumentException($""No serializer context found for type {typeof(T)}"");
}
return System.Text.Json.JsonSerializer.Deserialize<T>(json, (JsonTypeInfo<T>)context);
}
}
#region ContextualizationOfClasses
// space for adding context classes
#endregion
[AttributeUsage(AttributeTargets.Class)]
public class JsonAotAttribute : Attribute { }
}
";
File.WriteAllText("JSONHandler.cs", jsonHandlerContent);
Console.WriteLine("JSONHandler.cs created!");
}
public static void ModifyProgramCS()
{
string programCsPath = "Program.cs"; // Assume it's in the same directory
if (!File.Exists(programCsPath))
{
Console.Error.WriteLine("Error: Program.cs not found.");
return;
}
string[] programCsLines = File.ReadAllLines(programCsPath);
using (StreamWriter writer = new StreamWriter(programCsPath))
{
writer.WriteLine("global using static JSONHandler.JSONHandler; // Streamline serialization and deserialization\r\nglobal using JSONHandler;"); // Insert at the top
foreach (string line in programCsLines)
{
writer.WriteLine(line);
}
}
Console.WriteLine("Program.cs modified successfully!");
}
}
}
}