Created
August 28, 2014 13:59
-
-
Save polishdeveloper/4bb6e3c4747e1aedfedb to your computer and use it in GitHub Desktop.
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 | |
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Zmien linie 30 na to:
To wtedy czytelnego JSON'a wypluje do composer.lock i bedzie dalej mozna recznie edytowac jbc :P