Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CheckPlatformReqs: basic json format support #10979

Merged
merged 4 commits into from Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/03-cli.md
Expand Up @@ -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`
Expand Down
52 changes: 39 additions & 13 deletions src/Composer/Command/CheckPlatformReqsCommand.php
Expand Up @@ -15,11 +15,12 @@
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;
use Composer\Repository\InstalledRepository;
use Composer\Json\JsonFile;

class CheckPlatformReqsCommand extends BaseCommand
{
Expand All @@ -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', ['json', 'text']),
))
->setHelp(
<<<EOT
Expand Down Expand Up @@ -127,7 +129,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$candidate->getName() === $require ? $candidate->getPrettyName() : $require,
$candidateConstraint->getPrettyString(),
$link,
'<error>failed</error>'.($candidate->getName() === $require ? '' : ' <comment>provided by '.$candidate->getPrettyName().'</comment>'),
'<error>failed</error>',
$candidate->getName() === $require ? '' : '<comment>provided by '.$candidate->getPrettyName().'</comment>',
);

// skip to next candidate
Expand All @@ -139,7 +142,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$candidate->getName() === $require ? $candidate->getPrettyName() : $require,
$candidateConstraint->getPrettyString(),
null,
'<info>success</info>'.($candidate->getName() === $require ? '' : ' <comment>provided by '.$candidate->getPrettyName().'</comment>'),
'<info>success</info>',
$candidate->getName() === $require ? '' : '<comment>provided by '.$candidate->getPrettyName().'</comment>',
);

// candidate matched, skip to next requirement
Expand All @@ -158,13 +162,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'n/a',
$links[0],
'<error>missing</error>',
'',
);

$exitCode = max($exitCode, 2);
}
}

$this->printTable($output, $results);
$this->printTable($output, $results, $input->getOption('format'));

return $exitCode;
}
Expand All @@ -174,22 +179,43 @@ 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) {
/**
* @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,
);
list($platformPackage, $version, $link, $status, $provider) = $result;

if ('json' === $format) {
$rows[] = array(
"name" => $platformPackage,
"version" => $version,
"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,
$link ? sprintf('%s %s %s (%s)', $link->getSource(), $link->getDescription(), $link->getTarget(), $link->getPrettyConstraint()) : '',
rtrim($status.' '.$provider),
);
}
}

$this->renderTable($rows, $output);
if ('json' === $format) {
$this->getIO()->write(JsonFile::encode($rows));
} else {
$this->renderTable($rows, $output);
}
}
}