Skip to content

Instantly share code, notes, and snippets.

@chx
Created October 28, 2023 05:12
Show Gist options
  • Save chx/c0d506a7d870258a49f08e20bddfce3e to your computer and use it in GitHub Desktop.
Save chx/c0d506a7d870258a49f08e20bddfce3e to your computer and use it in GitHub Desktop.
composer branch recalc
<?php
/**
* Raise/update static version numbers in composer.json.
*
* Run on the CLI: "composer outdated --direct > outdated.txt"
*/
$composerJson = json_decode(file_get_contents('composer.json'), true);
$listOfOutdatedPackages = file('outdated.txt');
foreach($listOfOutdatedPackages as $line) {
$regexp = '#(?P<package>[\w-]+/[\w-]+).* (?P<currentVersion>\d+\.\d).*~ (?P<latestVersion>\d+\.\d)#';
preg_match($regexp, $line, $matches);
$matches = array_filter($matches, 'is_string', ARRAY_FILTER_USE_KEY);
if(isset($matches['package']))
{
$package = $matches['package'];
if(isset($composerJson['require'][$package]))
{
$currentVersion = $composerJson['require'][$package];
echo sprintf('Updating %s from %s to %s', $package, $currentVersion, $matches['latestVersion']);
$composerJson['require'][$package] = '~' . $matches['latestVersion'];
}
if(isset($composerJson['require-dev'][$package]))
{
$currentVersion = $composerJson['require-dev'][$package];
echo sprintf('Updating %s from %s to %s', $package, $currentVersion, $matches['latestVersion']);
$composerJson['require-dev'][$package] = $matches['latestVersion'];
}
}
}
file_put_contents('composer.json', json_encode($composerJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment