1
0
Fork 0

Allow forcing self-update to stick to 1.x or 2.x using --1 and --2 flags, fixes #8753

pull/8760/head
Jordi Boggiano 2020-04-09 15:17:30 +02:00
parent 067101dbf5
commit ccc8829ed9
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
2 changed files with 16 additions and 6 deletions

View File

@ -51,6 +51,8 @@ class SelfUpdateCommand extends BaseCommand
new InputOption('stable', null, InputOption::VALUE_NONE, 'Force an update to the stable channel'),
new InputOption('preview', null, InputOption::VALUE_NONE, 'Force an update to the preview channel'),
new InputOption('snapshot', null, InputOption::VALUE_NONE, 'Force an update to the snapshot channel'),
new InputOption('1', null, InputOption::VALUE_NONE, 'Force an update to the stable channel, but only use 1.x versions'),
new InputOption('2', null, InputOption::VALUE_NONE, 'Force an update to the stable channel, but only use 2.x versions'),
new InputOption('set-channel-only', null, InputOption::VALUE_NONE, 'Only store the channel as the default one and then exit'),
))
->setHelp(
@ -82,9 +84,10 @@ EOT
$versionsUtil = new Versions($config, $remoteFilesystem);
// switch channel if requested
foreach (array('stable', 'preview', 'snapshot') as $channel) {
foreach (Versions::CHANNELS as $channel) {
if ($input->getOption($channel)) {
$versionsUtil->setChannel($channel);
break;
}
}
@ -123,9 +126,14 @@ EOT
}
$latest = $versionsUtil->getLatest();
$latestStable = $versionsUtil->getLatest('stable');
$latestVersion = $latest['version'];
$updateVersion = $input->getArgument('version') ?: $latestVersion;
if (is_numeric($channel) && substr($latestStable['version'], 0, 1) !== $channel) {
$io->writeError('<warning>Warning: You forced the install of '.$latestVersion.' via --'.$channel.', but '.$latestStable['version'].' is the latest stable version. Updating to it via composer self-update --stable is recommended.</warning>');
}
if (preg_match('{^[0-9a-f]{40}$}', $updateVersion) && $updateVersion !== $latestVersion) {
$io->writeError('<error>You can not update to a specific SHA-1 as those phars are not available for download</error>');

View File

@ -21,6 +21,8 @@ use Composer\Json\JsonFile;
*/
class Versions
{
const CHANNELS = array('stable', 'preview', 'snapshot', '1', '2');
private $rfs;
private $config;
private $channel;
@ -50,21 +52,21 @@ class Versions
public function setChannel($channel)
{
if (!in_array($channel, array('stable', 'preview', 'snapshot'), true)) {
throw new \InvalidArgumentException('Invalid channel '.$channel.', must be one of: stable, preview, snapshot');
if (!in_array($channel, self::CHANNELS, true)) {
throw new \InvalidArgumentException('Invalid channel '.$channel.', must be one of: ' . implode(', ', self::CHANNELS));
}
$channelFile = $this->config->get('home').'/update-channel';
$this->channel = $channel;
file_put_contents($channelFile, $channel.PHP_EOL);
file_put_contents($channelFile, (is_numeric($channel) ? 'stable' : $channel).PHP_EOL);
}
public function getLatest()
public function getLatest($channel = null)
{
$protocol = extension_loaded('openssl') ? 'https' : 'http';
$versions = JsonFile::parseJson($this->rfs->getContents('getcomposer.org', $protocol . '://getcomposer.org/versions', false));
foreach ($versions[$this->getChannel()] as $version) {
foreach ($versions[$channel ?: $this->getChannel()] as $version) {
if ($version['min-php'] <= PHP_VERSION_ID) {
return $version;
}