-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateTagData.cs
More file actions
125 lines (102 loc) · 5.38 KB
/
CreateTagData.cs
File metadata and controls
125 lines (102 loc) · 5.38 KB
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
namespace ptr727.LanguageTags.Create;
internal sealed class CreateTagData(
string dataDirectory,
string codeDirectory,
CancellationToken cancellationToken
)
{
private Iso6392Data? _iso6392;
private string? _iso6392DataFile;
private string? _iso6392JsonFile;
private string? _iso6392CodeFile;
private Iso6393Data? _iso6393;
private string? _iso6393DataFile;
private string? _iso6393JsonFile;
private string? _iso6393CodeFile;
private Rfc5646Data? _rfc5646;
private string? _rfc5646DataFile;
private string? _rfc5646JsonFile;
private string? _rfc5646CodeFile;
internal async Task DownloadDataAsync()
{
// Download all the data files
Log.Information("Downloading all language tag data files ...");
Log.Information("Downloading ISO 639-2 data ...");
_iso6392DataFile = Path.Combine(dataDirectory, Iso6392Data.DataFileName);
await DownloadFileAsync(new Uri(Iso6392Data.DataUri), _iso6392DataFile)
.ConfigureAwait(false);
Log.Information("Downloading ISO 639-3 data ...");
_iso6393DataFile = Path.Combine(dataDirectory, Iso6393Data.DataFileName);
await DownloadFileAsync(new Uri(Iso6393Data.DataUri), _iso6393DataFile)
.ConfigureAwait(false);
Log.Information("Downloading RFC 5646 data ...");
_rfc5646DataFile = Path.Combine(dataDirectory, Rfc5646Data.DataFileName);
await DownloadFileAsync(new Uri(Rfc5646Data.DataUri), _rfc5646DataFile)
.ConfigureAwait(false);
Log.Information("Language tag data files downloaded successfully.");
}
internal async Task CreateJsonDataAsync()
{
ArgumentNullException.ThrowIfNull(_iso6392DataFile);
ArgumentNullException.ThrowIfNull(_iso6393DataFile);
ArgumentNullException.ThrowIfNull(_rfc5646DataFile);
// Convert data files to JSON
Log.Information("Converting data files to JSON ...");
Log.Information("Converting ISO 639-2 data to JSON ...");
_iso6392 = await Iso6392Data.FromDataAsync(_iso6392DataFile).ConfigureAwait(false);
_iso6392JsonFile = Path.Combine(dataDirectory, Iso6392Data.DataFileName + ".json");
Log.Information("Writing ISO 639-2 data to {JsonPath}", _iso6392JsonFile);
await _iso6392.SaveJsonAsync(_iso6392JsonFile).ConfigureAwait(false);
Log.Information("Converting ISO 639-3 data to JSON ...");
_iso6393 = await Iso6393Data.FromDataAsync(_iso6393DataFile).ConfigureAwait(false);
_iso6393JsonFile = Path.Combine(dataDirectory, Iso6393Data.DataFileName + ".json");
Log.Information("Writing ISO 639-3 data to {JsonPath}", _iso6393JsonFile);
await _iso6393.SaveJsonAsync(_iso6393JsonFile).ConfigureAwait(false);
Log.Information("Converting RFC 5646 data to JSON ...");
_rfc5646 = await Rfc5646Data.FromDataAsync(_rfc5646DataFile).ConfigureAwait(false);
_rfc5646JsonFile = Path.Combine(dataDirectory, Rfc5646Data.DataFileName + ".json");
Log.Information("Writing RFC 5646 data to {JsonPath}", _rfc5646JsonFile);
await _rfc5646.SaveJsonAsync(_rfc5646JsonFile).ConfigureAwait(false);
Log.Information("Data files converted to JSON successfully.");
}
internal async Task GenerateCodeAsync()
{
ArgumentNullException.ThrowIfNull(_iso6392);
ArgumentNullException.ThrowIfNull(_iso6393);
ArgumentNullException.ThrowIfNull(_rfc5646);
// Generate code files
Log.Information("Generating code files ...");
Log.Information("Generating ISO 639-2 code ...");
_iso6392CodeFile = Path.Combine(codeDirectory, nameof(Iso6392Data) + "Gen.cs");
Log.Information("Writing ISO 639-2 code to {CodePath}", _iso6392CodeFile);
await _iso6392.SaveCodeAsync(_iso6392CodeFile).ConfigureAwait(false);
Log.Information("Generating ISO 639-3 code ...");
_iso6393CodeFile = Path.Combine(codeDirectory, nameof(Iso6393Data) + "Gen.cs");
Log.Information("Writing ISO 639-3 code to {CodePath}", _iso6393CodeFile);
await _iso6393.SaveCodeAsync(_iso6393CodeFile).ConfigureAwait(false);
Log.Information("Generating RFC 5646 code ...");
_rfc5646CodeFile = Path.Combine(codeDirectory, nameof(Rfc5646Data) + "Gen.cs");
Log.Information("Writing RFC 5646 code to {CodePath}", _rfc5646CodeFile);
await _rfc5646.SaveCodeAsync(_rfc5646CodeFile).ConfigureAwait(false);
Log.Information("Code files generated successfully.");
}
private async Task DownloadFileAsync(Uri uri, string fileName)
{
ArgumentNullException.ThrowIfNull(uri);
ArgumentException.ThrowIfNullOrWhiteSpace(fileName);
Log.Information("Downloading \"{Uri}\" to \"{FileName}\" ...", uri.ToString(), fileName);
using Stream httpStream = await HttpClientFactory
.GetHttpClient()
.GetStreamAsync(uri, cancellationToken)
.ConfigureAwait(false);
using FileStream fileStream = new(
fileName,
FileMode.Create,
FileAccess.Write,
FileShare.None,
8192,
FileOptions.Asynchronous | FileOptions.SequentialScan
);
await httpStream.CopyToAsync(fileStream, cancellationToken).ConfigureAwait(false);
}
}