From e84e550df3b529d784f9e1b759cc2ead1bcec971 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 12 May 2012 17:32:13 +0200 Subject: [PATCH] Refactor validate command to offer more flexibility in adding validation --- CHANGELOG.md | 1 + src/Composer/Command/ValidateCommand.php | 61 +++++++++++++++++------- 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6af21571..e2cf9b706 100644 --- a/CHANGELOG.md +++ b/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 diff --git a/src/Composer/Command/ValidateCommand.php b/src/Composer/Command/ValidateCommand.php index 7126b4e4a..51cea8e81 100644 --- a/src/Composer/Command/ValidateCommand.php +++ b/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('' . $file . ' is valid for simple usage with composer but has'); - $output->writeln('strict errors that make it unable to be published as a package:'); - } else { - $output->writeln('' . $file . ' is invalid, the following errors were found:'); - } foreach ($e->getErrors() as $message) { - $output->writeln('' . $message . ''); + if ($laxValid) { + $publishErrors[] = 'Publish Error: ' . $message . ''; + } else { + $errors[] = '' . $message . ''; + } } - - return 1; } catch (\Exception $e) { $output->writeln('' . $file . ' contains a JSON Syntax Error:'); $output->writeln('' . $e->getMessage() . ''); @@ -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( - 'License "%s" is not a valid SPDX license identifier'."\n". - 'see http://www.spdx.org/licenses/ and http://getcomposer.org/doc/04-schema.md#license', - 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('No license specified.'); + $warnings[] = 'No license specified, it is recommended to do so'; + } + + // output errors/warnings + if (!$errors && !$publishErrors && !$warnings) { + $output->writeln('' . $file . ' is valid'); + } elseif (!$errors && !$publishErrors) { + $output->writeln('' . $file . ' is valid, but with a few warnings'); + $output->writeln('See http://getcomposer.org/doc/04-schema.md for details on the schema'); + } elseif (!$errors) { + $output->writeln('' . $file . ' is valid for simple usage with composer but has'); + $output->writeln('strict errors that make it unable to be published as a package:'); + $output->writeln('See http://getcomposer.org/doc/04-schema.md for details on the schema'); + } else { + $output->writeln('' . $file . ' is invalid, the following errors/warnings were found:'); } - $output->writeln('' . $file . ' is valid'); + $messages = array( + 'error' => $errors, + 'error' => $publishErrors, + 'warning' => $warnings, + ); + + foreach ($messages as $style => $msgs) { + foreach ($msgs as $msg) { + $output->writeln('<' . $style . '>' . $msg . ''); + } + } - return 0; + return $errors || $publishErrors ? 1 : 0; } }