-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Imported Tingle.Extensions.Primitives
- Loading branch information
1 parent
fd186cf
commit 326640b
Showing
68 changed files
with
45,300 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
[codespell] | ||
skip = .git,*.pdf,*.svg,*.csv,pnpm-lock.yaml,*/wwwroot | ||
skip = .git,*.pdf,*.svg,*.csv,*.g.cs,countries-20200124.json,iso-639-3.tab,iso-4217.json,countries.json,currencies.json,languages.json | ||
# some modules, parts of regexes, and variable names to ignore | ||
ignore-words-list = aci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
{ | ||
"cSpell.words": [ | ||
"etag", | ||
"Ksuid", | ||
"libphonenumber", | ||
"Newtonsoft", | ||
"Serilog", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
using System.CodeDom.Compiler; | ||
using System.Text; | ||
using System.Text.Json; | ||
|
||
namespace StaticDataGenerator; | ||
|
||
public interface IGenerator | ||
{ | ||
public string OutputFilePath { get; } | ||
public string DataFileName { get; } | ||
public string? OutputFilePathJson { get; } | ||
Task GenerateAsync(CancellationToken cancellationToken = default); | ||
} | ||
|
||
public abstract class AbstractGenerator<T> : IGenerator where T : class, new() | ||
{ | ||
private const string Header = @"//------------------------------------------------------------------------------ | ||
// <auto-generated> | ||
// This code was generated by the StaticDataGenerator source generator | ||
// | ||
// Changes to this file may cause incorrect behavior and will be lost if | ||
// the code is regenerated. | ||
// </auto-generated> | ||
//------------------------------------------------------------------------------ | ||
#nullable enable | ||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member | ||
"; | ||
|
||
private static readonly JsonSerializerOptions serializerOptions = new(JsonSerializerDefaults.Web); | ||
|
||
private readonly IHostEnvironment environment; | ||
private readonly FileDownloader downloader; | ||
|
||
protected AbstractGenerator(IHostEnvironment environment, FileDownloader downloader, string dataFileName) | ||
{ | ||
this.environment = environment ?? throw new ArgumentNullException(nameof(environment)); | ||
this.downloader = downloader ?? throw new ArgumentNullException(nameof(downloader)); | ||
DataFileName = dataFileName ?? throw new ArgumentNullException(nameof(dataFileName)); | ||
ClassName = GetType().Name.Replace("CodeGenerator", "").Replace("Generator", ""); | ||
|
||
var targetDirectory = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), $"../src/Tingle.Extensions.Primitives/Generated")); | ||
OutputFilePath = Path.Combine(targetDirectory, $"{ClassName}.g.cs"); | ||
OutputFilePathJson = environment.ContentRootFileProvider.GetFileInfo($"{ClassName.ToLower()}.json").PhysicalPath; | ||
} | ||
|
||
public string ClassName { get; } | ||
|
||
public string OutputFilePath { get; } | ||
|
||
public string DataFileName { get; } | ||
|
||
public string? OutputFilePathJson { get; } | ||
|
||
public async Task GenerateAsync(CancellationToken cancellationToken = default) | ||
{ | ||
var data = (await ParseAsync(cancellationToken)) ?? throw new InvalidOperationException("Deserialization failed"); | ||
|
||
var sb = new StringBuilder(); | ||
var writer = new IndentedTextWriter(new StringWriter(sb)); | ||
|
||
writer.WriteLine(Header); | ||
|
||
writer.WriteLine("using System.Collections;"); | ||
writer.WriteLine("using System.Collections.Generic;"); | ||
|
||
// write the namespace | ||
writer.WriteLine(); | ||
writer.WriteLine("namespace Tingle.Extensions.Primitives;"); | ||
writer.WriteLine(); | ||
|
||
// begin the class | ||
writer.WriteLine($"internal partial class {ClassName}"); | ||
writer.WriteLine("{"); | ||
|
||
// write the data | ||
writer.Indent++; | ||
WriteCode(writer, data); | ||
writer.Indent--; | ||
|
||
// end the class | ||
writer.WriteLine("}"); | ||
await writer.FlushAsync(cancellationToken); | ||
|
||
// output to file | ||
await File.WriteAllTextAsync(OutputFilePath, sb.ToString(), Encoding.UTF8, cancellationToken); | ||
|
||
// generate the JSON file | ||
if (OutputFilePathJson is not null) | ||
{ | ||
sb.Clear(); | ||
writer = new IndentedTextWriter(new StringWriter(sb), " "); // only 2 spaces for JSON | ||
WriteJson(writer, data); | ||
await writer.FlushAsync(cancellationToken); | ||
if (sb.Length > 0) | ||
{ | ||
await File.WriteAllTextAsync(OutputFilePathJson, sb.ToString(), Encoding.UTF8, cancellationToken); | ||
} | ||
} | ||
} | ||
|
||
protected virtual async ValueTask<T?> ParseAsync(CancellationToken cancellationToken = default) | ||
{ | ||
var fi = environment.ContentRootFileProvider.GetFileInfo(Path.Combine("Files", DataFileName)); | ||
using var dataStream = fi.CreateReadStream(); | ||
return await ParseAsync(dataStream, cancellationToken); | ||
} | ||
|
||
protected virtual ValueTask<T?> ParseAsync(Stream dataStream, CancellationToken cancellationToken = default) | ||
=> JsonSerializer.DeserializeAsync<T>(dataStream, serializerOptions, cancellationToken); | ||
|
||
protected abstract void WriteCode(IndentedTextWriter writer, T data); | ||
protected virtual void WriteJson(IndentedTextWriter writer, T data) { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System.CodeDom.Compiler; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace StaticDataGenerator; | ||
|
||
internal class CountriesGenerator(IHostEnvironment environment, FileDownloader downloader) | ||
: AbstractGenerator<List<CountriesGenerator.CountryImpl>>(environment, downloader, "countries-20200124.json") | ||
{ | ||
internal struct CountryImpl | ||
{ | ||
[JsonPropertyName("name")] | ||
public string Name { get; set; } | ||
|
||
[JsonPropertyName("name_safe")] | ||
public string NameSafe { get; set; } | ||
|
||
[JsonPropertyName("continent")] | ||
public string Continent { get; set; } | ||
|
||
[JsonPropertyName("code_numeric")] | ||
public string CodeNumeric { get; set; } | ||
|
||
[JsonPropertyName("code_2")] | ||
public string Code2 { get; set; } | ||
|
||
[JsonPropertyName("code_3")] | ||
public string Code3 { get; set; } | ||
|
||
[JsonPropertyName("flag_url")] | ||
public string FlagUrl { get; set; } | ||
|
||
[JsonPropertyName("currency")] | ||
public string Currency { get; set; } | ||
} | ||
|
||
protected override void WriteCode(IndentedTextWriter writer, List<CountryImpl> data) | ||
{ | ||
// remove entries without all the information | ||
data.RemoveAll(impl => | ||
{ | ||
return impl.Name is null | ||
|| impl.Continent is null | ||
|| impl.CodeNumeric is null | ||
|| impl.Code2 is null | ||
|| impl.Code3 is null | ||
|| impl.FlagUrl is null | ||
|| impl.Currency is null; | ||
}); | ||
|
||
|
||
writer.WriteLine("// The currencies"); | ||
foreach (var impl in data) | ||
{ | ||
writer.Write($"public static readonly Country {impl.NameSafe}"); | ||
writer.WriteLine($" = new(\"{impl.Name}\", \"{impl.Continent}\", \"{impl.CodeNumeric}\", \"{impl.Code2}\", \"{impl.Code3}\", \"{impl.FlagUrl}\", \"{impl.Currency}\");"); | ||
} | ||
writer.Indent--; | ||
|
||
// numeric code mapping | ||
writer.WriteLine(); | ||
writer.WriteLine("// The array/list"); | ||
writer.WriteLine("internal static readonly IReadOnlyList<Country> All = new List<Country>"); | ||
writer.WriteLine("{"); | ||
writer.Indent++; | ||
foreach (var impl in data) | ||
{ | ||
writer.WriteLine($"{ClassName}.{impl.NameSafe},"); | ||
} | ||
writer.Indent--; | ||
writer.WriteLine("};"); | ||
writer.Indent--; | ||
} | ||
|
||
protected override void WriteJson(IndentedTextWriter writer, List<CountryImpl> data) | ||
{ | ||
writer.WriteLine("["); | ||
writer.Indent++; | ||
foreach (var impl in data) | ||
{ | ||
writer.Write($"{{ \"name\": \"{impl.Name}\", \"codeAlpha2\": \"{impl.Code2}\", \"codeAlpha3\": \"{impl.Code3}\"}}"); | ||
writer.WriteLine(data.IndexOf(impl) == data.Count - 1 ? "" : ","); | ||
} | ||
writer.Indent--; | ||
writer.WriteLine("]"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System.CodeDom.Compiler; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace StaticDataGenerator; | ||
|
||
internal class CurrenciesGenerator(IHostEnvironment environment, FileDownloader downloader) | ||
: AbstractGenerator<Dictionary<string, CurrenciesGenerator.CurrencyImpl>>(environment, downloader, "iso-4217.json") | ||
{ | ||
internal struct CurrencyImpl | ||
{ | ||
[JsonPropertyName("symbol")] | ||
public string Symbol { get; set; } | ||
|
||
[JsonPropertyName("code")] | ||
public string Code { get; set; } | ||
|
||
[JsonPropertyName("name")] | ||
public string Name { get; set; } | ||
|
||
[JsonPropertyName("symbolNative")] | ||
public string SymbolNative { get; set; } | ||
|
||
[JsonPropertyName("decimalDigits")] | ||
public int? DecimalDigits { get; set; } | ||
|
||
[JsonPropertyName("namePlural")] | ||
public string NamePlural { get; set; } | ||
} | ||
|
||
protected override void WriteCode(IndentedTextWriter writer, Dictionary<string, CurrencyImpl> data) | ||
{ | ||
// remove entries without all the information | ||
foreach (var kvp in data.ToList()) | ||
{ | ||
var impl = kvp.Value; | ||
if (impl.Symbol is null | ||
|| impl.Code is null | ||
|| impl.Name is null | ||
|| impl.SymbolNative is null | ||
|| impl.DecimalDigits is null | ||
|| impl.NamePlural is null) | ||
{ | ||
data.Remove(kvp.Key); | ||
} | ||
} | ||
|
||
writer.WriteLine("// The currencies"); | ||
foreach (var kvp in data) | ||
{ | ||
var impl = kvp.Value; | ||
writer.Write($"public static readonly Currency {impl.Code}"); | ||
writer.WriteLine($" = new(\"{impl.Code}\", \"{impl.Symbol}\", \"{impl.SymbolNative}\", \"{impl.Name}\", \"{impl.NamePlural}\", \"{impl.DecimalDigits}\");"); | ||
} | ||
writer.Indent--; | ||
|
||
writer.WriteLine(); | ||
writer.Indent++; | ||
writer.WriteLine("// The array/list"); | ||
writer.WriteLine("internal static readonly IReadOnlyList<Currency> All = new List<Currency>"); | ||
writer.WriteLine("{"); | ||
writer.Indent++; | ||
foreach (var kvp in data) | ||
{ | ||
var impl = kvp.Value; | ||
writer.WriteLine($"{ClassName}.{impl.Code},"); | ||
} | ||
writer.Indent--; | ||
writer.WriteLine("};"); | ||
} | ||
|
||
protected override void WriteJson(IndentedTextWriter writer, Dictionary<string, CurrencyImpl> data) | ||
{ | ||
var impls = data.Values.ToList(); | ||
writer.WriteLine("["); | ||
writer.Indent++; | ||
foreach (var impl in impls) | ||
{ | ||
writer.Write($"{{ \"name\": \"{impl.Name}\", \"code\": \"{impl.Code}\"}}"); | ||
writer.WriteLine(impls.IndexOf(impl) == data.Count - 1 ? "" : ","); | ||
} | ||
writer.Indent--; | ||
writer.WriteLine("]"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#pragma warning disable CS9113 // Parameter is unread. | ||
|
||
namespace StaticDataGenerator; | ||
|
||
public class FileDownloader(HttpClient client) | ||
{ | ||
} |
Oops, something went wrong.