1
0
Fork 0

Add interactive prompt for which script/binary to run if run-script/exec is called without arg, fixes #11128 (#11157)

pull/11161/head
Jordi Boggiano 2022-10-28 14:25:18 +02:00 committed by GitHub
parent 6e55cb36d8
commit 6bf945017e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 8 deletions

View File

@ -51,6 +51,26 @@ EOT
; ;
} }
protected function interact(InputInterface $input, OutputInterface $output): void
{
$binaries = $this->getBinaries(false);
if (count($binaries) === 0) {
return;
}
$io = $this->getIO();
/** @var string $binary */
$binary = $io->select(
'Binary to run: ',
$binaries,
'',
1,
'Invalid binary name "%s"'
);
$input->setArgument('binary', $binaries[$binary]);
}
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$composer = $this->requireComposer(); $composer = $this->requireComposer();

View File

@ -52,7 +52,7 @@ class RunScriptCommand extends BaseCommand
->setDescription('Runs the scripts defined in composer.json') ->setDescription('Runs the scripts defined in composer.json')
->setDefinition([ ->setDefinition([
new InputArgument('script', InputArgument::OPTIONAL, 'Script name to run.', null, function () { new InputArgument('script', InputArgument::OPTIONAL, 'Script name to run.', null, function () {
return array_keys($this->requireComposer()->getPackage()->getScripts()); return array_map(static function ($script) { return $script['name']; }, $this->getScripts());
}), }),
new InputArgument('args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, ''), new InputArgument('args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, ''),
new InputOption('timeout', null, InputOption::VALUE_REQUIRED, 'Sets script timeout in seconds, or 0 for never.'), new InputOption('timeout', null, InputOption::VALUE_REQUIRED, 'Sets script timeout in seconds, or 0 for never.'),
@ -72,6 +72,29 @@ EOT
; ;
} }
protected function interact(InputInterface $input, OutputInterface $output): void
{
$scripts = $this->getScripts();
if (count($scripts) === 0) {
return;
}
$options = [];
foreach ($scripts as $script) {
$options[$script['name']] = $script['description'];
}
$io = $this->getIO();
$script = $io->select(
'Script to run: ',
$options,
'',
1,
'Invalid script name "%s"'
);
$input->setArgument('script', $script);
}
protected function execute(InputInterface $input, OutputInterface $output): int protected function execute(InputInterface $input, OutputInterface $output): int
{ {
if ($input->getOption('list')) { if ($input->getOption('list')) {
@ -114,15 +137,34 @@ EOT
protected function listScripts(OutputInterface $output): int protected function listScripts(OutputInterface $output): int
{ {
$scripts = $this->requireComposer()->getPackage()->getScripts(); $scripts = $this->getScripts();
if (count($scripts) === 0) {
if (!count($scripts)) {
return 0; return 0;
} }
$io = $this->getIO(); $io = $this->getIO();
$io->writeError('<info>scripts:</info>'); $io->writeError('<info>scripts:</info>');
$table = []; $table = [];
foreach ($scripts as $script) {
$table[] = [' '.$script['name'], $script['description']];
}
$this->renderTable($table, $output);
return 0;
}
/**
* @return list<array{name: string, description: string}>
*/
private function getScripts(): array
{
$scripts = $this->requireComposer()->getPackage()->getScripts();
if (count($scripts) === 0) {
return [];
}
$result = [];
foreach ($scripts as $name => $script) { foreach ($scripts as $name => $script) {
$description = ''; $description = '';
try { try {
@ -133,11 +175,9 @@ EOT
} catch (\Symfony\Component\Console\Exception\CommandNotFoundException $e) { } catch (\Symfony\Component\Console\Exception\CommandNotFoundException $e) {
// ignore scripts that have no command associated, like native Composer script listeners // ignore scripts that have no command associated, like native Composer script listeners
} }
$table[] = [' '.$name, $description]; $result[] = ['name' => $name, 'description' => $description];
} }
$this->renderTable($table, $output); return $result;
return 0;
} }
} }

View File

@ -310,6 +310,11 @@ class ConsoleIO extends BaseIO
$result = $helper->ask($this->input, $this->getErrorOutput(), $question); $result = $helper->ask($this->input, $this->getErrorOutput(), $question);
$isAssoc = (bool) \count(array_filter(array_keys($choices), 'is_string'));
if ($isAssoc) {
return $result;
}
if (!is_array($result)) { if (!is_array($result)) {
return (string) array_search($result, $choices, true); return (string) array_search($result, $choices, true);
} }