Display the stored channel when choosing a specific channel in self-update, fixes #10719

main
Jordi Boggiano 2 years ago
parent b0b364af19
commit 20d11bfdfb
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC

@ -104,7 +104,7 @@ EOT
foreach (Versions::$channels as $channel) { foreach (Versions::$channels as $channel) {
if ($input->getOption($channel)) { if ($input->getOption($channel)) {
$requestedChannel = $channel; $requestedChannel = $channel;
$versionsUtil->setChannel($channel); $versionsUtil->setChannel($channel, $io);
break; break;
} }
} }

@ -12,6 +12,7 @@
namespace Composer\SelfUpdate; namespace Composer\SelfUpdate;
use Composer\IO\IOInterface;
use Composer\Pcre\Preg; use Composer\Pcre\Preg;
use Composer\Util\HttpDownloader; use Composer\Util\HttpDownloader;
use Composer\Config; use Composer\Config;
@ -64,7 +65,7 @@ class Versions
* *
* @return void * @return void
*/ */
public function setChannel($channel) public function setChannel($channel, IOInterface $io = null)
{ {
if (!in_array($channel, self::$channels, true)) { if (!in_array($channel, self::$channels, true)) {
throw new \InvalidArgumentException('Invalid channel '.$channel.', must be one of: ' . implode(', ', self::$channels)); throw new \InvalidArgumentException('Invalid channel '.$channel.', must be one of: ' . implode(', ', self::$channels));
@ -72,8 +73,15 @@ class Versions
$channelFile = $this->config->get('home').'/update-channel'; $channelFile = $this->config->get('home').'/update-channel';
$this->channel = $channel; $this->channel = $channel;
$storedChannel = Preg::isMatch('{^\d+$}D', $channel) ? 'stable' : $channel;
$previouslyStored = file_exists($channelFile) ? trim((string) file_get_contents($channelFile)) : null;
// rewrite '2' and '1' channels to stable for future self-updates, but LTS ones like '2.2' remain pinned // rewrite '2' and '1' channels to stable for future self-updates, but LTS ones like '2.2' remain pinned
file_put_contents($channelFile, (Preg::isMatch('{^\d+$}D', $channel) ? 'stable' : $channel).PHP_EOL); file_put_contents($channelFile, $storedChannel.PHP_EOL);
if ($io !== null && $previouslyStored !== $storedChannel) {
$io->writeError('Storing "<info>'.$storedChannel.'</info>" as default update channel for the next self-update run.');
}
} }
/** /**

Loading…
Cancel
Save