Skip to content

Commit

Permalink
Try and add completion for global subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed May 12, 2022
1 parent 6be071f commit dd2e2d7
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/Composer/Command/GlobalCommand.php
Expand Up @@ -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);
}
}

/**
Expand Down Expand Up @@ -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');
Expand All @@ -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('<info>Changed current directory to '.$home.'</info>');
if (!$quiet) {
$this->getIO()->writeError('<info>Changed current directory to '.$home.'</info>');
}

// 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;
}

/**
Expand Down

0 comments on commit dd2e2d7

Please sign in to comment.