-
Notifications
You must be signed in to change notification settings - Fork 2
/
LanguagePackBuilder.cs
executable file
·349 lines (287 loc) · 9.64 KB
/
LanguagePackBuilder.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
#!/usr/bin/csexec
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Globalization;
using System.Collections.Generic;
public static class Program
{
public static void Main (string [] args)
{
try
{
var script = new LanguagePackBuilder () {
PackageName = args [1],
CultureCode = args [2].Replace ("_", "-")
};
script.Run ();
}
catch (Exception ex)
{
Console.WriteLine (ex.Message);
}
}
}
internal class PathMap
{
public string SrcPath;
public string DstPath;
}
internal class PathMapper
{
IList<PathMap> maps = new List<PathMap> ();
public void LoadPathMap (string filePath)
{
if (!File.Exists (filePath)) {
return;
}
var lines = File.ReadAllLines (filePath);
foreach (var line in lines) {
var lineParts = line.Split (':', StringSplitOptions.RemoveEmptyEntries);
if (lineParts.Length > 0) {
var map = new PathMap ();
map.SrcPath = lineParts [0].Trim ();
if (lineParts.Length > 1) {
map.DstPath = lineParts [1].Trim ();
}
else {
map.DstPath = string.Empty;
}
maps.Add (map);
}
}
}
public string MapPath (string path)
{
if (maps.Count == 0) {
return path;
}
var map = maps.FirstOrDefault (p => path.Contains (p.SrcPath));
if (map == null) {
return path;
}
var dstPath = path.Replace (map.SrcPath, map.DstPath);
return dstPath;
}
}
internal class LanguagePackBuilder
{
#region Parameters
public string PackageName { get; set; }
public string CultureCode { get; set; }
public string PackageVersion = Environment.GetEnvironmentVariable ("LPB_PACKAGE_VERSION");
public string PackageType = Environment.GetEnvironmentVariable ("LPB_PACKAGE_TYPE");
public string SourceVersion = Environment.GetEnvironmentVariable ("LPB_SOURCE_VERSION");
public string TranslationDir = Environment.GetEnvironmentVariable ("LPB_TRANSLATION_DIR");
public string PlatformType = Environment.GetEnvironmentVariable ("LBP_PLATFORM_TYPE");
public string ExtensionPackage = Environment.GetEnvironmentVariable ("LBP_EXTENSION_PACKAGE");
public string BuildDir = "_build";
public string ManifestFileNameTemplate = "R7_${PlatformType}_${PackageType}_${PackageName}_${CultureCode}.dnn";
public string PackFileNameTemplate = "ResourcePack.R7.${PlatformType}.${PackageType}.${PackageName}.${SourceVersion}-${PackageVersion}.${CultureCode}.zip";
#endregion
string ScriptDirectory;
public void Run ()
{
try
{
ScriptDirectory = Directory.GetCurrentDirectory ();
CdToPackageDirectory ();
var pathMapper = new PathMapper ();
pathMapper.LoadPathMap ("pathmap.txt");
Directory.SetCurrentDirectory (ScriptDirectory);
var manifestText = GenerateManifest (pathMapper);
CreatePackage (manifestText, pathMapper);
Directory.SetCurrentDirectory (ScriptDirectory);
}
catch (Exception ex)
{
throw ex;
}
}
string GenerateManifest (PathMapper pathMapper)
{
try
{
// TODO: Support for package-specific manifest template
var manifestTemplate = File.ReadAllText ("manifest_template.xml");
var startTag = "<languageFile>";
var endTag = "</languageFile>";
var begin = manifestTemplate.IndexOf (startTag);
var end = manifestTemplate.IndexOf (endTag) + endTag.Length;
// get template for languageFile entries
var langFileTemplate = manifestTemplate.Substring (begin, end - begin);
// get translation files
CdToTranslationDirectory ();
var files = Directory.GetFiles (".", "*." + CultureCode + ".resx", SearchOption.AllDirectories);
// for better formatting
var line = 0;
var langFiles = new StringBuilder (files.Length);
foreach (var file in files)
{
if (line++ > 0)
langFiles.Append ("\n\t\t\t\t\t\t");
var langEntry = langFileTemplate
.Replace ("${FilePath}", pathMapper.MapPath (Path.GetDirectoryName (file))
.Remove (0, 2)) // remove "./" prefix
.Replace ("${FileName}", Path.GetFileName (file));
langFiles.Append (langEntry);
}
CdToPackageDirectory ();
var manifest = manifestTemplate;
// must remove and insert before any other replacement done
manifest = manifest.Remove (begin, end - begin);
manifest = manifest.Insert (begin, langFiles.ToString());
manifest = ReplaceTags (manifest);
// add license
if (File.Exists ("license.txt"))
manifest = manifest.Replace ("${License}", "<license src=\"license.txt\" />");
else
manifest = manifest.Replace ("${License}", "<license />");
// add release notes
if (File.Exists ("releaseNotes.txt"))
manifest = manifest.Replace ("${ReleaseNotes}", "<releaseNotes src=\"releaseNotes.txt\" />");
else
manifest = manifest.Replace ("${ReleaseNotes}", "<releaseNotes />");
if (!string.IsNullOrEmpty (manifest)) {
Console.WriteLine ("Package manifest generated.");
}
return manifest;
}
catch (Exception ex)
{
throw ex;
}
}
void CdToPackageDirectory ()
{
Directory.SetCurrentDirectory (ScriptDirectory);
Directory.SetCurrentDirectory (PackageName);
}
void CdToTranslationDirectory ()
{
CdToPackageDirectory ();
Directory.SetCurrentDirectory (GetTranslationDirectory ());
}
string GetTranslationDirectory ()
{
if (!string.IsNullOrEmpty (TranslationDir)) {
return TranslationDir;
}
return CultureCode;
}
void CdToBuildDirectory ()
{
CdToPackageDirectory ();
Directory.SetCurrentDirectory (BuildDir);
}
void CreatePackage (string manifestText, PathMapper pathMapper)
{
try
{
CdToPackageDirectory ();
// recreate build dir
if (Directory.Exists (BuildDir)) {
Directory.Delete (BuildDir, true);
}
Directory.CreateDirectory (BuildDir);
Console.WriteLine ("Created build directory.");
// TODO: Support more license/releaseNotes formats
var packFileName = ReplaceTags (PackFileNameTemplate);
var licenseFileName = "license.txt";
var releaseNotesFileName = "releaseNotes.txt";
// create manifest file
var manifestFileName = ReplaceTags (ManifestFileNameTemplate);
File.WriteAllText (Path.Combine (BuildDir, manifestFileName), manifestText);
// copy license and release notes
if (File.Exists (licenseFileName)) {
File.Copy (licenseFileName, Path.Combine (BuildDir, licenseFileName));
Console.WriteLine ("License file copied.");
}
if (File.Exists (releaseNotesFileName)) {
File.Copy (releaseNotesFileName, Path.Combine (BuildDir, releaseNotesFileName));
Console.WriteLine ("Release notes file copied.");
}
// copy translation files
CdToTranslationDirectory ();
var files = Directory.GetFiles (".", "*." + CultureCode + ".resx", SearchOption.AllDirectories);
foreach (var filePath in files) {
var newFilePath = $"../../{BuildDir}/" + pathMapper.MapPath (filePath)
.Remove (0, 2); // remove "./" prefix
Directory.CreateDirectory (Path.GetDirectoryName (newFilePath));
File.Copy (filePath, newFilePath);
}
Console.WriteLine ("Translation files copied.");
// create package
CdToBuildDirectory ();
var zip = new Process ();
zip.StartInfo.FileName = "zip";
zip.StartInfo.Arguments = string.Format (@"-q -r -9 -i \*.resx \*.dnn \*.txt -UN=UTF8 ""{0}"" .", packFileName);
zip.Start ();
zip.WaitForExit ();
Console.WriteLine ("Language pack created.");
}
catch (Exception ex)
{
throw ex;
}
}
string ReplaceTags (string template)
{
var result = template;
if (PackageType == "Core")
{
result = result.Replace ("${PackageType}.${PackageName}", PackageType);
result = result.Replace ("${PackageType}_${PackageName}", PackageType);
result = result.Replace ("${ExtensionPackage}", string.Empty);
result = result.Replace ("${PackageDescription}", "For DNN Core version " + SourceVersion);
result = result.Replace ("${Dependencies}",
"<dependency type=\"CoreVersion\">" + SourceVersion + "</dependency>");
}
else
{
result = result.Replace ("${ExtensionPackage}", "<package>" + ExtensionPackage + "</package>");
result = result.Replace ("${PackageDescription}", "For " + ExtensionPackage + " version " + SourceVersion);
result = result.Replace ("${Dependencies}",
"<dependency type=\"Package\">" + ExtensionPackage + "</dependency>");
}
// Extension language pack name
result = result.Replace ("${ExtensionName}",
(PackageType == "Extension")? PackageName + " " : string.Empty);
result = result.Replace ("${CultureCode}", CultureCode);
result = result.Replace ("${CultureNameNative}", CultureInfo.GetCultureInfoByIetfLanguageTag (CultureCode).NativeName);
result = result.Replace ("${PackageType}", PackageType);
result = result.Replace ("${PackageName}", PackageName);
result = result.Replace ("${PlatformType}", PlatformType);
result = result.Replace ("${SourceVersion}", SourceVersion);
result = result.Replace ("${PackageVersion}", PackageVersion);
return result;
}
List<string> RunToLines (string command, string arguments)
{
var result = new List<string> ();
var process = new Process ();
process.StartInfo.FileName = command;
process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
try
{
process.Start ();
process.WaitForExit ();
while (!process.StandardOutput.EndOfStream) {
result.Add (process.StandardOutput.ReadLine ());
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
process.Close ();
}
return result;
}
}