Skip to content

Commit

Permalink
Update build.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Thulin committed Sep 1, 2023
1 parent 21bd3e7 commit dd72957
Showing 1 changed file with 51 additions and 14 deletions.
65 changes: 51 additions & 14 deletions build.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__));
Expand All @@ -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";
Expand All @@ -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 = '';
Expand All @@ -76,7 +113,7 @@ function executeCommand($command)
$liveOutput = fread($proc, 4096);
$completeOutput = $completeOutput . $liveOutput;
print $liveOutput;
@flush();
@ flush();
}

pclose($proc);
Expand All @@ -86,4 +123,4 @@ function executeCommand($command)

// Return exit status.
return intval($matches[0]);
}
}

0 comments on commit dd72957

Please sign in to comment.