From ddb1c0b1c75e5222b6a632b237f8c166a7e6f94f Mon Sep 17 00:00:00 2001 From: Mackan Date: Tue, 1 Oct 2024 14:11:11 +0200 Subject: [PATCH] Update build.php to latest version Updates build.php to be in line with the other plugins, so we can have a common base. --- build.php | 73 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 62 insertions(+), 11 deletions(-) diff --git a/build.php b/build.php index 7c58993..d1e0ba7 100644 --- a/build.php +++ b/build.php @@ -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__)); @@ -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"; @@ -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 = '';