forked from helsingborg-stad/Customer-feedback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.php
89 lines (77 loc) · 2.37 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
#!/bin/php
<?php
// Only allow run from cli.
if (php_sapi_name() !== 'cli') {
exit(0);
}
// Any command needed to run and build plugin assets when newly cheched out of repo.
$buildCommands = [
'npm ci --no-progress --no-audit',
'npx --yes browserslist@latest --update-db',
'npm run build',
'composer install --prefer-dist --no-progress'
];
// Files and directories not suitable for prod to be removed.
$removables = [
'.git',
'.gitignore',
'.github',
'build.php',
'composer.json',
'composer.lock',
'webpack.config.js',
'node_modules',
'package-lock.json',
'package.json'
];
$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 (isset($argv[1]) && $argv[1] === '--cleanup') {
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]);
}