1
0
Fork 0

Allow the validate command to validate dependencies with -A/--with-dependencies, refs #3202

pull/4432/head
Jordi Boggiano 2015-09-20 19:28:36 +01:00
parent ed95448523
commit e61b3a6370
1 changed files with 41 additions and 13 deletions

View File

@ -40,11 +40,16 @@ class ValidateCommand extends Command
new InputOption('no-check-all', null, InputOption::VALUE_NONE, 'Do not make a complete validation'), new InputOption('no-check-all', null, InputOption::VALUE_NONE, 'Do not make a complete validation'),
new InputOption('no-check-lock', null, InputOption::VALUE_NONE, 'Do not check if lock file is up to date'), new InputOption('no-check-lock', null, InputOption::VALUE_NONE, 'Do not check if lock file is up to date'),
new InputOption('no-check-publish', null, InputOption::VALUE_NONE, 'Do not check for publish errors'), new InputOption('no-check-publish', null, InputOption::VALUE_NONE, 'Do not check for publish errors'),
new InputOption('with-dependencies', 'A', InputOption::VALUE_NONE, 'Also validate the composer.json of all installed dependencies.'),
new InputArgument('file', InputArgument::OPTIONAL, 'path to composer.json file', './composer.json') new InputArgument('file', InputArgument::OPTIONAL, 'path to composer.json file', './composer.json')
)) ))
->setHelp(<<<EOT ->setHelp(<<<EOT
The validate command validates a given composer.json and composer.lock The validate command validates a given composer.json and composer.lock
Exit codes in case of errors are:
1 validation error(s)
3 file unreadable or missing
EOT EOT
); );
} }
@ -63,20 +68,19 @@ EOT
if (!file_exists($file)) { if (!file_exists($file)) {
$io->writeError('<error>' . $file . ' not found.</error>'); $io->writeError('<error>' . $file . ' not found.</error>');
return 1; return 3;
} }
if (!is_readable($file)) { if (!is_readable($file)) {
$io->writeError('<error>' . $file . ' is not readable.</error>'); $io->writeError('<error>' . $file . ' is not readable.</error>');
return 1; return 3;
} }
$validator = new ConfigValidator($io); $validator = new ConfigValidator($io);
$checkAll = $input->getOption('no-check-all') ? 0 : ValidatingArrayLoader::CHECK_ALL; $checkAll = $input->getOption('no-check-all') ? 0 : ValidatingArrayLoader::CHECK_ALL;
$checkPublish = !$input->getOption('no-check-publish'); $checkPublish = !$input->getOption('no-check-publish');
list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll);
$checkLock = !$input->getOption('no-check-lock'); $checkLock = !$input->getOption('no-check-lock');
list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll);
$lockErrors = array(); $lockErrors = array();
$composer = Factory::create($io, $file); $composer = Factory::create($io, $file);
@ -85,18 +89,44 @@ EOT
$lockErrors[] = 'The lock file is not up to date with the latest changes in composer.json.'; $lockErrors[] = 'The lock file is not up to date with the latest changes in composer.json.';
} }
// output errors/warnings $this->outputResult($io, $file, $errors, $warnings, $checkPublish, $publishErrors, $checkLock, $lockErrors, true);
$exitCode = $errors || ($publishErrors && $checkPublish) || ($lockErrors && $checkLock) ? 1 : 0;
if ($input->getOption('with-dependencies')) {
$localRepo = $composer->getRepositoryManager()->getLocalRepository();
foreach ($localRepo->getPackages() as $package) {
$path = $composer->getInstallationManager()->getInstallPath($package);
$file = $path . '/composer.json';
if (is_dir($path) && file_exists($file)) {
list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll);
$this->outputResult($io, $package->getPrettyName(), $errors, $warnings, $checkPublish, $publishErrors);
$exitCode = $exitCode === 1 || $errors || ($publishErrors && $checkPublish) ? 1 : 0;
}
}
}
return $exitCode;
}
private function outputResult($io, $name, $errors, $warnings, $checkPublish = false, $publishErrors = array(), $checkLock = false, $lockErrors = array(), $printSchemaUrl = false)
{
if (!$errors && !$publishErrors && !$warnings) { if (!$errors && !$publishErrors && !$warnings) {
$io->write('<info>' . $file . ' is valid</info>'); $io->write('<info>' . $name . ' is valid</info>');
} elseif (!$errors && !$publishErrors) { } elseif (!$errors && !$publishErrors) {
$io->writeError('<info>' . $file . ' is valid, but with a few warnings</info>'); $io->writeError('<info>' . $name . ' is valid, but with a few warnings</info>');
if ($printSchemaUrl) {
$io->writeError('<warning>See https://getcomposer.org/doc/04-schema.md for details on the schema</warning>'); $io->writeError('<warning>See https://getcomposer.org/doc/04-schema.md for details on the schema</warning>');
}
} elseif (!$errors) { } elseif (!$errors) {
$io->writeError('<info>' . $file . ' is valid for simple usage with composer but has</info>'); $io->writeError('<info>' . $name . ' is valid for simple usage with composer but has</info>');
$io->writeError('<info>strict errors that make it unable to be published as a package:</info>'); $io->writeError('<info>strict errors that make it unable to be published as a package:</info>');
if ($printSchemaUrl) {
$io->writeError('<warning>See https://getcomposer.org/doc/04-schema.md for details on the schema</warning>'); $io->writeError('<warning>See https://getcomposer.org/doc/04-schema.md for details on the schema</warning>');
}
} else { } else {
$io->writeError('<error>' . $file . ' is invalid, the following errors/warnings were found:</error>'); $io->writeError('<error>' . $name . ' is invalid, the following errors/warnings were found:</error>');
} }
$messages = array( $messages = array(
@ -123,7 +153,5 @@ EOT
$io->writeError('<' . $style . '>' . $msg . '</' . $style . '>'); $io->writeError('<' . $style . '>' . $msg . '</' . $style . '>');
} }
} }
return $errors || ($publishErrors && $checkPublish) || ($lockErrors && $checkLock) ? 1 : 0;
} }
} }