-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.php
More file actions
140 lines (123 loc) · 4.19 KB
/
build.php
File metadata and controls
140 lines (123 loc) · 4.19 KB
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
#!/bin/php
<?php
// Only allow run from cli.
if (php_sapi_name() !== 'cli') {
exit(0);
}
/* Parameters:
--no-composer Does not install vendors via composer
--cleanup Remove removeables
--install-npm Installs npm package as per package.json name field
--release Does not run composer install and does not remove .git
*/
// 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')) {
if (is_array($argv) && !in_array('--install-npm', $argv)) {
$buildCommands[] = 'npm ci --no-progress --no-audit';
$buildCommands[] = 'npm run build';
} else {
$npmPackage = json_decode(file_get_contents('package.json'));
$buildCommands[] = "npm install $npmPackage->name";
$buildCommands[] = "rm -rf ./dist";
$buildCommands[] = "mv node_modules/$npmPackage->name/dist ./";
}
} elseif (file_exists('package.json') && !file_exists('package-lock.json')) {
if (is_array($argv) && !in_array('--install-npm', $argv)) {
$buildCommands[] = 'npm install --no-progress --no-audit';
$buildCommands[] = 'npm run build';
} else {
$npmPackage = json_decode(file_get_contents('package.json'));
$buildCommands[] = "npm install $npmPackage->name";
$buildCommands[] = "rm -rf ./dist";
$buildCommands[] = "mv node_modules/$npmPackage->name/dist ./";
}
}
// Files and directories not suitable for prod to be removed.
$removables = [
'.gitignore',
'.github',
'.gitattributes',
'build.php',
'build.js',
'.npmrc',
//'composer.json',
'composer.lock',
'env-example',
'webpack.config.js',
'package-lock.json',
'package.json',
'phpunit.xml.dist',
'README.md',
'./node_modules/',
'./source/sass/',
'./source/js/',
'LICENSE',
'babel.config.js',
'yarn.lock',
'.devcontainer',
];
if (is_array($argv) && !in_array('--release', $argv)) {
$removables = array_merge($removables, ['.git']);
}
$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\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]);
}