-
Notifications
You must be signed in to change notification settings - Fork 272
/
gulpfile.js
89 lines (80 loc) · 2.16 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
const os = require('os');
const gulp = require('gulp');
const replace = require('gulp-replace');
const rename = require('gulp-rename');
const path = require('path');
const util = require('util');
const fs = require('fs');
const rmAsync = util.promisify(fs.rm);
const { spawn } = require('child_process');
const sourceDir = __dirname;
const outputDir = path.join(__dirname, '..', '..', '..', 'build-php');
async function cleanBuildDir() {
await rmAsync(outputDir, { recursive: true, force: true });
fs.mkdirSync(outputDir);
}
async function build() {
const phpVersion = process.env.PHP_VERSION || '8.0.24';
const withVRZNO = phpVersion.startsWith('7.') ? 'yes' : 'no';
const platform = process.env.PLATFORM === 'node' ? 'node' : 'web';
const withNodeFs = platform === 'node' ? 'yes' : 'no';
// Build PHP
await asyncSpawn(
'docker',
[
'build',
'.',
'--tag=php-wasm',
'--progress=plain',
'--build-arg',
`PHP_VERSION=${phpVersion}`,
'--build-arg',
`WITH_VRZNO=${withVRZNO}`,
'--build-arg',
`WITH_LIBXML=no`,
'--build-arg',
`WITH_LIBZIP=yes`,
'--build-arg',
`WITH_NODEFS=${withNodeFs}`,
'--build-arg',
`EMSCRIPTEN_ENVIRONMENT=${platform}`,
],
{ cwd: sourceDir, stdio: 'inherit' }
);
const targetJsFilename = platform === 'node' ? 'php.node.js' : 'php.js';
// Extract the PHP WASM module
await asyncSpawn(
'docker',
[
'run',
'--name',
'php-wasm-tmp',
'--rm',
'-v',
`${outputDir}:/output`,
'php-wasm',
// Use sh -c because wildcards are a shell feature and
// they don't work without running cp through shell.
'sh',
'-c',
`cp /root/output/php.js /output/${targetJsFilename} && ` +
`cp /root/output/php.wasm /output`,
],
{ cwd: sourceDir, stdio: 'inherit' }
);
}
exports.build = gulp.series(cleanBuildDir, build);
function asyncPipe(pipe) {
return new Promise(async (resolve, reject) => {
pipe.on('finish', resolve).on('error', reject);
});
}
function asyncSpawn(...args) {
return new Promise((resolve, reject) => {
const child = spawn(...args);
child.on('close', (code) => {
if (code === 0) resolve(code);
else reject(new Error(`Process exited with code ${code}`));
});
});
}