Add version arg, docs for --rollback and reorganize the code, refs #2522
parent
47a542ea89
commit
6ead35f189
|
@ -269,11 +269,20 @@ command. It will replace your `composer.phar` with the latest version.
|
||||||
|
|
||||||
$ php composer.phar self-update
|
$ php composer.phar self-update
|
||||||
|
|
||||||
|
If you would like to instead update to a specific release simply specify it:
|
||||||
|
|
||||||
|
$ composer self-update 1.0.0-alpha7
|
||||||
|
|
||||||
If you have installed composer for your entire system (see [global installation](00-intro.md#globally)),
|
If you have installed composer for your entire system (see [global installation](00-intro.md#globally)),
|
||||||
you have to run the command with `root` privileges
|
you may have to run the command with `root` privileges
|
||||||
|
|
||||||
$ sudo composer self-update
|
$ sudo composer self-update
|
||||||
|
|
||||||
|
### Options
|
||||||
|
|
||||||
|
* **--rollback (-r):** Rollback to the last version you had installed.
|
||||||
|
* **--clean-backups:** Delete old backups during an update. This makes the current version of composer the only backup available after the update.
|
||||||
|
|
||||||
## config
|
## config
|
||||||
|
|
||||||
The `config` command allows you to edit some basic composer settings in either
|
The `config` command allows you to edit some basic composer settings in either
|
||||||
|
|
|
@ -19,32 +19,19 @@ use Composer\Util\RemoteFilesystem;
|
||||||
use Composer\Downloader\FilesystemException;
|
use Composer\Downloader\FilesystemException;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Input\InputOption;
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Igor Wiedler <igor@wiedler.ch>
|
* @author Igor Wiedler <igor@wiedler.ch>
|
||||||
|
* @author Kevin Ran <kran@adobe.com>
|
||||||
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||||
*/
|
*/
|
||||||
class SelfUpdateCommand extends Command
|
class SelfUpdateCommand extends Command
|
||||||
{
|
{
|
||||||
const ROLLBACK = 'rollback';
|
|
||||||
const CLEAN_ROLLBACKS = 'clean-rollbacks';
|
|
||||||
const HOMEPAGE = 'getcomposer.org';
|
const HOMEPAGE = 'getcomposer.org';
|
||||||
const OLD_INSTALL_EXT = '-old.phar';
|
const OLD_INSTALL_EXT = '-old.phar';
|
||||||
|
|
||||||
protected $remoteFS;
|
|
||||||
protected $latestVersion;
|
|
||||||
protected $homepageURL;
|
|
||||||
protected $localFilename;
|
|
||||||
|
|
||||||
public function __construct($name = null)
|
|
||||||
{
|
|
||||||
parent::__construct($name);
|
|
||||||
$protocol = (extension_loaded('openssl') ? 'https' : 'http') . '://';
|
|
||||||
$this->homepageURL = $protocol . self::HOMEPAGE;
|
|
||||||
$this->remoteFS = new RemoteFilesystem($this->getIO());
|
|
||||||
$this->localFilename = realpath($_SERVER['argv'][0]) ?: $_SERVER['argv'][0];
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function configure()
|
protected function configure()
|
||||||
{
|
{
|
||||||
$this
|
$this
|
||||||
|
@ -52,8 +39,9 @@ class SelfUpdateCommand extends Command
|
||||||
->setAliases(array('selfupdate'))
|
->setAliases(array('selfupdate'))
|
||||||
->setDescription('Updates composer.phar to the latest version.')
|
->setDescription('Updates composer.phar to the latest version.')
|
||||||
->setDefinition(array(
|
->setDefinition(array(
|
||||||
new InputOption(self::ROLLBACK, 'r', InputOption::VALUE_NONE, 'Revert to an older installation of composer'),
|
new InputOption('rollback', 'r', InputOption::VALUE_NONE, 'Revert to an older installation of composer'),
|
||||||
new InputOption(self::CLEAN_ROLLBACKS, null, InputOption::VALUE_NONE, 'Delete old snapshots during an update. This makes the current version of composer the only rollback snapshot after the update')
|
new InputOption('clean-backups', null, InputOption::VALUE_NONE, 'Delete old backups during an update. This makes the current version of composer the only backup available after the update'),
|
||||||
|
new InputArgument('version', InputArgument::OPTIONAL, 'The version to update to'),
|
||||||
))
|
))
|
||||||
->setHelp(<<<EOT
|
->setHelp(<<<EOT
|
||||||
The <info>self-update</info> command checks getcomposer.org for newer
|
The <info>self-update</info> command checks getcomposer.org for newer
|
||||||
|
@ -68,34 +56,96 @@ EOT
|
||||||
|
|
||||||
protected function execute(InputInterface $input, OutputInterface $output)
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
{
|
{
|
||||||
|
$baseUrl = (extension_loaded('openssl') ? 'https' : 'http') . '://' . self::HOMEPAGE;
|
||||||
|
$remoteFilesystem = new RemoteFilesystem($this->getIO());
|
||||||
$config = Factory::createConfig();
|
$config = Factory::createConfig();
|
||||||
$cacheDir = rtrim($config->get('cache-dir'), '/');
|
$cacheDir = $config->get('cache-dir');
|
||||||
|
$rollbackDir = $config->get('home');
|
||||||
|
$localFilename = realpath($_SERVER['argv'][0]) ?: $_SERVER['argv'][0];
|
||||||
|
|
||||||
// Check if current dir is writable and if not try the cache dir from settings
|
// check if current dir is writable and if not try the cache dir from settings
|
||||||
$tmpDir = is_writable(dirname($this->localFilename))? dirname($this->localFilename) : $cacheDir;
|
$tmpDir = is_writable(dirname($localFilename)) ? dirname($localFilename) : $cacheDir;
|
||||||
|
|
||||||
// check for permissions in local filesystem before start connection process
|
// check for permissions in local filesystem before start connection process
|
||||||
if (!is_writable($tmpDir)) {
|
if (!is_writable($tmpDir)) {
|
||||||
throw new FilesystemException('Composer update failed: the "'.$tmpDir.'" directory used to download the temp file could not be written');
|
throw new FilesystemException('Composer update failed: the "'.$tmpDir.'" directory used to download the temp file could not be written');
|
||||||
}
|
}
|
||||||
|
if (!is_writable($localFilename)) {
|
||||||
if (!is_writable($this->localFilename)) {
|
throw new FilesystemException('Composer update failed: the "'.$localFilename.'" file could not be written');
|
||||||
throw new FilesystemException('Composer update failed: the "'.$this->localFilename.'" file could not be written');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$rollbackVersion = false;
|
if ($input->getOption('rollback')) {
|
||||||
$rollbackDir = rtrim($config->get('home'), '/');
|
return $this->rollback($output, $rollbackDir, $localFilename);
|
||||||
|
}
|
||||||
|
|
||||||
// rollback specified, get last phar
|
$latestVersion = trim($remoteFilesystem->getContents(self::HOMEPAGE, $baseUrl. '/version', false));
|
||||||
if ($input->getOption(self::ROLLBACK)) {
|
$updateVersion = $input->getArgument('version') ?: $latestVersion;
|
||||||
$rollbackVersion = $this->getLastVersion($rollbackDir);
|
|
||||||
|
if (preg_match('{^[0-9a-f]{40}$}', $updateVersion) && $updateVersion !== $latestVersion) {
|
||||||
|
$output->writeln('<error>You can not update to a specific SHA-1 as those phars are not available for download</error>');
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Composer::VERSION === $updateVersion) {
|
||||||
|
$output->writeln('<info>You are already using composer version '.$updateVersion.'.</info>');
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
$tempFilename = $tmpDir . '/' . basename($localFilename, '.phar').'-temp.phar';
|
||||||
|
$backupFile = sprintf(
|
||||||
|
'%s/%s-%s%s',
|
||||||
|
$rollbackDir,
|
||||||
|
strtr(Composer::RELEASE_DATE, ' :', '_-'),
|
||||||
|
preg_replace('{^([0-9a-f]{7})[0-9a-f]{33}$}', '$1', Composer::VERSION),
|
||||||
|
self::OLD_INSTALL_EXT
|
||||||
|
);
|
||||||
|
|
||||||
|
$output->writeln(sprintf("Updating to version <info>%s</info>.", $updateVersion));
|
||||||
|
$remoteFilename = $baseUrl . (preg_match('{^[0-9a-f]{40}$}', $updateVersion) ? '/composer.phar' : "/download/{$updateVersion}/composer.phar");
|
||||||
|
$remoteFilesystem->copy(self::HOMEPAGE, $remoteFilename, $tempFilename);
|
||||||
|
if (!file_exists($tempFilename)) {
|
||||||
|
$output->writeln('<error>The download of the new composer version failed for an unexpected reason');
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove saved installations of composer
|
||||||
|
if ($input->getOption('clean-backups')) {
|
||||||
|
$files = $this->getOldInstallationFiles($rollbackDir);
|
||||||
|
|
||||||
|
if (!empty($files)) {
|
||||||
|
$fs = new Filesystem;
|
||||||
|
|
||||||
|
foreach ($files as $file) {
|
||||||
|
$output->writeln('<info>Removing: '.$file);
|
||||||
|
$fs->remove($file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($err = $this->setLocalPhar($localFilename, $tempFilename, $backupFile)) {
|
||||||
|
$output->writeln('<error>The file is corrupted ('.$err->getMessage().').</error>');
|
||||||
|
$output->writeln('<error>Please re-run the self-update command to try again.</error>');
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file_exists($backupFile)) {
|
||||||
|
$output->writeln('Use <info>composer self-update --rollback</info> to return to version '.Composer::VERSION);
|
||||||
|
} else {
|
||||||
|
$output->writeln('<warning>A backup of the current version could not be written to '.$backupFile.', no rollback possible</warning>');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function rollback(OutputInterface $output, $rollbackDir, $localFilename)
|
||||||
|
{
|
||||||
|
$rollbackVersion = $this->getLastBackupVersion($rollbackDir);
|
||||||
if (!$rollbackVersion) {
|
if (!$rollbackVersion) {
|
||||||
throw new FilesystemException('Composer rollback failed: no installation to roll back to in "'.$rollbackDir.'"');
|
throw new \UnexpectedValueException('Composer rollback failed: no installation to roll back to in "'.$rollbackDir.'"');
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if a rollback version is specified, check for permissions and rollback installation
|
|
||||||
if ($rollbackVersion) {
|
|
||||||
if (!is_writable($rollbackDir)) {
|
if (!is_writable($rollbackDir)) {
|
||||||
throw new FilesystemException('Composer rollback failed: the "'.$rollbackDir.'" dir could not be written to');
|
throw new FilesystemException('Composer rollback failed: the "'.$rollbackDir.'" dir could not be written to');
|
||||||
}
|
}
|
||||||
|
@ -108,82 +158,38 @@ EOT
|
||||||
if (!is_readable($old)) {
|
if (!is_readable($old)) {
|
||||||
throw new FilesystemException('Composer rollback failed: "'.$old.'" could not be read');
|
throw new FilesystemException('Composer rollback failed: "'.$old.'" could not be read');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$oldFile = $rollbackDir . "/{$rollbackVersion}" . self::OLD_INSTALL_EXT;
|
||||||
|
$output->writeln(sprintf("Rolling back to version <info>%s</info>.", $rollbackVersion));
|
||||||
|
if ($err = $this->setLocalPhar($localFilename, $oldFile)) {
|
||||||
|
$output->writeln('<error>The backup file was corrupted ('.$err->getMessage().') and has been removed.</error>');
|
||||||
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
$updateVersion = ($rollbackVersion)? $rollbackVersion : $this->getLatestVersion();
|
|
||||||
|
|
||||||
if (Composer::VERSION === $updateVersion) {
|
|
||||||
$output->writeln('<info>You are already using composer version '.$updateVersion.'.</info>');
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
$tempFilename = $tmpDir . '/' . basename($this->localFilename, '.phar').'-temp.phar';
|
protected function setLocalPhar($localFilename, $newFilename, $backupTarget = null)
|
||||||
$backupFile = ($rollbackVersion)? false : $rollbackDir . '/' . Composer::VERSION . self::OLD_INSTALL_EXT;
|
|
||||||
|
|
||||||
if ($rollbackVersion) {
|
|
||||||
rename($rollbackDir . "/{$rollbackVersion}" . self::OLD_INSTALL_EXT, $tempFilename);
|
|
||||||
$output->writeln(sprintf("Rolling back to cached version <info>%s</info>.", $rollbackVersion));
|
|
||||||
} else {
|
|
||||||
$endpoint = ($updateVersion === $this->getLatestVersion()) ? '/composer.phar' : "/download/{$updateVersion}/composer.phar";
|
|
||||||
$remoteFilename = $this->homepageURL . $endpoint;
|
|
||||||
|
|
||||||
$output->writeln(sprintf("Updating to version <info>%s</info>.", $updateVersion));
|
|
||||||
|
|
||||||
$this->remoteFS->copy(self::HOMEPAGE, $remoteFilename, $tempFilename);
|
|
||||||
|
|
||||||
// @todo: handle snapshot versions not being found!
|
|
||||||
if (!file_exists($tempFilename)) {
|
|
||||||
$output->writeln('<error>The download of the new composer version failed for an unexpected reason');
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// remove saved installations of composer
|
|
||||||
if ($input->getOption(self::CLEAN_ROLLBACKS)) {
|
|
||||||
$files = $this->getOldInstallationFiles($rollbackDir);
|
|
||||||
|
|
||||||
if (!empty($files)) {
|
|
||||||
$fs = new Filesystem;
|
|
||||||
|
|
||||||
foreach ($files as $file) {
|
|
||||||
$output->writeln('<info>Removing: '.$file);
|
|
||||||
$fs->remove($file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($err = $this->setLocalPhar($tempFilename, $backupFile)) {
|
|
||||||
$output->writeln('<error>The file is corrupted ('.$err->getMessage().').</error>');
|
|
||||||
$output->writeln('<error>Please re-run the self-update command to try again.</error>');
|
|
||||||
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($backupFile) {
|
|
||||||
$output->writeln('<info>Saved rollback snapshot '.$backupFile);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function setLocalPhar($filename, $backupFile)
|
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
@chmod($filename, 0777 & ~umask());
|
@chmod($newFilename, 0777 & ~umask());
|
||||||
// test the phar validity
|
// test the phar validity
|
||||||
$phar = new \Phar($filename);
|
$phar = new \Phar($newFilename);
|
||||||
// free the variable to unlock the file
|
// free the variable to unlock the file
|
||||||
unset($phar);
|
unset($phar);
|
||||||
|
|
||||||
// copy current file into installations dir
|
// copy current file into installations dir
|
||||||
if ($backupFile) {
|
if ($backupTarget && file_exists($localFilename)) {
|
||||||
copy($this->localFilename, $backupFile);
|
@copy($localFilename, $backupTarget);
|
||||||
}
|
}
|
||||||
|
|
||||||
unset($phar);
|
unset($phar);
|
||||||
rename($filename, $this->localFilename);
|
rename($newFilename, $localFilename);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
@unlink($filename);
|
if ($backupTarget) {
|
||||||
|
@unlink($newFilename);
|
||||||
|
}
|
||||||
if (!$e instanceof \UnexpectedValueException && !$e instanceof \PharException) {
|
if (!$e instanceof \UnexpectedValueException && !$e instanceof \PharException) {
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
|
@ -192,31 +198,20 @@ EOT
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getLastVersion($rollbackDir)
|
protected function getLastBackupVersion($rollbackDir)
|
||||||
{
|
{
|
||||||
$files = $this->getOldInstallationFiles($rollbackDir);
|
$files = $this->getOldInstallationFiles($rollbackDir);
|
||||||
|
|
||||||
if (empty($files)) {
|
if (empty($files)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$fileTimes = array_map('filemtime', $files);
|
sort($files);
|
||||||
$map = array_combine($fileTimes, $files);
|
|
||||||
$latest = max($fileTimes);
|
return basename(end($files), self::OLD_INSTALL_EXT);
|
||||||
return basename($map[$latest], self::OLD_INSTALL_EXT);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getOldInstallationFiles($rollbackDir)
|
protected function getOldInstallationFiles($rollbackDir)
|
||||||
{
|
{
|
||||||
return glob($rollbackDir . '/*' . self::OLD_INSTALL_EXT);
|
return glob($rollbackDir . '/*' . self::OLD_INSTALL_EXT) ?: array();
|
||||||
}
|
|
||||||
|
|
||||||
protected function getLatestVersion()
|
|
||||||
{
|
|
||||||
if (!$this->latestVersion) {
|
|
||||||
$this->latestVersion = trim($this->remoteFS->getContents(self::HOMEPAGE, $this->homepageURL. '/version', false));
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->latestVersion;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue