From dd2e2d7e26d2698883960ee7dccda705ae89182e Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 12 May 2022 16:13:24 +0200 Subject: [PATCH] Try and add completion for global subcommands --- src/Composer/Command/GlobalCommand.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Composer/Command/GlobalCommand.php b/src/Composer/Command/GlobalCommand.php index d74bf2b14279..a99898e2835a 100644 --- a/src/Composer/Command/GlobalCommand.php +++ b/src/Composer/Command/GlobalCommand.php @@ -37,6 +37,14 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti return $command->isHidden() ? null : $command->getName(); }, $application->all()))); } + + if ($application->has($commandName = $input->getArgument('command-name'))) { + $input = $this->prepareSubcommandInput($input, true); + $input = CompletionInput::fromString($input->__toString(), 3); + $command = $this->getApplication()->find($commandName); + $input->bind($command->getDefinition()); + $command->complete($input, $suggestions); + } } /** @@ -100,6 +108,13 @@ public function run(InputInterface $input, OutputInterface $output): int return parent::run($input, $output); } + $input = $this->prepareSubcommandInput($input); + + return $this->getApplication()->run($input, $output); + } + + private function prepareSubcommandInput(InputInterface $input, bool $quiet = false): StringInput + { // The COMPOSER env var should not apply to the global execution scope if (Platform::getEnv('COMPOSER')) { Platform::clearEnv('COMPOSER'); @@ -122,13 +137,15 @@ public function run(InputInterface $input, OutputInterface $output): int } catch (\Exception $e) { throw new \RuntimeException('Could not switch to home directory "'.$home.'"', 0, $e); } - $this->getIO()->writeError('Changed current directory to '.$home.''); + if (!$quiet) { + $this->getIO()->writeError('Changed current directory to '.$home.''); + } // create new input without "global" command prefix $input = new StringInput(Preg::replace('{\bg(?:l(?:o(?:b(?:a(?:l)?)?)?)?)?\b}', '', $input->__toString(), 1)); $this->getApplication()->resetComposer(); - return $this->getApplication()->run($input, $output); + return $input; } /**