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()); } }