From e61b3a6370fad13f141016e249b581b5ef047558 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 20 Sep 2015 19:28:36 +0100 Subject: [PATCH] Allow the validate command to validate dependencies with -A/--with-dependencies, refs #3202 --- src/Composer/Command/ValidateCommand.php | 54 ++++++++++++++++++------ 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/src/Composer/Command/ValidateCommand.php b/src/Composer/Command/ValidateCommand.php index a26f1a868..c08cf2c10 100644 --- a/src/Composer/Command/ValidateCommand.php +++ b/src/Composer/Command/ValidateCommand.php @@ -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-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('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') )) ->setHelp(<<writeError('' . $file . ' not found.'); - return 1; + return 3; } if (!is_readable($file)) { $io->writeError('' . $file . ' is not readable.'); - return 1; + return 3; } $validator = new ConfigValidator($io); $checkAll = $input->getOption('no-check-all') ? 0 : ValidatingArrayLoader::CHECK_ALL; $checkPublish = !$input->getOption('no-check-publish'); - list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll); - $checkLock = !$input->getOption('no-check-lock'); + list($errors, $publishErrors, $warnings) = $validator->validate($file, $checkAll); $lockErrors = array(); $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.'; } - // 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) { - $io->write('' . $file . ' is valid'); + $io->write('' . $name . ' is valid'); } elseif (!$errors && !$publishErrors) { - $io->writeError('' . $file . ' is valid, but with a few warnings'); - $io->writeError('See https://getcomposer.org/doc/04-schema.md for details on the schema'); + $io->writeError('' . $name . ' is valid, but with a few warnings'); + if ($printSchemaUrl) { + $io->writeError('See https://getcomposer.org/doc/04-schema.md for details on the schema'); + } } elseif (!$errors) { - $io->writeError('' . $file . ' is valid for simple usage with composer but has'); + $io->writeError('' . $name . ' is valid for simple usage with composer but has'); $io->writeError('strict errors that make it unable to be published as a package:'); - $io->writeError('See https://getcomposer.org/doc/04-schema.md for details on the schema'); + if ($printSchemaUrl) { + $io->writeError('See https://getcomposer.org/doc/04-schema.md for details on the schema'); + } } else { - $io->writeError('' . $file . ' is invalid, the following errors/warnings were found:'); + $io->writeError('' . $name . ' is invalid, the following errors/warnings were found:'); } $messages = array( @@ -123,7 +153,5 @@ EOT $io->writeError('<' . $style . '>' . $msg . ''); } } - - return $errors || ($publishErrors && $checkPublish) || ($lockErrors && $checkLock) ? 1 : 0; } }