OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library validator; | 5 library validator; |
6 | 6 |
7 import 'entrypoint.dart'; | 7 import 'entrypoint.dart'; |
8 import 'io.dart'; | 8 import 'io.dart'; |
9 import 'system_cache.dart'; | 9 import 'system_cache.dart'; |
10 import 'utils.dart'; | 10 import 'utils.dart'; |
| 11 import 'validator/name.dart'; |
11 import 'validator/pubspec_field.dart'; | 12 import 'validator/pubspec_field.dart'; |
12 | 13 |
13 /// The base class for validators that check whether a package is fit for | 14 /// The base class for validators that check whether a package is fit for |
14 /// uploading. Each validator should override [errors], [warnings], or both to | 15 /// uploading. Each validator should override [errors], [warnings], or both to |
15 /// return lists of errors or warnings to display to the user. Errors will cause | 16 /// return lists of errors or warnings to display to the user. Errors will cause |
16 /// the package not to be uploaded; warnings will require the user to confirm | 17 /// the package not to be uploaded; warnings will require the user to confirm |
17 /// the upload. | 18 /// the upload. |
18 abstract class Validator { | 19 abstract class Validator { |
19 /// The entrypoint that's being validated. | 20 /// The entrypoint that's being validated. |
20 final Entrypoint entrypoint; | 21 final Entrypoint entrypoint; |
21 | 22 |
22 /// The accumulated errors for this validator. Filled by calling [validate]. | 23 /// The accumulated errors for this validator. Filled by calling [validate]. |
23 final errors = <String>[]; | 24 final errors = <String>[]; |
24 | 25 |
25 /// The accumulated warnings for this validator. Filled by calling [validate]. | 26 /// The accumulated warnings for this validator. Filled by calling [validate]. |
26 final warnings = <String>[]; | 27 final warnings = <String>[]; |
27 | 28 |
28 Validator(this.entrypoint); | 29 Validator(this.entrypoint); |
29 | 30 |
30 /// Validates the entrypoint, adding any errors and warnings to [errors] and | 31 /// Validates the entrypoint, adding any errors and warnings to [errors] and |
31 /// [warnings], respectively. | 32 /// [warnings], respectively. |
32 Future validate(); | 33 Future validate(); |
33 | 34 |
34 /// Run all validators on the [entrypoint] package and print their results. | 35 /// Run all validators on the [entrypoint] package and print their results. |
35 /// The future will complete with the error and warning messages, | 36 /// The future will complete with the error and warning messages, |
36 /// respectively. | 37 /// respectively. |
37 static Future<Pair<List<String>, List<String>>> runAll( | 38 static Future<Pair<List<String>, List<String>>> runAll( |
38 Entrypoint entrypoint) { | 39 Entrypoint entrypoint) { |
39 var validators = [ | 40 var validators = [ |
| 41 new NameValidator(entrypoint), |
40 new PubspecFieldValidator(entrypoint) | 42 new PubspecFieldValidator(entrypoint) |
41 ]; | 43 ]; |
42 | 44 |
43 // TODO(nweiz): The sleep 0 here forces us to go async. This works around | 45 // TODO(nweiz): The sleep 0 here forces us to go async. This works around |
44 // 3356, which causes a bug if all validators are (synchronously) using | 46 // 3356, which causes a bug if all validators are (synchronously) using |
45 // Future.immediate and an error is thrown before a handler is set up. | 47 // Future.immediate and an error is thrown before a handler is set up. |
46 return sleep(0).chain((_) { | 48 return sleep(0).chain((_) { |
47 return Futures.wait(validators.map((validator) => validator.validate())); | 49 return Futures.wait(validators.map((validator) => validator.validate())); |
48 }).transform((_) { | 50 }).transform((_) { |
49 var errors = flatten(validators.map((validator) => validator.errors)); | 51 var errors = flatten(validators.map((validator) => validator.errors)); |
(...skipping 12 matching lines...) Expand all Loading... |
62 for (var warning in warnings) { | 64 for (var warning in warnings) { |
63 printError("* $warning"); | 65 printError("* $warning"); |
64 } | 66 } |
65 printError(""); | 67 printError(""); |
66 } | 68 } |
67 | 69 |
68 return new Pair<List<String>, List<String>>(errors, warnings); | 70 return new Pair<List<String>, List<String>>(errors, warnings); |
69 }); | 71 }); |
70 } | 72 } |
71 } | 73 } |
OLD | NEW |