Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 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
| |
| 6 | |
| 7 import 'dart:io'; | |
| 8 | |
| 9 import 'entrypoint.dart'; | |
| 10 import 'io.dart'; | |
| 11 import 'system_cache.dart'; | |
| 12 import 'utils.dart'; | |
| 13 import 'validator/pubspec_field.dart'; | |
| 14 | |
| 15 /// The base class for validators that check whether a package is fit for | |
| 16 /// uploading. Each validator should override [errors], [warnings], or both to | |
| 17 /// return lists of errors or warnings to display to the user. Errors will cause | |
| 18 /// the package not to be uploaded; warnings will require the user to confirm | |
| 19 /// the upload. | |
| 20 abstract class Validator { | |
| 21 /// The user's system cache. | |
| 22 final SystemCache cache; | |
| 23 | |
| 24 /// The entrypoint that's being validated. | |
| 25 final Entrypoint entrypoint; | |
| 26 | |
| 27 /// The accumulated errors for this validator. Filled by calling [validate]. | |
| 28 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
| |
| 29 | |
| 30 /// The accumulated warnings for this validator. Filled by calling [validate]. | |
| 31 final warnings = <String>[]; | |
| 32 | |
| 33 Validator(this.cache, this.entrypoint); | |
| 34 | |
| 35 /// Validates the entrypoint, adding any errors and warnings to [errors] and | |
| 36 /// [warnings], respectively. | |
| 37 Future validate(); | |
| 38 | |
| 39 /// Run all validators on the [entrypoint] package. The future will complete | |
| 40 /// successfully if validation succeeded, and complete with an error if it | |
| 41 /// failed. | |
| 42 static Future runAll(SystemCache cache, Entrypoint entrypoint) { | |
| 43 var validators = [ | |
| 44 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
| |
| 45 ]; | |
| 46 | |
| 47 // TODO(nweiz): The sleep 0 here forces us to go async. This works around | |
| 48 // 3356, which causes a bug if all validators are (synchronously) using | |
| 49 // Future.immediate and an error is thrown before a handler is set up. | |
| 50 return sleep(0).chain((_) { | |
| 51 return Futures.wait(validators.map((validator) => validator.validate())); | |
| 52 }).chain((_) { | |
| 53 var errors = flatten(validators.map((validator) => validator.errors)); | |
| 54 var warnings = flatten(validators.map((validator) => validator.warnings)); | |
| 55 | |
| 56 if (errors.isEmpty && warnings.isEmpty) return new Future.immediate(null); | |
| 57 | |
| 58 if (!errors.isEmpty) { | |
| 59 printError("== Errors:"); | |
| 60 for (var error in errors) { | |
| 61 printError("* $error"); | |
| 62 } | |
| 63 printError(""); | |
| 64 } | |
| 65 | |
| 66 if (!warnings.isEmpty) { | |
| 67 printError("== Warnings:"); | |
| 68 for (var warning in warnings) { | |
| 69 printError("* $warning"); | |
| 70 } | |
| 71 printError(""); | |
| 72 } | |
| 73 | |
| 74 if (!errors.isEmpty) throw "Package validation failed."; | |
| 75 | |
| 76 var s = warnings.length == 1 ? '' : 's'; | |
| 77 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.
| |
| 78 "anyway (y/n)? "); | |
| 79 return readLine().transform((line) { | |
| 80 if (new RegExp(r"^[yY]").hasMatch(line)) return; | |
| 81 throw "Package upload aborted."; | |
|
Bob Nystrom
2012/12/05 18:57:40
"aborted" -> "cancelled"
nweiz
2012/12/05 21:59:59
Done.
| |
| 82 }); | |
| 83 }); | |
| 84 } | |
| 85 } | |
| OLD | NEW |