Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update build.php to latest version #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 62 additions & 11 deletions build.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,68 @@
exit(0);
}

/* Parameters:
--no-composer Does not install vendors. Just create the autoloader.
--cleanup Remove removeables.
--install-npm Install NPM package instead
*/

// Any command needed to run and build plugin assets when newly cheched out of repo.
$buildCommands = [
'npm install --no-progress',
'npx browserslist@latest --update-db',
'npm run build',
'composer install --prefer-dist --no-progress'
];
$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';
} 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';
} 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 = [
'.git',
'.gitignore',
'.github',
'.gitattributes',
'build.php',
'.npmrc',
'composer.json',
'composer.lock',
'env-example',
'webpack.config.js',
'node_modules',
'package-lock.json',
'package.json'
'package.json',
'phpunit.xml.dist',
'README.md',
'./node_modules/',
'./source/sass/',
'./source/js/',
'LICENSE',
'babel.config.js',
'yarn.lock'
];

$dirName = basename(dirname(__FILE__));
Expand All @@ -34,15 +76,17 @@
$exitCode = 0;
foreach ($buildCommands as $buildCommand) {
print "---- Running build command '$buildCommand' for $dirName. ----\n";
$timeStart = microtime(true);
$exitCode = executeCommand($buildCommand);
print "---- Done build command '$buildCommand' for $dirName. ----\n";
$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') {
if(is_array($argv) && in_array('--cleanup', $argv)) {
foreach ($removables as $removable) {
if (file_exists($removable)) {
print "Removing $removable from $dirName\n";
Expand All @@ -58,7 +102,14 @@
*/
function executeCommand($command)
{
$proc = popen("$command 2>&1 ; echo Exit status : $?", 'r');
$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 = '';
Expand Down