1
0
Fork 0

Refactor validate command to offer more flexibility in adding validation

pull/592/merge
Jordi Boggiano 2012-05-12 17:32:13 +02:00
parent fd38971777
commit e84e550df3
2 changed files with 44 additions and 18 deletions

View File

@ -14,6 +14,7 @@
* Added autoloading support for root packages that use target-dir * 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 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 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 * Improved repository protocol to have large cacheable parts
* Fixed various bugs relating to package aliasing, proxy configuration, binaries * Fixed various bugs relating to package aliasing, proxy configuration, binaries
* Various bug fixes and docs improvements * Various bug fixes and docs improvements

View File

@ -67,7 +67,13 @@ EOT
return 1; return 1;
} }
$errors = array();
$publishErrors = array();
$warnings = array();
// validate json schema
$laxValid = false; $laxValid = false;
$valid = false;
try { try {
$json = new JsonFile($file, new RemoteFilesystem($this->getIO())); $json = new JsonFile($file, new RemoteFilesystem($this->getIO()));
$manifest = $json->read(); $manifest = $json->read();
@ -75,18 +81,15 @@ EOT
$json->validateSchema(JsonFile::LAX_SCHEMA); $json->validateSchema(JsonFile::LAX_SCHEMA);
$laxValid = true; $laxValid = true;
$json->validateSchema(); $json->validateSchema();
$valid = true;
} catch (JsonValidationException $e) { } 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) { 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) { } catch (\Exception $e) {
$output->writeln('<error>' . $file . ' contains a JSON Syntax Error:</error>'); $output->writeln('<error>' . $file . ' contains a JSON Syntax Error:</error>');
$output->writeln('<error>' . $e->getMessage() . '</error>'); $output->writeln('<error>' . $e->getMessage() . '</error>');
@ -94,21 +97,43 @@ EOT
return 1; return 1;
} }
if (isset($manifest['license'])) { // validate actual data
if (!empty($manifest['license'])) {
$licenseValidator = new SpdxLicenseIdentifier(); $licenseValidator = new SpdxLicenseIdentifier();
if (!$licenseValidator->validate($manifest['license'])) { if (!$licenseValidator->validate($manifest['license'])) {
$output->writeln(sprintf( $warnings[] = sprintf('License %s is not a valid SPDX license identifier', json_encode($manifest['license']));
'<warning>License "%s" is not a valid SPDX license identifier</warning>'."\n". $warnings[] = 'see http://www.spdx.org/licenses/';
'<warning>see http://www.spdx.org/licenses/ and http://getcomposer.org/doc/04-schema.md#license</warning>',
print_r($manifest['license'], true)
));
} }
} else { } else {
$output->writeln('<warning>No license specified.</warning>'); $warnings[] = 'No license specified, it is recommended to do so';
} }
$output->writeln('<info>' . $file . ' is valid</info>'); // 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>');
}
return 0; $messages = array(
'error' => $errors,
'error' => $publishErrors,
'warning' => $warnings,
);
foreach ($messages as $style => $msgs) {
foreach ($msgs as $msg) {
$output->writeln('<' . $style . '>' . $msg . '</' . $style . '>');
}
}
return $errors || $publishErrors ? 1 : 0;
} }
} }