1
0
Fork 0

SelfUpdateCommand: do not delete old snapshots, allow user to clean them

pull/2522/head
chr0n1x 2013-12-22 00:36:24 -05:00
parent bc5ce1ce04
commit 0c76bba8bb
1 changed files with 25 additions and 20 deletions

View File

@ -27,6 +27,7 @@ use Symfony\Component\Console\Output\OutputInterface;
class SelfUpdateCommand extends Command class SelfUpdateCommand extends Command
{ {
const ROLLBACK = 'rollback'; 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';
@ -51,7 +52,8 @@ 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(self::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')
)) ))
->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
@ -82,26 +84,25 @@ EOT
} }
$rollbackVersion = false; $rollbackVersion = false;
$rollbackDir = rtrim($config->get('home'), '/');
// rollback specified, get last phar // rollback specified, get last phar
if ($input->getOption(self::ROLLBACK)) { if ($input->getOption(self::ROLLBACK)) {
$rollbackVersion = $this->getLastVersion($saveDir); $rollbackVersion = $this->getLastVersion($rollbackDir);
if (!$rollbackVersion) { if (!$rollbackVersion) {
throw new FilesystemException('Composer rollback failed: no installation to roll back to in "'.$saveDir.'"'); throw new FilesystemException('Composer rollback failed: no installation to roll back to in "'.$rollbackDir.'"');
return 1; return 1;
} }
} }
$saveDir = rtrim($config->get('home'), '/');
// if a rollback version is specified, check for permissions and rollback installation // if a rollback version is specified, check for permissions and rollback installation
if ($rollbackVersion) { if ($rollbackVersion) {
if (!is_writable($saveDir)) { if (!is_writable($rollbackDir)) {
throw new FilesystemException('Composer rollback failed: the "'.$saveDir.'" dir could not be written to'); throw new FilesystemException('Composer rollback failed: the "'.$rollbackDir.'" dir could not be written to');
} }
$old = $saveDir . '/' . $rollbackVersion . self::OLD_INSTALL_EXT; $old = $rollbackDir . '/' . $rollbackVersion . self::OLD_INSTALL_EXT;
if (!is_file($old)) { if (!is_file($old)) {
throw new FilesystemException('Composer rollback failed: "'.$old.'" could not be found'); throw new FilesystemException('Composer rollback failed: "'.$old.'" could not be found');
@ -120,10 +121,10 @@ EOT
} }
$tempFilename = $tmpDir . '/' . basename($this->localFilename, '.phar').'-temp.phar'; $tempFilename = $tmpDir . '/' . basename($this->localFilename, '.phar').'-temp.phar';
$backupFile = ($rollbackVersion)? false : $saveDir . '/' . Composer::VERSION . self::OLD_INSTALL_EXT; $backupFile = ($rollbackVersion)? false : $rollbackDir . '/' . Composer::VERSION . self::OLD_INSTALL_EXT;
if ($rollbackVersion) { if ($rollbackVersion) {
rename($saveDir . "/{$rollbackVersion}" . self::OLD_INSTALL_EXT, $tempFilename); rename($rollbackDir . "/{$rollbackVersion}" . self::OLD_INSTALL_EXT, $tempFilename);
$output->writeln(sprintf("Rolling back to cached version <info>%s</info>.", $rollbackVersion)); $output->writeln(sprintf("Rolling back to cached version <info>%s</info>.", $rollbackVersion));
} else { } else {
$endpoint = ($updateVersion === $this->getLatestVersion()) ? '/composer.phar' : "/download/{$updateVersion}/composer.phar"; $endpoint = ($updateVersion === $this->getLatestVersion()) ? '/composer.phar' : "/download/{$updateVersion}/composer.phar";
@ -141,15 +142,19 @@ EOT
} }
// remove saved installations of composer // remove saved installations of composer
$files = $this->getOldInstallationFiles($saveDir); if ($input->getOption(self::CLEAN_ROLLBACKS)) {
$files = $this->getOldInstallationFiles($rollbackDir);
if (!empty($files)) { if (!empty($files)) {
$fs = new Filesystem; $fs = new Filesystem;
foreach ($files as $file) { foreach ($files as $file) {
$output->writeln('<info>Removing: '.$file); $output->writeln('<info>Removing: '.$file);
$fs->remove($file); $fs->remove($file);
} }
} }
} }
}
if ($err = $this->setLocalPhar($tempFilename, $backupFile)) { if ($err = $this->setLocalPhar($tempFilename, $backupFile)) {
$output->writeln('<error>The file is corrupted ('.$err->getMessage().').</error>'); $output->writeln('<error>The file is corrupted ('.$err->getMessage().').</error>');
@ -189,9 +194,9 @@ EOT
} }
} }
protected function getLastVersion($saveDir) protected function getLastVersion($rollbackDir)
{ {
$files = $this->getOldInstallationFiles($saveDir); $files = $this->getOldInstallationFiles($rollbackDir);
if (empty($files)) { if (empty($files)) {
return false; return false;
@ -203,9 +208,9 @@ EOT
return basename($map[$latest], self::OLD_INSTALL_EXT); return basename($map[$latest], self::OLD_INSTALL_EXT);
} }
protected function getOldInstallationFiles($saveDir) protected function getOldInstallationFiles($rollbackDir)
{ {
return glob($saveDir . '/*' . self::OLD_INSTALL_EXT); return glob($rollbackDir . '/*' . self::OLD_INSTALL_EXT);
} }
protected function getLatestVersion() protected function getLatestVersion()