-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
438 lines (376 loc) · 11.9 KB
/
gulpfile.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
'use strict';
const Path = require('path');
const spawn = require('child_process').spawn;
const File = require('vinyl');
const FS = require('fs-extra');
const Gulp = require('gulp');
const Promise = require('thenfail').Promise;
const argv = require('yargs').argv;
const filter = require('gulp-filter');
const glob = require('glob');
const gulpIf = require('gulp-if');
const header = require('gulp-header');
const merge = require('merge-stream');
const rename = require('gulp-rename');
const through = require('through2');
const preCompile = require('./tools/pre-compile');
process.env.NAME_SERVER = '192.168.31.1';
const VERSION = require('./package').version;
const WIN32 = process.platform === 'win32';
const EMBEDDED = !!argv.embedded;
const BYTECODE = EMBEDDED || !!argv.bytecode;
const NINJA = !WIN32 && !EMBEDDED;
const TARGET_PLATFORM = argv.platform;
const BUILD_DIR = Path.join(__dirname, 'build');
const RUFF_COMPILER_PROJECT_DIR = Path.join(__dirname, 'tools/compiler');
const RUFF_COMPILER_BUILD_DIR = Path.join(BUILD_DIR, 'ruff-compiler');
const RUFF_BUILD_DIR = Path.join(BUILD_DIR, `ruff${TARGET_PLATFORM ? `-${TARGET_PLATFORM}` : ''}`);
const RUFF_EXECUTABLE_PATH = Path.join(RUFF_BUILD_DIR, 'bin', WIN32 ? 'ruff.exe' : 'ruff');
const MODULES_DIR = Path.join(__dirname, 'modules/ruff_modules');
const DEPS_MODULES_DIR = Path.join(__dirname, 'dist/deps/ruff_modules');
const RUFF_MODULES_BUILD_DIR = Path.join(RUFF_BUILD_DIR, 'ruff_modules');
const NODE_MODULES_BUILD_DIR = Path.join(RUFF_BUILD_DIR, 'node_modules');
const MODULES_TEST_FILES_PATTERN = Path.join(MODULES_DIR, '*/test/**/*test.js');
const ISTANBUL_CLI_PATH = Path.join(__dirname, 'ruff_modules/istanbul/lib/cli.js');
const ISTANBUL_COVERAGE_DIR = Path.join(__dirname, 'coverage');
const DOCS_BUILD_DIR = Path.join(RUFF_BUILD_DIR, 'doc');
const RUFFAPI_BUILD_DIR = Path.join(__dirname, 'ruff-api');
const DOCS_DIR = Path.join(__dirname, 'ruff-api/doc');
const COPYRIGHT_HEADER = '\
/*!\n\
* Copyright (c) 2015-2017, Nanchao, Inc.\n\
* All rights reserved.\n\
*/\n\n';
const SHARED_MODULE_NAMES = [
'adc',
'assert',
'child_process',
'dgram',
'dns',
'gpio',
'http',
'i2c',
'net',
'pwm',
'ruff-driver',
'ruff-async',
'querystring',
'stream',
'trait',
'uart',
'url',
'fs',
'https'
];
const EMBEDDED_SPECIFIC_MODULE_NAMES = [
'ruff',
'launcher',
'process',
'os',
'kernel-module',
'_console_eval',
'_file',
'poll',
'ruff-util',
'network-manager',
'network-manager-5350',
'connection-manager',
'mobile-manager-5350'
];
const SDK_SPECIFIC_MODULE_NAMES = [
'test',
'ruff-mock',
'ruff-driver-runner',
'ruff-app-runner'
];
const TARGET_NODE_MODULE_NAMES = [
'ruff',
'launcher',
'ruff-driver',
'trait',
'uart'
];
const TARGET_MODULE_NAMES = SHARED_MODULE_NAMES.concat(EMBEDDED ? EMBEDDED_SPECIFIC_MODULE_NAMES : SDK_SPECIFIC_MODULE_NAMES);
////////////////////
// ruff-api BUILD //
////////////////////
Gulp.task('build-ruff-api', () => {
let ruffModulesSrcPath = Path.join(RUFFAPI_BUILD_DIR, 'dist/ruff/ruff_modules');
let ruffModulesDestPath = MODULES_DIR;
let npmcmd = findCommand('npm');
return Promise.invoke(FS.remove, ruffModulesDestPath)
.then(() => executeCommand({
cwd: RUFFAPI_BUILD_DIR,
name: npmcmd,
args: ['run', 'gulp', 'clean', '--', '--tag=ruff']
}))
.then(() => executeCommand({
cwd: RUFFAPI_BUILD_DIR,
name: npmcmd,
args: ['run', 'gulp', 'build-modules', '--', '--tag=ruff']
}))
.then(() => Promise.invoke(FS.copy, ruffModulesSrcPath, ruffModulesDestPath));
});
/////////////
// C BUILD //
/////////////
Gulp.task('generate-ruff-compiler', () => {
return generateProject(RUFF_COMPILER_PROJECT_DIR, RUFF_COMPILER_BUILD_DIR);
});
Gulp.task('build-ruff-compiler', () => {
return buildProject(RUFF_COMPILER_BUILD_DIR);
});
Gulp.task('clean-ruff-compiler', () => {
return Promise.invoke(FS.remove, RUFF_COMPILER_BUILD_DIR);
});
Gulp.task('generate-ruff', () => {
return generateProject(__dirname, RUFF_BUILD_DIR, ['-DDUK_DEBUG="FALSE"']);
});
Gulp.task('build-ruff', () => {
return buildProject(RUFF_BUILD_DIR);
});
Gulp.task('ruff-sanity-check', Gulp.series('build-ruff', () => {
return executeCommand({
name: 'node',
cwd: process.cwd(),
args: [
'sanity_tests/func_bc_test.js',
RUFF_EXECUTABLE_PATH
]
});
}));
Gulp.task('clean-ruff', () => {
return Promise.invoke(FS.remove, RUFF_BUILD_DIR);
});
function generateProject(projectDir, buildDir, extraArgs) {
return executeCommand({
name: 'cmake',
cwd: projectDir,
args: [
'-H.',
`-B${buildDir}`,
`-DRUFF_VERSION=${VERSION}`,
...(NINJA ? ['-GNinja'] : []),
...(extraArgs || [])
]
});
}
function buildProject(dir) {
if (WIN32) {
// Notes: Use debug version build for Windows
// When upgrade duktape to 2.5.0 release build will cause test break(ruff may exit unnormal)
// the reason may releated to build flag or optimize issue just bypass
return executeCommand({
name: 'msbuild',
cwd: dir,
args: [
'INSTALL.vcxproj',
'/p:Configuration=Debug'
]
});
} else {
let commandName = NINJA ? 'ninja' : 'make';
return executeCommand({
name: commandName,
cwd: dir
})
.then(() => executeCommand({
name: commandName,
cwd: dir,
args: [
'install'
]
}));
}
}
////////////////
// JS MODULES //
////////////////
Gulp.task('prepare-modules', () => {
let streams = TARGET_MODULE_NAMES
.map(name => getModuleStream(name, RUFF_MODULES_BUILD_DIR, true))
.filter(stream => !!stream);
return merge(...streams);
});
Gulp.task('prepare-node-modules', () => {
let streams = TARGET_NODE_MODULE_NAMES
.map(name => getModuleStream(name, NODE_MODULES_BUILD_DIR, false))
.filter(stream => !!stream);
return merge(...streams);
});
Gulp.task('copy-native-modules', () => {
// var child_process = require('child_process');
// // Workaround: copy tls/crypto dirs seperately
// // it's because that spawn `cp dir_a/* dir_b` failed on Mac
// child_process.spawn('cp', ['-r', './dist/deps/native-modules/ruff_modules/crypto', './build/ruff/ruff_modules']);
// child_process.spawn('cp', ['-r', './dist/deps/native-modules/ruff_modules/tls', './build/ruff/ruff_modules']);
return executeCommand({
name: 'cp',
args: ['-r', './dist/deps/native-modules/ruff_modules/crypto', './build/ruff/ruff_modules'],
}).then(() => {
return executeCommand({
name: 'cp',
args: ['-r', './dist/deps/native-modules/ruff_modules/tls', './build/ruff/ruff_modules'],
});
});
});
Gulp.task('test-modules', Gulp.series('ruff-sanity-check', () => {
return Promise
.invoke(FS.remove, ISTANBUL_COVERAGE_DIR)
.then(() => Promise.invoke(glob, MODULES_TEST_FILES_PATTERN))
.each(path => {
let relPath = Path.relative(MODULES_DIR, path);
let moduleName = relPath.replace(/[\\/].+$/, '');
return executeCommand({
name: RUFF_EXECUTABLE_PATH,
cwd: MODULES_DIR,
args: [
ISTANBUL_CLI_PATH,
'cover',
path,
'--dir',
Path.resolve('coverage', moduleName)
]
});
});
}));
Gulp.task('clean-modules', () => {
return Promise.invoke(FS.remove, RUFF_MODULES_BUILD_DIR);
});
Gulp.task('clean-node-modules', () => {
return Promise.invoke(FS.remove, NODE_MODULES_BUILD_DIR);
});
function getModuleStream(name, destDir, isBytecode) {
let moduleDir = Path.join(MODULES_DIR, name);
let moduleSrcDir = Path.join(moduleDir, 'src');
let singleJsFile;
try {
singleJsFile = FS.readdirSync(moduleSrcDir).length === 1;
} catch (error) {
moduleDir = Path.join(DEPS_MODULES_DIR, name);
moduleSrcDir = Path.join(moduleDir, 'src');
singleJsFile = FS.readdirSync(moduleSrcDir).length === 1;
}
let sources;
let dest;
if (singleJsFile) {
sources = [
'src/**/*.js'
];
dest = destDir;
} else {
sources = [
'src/**/*.js',
'src/**/*.so',
'package.json'
];
dest = Path.join(destDir, name);
}
let jsFilter = filter('**/*.js', { restore: true });
if (isBytecode) {
return Gulp
.src(sources, {
cwd: moduleDir,
base: singleJsFile ? moduleSrcDir : moduleDir
})
.pipe(jsFilter)
.pipe(header(COPYRIGHT_HEADER))
.pipe(gulpIf(singleJsFile, rename({ basename: name })))
.pipe(preCompileThrough(singleJsFile))
.pipe(jsFilter.restore)
.pipe(Gulp.dest(dest));
} else {
return Gulp
.src(sources, {
cwd: moduleDir,
base: singleJsFile ? moduleSrcDir : moduleDir
})
.pipe(jsFilter)
.pipe(header(COPYRIGHT_HEADER))
.pipe(gulpIf(singleJsFile, rename({ basename: name })))
.pipe(jsFilter.restore)
.pipe(Gulp.dest(dest));
}
}
function preCompileThrough(single) {
return through.obj(function (file, encoding, callback) {
if (!BYTECODE) {
this.push(file);
callback();
return;
}
let relativePath = single ?
Path.basename(file.path) :
Path.relative(MODULES_DIR, file.path);
let bytecode = preCompile(file.contents, Path.join('~runtime~', relativePath));
let bcFile = new File({
cwd: file.cwd,
base: file.base,
path: `${file.path}.bc`,
contents: bytecode
});
this.push(bcFile);
callback();
});
}
//////////
// DOCS //
//////////
Gulp.task('build-docs', () => {
let docCount = 0;
return Promise
.invoke(FS.ensureDir, DOCS_BUILD_DIR)
.then(() => Promise.invoke(glob, `${DOCS_DIR}/*.md`))
.each(path => {
docCount++;
console.log(`Compiling markdown "${path}"...`);
let cp = spawn(
'node',
[
'modules/ruff_scripts/generate.js',
'--format=html',
'--template=modules/ruff_scripts/template.html',
path
]
);
let htmlPath = Path.join(DOCS_BUILD_DIR, `${Path.basename(path, '.md')}.html`);
let fileStream = FS.createWriteStream(htmlPath);
cp.stdout.pipe(fileStream);
return Promise.all([
Promise.for(cp),
Promise.for(fileStream, 'close')
]);
})
.then(() => {
if (docCount <= 0) {
return Promise.reject('Error: no doc files found!');
}
console.log(`Total ${docCount} doc files are generated.`);
return Promise.resolve();
});
});
////////////
// COMMON //
////////////
Gulp.task('clean', () => {
return Promise.invoke(FS.remove, BUILD_DIR);
});
function executeCommand(command) {
let cp = spawn(
command.name,
command.args || [],
{
cwd: command.cwd,
stdio: 'inherit'
}
);
return Promise.for(cp);
}
function findCommand(cmd) {
const which = require('which');
try {
return which.sync(cmd);
} catch (e) {
return '';
}
}