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 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'entrypoint.dart'; | 9 import 'entrypoint.dart'; |
10 import 'log.dart' as log; | 10 import 'log.dart' as log; |
11 import 'io.dart'; | 11 import 'io.dart'; |
12 import 'system_cache.dart'; | 12 import 'system_cache.dart'; |
13 import 'utils.dart'; | 13 import 'utils.dart'; |
14 import 'validator/compiled_dartdoc.dart'; | 14 import 'validator/compiled_dartdoc.dart'; |
15 import 'validator/dependency.dart'; | 15 import 'validator/dependency.dart'; |
16 import 'validator/directory.dart'; | 16 import 'validator/directory.dart'; |
17 import 'validator/lib.dart'; | 17 import 'validator/lib.dart'; |
18 import 'validator/license.dart'; | 18 import 'validator/license.dart'; |
19 import 'validator/name.dart'; | 19 import 'validator/name.dart'; |
20 import 'validator/pubspec_field.dart'; | 20 import 'validator/pubspec_field.dart'; |
| 21 import 'validator/size.dart'; |
21 import 'validator/utf8_readme.dart'; | 22 import 'validator/utf8_readme.dart'; |
22 | 23 |
23 /// The base class for validators that check whether a package is fit for | 24 /// The base class for validators that check whether a package is fit for |
24 /// uploading. Each validator should override [errors], [warnings], or both to | 25 /// uploading. Each validator should override [errors], [warnings], or both to |
25 /// return lists of errors or warnings to display to the user. Errors will cause | 26 /// return lists of errors or warnings to display to the user. Errors will cause |
26 /// the package not to be uploaded; warnings will require the user to confirm | 27 /// the package not to be uploaded; warnings will require the user to confirm |
27 /// the upload. | 28 /// the upload. |
28 abstract class Validator { | 29 abstract class Validator { |
29 /// The entrypoint that's being validated. | 30 /// The entrypoint that's being validated. |
30 final Entrypoint entrypoint; | 31 final Entrypoint entrypoint; |
31 | 32 |
32 /// The accumulated errors for this validator. Filled by calling [validate]. | 33 /// The accumulated errors for this validator. Filled by calling [validate]. |
33 final errors = <String>[]; | 34 final errors = <String>[]; |
34 | 35 |
35 /// The accumulated warnings for this validator. Filled by calling [validate]. | 36 /// The accumulated warnings for this validator. Filled by calling [validate]. |
36 final warnings = <String>[]; | 37 final warnings = <String>[]; |
37 | 38 |
38 Validator(this.entrypoint); | 39 Validator(this.entrypoint); |
39 | 40 |
40 /// Validates the entrypoint, adding any errors and warnings to [errors] and | 41 /// Validates the entrypoint, adding any errors and warnings to [errors] and |
41 /// [warnings], respectively. | 42 /// [warnings], respectively. |
42 Future validate(); | 43 Future validate(); |
43 | 44 |
44 /// Run all validators on the [entrypoint] package and print their results. | 45 /// Run all validators on the [entrypoint] package and print their results. |
45 /// The future will complete with the error and warning messages, | 46 /// The future will complete with the error and warning messages, |
46 /// respectively. | 47 /// respectively. |
| 48 /// |
| 49 /// [packageSize], if passed, should complete to the size of the tarred |
| 50 /// package, in bytes. This is used to validate that it's not too big to |
| 51 /// upload to the server. |
47 static Future<Pair<List<String>, List<String>>> runAll( | 52 static Future<Pair<List<String>, List<String>>> runAll( |
48 Entrypoint entrypoint) { | 53 Entrypoint entrypoint, [Future<int> packageSize]) { |
49 var validators = [ | 54 var validators = [ |
50 new LibValidator(entrypoint), | 55 new LibValidator(entrypoint), |
51 new LicenseValidator(entrypoint), | 56 new LicenseValidator(entrypoint), |
52 new NameValidator(entrypoint), | 57 new NameValidator(entrypoint), |
53 new PubspecFieldValidator(entrypoint), | 58 new PubspecFieldValidator(entrypoint), |
54 new DependencyValidator(entrypoint), | 59 new DependencyValidator(entrypoint), |
55 new DirectoryValidator(entrypoint), | 60 new DirectoryValidator(entrypoint), |
56 new CompiledDartdocValidator(entrypoint), | 61 new CompiledDartdocValidator(entrypoint), |
57 new Utf8ReadmeValidator(entrypoint) | 62 new Utf8ReadmeValidator(entrypoint) |
58 ]; | 63 ]; |
| 64 if (packageSize != null) { |
| 65 validators.add(new SizeValidator(entrypoint, packageSize)); |
| 66 } |
59 | 67 |
60 return Future.wait(validators.map((validator) => validator.validate())) | 68 return Future.wait(validators.map((validator) => validator.validate())) |
61 .then((_) { | 69 .then((_) { |
62 var errors = | 70 var errors = |
63 flatten(validators.map((validator) => validator.errors)); | 71 flatten(validators.map((validator) => validator.errors)); |
64 var warnings = | 72 var warnings = |
65 flatten(validators.map((validator) => validator.warnings)); | 73 flatten(validators.map((validator) => validator.warnings)); |
66 | 74 |
67 if (!errors.isEmpty) { | 75 if (!errors.isEmpty) { |
68 log.error("Missing requirements:"); | 76 log.error("Missing requirements:"); |
69 for (var error in errors) { | 77 for (var error in errors) { |
70 log.error("* ${Strings.join(error.split('\n'), '\n ')}"); | 78 log.error("* ${Strings.join(error.split('\n'), '\n ')}"); |
71 } | 79 } |
72 log.error(""); | 80 log.error(""); |
73 } | 81 } |
74 | 82 |
75 if (!warnings.isEmpty) { | 83 if (!warnings.isEmpty) { |
76 log.warning("Suggestions:"); | 84 log.warning("Suggestions:"); |
77 for (var warning in warnings) { | 85 for (var warning in warnings) { |
78 log.warning("* ${Strings.join(warning.split('\n'), '\n ')}"); | 86 log.warning("* ${Strings.join(warning.split('\n'), '\n ')}"); |
79 } | 87 } |
80 log.warning(""); | 88 log.warning(""); |
81 } | 89 } |
82 | 90 |
83 return new Pair<List<String>, List<String>>(errors, warnings); | 91 return new Pair<List<String>, List<String>>(errors, warnings); |
84 }); | 92 }); |
85 } | 93 } |
86 } | 94 } |
OLD | NEW |