From dd7295726644eed3a8ba7fffdb2b0c85fc610423 Mon Sep 17 00:00:00 2001 From: Sebastian Thulin Date: Fri, 1 Sep 2023 09:54:14 +0200 Subject: [PATCH] Update build.php --- build.php | 65 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 14 deletions(-) diff --git a/build.php b/build.php index b244836..3dfb7a3 100644 --- a/build.php +++ b/build.php @@ -5,35 +5,63 @@ 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 = [ - 'yarn --frozen-lockfile', - 'npx browserslist@latest --update-db', - 'yarn build' -]; +$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', - 'node_modules', 'package-lock.json', 'package.json', - 'patchwork.json', - 'phpunit.xml', - 'source/tests' + 'phpunit.xml.dist', + 'README.md', + 'gulpfile.js', + './node_modules/', + './source/sass/', + './source/js/', + 'LICENSE', + 'babel.config.js', + 'yarn.lock' ]; $dirName = basename(dirname(__FILE__)); @@ -43,15 +71,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"; @@ -67,7 +97,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 = ''; @@ -76,7 +113,7 @@ function executeCommand($command) $liveOutput = fread($proc, 4096); $completeOutput = $completeOutput . $liveOutput; print $liveOutput; - @flush(); + @ flush(); } pclose($proc); @@ -86,4 +123,4 @@ function executeCommand($command) // Return exit status. return intval($matches[0]); -} +} \ No newline at end of file