-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.php
131 lines (115 loc) · 3.63 KB
/
build.php
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
#!/bin/php
<?php
// Only allow run from cli.
if (php_sapi_name() !== 'cli') {
exit(0);
}
/* Parameters:
--no-composer Does not install vendors. Just create the autoloader.
--cleanup Remove removeables.
--allow-gulp Allow gulp to be used.
*/
// Any command needed to run and build plugin assets when newly cheched out of repo.
$buildCommands = [];
//Add composer build, if flag --no-composer is undefined.
//Dump autloader.
//Only if composer.json exists.
if (file_exists('composer.json')) {
if (is_array($argv) && !in_array('--no-composer', $argv)) {
$buildCommands[] = 'composer install --prefer-dist --no-progress --no-dev';
}
$buildCommands[] = 'composer dump-autoload';
}
//Run npm if package.json is found
if (file_exists('package.json') && file_exists('package-lock.json')) {
$buildCommands[] = 'npm ci --no-progress --no-audit';
} elseif (file_exists('package.json') && !file_exists('package-lock.json')) {
$buildCommands[] = 'npm install --no-progress --no-audit';
}
//Run build if package-lock.json is found
if (file_exists('package-lock.json') && !file_exists('gulp.js')) {
$buildCommands[] = 'npx --yes browserslist@latest --update-db';
$buildCommands[] = 'npm run build';
} elseif (
file_exists('package-lock.json') &&
file_exists('gulp.js') &&
is_array($argv) &&
in_array('--allow-gulp', $argv)
) {
$buildCommands[] = 'gulp';
}
// Files and directories not suitable for prod to be removed.
$removables = [
'.git',
'.gitignore',
'.github',
'.gitattributes',
'build.php',
'.npmrc',
'composer.json',
'composer.lock',
'env-example',
'webpack.config.js',
'package-lock.json',
'package.json',
'phpunit.xml.dist',
'README.md',
'gulpfile.js',
'./node_modules/',
'./source/sass/',
'./source/js/',
'LICENSE',
'babel.config.js',
'yarn.lock',
];
$dirName = basename(dirname(__FILE__));
// Run all build commands.
$output = '';
$exitCode = 0;
foreach ($buildCommands as $buildCommand) {
print "---- Running build command '$buildCommand' for $dirName. ----\n";
$timeStart = microtime(true);
$exitCode = executeCommand($buildCommand);
$buildTime = round(microtime(true) - $timeStart);
print "---- Done build command '$buildCommand' for $dirName. Build time: $buildTime seconds. ----\n";
if ($exitCode > 0) {
exit($exitCode);
}
}
// Remove files and directories if '--cleanup' argument is supplied to save local developers from disasters.
if (is_array($argv) && in_array('--cleanup', $argv)) {
foreach ($removables as $removable) {
if (file_exists($removable)) {
print "Removing $removable from $dirName\n";
shell_exec("rm -rf $removable");
}
}
}
/**
* Better shell script execution with live output to STDOUT and status code return.
* @param string $command Command to execute in shell.
* @return int Exit code.
*/
function executeCommand($command)
{
$fullCommand = '';
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$fullCommand = "cmd /v:on /c \"$command 2>&1 & echo Exit status : !ErrorLevel!\"";
} else {
$fullCommand = "$command 2>&1 ; echo Exit status : $?";
}
$proc = popen($fullCommand, 'r');
$liveOutput = '';
$completeOutput = '';
while (!feof($proc)) {
$liveOutput = fread($proc, 4096);
$completeOutput = $completeOutput . $liveOutput;
print $liveOutput;
@ flush();
}
pclose($proc);
// Get exit status.
preg_match('/[0-9]+$/', $completeOutput, $matches);
// Return exit status.
return intval($matches[0]);
}