From d6d56b07709ac59424e37e2d382216f402289b19 Mon Sep 17 00:00:00 2001 From: Jellyfrog Date: Mon, 1 Aug 2022 17:26:13 +0200 Subject: [PATCH 1/4] CheckPlatformReqs: basic json format support --- .../Command/CheckPlatformReqsCommand.php | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/src/Composer/Command/CheckPlatformReqsCommand.php b/src/Composer/Command/CheckPlatformReqsCommand.php index 1053fc62c51a..07a97b9410c6 100644 --- a/src/Composer/Command/CheckPlatformReqsCommand.php +++ b/src/Composer/Command/CheckPlatformReqsCommand.php @@ -20,6 +20,7 @@ use Composer\Repository\PlatformRepository; use Composer\Repository\RootPackageRepository; use Composer\Repository\InstalledRepository; +use Composer\Json\JsonFile; class CheckPlatformReqsCommand extends BaseCommand { @@ -33,6 +34,7 @@ protected function configure(): void ->setDefinition(array( new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables checking of require-dev packages requirements.'), new InputOption('lock', null, InputOption::VALUE_NONE, 'Checks requirements only from the lock file, not from installed packages.'), + new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text or json', 'text'), )) ->setHelp( <<printTable($output, $results); + $this->printTable($output, $results, $input->getOption('format')); return $exitCode; } @@ -174,7 +176,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int * * @return void */ - protected function printTable(OutputInterface $output, array $results): void + protected function printTable(OutputInterface $output, array $results, string $format): void { $rows = array(); foreach ($results as $result) { @@ -182,14 +184,29 @@ protected function printTable(OutputInterface $output, array $results): void * @var Link|null $link */ list($platformPackage, $version, $link, $status) = $result; - $rows[] = array( - $platformPackage, - $version, - $link ? sprintf('%s %s %s (%s)', $link->getSource(), $link->getDescription(), $link->getTarget(), $link->getPrettyConstraint()) : '', - $status, - ); + $link = $link ? sprintf('%s %s %s (%s)', $link->getSource(), $link->getDescription(), $link->getTarget(), $link->getPrettyConstraint()) : ''; + + if ('json' === $format) { + $rows[] = array( + "name" => $platformPackage, + "version" => $version, + "link" => $link, + "status" => $status, + ); + } else { + $rows[] = array( + $platformPackage, + $version, + $link, + $status, + ); + } } - $this->renderTable($rows, $output); + if ('json' === $format) { + $this->getIO()->write(JsonFile::encode($rows)); + } else { + $this->renderTable($rows, $output); + } } } From 3ee0ced21e7abd895918ad1d9b4fb2ff9e053aaa Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Tue, 16 Aug 2022 13:46:21 +0200 Subject: [PATCH 2/4] Add suggestion for format --- src/Composer/Command/CheckPlatformReqsCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Composer/Command/CheckPlatformReqsCommand.php b/src/Composer/Command/CheckPlatformReqsCommand.php index 07a97b9410c6..e91650056fdf 100644 --- a/src/Composer/Command/CheckPlatformReqsCommand.php +++ b/src/Composer/Command/CheckPlatformReqsCommand.php @@ -15,7 +15,7 @@ use Composer\Package\Link; use Composer\Semver\Constraint\Constraint; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; +use Composer\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Composer\Repository\PlatformRepository; use Composer\Repository\RootPackageRepository; @@ -34,7 +34,7 @@ protected function configure(): void ->setDefinition(array( new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables checking of require-dev packages requirements.'), new InputOption('lock', null, InputOption::VALUE_NONE, 'Checks requirements only from the lock file, not from installed packages.'), - new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text or json', 'text'), + new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text or json', 'text', ['json', 'text']), )) ->setHelp( << Date: Tue, 16 Aug 2022 13:48:34 +0200 Subject: [PATCH 3/4] Add options to docs for check-platform-reqs --- doc/03-cli.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/03-cli.md b/doc/03-cli.md index 84ce09873820..e0bff3efc995 100644 --- a/doc/03-cli.md +++ b/doc/03-cli.md @@ -413,6 +413,12 @@ Unlike update/install, this command will ignore config.platform settings and check the real platform packages so you can be certain you have the required platform dependencies. +### Options + +* **--lock:** Checks requirements only from the lock file, not from installed packages. +* **--no-dev:** Disables checking of require-dev packages requirements. +* **--format (-f):** Format of the output: text (default) or json + ## global The global command allows you to run other commands like `install`, `remove`, `require` From db75afff57d441729100c24beba84b8ed72a92bb Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Tue, 16 Aug 2022 14:14:49 +0200 Subject: [PATCH 4/4] Fix json output field names and remove formatting --- .../Command/CheckPlatformReqsCommand.php | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/Composer/Command/CheckPlatformReqsCommand.php b/src/Composer/Command/CheckPlatformReqsCommand.php index e91650056fdf..d3ca8b4c4c21 100644 --- a/src/Composer/Command/CheckPlatformReqsCommand.php +++ b/src/Composer/Command/CheckPlatformReqsCommand.php @@ -129,7 +129,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $candidate->getName() === $require ? $candidate->getPrettyName() : $require, $candidateConstraint->getPrettyString(), $link, - 'failed'.($candidate->getName() === $require ? '' : ' provided by '.$candidate->getPrettyName().''), + 'failed', + $candidate->getName() === $require ? '' : 'provided by '.$candidate->getPrettyName().'', ); // skip to next candidate @@ -141,7 +142,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $candidate->getName() === $require ? $candidate->getPrettyName() : $require, $candidateConstraint->getPrettyString(), null, - 'success'.($candidate->getName() === $require ? '' : ' provided by '.$candidate->getPrettyName().''), + 'success', + $candidate->getName() === $require ? '' : 'provided by '.$candidate->getPrettyName().'', ); // candidate matched, skip to next requirement @@ -160,6 +162,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int 'n/a', $links[0], 'missing', + '', ); $exitCode = max($exitCode, 2); @@ -183,22 +186,28 @@ protected function printTable(OutputInterface $output, array $results, string $f /** * @var Link|null $link */ - list($platformPackage, $version, $link, $status) = $result; - $link = $link ? sprintf('%s %s %s (%s)', $link->getSource(), $link->getDescription(), $link->getTarget(), $link->getPrettyConstraint()) : ''; + list($platformPackage, $version, $link, $status, $provider) = $result; if ('json' === $format) { $rows[] = array( "name" => $platformPackage, "version" => $version, - "link" => $link, - "status" => $status, + "status" => strip_tags($status), + "failed_requirement" => $link instanceof Link ? [ + 'source' => $link->getSource(), + 'type' => $link->getDescription(), + 'target' => $link->getTarget(), + 'constraint' => $link->getPrettyConstraint(), + ] : null, + "provider" => $provider === '' ? null : strip_tags($provider), ); } else { $rows[] = array( $platformPackage, $version, $link, - $status, + $link ? sprintf('%s %s %s (%s)', $link->getSource(), $link->getDescription(), $link->getTarget(), $link->getPrettyConstraint()) : '', + rtrim($status.' '.$provider), ); } }