Last active
July 11, 2017 09:22
-
-
Save Blackskyliner/a1c7e80959beb27c3113e525ab5aad61 to your computer and use it in GitHub Desktop.
[OSX][BREW] Handle multiple PHP versions gracefully while 'brew upgrade'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
$packageArray = []; | |
// Save actual php version for restore at the end | |
$prevPHPVersion = exec('php -r' . escapeshellarg('echo PHP_VERSION;')); | |
list($major, $minor, $patch) = explode('.', $prevPHPVersion); | |
$prevPHPVersion = "$major$minor"; | |
// Update brew repository | |
passthru('brew update'); | |
// Get all php packages | |
exec('brew ls | grep -e ' . escapeshellarg('php\d\d'), $packageArray); | |
// Get all PHP Versions for unlink | |
$phpVersions = []; | |
foreach ($packageArray as $package) { | |
if (strpos($package, '-') === false) { | |
$phpVersions[] = $package; | |
} | |
} | |
// Get all PHP packages which need an update | |
$updatePackages = []; | |
foreach ($packageArray as $package) { | |
$info = current(json_decode(exec('brew info --json=v1 ' . escapeshellarg($package)), true)); | |
if ($info['outdated']) { | |
list($version) = explode('-', $package, 2); | |
$updatePackages[$version][] = $package; | |
} | |
} | |
if ($updatePackages) { | |
echo 'Found PHP packages to update ' . implode( | |
', ', | |
iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($updatePackages))) | |
) . PHP_EOL; | |
// Upgrade all Versions seperate | |
foreach ($updatePackages as $version => $versionPackages) { | |
// Unlink all installed version | |
exec('brew unlink ' . implode(' ', $phpVersions)); | |
// Link the one we want to update | |
exec('brew link ' . $version); | |
echo 'Updating' . implode(' ', $versionPackages) . PHP_EOL; | |
// Upgrade the version and it's packages | |
passthru('brew upgrade ' . implode(' ', $versionPackages)); | |
} | |
// Restore PHP Version | |
echo 'Restore original php' . $prevPHPVersion . PHP_EOL; | |
exec('brew unlink ' . implode(' ', $phpVersions)); | |
exec('brew link php' . $prevPHPVersion); | |
} | |
// Upgrade the rest | |
passthru('brew upgrade'); | |
// Cleanup | |
passthru('brew cleanup'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment