- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateTagData.cs
More file actions
Latest commit
180 lines (151 loc) · 7.54 KB
/
Copy pathCreateTagData.cs
File metadata and controls
180 lines (151 loc) · 7.54 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
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
namespaceptr727.LanguageTags.Create;
internalsealedclassCreateTagData(
stringdataDirectory,
stringcodeDirectory,
CancellationTokencancellationToken
)
{
privateIso6392Data?_iso6392;
privatestring?_iso6392DataFile;
privatestring?_iso6392JsonFile;
privatestring?_iso6392CodeFile;
privateIso6393Data?_iso6393;
privatestring?_iso6393DataFile;
privatestring?_iso6393JsonFile;
privatestring?_iso6393CodeFile;
privateRfc5646Data?_rfc5646;
privatestring?_rfc5646DataFile;
privatestring?_rfc5646JsonFile;
privatestring?_rfc5646CodeFile;
privateUnM49Data?_unM49;
privatestring?_unM49DataFile;
privatestring?_unM49JsonFile;
privatestring?_unM49CodeFile;
internalasyncTaskDownloadDataAsync()
{
// Download all the data files
Log.Information("Downloading all language tag data files ...");
ResolveDataFilePaths();
Log.Information("Downloading ISO 639-2 data ...");
awaitDownloadFileAsync(newUri(Iso6392Data.DataUri),_iso6392DataFile!)
.ConfigureAwait(false);
Log.Information("Downloading ISO 639-3 data ...");
awaitDownloadFileAsync(newUri(Iso6393Data.DataUri),_iso6393DataFile!)
.ConfigureAwait(false);
Log.Information("Downloading RFC 5646 data ...");
awaitDownloadFileAsync(newUri(Rfc5646Data.DataUri),_rfc5646DataFile!)
.ConfigureAwait(false);
Log.Information("Downloading UN M.49 data ...");
awaitDownloadFileAsync(newUri(UnM49Data.DataUri),_unM49DataFile!).ConfigureAwait(false);
Log.Information("Language tag data files downloaded successfully.");
}
internalvoidUseExistingData()
{
// Offline mode: regenerate from the already-committed LanguageData/ directory with no network
// access. Resolve the same data file paths a download would, then verify each is present.
Log.Information(
"Using existing language tag data files in {DataDirectory} ...",
dataDirectory
);
ResolveDataFilePaths();
string[]dataFiles=
[
_iso6392DataFile!,
_iso6393DataFile!,
_rfc5646DataFile!,
_unM49DataFile!,
];
foreach(stringdataFileindataFiles)
{
if(!File.Exists(dataFile))
{
thrownewFileNotFoundException(
$"Required data file does not exist: {dataFile}",
dataFile
);
}
}
}
privatevoidResolveDataFilePaths()
{
_iso6392DataFile=Path.Combine(dataDirectory,Iso6392Data.DataFileName);
_iso6393DataFile=Path.Combine(dataDirectory,Iso6393Data.DataFileName);
_rfc5646DataFile=Path.Combine(dataDirectory,Rfc5646Data.DataFileName);
_unM49DataFile=Path.Combine(dataDirectory,UnM49Data.DataFileName);
}
internalasyncTaskCreateJsonDataAsync()
{
ArgumentNullException.ThrowIfNull(_iso6392DataFile);
ArgumentNullException.ThrowIfNull(_iso6393DataFile);
ArgumentNullException.ThrowIfNull(_rfc5646DataFile);
ArgumentNullException.ThrowIfNull(_unM49DataFile);
// Convert data files to JSON
Log.Information("Converting data files to JSON ...");
Log.Information("Converting ISO 639-2 data to JSON ...");
_iso6392=awaitIso6392Data.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=awaitIso6393Data.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=awaitRfc5646Data.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("Converting UN M.49 data to JSON ...");
_unM49=awaitUnM49Data.FromDataAsync(_unM49DataFile).ConfigureAwait(false);
_unM49JsonFile=Path.Combine(dataDirectory,UnM49Data.DataFileName+".json");
Log.Information("Writing UN M.49 data to {JsonPath}",_unM49JsonFile);
await_unM49.SaveJsonAsync(_unM49JsonFile).ConfigureAwait(false);
Log.Information("Data files converted to JSON successfully.");
}
internalasyncTaskGenerateCodeAsync()
{
ArgumentNullException.ThrowIfNull(_iso6392);
ArgumentNullException.ThrowIfNull(_iso6393);
ArgumentNullException.ThrowIfNull(_rfc5646);
ArgumentNullException.ThrowIfNull(_unM49);
// 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("Generating UN M.49 code ...");
_unM49CodeFile=Path.Combine(codeDirectory,nameof(UnM49Data)+"Gen.cs");
Log.Information("Writing UN M.49 code to {CodePath}",_unM49CodeFile);
await_unM49.SaveCodeAsync(_unM49CodeFile).ConfigureAwait(false);
Log.Information("Code files generated successfully.");
}
privateasyncTaskDownloadFileAsync(Uriuri,stringfileName)
{
ArgumentNullException.ThrowIfNull(uri);
ArgumentException.ThrowIfNullOrWhiteSpace(fileName);
Log.Information("Downloading \"{Uri}\" to \"{FileName}\" ...",uri.ToString(),fileName);
usingStreamhttpStream=awaitUtilities
.HttpClientFactory.GetClient()
.GetStreamAsync(uri,cancellationToken)
.ConfigureAwait(false);
usingFileStreamfileStream=new(
fileName,
FileMode.Create,
FileAccess.Write,
FileShare.None,
8192,
FileOptions.Asynchronous|FileOptions.SequentialScan
);
awaithttpStream.CopyToAsync(fileStream,cancellationToken).ConfigureAwait(false);
}
}