1
0
Fork 0

implement a self-update command (closes #83)

pull/107/head
Igor Wiedler 2011-11-12 19:44:24 +01:00
parent e060ead6c2
commit b15ec17a06
5 changed files with 70 additions and 2 deletions

View File

@ -20,7 +20,8 @@
"require": { "require": {
"php": ">=5.3.0", "php": ">=5.3.0",
"symfony/console": "2.0.5", "symfony/console": "2.0.5",
"symfony/finder": "2.0.5" "symfony/finder": "2.0.5",
"symfony/process": "2.0.5"
}, },
"recommends": { "recommends": {
"ext-zip": "*" "ext-zip": "*"

View File

@ -0,0 +1,57 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* 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 <igor@wiedler.ch>
*/
class SelfUpdateCommand extends Command
{
protected function configure()
{
$this
->setName('self-update')
->setDescription('Updates composer.phar to the latest version.')
->setHelp(<<<EOT
The <info>self-update</info> command checks getcomposer.org for newer
versions of composer and if found, installs the latest.
<info>php composer.phar self-update</info>
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.");
}
}
}

View File

@ -13,6 +13,7 @@
namespace Composer; namespace Composer;
use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Process;
/** /**
* The Compiler class compiles composer into a phar * The Compiler class compiles composer into a phar
@ -34,6 +35,12 @@ class Compiler
unlink($pharFile); 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 = new \Phar($pharFile, 0, 'composer.phar');
$phar->setSignatureAlgorithm(\Phar::SHA1); $phar->setSignatureAlgorithm(\Phar::SHA1);
@ -88,6 +95,8 @@ class Compiler
$content = "\n".file_get_contents($file)."\n"; $content = "\n".file_get_contents($file)."\n";
} }
$content = str_replace('@package_version@', $this->version, $content);
$phar->addFromString($path, $content); $phar->addFromString($path, $content);
} }

View File

@ -24,7 +24,7 @@ use Composer\Downloader\DownloadManager;
*/ */
class Composer class Composer
{ {
const VERSION = '1.0.0-DEV'; const VERSION = '@package_version@';
private $package; private $package;
private $locker; private $locker;

View File

@ -66,6 +66,7 @@ class Application extends BaseApplication
$this->add(new Command\AboutCommand()); $this->add(new Command\AboutCommand());
$this->add(new Command\InstallCommand()); $this->add(new Command\InstallCommand());
$this->add(new Command\UpdateCommand()); $this->add(new Command\UpdateCommand());
$this->add(new Command\SelfUpdateCommand());
$this->add(new Command\DebugPackagesCommand()); $this->add(new Command\DebugPackagesCommand());
} }
} }