Skip to content

Instantly share code, notes, and snippets.

@polishdeveloper
Created August 28, 2014 13:59
Show Gist options
  • Save polishdeveloper/4bb6e3c4747e1aedfedb to your computer and use it in GitHub Desktop.
Save polishdeveloper/4bb6e3c4747e1aedfedb to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
class ComposerLockUpdater {
private $vendor;
private $lockData;
private $packageIdx = false;
private $composerLockPath;
public function __construct($vendorComposerPath, $composerLockPath)
{
$this->vendor = json_decode(file_get_contents($vendorComposerPath), true);
$this->lockData = json_decode(file_get_contents($composerLockPath), true);
$this->composerLockPath = $composerLockPath;
}
public function update()
{
$this->output("Update'ing package {$this->vendor['name']} ...");
$this->findPackageIdx();
$remoteRef = $this->getRemoteReference();
$localRef = $this->lockData['packages'][$this->packageIdx]['source']['reference'];
$this->output("Lock reference $localRef, Remote reference $remoteRef");
if ($remoteRef == $localRef) {
$this->output('Refenreces are the same, skipping');
} else {
$this->output("Updating lock file");
$this->lockData['packages'][$this->packageIdx]['source']['reference'] = $remoteRef;
file_put_contents($this->composerLockPath, json_encode($this->lockData));
}
$this->output('Done. Have a nice day');
}
private function output($message)
{
echo $message . "\n";
}
private function findPackageIdx()
{
foreach($this->lockData['packages'] as $idx => $package) {
if ($this->vendor['name'] == $package['name']) {
$this->packageIdx = $idx;
return;
}
}
$this->output("Package {$this->vendor['name']} not found in composer.lock file");
die;
}
private function getRemoteReference()
{
$remoteBranch = trim(shell_exec("git status | head -n 2 | tail -n 1 | awk '{print $6}' | tr -d \"'\" | tr -d '.'"));
return trim(shell_exec("git log $remoteBranch | head -1 | awk '{print $2}'"));
}
}
$updater = new ComposerLockUpdater('composer.json', '../../../../../composer.lock');
$updater->update();
@michaldudek
Copy link

Tylko w php 5.4 co prawda.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment