-
Notifications
You must be signed in to change notification settings - Fork 2
/
builder.js
243 lines (224 loc) · 7.32 KB
/
builder.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
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
const PluginError = require('plugin-error');
const Vinyl = require("vinyl");
const log = require("fancy-log");
const os = require("os");
const path = require("path");
const semver = require("semver");
const {spawn, spawnSync} = require("child_process");
const through = require("through2");
const {PLUGIN_NAME, isfile, isdir} = require("./util");
function compile(self, slice2js, files, args, cb)
{
const info = [];
const p = slice2js(args.concat(files.map(f => f.path)));
const chunks = [];
p.stdout.on("data", data =>
{
chunks.push(data);
});
p.stderr.on("data", data =>
{
log.error("'slice2js error'", data.toString());
});
p.on('close', code =>
{
if(code === 0)
{
let generated = null;
const re = /\/\*\* slice2js: (.*) generated begin (.*) \*\*\//;
const reend = /\/\*\* slice2js: generated end \*\*\//;
const lines = Buffer.concat(chunks).toString("utf-8").split(/\r\n|\r|\n/);
let data = "";
for(const line of lines)
{
let m = re.exec(line);
if(m)
{
generated = new Vinyl({cwd: "./", path: m[1]});
m = (/module:"(.*)"/).exec(line);
if(m)
{
info.push(
{
module: m[1],
file: generated.path
});
}
data = [];
}
else if(reend.exec(line))
{
generated.contents = Buffer.from(data);
self.push(generated);
}
else
{
data += line + os.EOL;
}
}
if(info.length > 0)
{
self.push(new Vinyl(
{
cwd: "./",
path: "module.json",
contents: Buffer.from(JSON.stringify(info))
}));
}
if(generated === null)
{
const basename = path.basename(files[0].path, ".ice");
const outputPath = args.some(v => ["--depend", "--depend-xml", "--depend-json"].includes(v)) ?
`${basename}.d` : `${basename}.js`;
self.push(new Vinyl(
{
cwd: "./",
path: `${outputPath}`,
contents: Buffer.from(data)
}));
}
cb();
}
else
{
cb(new PluginError(PLUGIN_NAME, `slice2js exit with error code: ${code}`));
}
});
}
function version(slice2js)
{
return spawnSync(slice2js, ["-v"], {stdio: 'pipe'}).output[2].toString().trim();
}
function builder(options)
{
const opts = options || {};
let slice2js;
let exe = (os.platform == "win32" ? "slice2js.exe" : "slice2js");
const iceHome = opts.iceHome;
let iceToolsPath = opts.iceToolsPath;
const include = opts.include || [];
let args = opts.args || [];
args = args.concat(include.map(d => `-I${d}`));
args.push("--stdout");
let v;
if(iceHome)
{
if(!iceToolsPath)
{
if(isdir(path.resolve(iceHome, "cpp", "bin")))
{
iceToolsPath = path.resolve(iceHome, "cpp", "bin");
}
else
{
iceToolsPath = path.resolve(iceHome, "bin");
}
}
exe = path.resolve(iceToolsPath, exe);
if(!isfile(path.join(exe)))
{
throw new PluginError(PLUGIN_NAME, `Unable to locate slice2js compiler in ${iceToolsPath}"`);
}
v = version(exe);
const slicedir = [path.resolve(iceHome, "slice"),
path.resolve(iceHome, "share", `Ice-${v}`, "slice"),
path.resolve(iceHome, "share", "slice"),
path.resolve(iceHome, "share", "ice", "slice")].find(d => isdir(d));
if(!slicedir)
{
throw new PluginError(PLUGIN_NAME, `Unable to locate Slice directory in "${iceHome}"`);
}
args.push(`-I${slicedir}`);
slice2js = args => spawn(path.resolve(iceToolsPath, exe), args);
}
else // Using npm slice2js package
{
try
{
const SLICE2JS_PACKAGE_NAME = "slice2js";
// First check if the slice2js package contains the slice2js executable path
// If it doesn't then we guess the path based on its location inside the package
if(require(SLICE2JS_PACKAGE_NAME).slice2js)
{
iceToolsPath = path.dirname(require(SLICE2JS_PACKAGE_NAME).slice2js);
}
else
{
iceToolsPath = path.join(path.dirname(require.resolve(SLICE2JS_PACKAGE_NAME)), 'build', 'Release');
}
slice2js = require(SLICE2JS_PACKAGE_NAME).compile;
}
catch(e)
{
throw new PluginError(PLUGIN_NAME, "Unable to load slice2js package");
}
exe = path.resolve(iceToolsPath, exe);
if(!isfile(path.join(exe)))
{
throw new PluginError(PLUGIN_NAME, `Unable to locate slice2js compiler in ${iceToolsPath}"`);
}
v = version(exe);
}
if(semver.valid(v) !== null && semver.gt(v, "3.7.1"))
{
const files = [];
return through.obj(
function(file, enc, cb)
{
if(file.isNull())
{
cb();
}
else if(path.extname(file.path) != ".ice")
{
//
// Let non Slice files pass-through
//
cb(null, file);
}
else if(file.isStream())
{
cb(new PluginError(PLUGIN_NAME, "Streaming not supported"));
}
else
{
files.push(file);
cb();
}
},
function(cb)
{
if(files.length > 0)
{
compile(this, slice2js, files, args, cb);
}
else
{
cb();
}
});
}
else
{
return through.obj(
function(file, enc, cb)
{
if(file.isNull())
{
cb();
}
else if(file.isStream())
{
cb(new PluginError(PLUGIN_NAME, "Streaming not supported"));
}
else
{
compile(this, slice2js, [file], args, cb);
}
});
}
}
module.exports = builder;