From 0fca3e55215a45e324953f8dc66b00479af098cb Mon Sep 17 00:00:00 2001 From: PrinsFrank <25006490+PrinsFrank@users.noreply.github.com> Date: Thu, 3 Nov 2022 13:11:34 +0100 Subject: [PATCH] Allow the 'composer remove --unused' command to run in non-interactive mode (#11166) --- src/Composer/Command/RemoveCommand.php | 32 ++++++++++++-------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/Composer/Command/RemoveCommand.php b/src/Composer/Command/RemoveCommand.php index 49b9168a4..f4717439e 100644 --- a/src/Composer/Command/RemoveCommand.php +++ b/src/Composer/Command/RemoveCommand.php @@ -20,6 +20,7 @@ use Composer\Plugin\CommandEvent; use Composer\Plugin\PluginEvents; use Composer\Json\JsonFile; use Composer\Factory; +use Symfony\Component\Console\Exception\InvalidArgumentException; use Symfony\Component\Console\Input\InputInterface; use Composer\Console\Input\InputOption; use Composer\Console\Input\InputArgument; @@ -44,7 +45,7 @@ class RemoveCommand extends BaseCommand ->setName('remove') ->setDescription('Removes a package from the require or require-dev') ->setDefinition([ - new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'Packages that should be removed.', null, $this->suggestRootRequirement()), + new InputArgument('packages', InputArgument::IS_ARRAY, 'Packages that should be removed.', null, $this->suggestRootRequirement()), new InputOption('dev', null, InputOption::VALUE_NONE, 'Removes a package from the require-dev section.'), new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Outputs the operations but will not execute anything (implicitly enables --verbose).'), new InputOption('no-progress', null, InputOption::VALUE_NONE, 'Do not output download progress.'), @@ -79,10 +80,17 @@ EOT } /** - * @return void + * @throws \Seld\JsonLint\ParsingException */ - protected function interact(InputInterface $input, OutputInterface $output) + protected function execute(InputInterface $input, OutputInterface $output) { + if ($input->getArgument('packages') === [] && !$input->getOption('unused')) { + throw new InvalidArgumentException('Not enough arguments (missing: "packages")'); + } + + $packages = $input->getArgument('packages'); + $packages = array_map('strtolower', $packages); + if ($input->getOption('unused')) { $composer = $this->requireComposer(); $locker = $composer->getLocker(); @@ -117,24 +125,14 @@ EOT foreach ($lockedPackages as $package) { $unused[] = $package->getName(); } - $input->setArgument('packages', array_merge($input->getArgument('packages'), $unused)); + $packages = array_merge($packages, $unused); - if (count($input->getArgument('packages')) === 0) { + if (count($packages) === 0) { $this->getIO()->writeError('No unused packages to remove'); - $this->setCode(static function (): int { - return 0; - }); + + return 0; } } - } - - /** - * @throws \Seld\JsonLint\ParsingException - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - $packages = $input->getArgument('packages'); - $packages = array_map('strtolower', $packages); $file = Factory::getComposerFile();