Index: utils/pub/validator.dart |
diff --git a/utils/pub/validator.dart b/utils/pub/validator.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5431c69612fa045828f486d56f621e9a247475f6 |
--- /dev/null |
+++ b/utils/pub/validator.dart |
@@ -0,0 +1,85 @@ |
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+library validator; |
Bob Nystrom
2012/12/05 18:57:40
"validation"?
nweiz
2012/12/05 21:59:59
I think the library name should match the name of
Bob Nystrom
2012/12/05 22:18:41
I actually think it shouldn't since it may make th
nweiz
2012/12/06 00:06:10
I think open-ended names are good for libraries th
|
+ |
+import 'dart:io'; |
+ |
+import 'entrypoint.dart'; |
+import 'io.dart'; |
+import 'system_cache.dart'; |
+import 'utils.dart'; |
+import 'validator/pubspec_field.dart'; |
+ |
+/// The base class for validators that check whether a package is fit for |
+/// uploading. Each validator should override [errors], [warnings], or both to |
+/// return lists of errors or warnings to display to the user. Errors will cause |
+/// the package not to be uploaded; warnings will require the user to confirm |
+/// the upload. |
+abstract class Validator { |
+ /// The user's system cache. |
+ final SystemCache cache; |
+ |
+ /// The entrypoint that's being validated. |
+ final Entrypoint entrypoint; |
+ |
+ /// The accumulated errors for this validator. Filled by calling [validate]. |
+ final errors = <String>[]; |
Bob Nystrom
2012/12/05 18:57:40
Instead of exposing these directly to the subclass
nweiz
2012/12/05 21:59:59
That seems like more machinery than it's worth to
Bob Nystrom
2012/12/05 22:18:41
This would only require one extra parameter declar
nweiz
2012/12/06 00:06:10
If we do it that way, it becomes impossible to use
|
+ |
+ /// The accumulated warnings for this validator. Filled by calling [validate]. |
+ final warnings = <String>[]; |
+ |
+ Validator(this.cache, this.entrypoint); |
+ |
+ /// Validates the entrypoint, adding any errors and warnings to [errors] and |
+ /// [warnings], respectively. |
+ Future validate(); |
+ |
+ /// Run all validators on the [entrypoint] package. The future will complete |
+ /// successfully if validation succeeded, and complete with an error if it |
+ /// failed. |
+ static Future runAll(SystemCache cache, Entrypoint entrypoint) { |
+ var validators = [ |
+ new PubspecFieldValidator(cache, entrypoint) |
Bob Nystrom
2012/12/05 18:57:40
What's the granularity that you have in mind for a
nweiz
2012/12/05 21:59:59
It's a bit wishy-washy; basically I wanted to stri
Bob Nystrom
2012/12/05 22:18:41
OK. Once we have more validation in place, we can
|
+ ]; |
+ |
+ // TODO(nweiz): The sleep 0 here forces us to go async. This works around |
+ // 3356, which causes a bug if all validators are (synchronously) using |
+ // Future.immediate and an error is thrown before a handler is set up. |
+ return sleep(0).chain((_) { |
+ return Futures.wait(validators.map((validator) => validator.validate())); |
+ }).chain((_) { |
+ var errors = flatten(validators.map((validator) => validator.errors)); |
+ var warnings = flatten(validators.map((validator) => validator.warnings)); |
+ |
+ if (errors.isEmpty && warnings.isEmpty) return new Future.immediate(null); |
+ |
+ if (!errors.isEmpty) { |
+ printError("== Errors:"); |
+ for (var error in errors) { |
+ printError("* $error"); |
+ } |
+ printError(""); |
+ } |
+ |
+ if (!warnings.isEmpty) { |
+ printError("== Warnings:"); |
+ for (var warning in warnings) { |
+ printError("* $warning"); |
+ } |
+ printError(""); |
+ } |
+ |
+ if (!errors.isEmpty) throw "Package validation failed."; |
+ |
+ var s = warnings.length == 1 ? '' : 's'; |
+ stdout.writeString("Package has ${warnings.length} warning$s. Upload " |
Bob Nystrom
2012/12/05 18:57:40
I think this should be moved out of the validation
nweiz
2012/12/05 21:59:59
Done.
|
+ "anyway (y/n)? "); |
+ return readLine().transform((line) { |
+ if (new RegExp(r"^[yY]").hasMatch(line)) return; |
+ throw "Package upload aborted."; |
Bob Nystrom
2012/12/05 18:57:40
"aborted" -> "cancelled"
nweiz
2012/12/05 21:59:59
Done.
|
+ }); |
+ }); |
+ } |
+} |