Prechádzať zdrojové kódy

Refactor validate command to offer more flexibility in adding validation

Jordi Boggiano 13 rokov pred
rodič
commit
e84e550df3
2 zmenil súbory, kde vykonal 44 pridanie a 18 odobranie
  1. 1 0
      CHANGELOG.md
  2. 43 18
      src/Composer/Command/ValidateCommand.php

+ 1 - 0
CHANGELOG.md

@@ -14,6 +14,7 @@
   * Added autoloading support for root packages that use target-dir
   * Added awareness of the root package presence and support for it's provide/replace/conflict keys
   * Added IOInterface::isDecorated to test for colored output support
+  * Added validation of licenses based on the [SPDX registry](http://www.spdx.org/licenses/)
   * Improved repository protocol to have large cacheable parts
   * Fixed various bugs relating to package aliasing, proxy configuration, binaries
   * Various bug fixes and docs improvements

+ 43 - 18
src/Composer/Command/ValidateCommand.php

@@ -67,7 +67,13 @@ EOT
             return 1;
         }
 
+        $errors = array();
+        $publishErrors = array();
+        $warnings = array();
+
+        // validate json schema
         $laxValid = false;
+        $valid = false;
         try {
             $json = new JsonFile($file, new RemoteFilesystem($this->getIO()));
             $manifest = $json->read();
@@ -75,18 +81,15 @@ EOT
             $json->validateSchema(JsonFile::LAX_SCHEMA);
             $laxValid = true;
             $json->validateSchema();
+            $valid = true;
         } catch (JsonValidationException $e) {
-            if ($laxValid) {
-                $output->writeln('<info>' . $file . ' is valid for simple usage with composer but has</info>');
-                $output->writeln('<info>strict errors that make it unable to be published as a package:</info>');
-            } else {
-                $output->writeln('<error>' . $file . ' is invalid, the following errors were found:</error>');
-            }
             foreach ($e->getErrors() as $message) {
-                $output->writeln('<error>' . $message . '</error>');
+                if ($laxValid) {
+                    $publishErrors[] = '<error>Publish Error: ' . $message . '</error>';
+                } else {
+                    $errors[] = '<error>' . $message . '</error>';
+                }
             }
-
-            return 1;
         } catch (\Exception $e) {
             $output->writeln('<error>' . $file . ' contains a JSON Syntax Error:</error>');
             $output->writeln('<error>' . $e->getMessage() . '</error>');
@@ -94,21 +97,43 @@ EOT
             return 1;
         }
 
-        if (isset($manifest['license'])) {
+        // validate actual data
+        if (!empty($manifest['license'])) {
             $licenseValidator = new SpdxLicenseIdentifier();
             if (!$licenseValidator->validate($manifest['license'])) {
-                $output->writeln(sprintf(
-                    '<warning>License "%s" is not a valid SPDX license identifier</warning>'."\n".
-                    '<warning>see http://www.spdx.org/licenses/ and http://getcomposer.org/doc/04-schema.md#license</warning>',
-                    print_r($manifest['license'], true)
-                ));
+                $warnings[] = sprintf('License %s is not a valid SPDX license identifier', json_encode($manifest['license']));
+                $warnings[] = 'see http://www.spdx.org/licenses/';
             }
         } else {
-            $output->writeln('<warning>No license specified.</warning>');
+            $warnings[] = 'No license specified, it is recommended to do so';
+        }
+
+        // output errors/warnings
+        if (!$errors && !$publishErrors && !$warnings) {
+            $output->writeln('<info>' . $file . ' is valid</info>');
+        } elseif (!$errors && !$publishErrors) {
+            $output->writeln('<info>' . $file . ' is valid, but with a few warnings</info>');
+            $output->writeln('<warning>See http://getcomposer.org/doc/04-schema.md for details on the schema</warning>');
+        } elseif (!$errors) {
+            $output->writeln('<info>' . $file . ' is valid for simple usage with composer but has</info>');
+            $output->writeln('<info>strict errors that make it unable to be published as a package:</info>');
+            $output->writeln('<warning>See http://getcomposer.org/doc/04-schema.md for details on the schema</warning>');
+        } else {
+            $output->writeln('<error>' . $file . ' is invalid, the following errors/warnings were found:</error>');
         }
 
-        $output->writeln('<info>' . $file . ' is valid</info>');
+        $messages = array(
+            'error' => $errors,
+            'error' => $publishErrors,
+            'warning' => $warnings,
+        );
+
+        foreach ($messages as $style => $msgs) {
+            foreach ($msgs as $msg) {
+                $output->writeln('<' . $style . '>' . $msg . '</' . $style . '>');
+            }
+        }
 
-        return 0;
+        return $errors || $publishErrors ? 1 : 0;
     }
 }