-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppSettingsGen.cs
600 lines (510 loc) · 20.5 KB
/
AppSettingsGen.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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using NotNot.AppSettings;
[assembly: InternalsVisibleTo("NotNot.AppSettings.Tests")]
namespace NotNot;
/// <summary>
/// generate settings classes from appsettings.json files. These will be namespaced as [TargetProjectNamespaceRoot].AppSettings.[ConfigName]
/// </summary>
[Generator]
internal class AppSettingsGen : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
#if DEBUG
////if enabled, this will allow you to attach and debug sourcegen when building the target project.
//if (!Debugger.IsAttached)
//{
// Debugger.Launch();
//}
#endif
///////////// NEW ADDITIONAL FILES WORKFLOW
{
// Get the MSBuild property <NotNot_AppSettings_GenPublic>true</NotNot_AppSettings_GenPublic> from the consuming project .csproj file
var genPublicProvider = context.AnalyzerConfigOptionsProvider
.Select((provider, ct) =>
{
//IMPORTANT!: this property has to be whitelisted in the `NotNot.AppSettings.targets` file, which is included in the consuming project.
// First try reading directly from build_property
provider.GlobalOptions.TryGetValue("build_property.NotNot_AppSettings_GenPublic", out var genPublic);
// If not found or empty, try reading from MSBuild properties
if (string.IsNullOrEmpty(genPublic))
{
provider.GlobalOptions.TryGetValue("build_metadata.NotNot_AppSettings_GenPublic", out genPublic);
}
// Parse as boolean, defaulting to false if parsing fails or value is not found
return !string.IsNullOrEmpty(genPublic) && bool.TryParse(genPublic, out var result) && result;
});
//get appsettings*.json via AdditionalFiles
var regex = new Regex(@"\\appsettings\..*json$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
var additionalFiles = context.AdditionalTextsProvider.Where(file =>
{
var result = regex.IsMatch(file.Path);
return result;
});
// Transform additionalFiles to a single Dictionary entry
var combinedSourceTextsProvider = additionalFiles.Collect().Select((files, ct) =>
{
//Debug.WriteLine($"additionalFiles = {files.Length}");
var combinedFiles = new Dictionary<string, SourceText>();
foreach (var file in files)
{
//Debug.WriteLine($"file = {file.Path}");
var sourceText = file.GetText(ct);
if (sourceText != null)
{
combinedFiles[file.Path] = sourceText;
}
}
return combinedFiles;
});
var namespaceProvider = context.AnalyzerConfigOptionsProvider
.Select(static (provider, ct) =>
{
provider.GlobalOptions.TryGetValue("build_property.rootnamespace", out string? rootNamespace);
return rootNamespace;
});
var combinedProvider = namespaceProvider.Combine(combinedSourceTextsProvider).Combine(genPublicProvider);
context.RegisterSourceOutput(
combinedProvider,
(spc, content) =>
{
var rootNamespace = content.Left.Left;
var combinedSourceTexts = content.Left.Right;
var genPublic = content.Right;
string version = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion
?? Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyVersionAttribute>()?.Version.ToString()
?? Assembly.GetExecutingAssembly().GetName().ToString();
if(version?.IndexOf("+")>0)
{
version = version.Substring(0, version.IndexOf("+"));
}
var config = new AppSettingsGenConfig
{
RootNamespace = rootNamespace,
IsPublic = genPublic,
CombinedSourceTexts = combinedSourceTexts,
NugetVersion =version,
};
//config.GenSemVer += $" {config.IsPublic} {DateTime.Now}";
ExecuteGenerator(spc, config);
});
}
//////////////// OLD FILE.IO WORKFLOW Works but frowned upon for sourcegen. Switched to SourceText
//{
// var projectDirProvider = context.AnalyzerConfigOptionsProvider
// .Select(static (provider, ct) =>
// {
// provider.GlobalOptions.TryGetValue("build_property.projectdir", out string? projectDirectory);
// provider.GlobalOptions.TryGetValue("build_property.rootnamespace", out string? assemblyName);
// return (projectDirectory, assemblyName);
// });
// context.RegisterSourceOutput(
// projectDirProvider,
// (spc, settings) =>
// {
// ExecuteGenerator_FileIo(spc, settings);
// });
//}
}
public void ExecuteGenerator(SourceProductionContext spc, AppSettingsGenConfig config)
{
var diagReports = new List<Diagnostic>();
var results = GenerateSourceFiles(config, diagReports);
foreach (var report in diagReports)
{
spc.ReportDiagnostic(report);
}
foreach (var result in results)
{
spc.AddSource(result.Key, result.Value);
}
spc._Info("done");
}
/// <summary>
/// will generate strongly typed c# classes for each matched (appsettings).json
/// </summary>
/// <param name="diagReport">helper for accumulating diag messages. caller should relay them to appropriate log writer afterwards.</param>
/// <param name="appSettingsJsonSourceFiles">the appsettings.json files</param>
/// <param name="rootNamespace">{rootNamespace}.AppSettingsGen is used as the namespace of generated files</param>
/// <returns>the "output" C#, source generated files</returns>
public Dictionary<string, SourceText> GenerateSourceFiles(AppSettingsGenConfig config, List<Diagnostic> diagReport)
{
var toReturn = new Dictionary<string, SourceText>();
//if (rootNamespace is null)
//{
// diagReport._Error($"missing required inputs. rootNamespace={rootNamespace}");
// return toReturn;
//}
if (config.CombinedSourceTexts.Count == 0)
{
diagReport._Error($"No appSettings.json files were found in your project. SourceGen aborted. In Project Properties, Make sure it's BuildAction=C# Analyzer, and copy-to-output=ALWAYS.");
return toReturn;
}
diagReport._Info($"rootNamespace={config.RootNamespace}, appSettingsJsonSourceFiles.Count={config.CombinedSourceTexts.Count}");
//merge into one big json
var allJsonDict = JsonMerger.MergeJsonFiles(config.CombinedSourceTexts, diagReport);
//generate classes for the entire json hiearchy
GenerateFilesWorker(diagReport, toReturn, allJsonDict, "AppSettings", $"{config.StartingNamespace}", config);
AddBinderShims(diagReport, toReturn, config);
return toReturn;
}
/// <summary>
/// add helper service to automatically populate appsettings from disk
/// </summary>
/// <param name="diagReport"></param>
/// <param name="toReturn"></param>
/// <param name="startingNamespace"></param>
private void AddBinderShims(List<Diagnostic> diagReport, Dictionary<string, SourceText> toReturn, AppSettingsGenConfig config)
{
var builder = new StringBuilder();
builder.Append(@$"
/**
* This file is generated by the NotNot.AppSettings nuget package (v{config.NugetVersion}).
* Do not edit this file directly, instead edit the appsettings.json files and rebuild the project.
* `AddBinderShims()` was called.
**/
using Microsoft.Extensions.Configuration;
namespace {config.StartingNamespace};
/// <summary>
/// Strongly typed AppSettings.json, recreated every build.
/// <para>You can use this directly, extend it (it's a partial class),
/// or get a populated instance of it via the <see cref=""AppSettingsBinder""/> DI service</para>
/// </summary>
{config.GenAccessModifier} partial class AppSettings
{{
}}
/// <summary>
/// a DI service that contains a strongly-typed copy of your appsettings.json
/// <para><strong>DI Usage:</strong></para>
/// <para><c>builder.Services.AddSingleton<IAppSettingsBinder, AppSettingsBinder>();</c></para>
/// <para><c>var app = builder.Build();</c></para>
/// <para><c>var appSettings = app.Services.GetRequiredService<IAppSettingsBinder>().AppSettings;</c></para>
/// <para><strong>Non-DI Usage:</strong></para>
/// <para><c>var appSettings = AppSettingsBinder.LoadDirect();</c></para>
/// </summary>
{config.GenAccessModifier} partial class AppSettingsBinder : IAppSettingsBinder
{{
public AppSettings AppSettings {{ get; protected set; }}
public AppSettingsBinder(IConfiguration _config)
{{
AppSettings = new AppSettings();
//automatically reads and binds to config file
_config.Bind(AppSettings);
}}
/// <summary>
/// Manually construct an AppSettings from your appsettings.json files.
/// <para>NOTE: This method is provided for non-DI users. If you use DI, don't use this method. Instead just register this class as a service.</para>
/// </summary>
/// <param name=""appSettingsLocation"">folder where to search for appsettings.json. defaults to current app folder.</param>
/// <param name=""appSettingsFileNames"">lets you override the files to load up. defaults to 'appsettings.json' and 'appsettings.{{DOTNET_ENVIRONMENT}}.json'</param>
/// <param name=""throwIfFilesMissing"">default is to silently ignore if any of the .json files are missing.</param>
/// <returns>your strongly typed appsettings with values from your .json loaded in</returns>
public static AppSettings LoadDirect(string? appSettingsLocation = null,IEnumerable<string>? appSettingsFileNames=null,bool throwIfFilesMissing=false )
{{
//pick what .json files to load
if (appSettingsFileNames is null)
{{
//figure out what env
var env = Environment.GetEnvironmentVariable(""ASPNETCORE_ENVIRONMENT"");
env ??= Environment.GetEnvironmentVariable(""DOTNET_ENVIRONMENT"");
env ??= Environment.GetEnvironmentVariable(""ENVIRONMENT"");
//env ??= ""Development""; //default to ""Development
if (env is null)
{{
appSettingsFileNames = new[] {{ ""appsettings.json"" }};
}}
else
{{
appSettingsFileNames = new[] {{ ""appsettings.json"", $""appsettings.{{env}}.json"" }};
}}
}}
//build a config from the specified files
var builder = new ConfigurationBuilder();
if (appSettingsLocation != null)
{{
builder.SetBasePath(appSettingsLocation);
}}
var optional = !throwIfFilesMissing;
foreach (var fileName in appSettingsFileNames)
{{
builder.AddJsonFile(fileName, optional: optional, reloadOnChange: false); // Add appsettings.json
}}
IConfigurationRoot configuration = builder.Build();
//now finally get the appsettings we care about
var binder = new AppSettingsBinder(configuration);
return binder.AppSettings;
}}
/// <summary>
/// helper to create an AppSettings from a string containing your json
/// </summary>
/// <param name=""appSettingsJsonText""></param>
/// <returns></returns>
public static AppSettings LoadDirectFromText(string appSettingsJsonText)
{{
//build a config from the specified files
var builder = new ConfigurationBuilder();
var configurationBuilder = new ConfigurationBuilder();
IConfigurationRoot configuration;
using (var stream = new MemoryStream())
{{
using (var writer = new StreamWriter(stream))
{{
writer.Write(appSettingsJsonText);
writer.Flush();
stream.Position = 0;
configurationBuilder.AddJsonStream(stream);
configuration = configurationBuilder.Build();
}}
}}
//now finally get the appsettings we care about
var binder = new AppSettingsBinder(configuration);
return binder.AppSettings;
}}
/// <summary>
/// helper to create an AppSettings from strings containing your json
/// </summary>
/// <param name=""appSettingsJsonText""></param>
/// <returns></returns>
public static AppSettings LoadDirectFromTexts(params string[] appSettingsJsonTexts)
{{
//build a config from the specified files
var configurationBuilder = new ConfigurationBuilder();
IConfigurationRoot RecursiveLoader(Queue<string> textsQueue)
{{
using var stream = new MemoryStream();
using var writer = new StreamWriter(stream);
if (textsQueue.Count > 0)
{{
var appSettingsJsonText = textsQueue.Dequeue();
writer.Write(appSettingsJsonText);
writer.Flush();
stream.Position = 0;
configurationBuilder.AddJsonStream(stream);
return RecursiveLoader(textsQueue);
}}
else
{{
return configurationBuilder.Build();
}}
}}
var configuration = RecursiveLoader(new Queue<string>(appSettingsJsonTexts));
//now finally get the appsettings we care about
var binder = new AppSettingsBinder(configuration);
return binder.AppSettings;
}}
}}
/// <summary>
/// a DI service that contains a strongly-typed copy of your appsettings.json
/// <para><strong>DI Usage:</strong></para>
/// <para><c>builder.Services.AddSingleton<IAppSettingsBinder, AppSettingsBinder>();</c></para>
/// <para><c>var app = builder.Build();</c></para>
/// <para><c>var appSettings = app.Services.GetRequiredService<IAppSettingsBinder>().AppSettings;</c></para>
/// <para><strong>Non-DI Usage:</strong></para>
/// <para><c>var appSettings = AppSettingsBinder.LoadDirect();</c></para>
/// </summary>
{config.GenAccessModifier} interface IAppSettingsBinder
{{
public AppSettings AppSettings {{ get; }}
}}
");
var source = SourceText.From(builder.ToString(), Encoding.UTF8);
toReturn.Add("_BinderShims.cs", source);
}
/// <summary>
/// get the c# type of the element. however keep in mind that arrays won't include the '[]'. Check if array via `elm.ValueKind == JsonValueKind.Array`
/// </summary>
/// <param name="elm"></param>
/// <param name="currentName"></param>
/// <param name="currentNamespace"></param>
/// <returns>returns name of json primitive, "object" for null/undefined nodes, named nodes for other json objects</returns>
/// <exception cref="ArgumentException"></exception>
public string GetSourceTypeName(JsonElement elm, string currentName, string currentNamespace, AppSettingsGenConfig config)
{
string toReturn;
switch (elm.ValueKind)
{
case JsonValueKind.String:
toReturn = "string";
break;
case JsonValueKind.Number:
toReturn = "double";
break;
case JsonValueKind.True:
case JsonValueKind.False:
toReturn = "bool";
break;
case JsonValueKind.Null:
case JsonValueKind.Undefined:
toReturn = "object";
break;
case JsonValueKind.Object:
toReturn = $"{currentNamespace}.{currentName}";
break;
case JsonValueKind.Array:
//unify all children into one type
//if mix of various types (such as object+primitive, or different primitive types), will return back "object" and user will have to cast manually.
string? unifiedChildType = null;
foreach (var child in elm.EnumerateArray())
{
var childType = GetSourceTypeName(child, currentName, currentNamespace, config);
if (unifiedChildType is null)
{
unifiedChildType = childType;
}
else if (unifiedChildType != childType)
{
unifiedChildType = "object";
break;
}
}
unifiedChildType ??= "object";
toReturn = unifiedChildType;
break;
default:
throw new ArgumentException($"unknown type returned from json, {elm}", nameof(elm));
}
return toReturn;
}
/// <summary>
/// generate files for the given json hierarchy, recursively calling itself for each child node
/// </summary>
protected void GenerateFilesWorker(List<Diagnostic> diagReport, Dictionary<string, SourceText> generatedSourceFiles, Dictionary<string, JsonElement> currentNode, string currentNodeName, string currentNamespace, AppSettingsGenConfig config)
{
//build currentNode into file
var currentClassName = currentNodeName._ConvertToAlphanumericCaps();
var filename = $"{currentNamespace}.{currentClassName}.cs";
var propertyBuilder = new StringBuilder();
foreach (var kvp in currentNode)
{
var propertyName = kvp.Key._ConvertToAlphanumericCaps();
var propertyNamespace = $"{currentNamespace}._{currentClassName}";
var valueType = GetSourceTypeName(kvp.Value, propertyName, propertyNamespace, config);
if (kvp.Value.ValueKind == JsonValueKind.Array)
{
valueType += "[]";
}
propertyBuilder.Append($" public {valueType}? {propertyName}{{get; set;}}\n");
}
var sourceBuilder = new StringBuilder();
sourceBuilder.Append(@$"
/**
* This file is generated by the NotNot.AppSettings nuget package (v{config.NugetVersion}).
* Do not edit this file directly, instead edit the appsettings.json files and rebuild the project.
* `GenerateFilesWorker()` was called for {currentNodeName}
**/
using System;
using System.Runtime.CompilerServices;
namespace {currentNamespace};
[CompilerGenerated]
{config.GenAccessModifier} partial class {currentClassName} {{
{propertyBuilder}
}}
");
var source = SourceText.From(sourceBuilder.ToString(), Encoding.UTF8);
generatedSourceFiles.Add(filename, source);
//recurse into children
foreach (var kvp in currentNode)
{
var propertyNamespace = $"{currentNamespace}._{currentClassName}";
var jsonKind = kvp.Value.ValueKind;
var propertyName = kvp.Key._ConvertToAlphanumericCaps();
switch (jsonKind)
{
case JsonValueKind.Object:
{
var childNode = kvp.Value.Deserialize<Dictionary<string, JsonElement>>(JsonMerger._serializerOptions)!;
GenerateFilesWorker(diagReport, generatedSourceFiles, childNode, propertyName, propertyNamespace,config);
}
break;
case JsonValueKind.Array:
{
var childNodes = kvp.Value.Deserialize<List<JsonElement>>(JsonMerger._serializerOptions)!;
//get name of node
var arrayTypeName = GetSourceTypeName(kvp.Value, propertyName, propertyNamespace, config);
switch (arrayTypeName)
{
case "string":
case "double":
case "bool":
case "object": //returns object for null/undefined nodes. (named nodes for other objects)
//no need to recurse
break;
default:
//squash children into singular object then generate for it
var squashedChildren = new Dictionary<string, JsonElement>();
foreach (var child in childNodes)
{
JsonMerger.MergeJson(squashedChildren, child);
}
GenerateFilesWorker(diagReport, generatedSourceFiles, squashedChildren, propertyName, propertyNamespace, config);
break;
}
}
break;
default:
//a "primitive" json type, so no need to recurse
break;
}
}
}
//[Obsolete("uses File.IO to read. Works but frowned upon for sourcegen. Switched to SourceText", true)]
//public void ExecuteGenerator_FileIo(SourceProductionContext spc, (string? projectDirectory, string? startingNamespace) settings)
//{
// var diagReports = new List<Diagnostic>();
// var results = GenerateSourceFiles_FileIo(settings, diagReports);
// foreach (var report in diagReports)
// {
// spc.ReportDiagnostic(report);
// }
// foreach (var result in results)
// {
// spc.AddSource(result.Key, result.Value);
// }
// spc._Info("done");
//}
///// <summary>
///// for the given fileSearchPattern, will generate strongly typed c# classes for each matched (appsettings).json file found in the projectDirectory
///// </summary>
///// <param name="settings"></param>
///// <param name="diagReport">helper for accumulating diag messages. caller should relay them to appropriate log writer afterwards.</param>
///// <param name="fileSearchPattern">defaults to "appsettings*.json"</param>
///// <returns></returns>
//[Obsolete("uses File.IO to read. Works but frowned upon for sourcegen. Switched to SourceText", true)]
//public Dictionary<string, SourceText> GenerateSourceFiles_FileIo((string? projectDirectory
// , string? startingNamespace) settings, List<Diagnostic> diagReport
// , string fileSearchPattern = "appsettings*.json")
//{
// var (projectDir, startingNamespace) = settings;
// var toReturn = new Dictionary<string, SourceText>();
// if (projectDir is null || startingNamespace is null)
// {
// diagReport._Error($"null required inputs projectDir={projectDir}, startingNamespace={startingNamespace}");
// return toReturn;
// }
// else
// {
// diagReport._Info($"projectDir {projectDir} ");
// }
// startingNamespace = $"{startingNamespace}.AppSettingsGen";
// //do stuff with project dir
// var dir = new DirectoryInfo(projectDir);
// var files = dir.EnumerateFiles(fileSearchPattern, SearchOption.TopDirectoryOnly).ToList();
// diagReport._Info($"files count {files.Count()} ");
// //merge into one big json
// var allJsonDict = JsonMerger.MergeJsonFiles(files, diagReport);
// //generate classes for the entire json hiearchy
// GenerateFilesWorker(diagReport, toReturn, allJsonDict, "AppSettings", $"{startingNamespace}");
// AddBinderShims(diagReport, toReturn, startingNamespace);
// return toReturn;
//}
}