-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtransformer.js
More file actions
262 lines (225 loc) · 7.72 KB
/
Copy pathtransformer.js
File metadata and controls
262 lines (225 loc) · 7.72 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
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
"use strict";
const path = require("path");
const co = require("co");
const fs = require("pn/fs");
const webidl = require("webidl2");
const prettier = require("prettier");
const Context = require("./context");
const Typedef = require("./constructs/typedef");
const Interface = require("./constructs/interface");
const Dictionary = require("./constructs/dictionary");
const Enumeration = require("./constructs/enumeration");
class Transformer {
constructor(opts = {}) {
this.ctx = new Context({
implSuffix: opts.implSuffix,
options: {
suppressErrors: Boolean(opts.suppressErrors)
}
});
this.sources = []; // Absolute paths to the IDL and Impl directories.
this.utilPath = null;
}
addSource(idl, impl) {
if (typeof idl !== "string") {
throw new TypeError("idl path has to be a string");
}
if (typeof impl !== "string") {
throw new TypeError("impl path has to be a string");
}
this.sources.push({ idlPath: path.resolve(idl), impl: path.resolve(impl) });
return this;
}
* _collectSources() {
const stats = yield Promise.all(this.sources.map(src => fs.stat(src.idlPath)));
const files = [];
for (let i = 0; i < stats.length; ++i) {
if (stats[i].isDirectory()) {
const folderContents = yield fs.readdir(this.sources[i].idlPath);
for (const file of folderContents) {
if (file.endsWith(".webidl")) {
files.push({
idlPath: path.join(this.sources[i].idlPath, file),
impl: this.sources[i].impl
});
}
}
} else {
files.push({
idlPath: this.sources[i].idlPath,
impl: this.sources[i].impl
});
}
}
return files;
}
* _readFiles(files) {
const zipped = [];
const fileContents = yield Promise.all(files.map(f => fs.readFile(f.idlPath, { encoding: "utf-8" })));
for (let i = 0; i < files.length; ++i) {
zipped.push({
idlContent: fileContents[i],
impl: files[i].impl
});
}
return zipped;
}
_parse(outputDir, contents) {
const parsed = contents.map(content => ({
idl: webidl.parse(content.idlContent),
impl: content.impl
}));
this.ctx.initialize();
const { interfaces, dictionaries, enumerations, typedefs } = this.ctx;
// first we're gathering all full interfaces and ignore partial ones
for (const file of parsed) {
for (const instruction of file.idl) {
let obj;
switch (instruction.type) {
case "interface":
if (instruction.partial) {
break;
}
obj = new Interface(this.ctx, instruction, {
implDir: file.impl
});
interfaces.set(obj.name, obj);
break;
case "implements":
break; // handled later
case "dictionary":
if (instruction.partial) {
break;
}
obj = new Dictionary(this.ctx, instruction);
dictionaries.set(obj.name, obj);
break;
case "enum":
obj = new Enumeration(this.ctx, instruction);
enumerations.set(obj.name, obj);
break;
case "typedef":
obj = new Typedef(this.ctx, instruction);
typedefs.set(obj.name, obj);
break;
default:
if (!this.ctx.options.suppressErrors) {
throw new Error("Can't convert type '" + instruction.type + "'");
}
}
}
}
// second we add all partial members and handle implements
for (const file of parsed) {
for (const instruction of file.idl) {
let oldMembers;
let extAttrs;
switch (instruction.type) {
case "interface":
if (!instruction.partial) {
break;
}
if (this.ctx.options.suppressErrors && !interfaces.has(instruction.name)) {
break;
}
oldMembers = interfaces.get(instruction.name).idl.members;
oldMembers.push(...instruction.members);
extAttrs = interfaces.get(instruction.name).idl.extAttrs;
extAttrs.push(...instruction.extAttrs);
break;
case "dictionary":
if (!instruction.partial) {
break;
}
if (this.ctx.options.suppressErrors && !dictionaries.has(instruction.name)) {
break;
}
oldMembers = dictionaries.get(instruction.name).idl.members;
oldMembers.push(...instruction.members);
extAttrs = dictionaries.get(instruction.name).idl.extAttrs;
extAttrs.push(...instruction.extAttrs);
break;
case "implements":
if (this.ctx.options.suppressErrors && !interfaces.has(instruction.target)) {
break;
}
interfaces.get(instruction.target).implements(instruction.implements);
break;
}
}
}
}
* _writeFiles(outputDir) {
const utilsText = yield fs.readFile(path.resolve(__dirname, "output/utils.js"));
yield fs.writeFile(this.utilPath, utilsText);
const { interfaces, dictionaries, enumerations } = this.ctx;
const allExports = [];
for (const obj of interfaces.values()) {
let source = obj.toString();
let implFile = path.relative(outputDir, path.resolve(obj.opts.implDir, obj.name + this.ctx.implSuffix));
implFile = implFile.replace(/\\/g, "/"); // fix windows file paths
if (implFile[0] !== ".") {
implFile = "./" + implFile;
}
let relativeUtils = path.relative(outputDir, this.utilPath).replace(/\\/g, "/");
if (relativeUtils[0] !== ".") {
relativeUtils = "./" + relativeUtils;
}
source = `
"use strict";
const conversions = require("webidl-conversions");
const utils = require("${relativeUtils}");
${source}
const Impl = require("${implFile}.js");
`;
source = this._prettify(source);
yield fs.writeFile(path.join(outputDir, obj.name + ".js"), source);
allExports.push(obj.name);
}
for (const obj of dictionaries.values()) {
let source = obj.toString();
let relativeUtils = path.relative(outputDir, this.utilPath).replace(/\\/g, "/");
if (relativeUtils[0] !== ".") {
relativeUtils = "./" + relativeUtils;
}
source = `
"use strict";
const conversions = require("webidl-conversions");
const utils = require("${relativeUtils}");
${source}
`;
source = this._prettify(source);
yield fs.writeFile(path.join(outputDir, obj.name + ".js"), source);
allExports.push(obj.name);
}
for (const obj of enumerations.values()) {
const source = this._prettify(`
"use strict";
${obj.toString()}
`);
yield fs.writeFile(path.join(outputDir, obj.name + ".js"), source);
allExports.push(obj.name);
}
const bundleEntryText = yield fs.readFile(path.resolve(__dirname, "output/bundle-entry.js"));
const out = path.join(outputDir, "bundle-entry.js");
yield fs.writeFile(out, bundleEntryText);
yield fs.appendFile(out, allExports.map(e => `allExports[${JSON.stringify(e)}] = require("./${e}");\n`).join(""));
}
_prettify(source) {
return prettier.format(source, {
printWidth: 120
});
}
generate(outputDir) {
if (!this.utilPath) {
this.utilPath = path.join(outputDir, "utils.js");
}
return co(function* () {
const sources = yield* this._collectSources();
const contents = yield* this._readFiles(sources);
this._parse(outputDir, contents);
yield* this._writeFiles(outputDir);
}.bind(this));
}
}
module.exports = Transformer;