Просмотр исходного кода

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

Jordi Boggiano 9 лет назад
Родитель
Сommit
e61b3a6370
1 измененных файлов с 41 добавлено и 13 удалено
  1. 41 13
      src/Composer/Command/ValidateCommand.php

+ 41 - 13
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(<<<EOT
 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
             );
     }
@@ -63,20 +68,19 @@ EOT
         if (!file_exists($file)) {
             $io->writeError('<error>' . $file . ' not found.</error>');
 
-            return 1;
+            return 3;
         }
         if (!is_readable($file)) {
             $io->writeError('<error>' . $file . ' is not readable.</error>');
 
-            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('<info>' . $file . ' is valid</info>');
+            $io->write('<info>' . $name . ' is valid</info>');
         } elseif (!$errors && !$publishErrors) {
-            $io->writeError('<info>' . $file . ' is valid, but with a few warnings</info>');
-            $io->writeError('<warning>See https://getcomposer.org/doc/04-schema.md for details on the schema</warning>');
+            $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>');
+            }
         } 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('<warning>See https://getcomposer.org/doc/04-schema.md for details on the schema</warning>');
+            if ($printSchemaUrl) {
+                $io->writeError('<warning>See https://getcomposer.org/doc/04-schema.md for details on the schema</warning>');
+            }
         } 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(
@@ -123,7 +153,5 @@ EOT
                 $io->writeError('<' . $style . '>' . $msg . '</' . $style . '>');
             }
         }
-
-        return $errors || ($publishErrors && $checkPublish) || ($lockErrors && $checkLock) ? 1 : 0;
     }
 }