-
Notifications
You must be signed in to change notification settings - Fork 59
/
make.js
364 lines (323 loc) · 11.6 KB
/
make.js
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
require('shelljs/make');
var fs = require('fs');
var path = require('path');
var tl = require('azure-pipelines-task-lib/task');
var os = require('os');
var xml2js = require('xml2js');
// util functions
var util = require('./make-util');
var cd = util.cd;
var cp = util.cp;
var mkdir = util.mkdir;
var rm = util.rm;
var test = util.test;
var run = util.run;
var banner = util.banner;
var rp = util.rp;
var fail = util.fail;
var ensureExists = util.ensureExists;
var pathExists = util.pathExists;
var addPath = util.addPath;
var ensureTool = util.ensureTool;
// add node modules .bin to the path so we can dictate version of tsc etc...
var binPath = path.join(__dirname, 'node_modules', '.bin');
if (!test('-d', binPath)) {
fail('node modules bin not found. ensure npm install has been run.');
}
addPath(binPath);
var buildPath = path.join(__dirname, '_build');
var unitPath = path.join(__dirname, '_test', 'units');
var testPath = path.join(__dirname, '_test', 'tests');
var cultures = ['en-US', 'de-DE', 'es-ES', 'fr-FR', 'it-IT', 'ja-JP', 'ko-KR', 'ru-RU', 'zh-CN', 'zh-TW'];
target.clean = function () {
rm('-Rf', buildPath);
rm('-Rf', unitPath);
rm('-Rf', testPath);
};
target.build = function () {
target.clean();
target.loc();
run('tsc --version', true);
run('tsc --outDir ' + buildPath, true);
//cp(rp('typings.json'), buildPath);
cp(rp('package.json'), buildPath);
cp(rp('README.md'), buildPath);
cp(rp('LICENSE'), buildPath);
cp(rp('Invoke-7zdec.ps1'), buildPath);
cp(rp('lib.json'), buildPath);
cp('-R', rp('externals'), buildPath);
cp('-Rf', rp('Strings'), buildPath);
// just a bootstrap file to avoid /// in final js and .d.ts file
rm(path.join(buildPath, 'index.*'));
}
target.loc = function () {
// create a key->value map of the default strings
var defaultStrings = {};
var lib = JSON.parse(fs.readFileSync(path.join(__dirname, 'lib.json')));
if (lib.messages) {
for (var key of Object.keys(lib.messages)) {
// skip resjson-style comments for localizers
if (!key || key.match(/^_.+\.comment$/)) {
continue;
}
defaultStrings[`loc.messages.${key}`] = lib.messages[key];
}
}
// create the culture-specific resjson files
for (var culture of cultures) {
// initialize the culture-specific strings from the default strings
var cultureStrings = {};
for (var key of Object.keys(defaultStrings)) {
cultureStrings[key] = defaultStrings[key];
}
// load the culture-specific xliff file
var xliffPath = path.join(__dirname, 'xliff', `${culture}.xlf`);
var stats;
try {
stats = fs.statSync(xliffPath);
}
catch (err) {
if (err.code != 'ENOENT') {
throw err;
}
}
if (stats) {
// parse the culture-specific xliff contents
var parser = new xml2js.Parser();
var xliff;
parser.parseString(
fs.readFileSync(xliffPath),
function (err, result) {
if (err) {
throw err;
}
xliff = result;
});
// overlay the translated strings
for (var unit of xliff.xliff.file[0].body[0]['trans-unit']) {
if (unit.target[0].$.state == 'translated' &&
defaultStrings.hasOwnProperty(unit.$.id) &&
defaultStrings[unit.$.id] == unit.source[0]) {
cultureStrings[unit.$.id] = unit.target[0]._;
}
}
}
// write the culture-specific resjson file
var resjsonPath = path.join(__dirname, 'Strings', 'resources.resjson', culture, 'resources.resjson');
var resjsonContents = JSON.stringify(cultureStrings, null, 2);
tl.mkdirP(path.dirname(resjsonPath));
fs.writeFileSync(resjsonPath, resjsonContents);
}
}
var runTests = function (testPath) {
cp('-R', path.join(__dirname, 'test', 'data'), testPath);
//cp('-Rf', rp('test/scripts'), testPath);
// tool lib requires a 2.115.0 agent or higher
process.env['AGENT_VERSION'] = '2.115.0';
// creating a cache dir in the build dir. agent would do this
var cacheDir = path.join(process.cwd(), 'CACHE');
process.env['AGENT_TOOLSDIRECTORY'] = cacheDir;
tl.mkdirP(cacheDir);
// redirecting TEMP (agent would do this per build)
var tempDir = path.join(process.cwd(), 'TEMP');
tl.mkdirP(tempDir);
process.env['AGENT_TEMPDIRECTORY'] = tempDir;
if (os.platform() == 'win32') {
process.env['TEMP'] = tempDir;
process.env['TMP'] = tempDir;
}
else {
process.env['TMPDIR'] = tempDir;
tl.mkdirP(tempDir);
}
run('mocha ' + testPath + ' --recursive --timeout 20000', true);
}
target.units = function () {
console.log("-------Unit Tests-------");
run('tsc -p ./test/units --outDir ' + unitPath, true);
runTests(unitPath);
}
target.test = function () {
target.build();
target.units();
console.log("-------Other Tests-------");
run('tsc -p ./test/tests --outDir ' + testPath, true);
runTests(testPath);
}
// run the sample
// building again is the way to clear the tool cache (creates it in the build dir)
target.sample = function () {
tl.pushd(buildPath);
// tool lib requires a 2.115.0 agent or higher
process.env['AGENT_VERSION'] = '2.115.0';
// creating a cache dir in the build dir. agent would do this
var cacheDir = path.join(process.cwd(), 'CACHE');
process.env['AGENT_TOOLSDIRECTORY'] = cacheDir;
tl.mkdirP(cacheDir);
// redirecting TEMP (agent would do this per build)
var tempDir = path.join(process.cwd(), 'TEMP');
tl.mkdirP(tempDir);
process.env['AGENT_TEMPDIRECTORY'] = tempDir;
if (os.platform() == 'win32') {
process.env['TEMP'] = tempDir;
process.env['TMP'] = tempDir;
}
else {
process.env['TMPDIR'] = tempDir;
tl.mkdirP(tempDir);
}
run('node sample.js', true);
tl.popd();
}
target.handoff = function () {
// create a key->value map of default strings and comments for localizers
//
// resjson-style resources:
// "greeting": "Hello",
// "_greeting.comment": "A welcome greeting.",
//
// for more details about resjson: https://msdn.microsoft.com/en-us/library/windows/apps/hh465254.aspx
var defaultStrings = {};
var comments = {};
var lib = JSON.parse(fs.readFileSync(path.join(__dirname, 'lib.json')));
if (lib.messages) {
for (var key of Object.keys(lib.messages)) {
if (!key) {
throw new Error('key cannot be empty: lib.messages.<key>');
}
if (key.match(/^_.+\.comment$/)) {
var commentKey = key;
var valueKey = commentKey.substr( // trim leading "_"
'_'.length, // trim trailing ".comment"
commentKey.length - '_.comment'.length);
comments[`loc.messages.${valueKey}`] = lib.messages[commentKey];
continue;
}
else {
defaultStrings[`loc.messages.${key}`] = lib.messages[key];
}
}
}
// create or update the culture-specific xlf files
for (var culture of cultures) {
if (culture.toUpperCase() == 'EN-US') {
continue;
}
// test whether xliff file exists
var xliffPath = path.join(__dirname, 'xliff', `${culture}.xlf`);
var stats;
try {
stats = fs.statSync(xliffPath);
}
catch (err) {
if (err.code != 'ENOENT') {
throw err;
}
}
var xliff;
if (stats) {
// parse the file
var parser = new xml2js.Parser();
parser.parseString(
fs.readFileSync(xliffPath),
function (err, result) {
if (err) {
throw err;
}
xliff = result;
}
)
}
else {
// create the initial xliff object
xliff = {
"xliff": {
"$": {
"version": "1.2"
},
"file": [
{
"$": {
"original": "lib.json",
"source-language": "en-US",
"target-language": culture,
"datatype": "plaintext"
},
"body": [
{
"trans-unit": []
}
]
}
]
}
}
}
// create a map of trans-unit
var unitMap = {};
for (var unit of xliff.xliff.file[0].body[0]['trans-unit']) {
unitMap[unit['$'].id] = unit;
}
for (var key of Object.keys(defaultStrings)) {
// add the trans-unit
if (!unitMap.hasOwnProperty(key)) {
unitMap[key] = {
"$": {
"id": key
},
"source": [
defaultStrings[key]
],
"target": [
{
"$": {
"state": "new"
},
"_": ""
}
]
};
}
// update the source, target state, and note
else if (unitMap[key].source[0] != defaultStrings[key]) {
unitMap[key].source = [
defaultStrings[key]
];
if (unitMap[key].target[0]['$'].state != 'new') {
unitMap[key].target[0]['$'].state = "needs-translation";
}
}
// always update the note
unitMap[key].note = [
(comments[key] || "")
];
}
for (var key of Object.keys(unitMap)) {
// delete the trans-unit
if (!defaultStrings.hasOwnProperty(key)) {
delete unitMap[key];
}
}
// update the body of the xliff object
xliff.xliff.file[0].body[0]['trans-unit'] = [];
for (var key of Object.keys(unitMap).sort()) {
xliff.xliff.file[0].body[0]['trans-unit'].push(unitMap[key]);
}
// write the xliff file
var options = {
"renderOpts": {
"pretty": true,
"indent": " ",
"newline": os.EOL
},
"xmldec": {
"version": "1.0",
"encoding": "utf-8"
}
};
var builder = new xml2js.Builder(options);
var xml = builder.buildObject(xliff);
mkdir('-p', path.dirname(xliffPath));
fs.writeFileSync(xliffPath, '\ufeff' + xml);
}
}