-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
513 lines (488 loc) · 16.3 KB
/
build.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/**
* @fileoverview anyway make tern-definition file.
* @author PrsPrsBK
*/
const fs = require('fs');
const path = require('path');
const stripJsonComments = require('strip-json-comments');
const mdnData = require('@mdn/browser-compat-data').webextensions.api;
let mozillaRepo = '';
let commRepo = '';
let channel = 'nightly';
let goShrink = false;
const getOutputFileName = (prefix) => {
return `${prefix}-${channel}`;
};
const summary = [];
/**
* This script writes only one file, and utilizes two repositories.
* When script can not those repositories, output file will not include data relevant APIs.
* 1. comm - you get Thunderbird's APIs.
* 2. mozilla - you get Firefox's APIs, that can be used with Thunderbird Extension.
*/
const outputSpec = {
prefix: 'tbext',
resultBase: {
'!name': 'tbext',
'!define': {},
browser: {},
},
groupList: [
/** APIs reside within comm repository. */
{
name: 'commAPI',
getRepository: () => { return commRepo; },
schemaDir: 'mail/components/extensions/schemas/',
apiListFile: 'mail/components/extensions/ext-mail.json',
useMdn: false,
schemaList: [],
},
/** APIs reside within mozilla repository. */
{
name: 'mozillaAPI',
getRepository: () => { return mozillaRepo; },
//dummy
schemaDir: 'toolkit/',
// apiListFile: '',
useMdn: true,
schemaList: [
{
name: 'browsing_data',
schema: 'toolkit/components/extensions/schemas/browsing_data.json',
},
{
name: 'commands',
schema: 'browser/components/extensions/schemas/commands.json',
},
{
name: 'contentScripts',
schema: 'toolkit/components/extensions/schemas/content_scripts.json',
},
{
name: 'experiments',
schema: 'toolkit/components/extensions/schemas/experiments.json',
},
{
name: 'extension',
schema: 'toolkit/components/extensions/schemas/extension.json',
},
{
name: 'extension_protocol_handlers',
schema: 'toolkit/components/extensions/schemas/extension_protocol_handlers.json',
},
{
name: 'extension_types',
schema: 'toolkit/components/extensions/schemas/extension_types.json',
},
{
name: 'geckoProfiler',
schema: 'toolkit/components/extensions/schemas/geckoProfiler.json',
},
{
name: 'i18n',
schema: 'toolkit/components/extensions/schemas/i18n.json',
},
{
name: 'identity',
schema: 'toolkit/components/extensions/schemas/identity.json',
},
{
name: 'management',
schema: 'toolkit/components/extensions/schemas/management.json',
},
{
name: 'permissions',
schema: 'toolkit/components/extensions/schemas/permissions.json',
},
{
name: 'pkcs11',
schema: 'browser/components/extensions/schemas/pkcs11.json',
},
{
name: 'runtime',
schema: 'toolkit/components/extensions/schemas/runtime.json',
},
{
name: 'theme',
schema: 'toolkit/components/extensions/schemas/theme.json',
},
],
},
]
};
/**
* distill from argments.
* @returns {Object} report
*/
const numerateArgs = () => {
const report = {
isValid: true,
message: [],
};
process.argv.forEach((arg, idx) => {
if(arg === '--mozilla-repo') {
if(idx + 1 < process.argv.length) {
mozillaRepo = process.argv[idx + 1];
}
else {
report.isValid = false;
report.message.push(`please specify as ${arg} somevalue`);
}
}
else if(arg === '--comm-repo') {
if(idx + 1 < process.argv.length) {
commRepo = process.argv[idx + 1];
}
else {
report.isValid = false;
report.message.push(`please specify as ${arg} somevalue`);
}
}
else if(arg === '--channel') {
if(idx + 1 < process.argv.length) {
channel = process.argv[idx + 1];
}
else {
report.isValid = false;
report.message.push(`please specify as ${arg} somevalue`);
}
}
else if(arg === '--shrink') {
goShrink = true;
}
});
return report;
};
/**
* Check the structure of repository.
* @param {string} rootDir root directory of repository
* @returns {{isValid: boolean, message: string}} the repository has assumed dirs or not
*/
const checkRepositoryDirs = (rootDir, apiGroup) => {
const report = {
isValid: true,
message: [],
};
if(rootDir === '') {
report.isValid = false;
report.message.push('Lack of arg: --mozilla-repo foo --comm-repo bar');
}
else if(fs.existsSync(rootDir) === false) {
report.isValid = false;
report.message.push(`root dir does not exist: ${rootDir}`);
}
else {
const schemaDirFull = path.join(rootDir, apiGroup.schemaDir);
if(fs.existsSync(schemaDirFull) === false) {
report.isValid = false;
report.message.push(`schema dir does not exist: ${apiGroup.schemaDir}`);
}
}
return report;
};
const chromeUri2Path = (chromeUri) => {
const regexSchemaPath = /.+\/([^/]+json)$/;
//identity is in browser-ui api, but its schema is in toolkit dir. only-one case.
if(chromeUri.startsWith('chrome://extensions/content/schemas/')) {
return `toolkit/components/extensions/schemas/${regexSchemaPath.exec(chromeUri)[1]}`;
}
else if(chromeUri.startsWith('chrome://browser/content/schemas/')) {
return `browser/components/extensions/schemas/${regexSchemaPath.exec(chromeUri)[1]}`;
}
else if(chromeUri.startsWith('chrome://messenger/content/schemas/')) {
return `mail/components/extensions/schemas/${regexSchemaPath.exec(chromeUri)[1]}`;
}
else {
return '';
}
};
/**
* Distill JSON file names from API schema file's content.
* @param {string} rootDir
* @param {Object[]} apiGroup
*/
const makeSchemaList = (rootDir, apiGroup) => {
if(apiGroup.apiListFile !== undefined) {
const apiListFileFull = path.join(rootDir, apiGroup.apiListFile);
const apiItemList = JSON.parse(stripJsonComments(fs.readFileSync(apiListFileFull, 'utf8')));
for(const apiName in apiItemList) {
if(apiItemList[apiName].schema !== undefined) { //only background page of mozilla?
const schema = chromeUri2Path(apiItemList[apiName].schema);
if(schema !== '') {
const apiItem = {
name: apiName,
schema,
};
apiGroup.schemaList.push(apiItem);
}
else {
console.log(`skipped: irregular path for ${apiName}. ${apiItemList[apiName].schema}`);
}
}
}
}
};
const escapeTag = docText => {
// return docText
// .replace(/<\/?(?:code|var)>/g, '`')
// .replace(/<\/?em>/g, '*');
return docText;
};
const makeTernDefTree = (declaredAt, nameTree, curItem, useMdn, options = {}) => {
const isDefZone = ('isDefZone' in options) ? options.isDefZone : false;
const defZoneStep = ('defZoneStep' in options) ? options.defZoneStep : 0;
const toTernAtom = (exprAtSchema) => {
let ternAtom = exprAtSchema;
if(exprAtSchema.type !== undefined) {
if(exprAtSchema.type === 'boolean') {
ternAtom = 'bool';
}
else if(exprAtSchema.type === 'integer') {
ternAtom = 'number';
}
else if(exprAtSchema.type === 'any') {
ternAtom = '?';
}
else if(exprAtSchema.type === 'array') { // array only exists in definition
// array with choices may only in events.UrlFilter.ports
// and "!type": "[number]?, [[number]]?" has no error...
if(exprAtSchema.items.choices !== undefined) {
const ternChoices = [];
for(const cho of exprAtSchema.items.choices) {
ternChoices.push(`[${toTernAtom(cho)}]?`);
}
ternAtom = `${ternChoices.join(', ')}`;
}
else {
ternAtom = `[${toTernAtom(exprAtSchema.items)}]`;
}
}
else if(exprAtSchema.type === 'function') {
// you SHOULD TRIM param.name. 'fn( arg: string...)' result in error
// and hard to notice.
const paramArr = [];
if(exprAtSchema.parameters !== undefined) {
for(const param of exprAtSchema.parameters) {
if(!param.name) {
param.name = 'nameless';
}
if(param.choices !== undefined) {
for(const cho of param.choices) {
paramArr.push(`${param.name.trim()}?: ${toTernAtom(cho)}`);
}
}
else {
const atomString = toTernAtom(param);
// anyway avoid "!type": "object". [object] is not problemsome.
paramArr.push(`${param.name.trim()}: ${atomString}`);
}
}
}
ternAtom = `fn(${paramArr.join(', ')})`;
}
else {
ternAtom = exprAtSchema.type;
}
}
else if(exprAtSchema.choices !== undefined) {
//browserUI has purely choices
const ternChoices = [];
for(const cho of exprAtSchema.choices) {
ternChoices.push(`[${toTernAtom(cho)}]?`);
}
ternAtom = `${ternChoices.join(', ')}`;
}
else if(exprAtSchema.value !== undefined) {
ternAtom = 'number';
}
else if(exprAtSchema['$ref'] !== undefined) {
if(exprAtSchema['$ref'].indexOf('.') !== -1) {
ternAtom = `+${exprAtSchema['$ref']}`; // tabs.Tab or so
}
else {
ternAtom = `+${declaredAt}.${exprAtSchema['$ref']}`;
}
}
return ternAtom;
};
const result = {};
if(curItem.description !== undefined) {
result['!doc'] = escapeTag(curItem.description);
}
const atomString = toTernAtom(curItem);
// anyway avoid "!type": "object". [object] is not problemsome.
if(atomString !== 'object') {
result['!type'] = atomString;
}
if(useMdn) {
let bcdTree = mdnData;
for(const nd of nameTree) {
if(bcdTree === undefined) {
break;
}
bcdTree = bcdTree[nd];
}
if(bcdTree !== undefined) {
if(bcdTree.__compat !== undefined) {
result['!url'] = bcdTree.__compat.mdn_url;
}
}
}
if(curItem.functions !== undefined) {
for(const fun of curItem.functions) {
result[fun.name] = makeTernDefTree(declaredAt, nameTree.concat(fun.name), fun, useMdn, { isDefZone, defZoneStep: (defZoneStep + 1) });
}
}
if(curItem.properties !== undefined) {
for(const prop in curItem.properties) {
result[prop] = makeTernDefTree(declaredAt, nameTree.concat(prop), curItem.properties[prop], useMdn, { isDefZone, defZoneStep: (defZoneStep + 1) });
}
}
return result;
};
const makeTernDefineZone = (declaredAt, nameTree, curItem, useMdn) => {
return makeTernDefTree(declaredAt, nameTree, curItem, useMdn, { isDefZone: true, defZoneStep: 0});
};
const makeTernNonDefZone = (declaredAt, nameTree, curItem, useMdn) => {
return makeTernDefTree(declaredAt, nameTree, curItem, useMdn, { isDefZone: false });
};
const build = (rootDir, apiGroup, result, summary) => {
const subSummary = {
name: apiGroup.name,
schemaList: [],
};
makeSchemaList(rootDir, apiGroup);
const ternDefineObj = result['!define'];
const browserObj = result.browser;
const useMdn = apiGroup.useMdn;
for(const schemaItem of apiGroup.schemaList) {
const apiSummary = {
file: schemaItem.schema,
namespaceList: [],
};
const schemaFileFull = path.join(rootDir, schemaItem.schema);
try {
const apiSpecList = JSON.parse(stripJsonComments(fs.readFileSync(schemaFileFull, 'utf8')));
apiSpecList.forEach(apiSpec => {
const nsSummary = {
name: apiSpec.namespace,
permissions: apiSpec.permissions,
};
// if namespace is 'manifest', Object.keys => ["namespace", "types"]
// namespace is not common between files. except 'manifest'
if(apiSpec.namespace !== 'manifest') {
const ternApiObj = {};
if(apiSpec.description !== undefined) {
ternApiObj['!doc'] = escapeTag(apiSpec.description);
}
//privacy.xxx, devtools.xxx.... not match tern and not go straight with compat-table
const nameTreeTop = apiSpec.namespace.split('.');
if(apiSpec.types !== undefined) { // !define is common in specific apiGroup
nsSummary.types = apiSpec.types.length;
for(const typ of apiSpec.types) {
const curDefObj = makeTernDefineZone(apiSpec.namespace, nameTreeTop.concat(typ.id), typ, useMdn);
if(Object.keys(curDefObj).length !== 0) {
ternDefineObj[`${apiSpec.namespace}.${typ.id}`] = curDefObj;
}
}
}
if(apiSpec.functions !== undefined) {
nsSummary.functions = apiSpec.functions.length;
for(const fun of apiSpec.functions) {
ternApiObj[fun.name] = makeTernNonDefZone(apiSpec.namespace, nameTreeTop.concat(fun.name), fun, useMdn);
}
}
if(apiSpec.events !== undefined) {
nsSummary.events = apiSpec.events.length;
for(const evt of apiSpec.events) {
ternApiObj[evt.name] = makeTernNonDefZone(apiSpec.namespace, nameTreeTop.concat(evt.name), evt, useMdn);
}
}
if(apiSpec.properties !== undefined) {
nsSummary.properties = apiSpec.properties.length;
for(const prop in apiSpec.properties) {
ternApiObj[prop] = makeTernNonDefZone(apiSpec.namespace, nameTreeTop.concat(prop), apiSpec.properties[prop], useMdn);
}
}
if(nameTreeTop.length === 1) {
// case:split over not only one file: menus
if(browserObj[apiSpec.namespace] === undefined) {
browserObj[apiSpec.namespace] = ternApiObj;
}
else {
console.log(`WARN:split over some files ${apiSpec.namespace}`);
for(const key of Object.keys(ternApiObj)) {
if(browserObj[apiSpec.namespace][key] !== undefined) {
console.log(` Problem:dup at ${apiSpec.namespace} ${key}`);
}
browserObj[apiSpec.namespace][key] = ternApiObj[key];
}
}
}
else {
//console.log(` namespace contains dot ${apiSpec.namespace}`);
if(browserObj[nameTreeTop[0]][nameTreeTop[1]] === undefined) {
browserObj[nameTreeTop[0]][nameTreeTop[1]] = ternApiObj; // length 2 is maybe enough
}
else {
console.log(`WARN:split over some files ${apiSpec.namespace}`);
}
}
}
else {
if(apiSpec.types !== undefined) {
nsSummary.types = apiSpec.types.length;
}
}
apiSummary.namespaceList.push(nsSummary);
});
const origContents = fs.readFileSync(schemaFileFull, 'utf8');
if(origContents.includes('BSD-style')) {
apiSummary.license = 'BSD';
}
else if(origContents.includes('MPL')) {
apiSummary.license = 'MPL';
}
else {
apiSummary.license = 'None';
}
} catch(err) {
// e.g. comm-central does not have a file for pkcs11, so fs.readFileSync() fails.
console.log(`(API: ${apiGroup.name}, Schema Name: ${schemaItem.name}): ${err}`);
apiSummary.error = `${err.code}: ${err.syscall}`;
}
subSummary.schemaList.push(apiSummary);
}
summary.push(subSummary);
};
const isValidEnv = (report) => {
report.message.forEach(m => {
console.log(m);
});
return report.isValid;
};
const program = () => {
if(isValidEnv(numerateArgs()) === false) {
return;
}
const result = outputSpec.resultBase;
outputSpec.groupList.forEach(apiGroup => {
const tgtRepo = apiGroup.getRepository();
if(tgtRepo !== '' && isValidEnv(checkRepositoryDirs(tgtRepo, apiGroup))) {
build(tgtRepo, apiGroup, result, summary);
}
});
if(fs.existsSync('defs') === false) {
fs.mkdir('defs');
}
if(goShrink) {
fs.writeFileSync(`defs/${getOutputFileName(outputSpec.prefix)}.json`, JSON.stringify(result));
}
fs.writeFileSync(`defs/${getOutputFileName(outputSpec.prefix)}.expand.json`, JSON.stringify(result, null, 2));
if(fs.existsSync('docs') === false) {
fs.mkdir('docs');
}
fs.writeFileSync('docs/summary.json', JSON.stringify(summary, null, 2));
};
program();
// vim:expandtab ff=unix fenc=utf-8 sw=2