Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Lock your composer.json package versions to the latest installed version and prepend a tilde to lock to patch releases only
<?php
// One-line all the things !1!1!!
// wget https://gist.githubusercontent.com/peterjaap/f89df7260aab48b1a1700bffd2779a64/raw/lockComposerJsonToPatchReleases.php && php lockComposerJsonToPatchReleases.php && rm lockComposerJsonToPatchReleases.php
$composerLockData = json_decode(file_get_contents('composer.lock'), true);
$composerJsonData = json_decode(file_get_contents('composer.json'), true);
$excludedPackages = ['magento/product-community-edition', 'composer/composer'];
foreach ($composerJsonData['require'] as $package => $version) {
foreach ($composerLockData['packages'] as $lockedPackage) {
if ($lockedPackage['name'] === $package) {
if (
version_compare( $lockedPackage['version'], '0.0.1', '>=' )
&&
!in_array($package, $excludedPackages)
) {
$composerJsonData['require'][$package] = '~' . $lockedPackage['version'];
echo 'Locking ' . $package . ' to patch releases only; ' . $composerJsonData['require'][$package] . PHP_EOL;
}
}
}
}
echo PHP_EOL . 'Double-check the changes with `git diff composer.json`' . PHP_EOL;
file_put_contents('composer.json', json_encode($composerJsonData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.