From b15ec17a06485f096fd91a0ad48689a2beda8edf Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Sat, 12 Nov 2011 19:44:24 +0100 Subject: [PATCH] implement a self-update command (closes #83) --- composer.json | 3 +- src/Composer/Command/SelfUpdateCommand.php | 57 ++++++++++++++++++++++ src/Composer/Compiler.php | 9 ++++ src/Composer/Composer.php | 2 +- src/Composer/Console/Application.php | 1 + 5 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 src/Composer/Command/SelfUpdateCommand.php diff --git a/composer.json b/composer.json index 36da29dda..7c2825a68 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,8 @@ "require": { "php": ">=5.3.0", "symfony/console": "2.0.5", - "symfony/finder": "2.0.5" + "symfony/finder": "2.0.5", + "symfony/process": "2.0.5" }, "recommends": { "ext-zip": "*" diff --git a/src/Composer/Command/SelfUpdateCommand.php b/src/Composer/Command/SelfUpdateCommand.php new file mode 100644 index 000000000..df3ee4014 --- /dev/null +++ b/src/Composer/Command/SelfUpdateCommand.php @@ -0,0 +1,57 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Command; + +use Composer\Composer; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * @author Igor Wiedler + */ +class SelfUpdateCommand extends Command +{ + protected function configure() + { + $this + ->setName('self-update') + ->setDescription('Updates composer.phar to the latest version.') + ->setHelp(<<self-update command checks getcomposer.org for newer +versions of composer and if found, installs the latest. + +php composer.phar self-update + +EOT + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $composer = $this->getComposer(); + + $latest = file_get_contents('http://getcomposer.org/version'); + + if (Composer::VERSION !== $latest) { + $output->writeln(sprintf("Updating to version %s.", $latest)); + + $remoteFilename = 'http://getcomposer.org/composer.phar'; + $localFilename = getcwd().'/composer.phar'; + + file_put_contents($localFilename, file_get_contents($remoteFilename)); + } else { + $output->writeln("You are using the latest composer version."); + } + } +} diff --git a/src/Composer/Compiler.php b/src/Composer/Compiler.php index 1094baf02..7473b49b4 100644 --- a/src/Composer/Compiler.php +++ b/src/Composer/Compiler.php @@ -13,6 +13,7 @@ namespace Composer; use Symfony\Component\Finder\Finder; +use Symfony\Component\Process\Process; /** * The Compiler class compiles composer into a phar @@ -34,6 +35,12 @@ class Compiler unlink($pharFile); } + $process = new Process('git log --pretty="%h" -n1 HEAD'); + if ($process->run() > 0) { + throw new \RuntimeException('The git binary cannot be found.'); + } + $this->version = trim($process->getOutput()); + $phar = new \Phar($pharFile, 0, 'composer.phar'); $phar->setSignatureAlgorithm(\Phar::SHA1); @@ -88,6 +95,8 @@ class Compiler $content = "\n".file_get_contents($file)."\n"; } + $content = str_replace('@package_version@', $this->version, $content); + $phar->addFromString($path, $content); } diff --git a/src/Composer/Composer.php b/src/Composer/Composer.php index be34af653..54b2c496a 100644 --- a/src/Composer/Composer.php +++ b/src/Composer/Composer.php @@ -24,7 +24,7 @@ use Composer\Downloader\DownloadManager; */ class Composer { - const VERSION = '1.0.0-DEV'; + const VERSION = '@package_version@'; private $package; private $locker; diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index a5fbbd6ad..8bb590211 100644 --- a/src/Composer/Console/Application.php +++ b/src/Composer/Console/Application.php @@ -66,6 +66,7 @@ class Application extends BaseApplication $this->add(new Command\AboutCommand()); $this->add(new Command\InstallCommand()); $this->add(new Command\UpdateCommand()); + $this->add(new Command\SelfUpdateCommand()); $this->add(new Command\DebugPackagesCommand()); } }