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 pub.validator.dependency; | 5 library pub.validator.dependency; |
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 '../package.dart'; | 11 import '../package.dart'; |
11 import '../validator.dart'; | 12 import '../validator.dart'; |
12 import '../version.dart'; | 13 import '../version.dart'; |
13 | 14 |
14 /// A validator that validates a package's dependencies. | 15 /// A validator that validates a package's dependencies. |
15 class DependencyValidator extends Validator { | 16 class DependencyValidator extends Validator { |
16 DependencyValidator(Entrypoint entrypoint) | 17 DependencyValidator(Entrypoint entrypoint) |
17 : super(entrypoint); | 18 : super(entrypoint); |
18 | 19 |
19 Future validate() { | 20 Future validate() { |
20 return Future.forEach(entrypoint.root.pubspec.dependencies, (dependency) { | 21 return Future.forEach(entrypoint.root.pubspec.dependencies, (dependency) { |
21 if (dependency.source != "hosted") { | 22 if (dependency.source != "hosted") { |
22 return _warnAboutSource(dependency); | 23 return _warnAboutSource(dependency); |
23 } | 24 } |
24 | 25 |
25 if (dependency.constraint.isAny) _warnAboutConstraint(dependency); | 26 if (dependency.constraint.isAny) { |
| 27 _warnAboutNoConstraint(dependency); |
| 28 } else if (dependency.constraint is Version) { |
| 29 _warnAboutSingleVersionConstraint(dependency); |
| 30 } else if (dependency.constraint is VersionRange) { |
| 31 if (dependency.constraint.min == null) { |
| 32 _warnAboutNoConstraintLowerBound(dependency); |
| 33 } else if (dependency.constraint.max == null) { |
| 34 _warnAboutNoConstraintUpperBound(dependency); |
| 35 } |
| 36 } |
26 | 37 |
27 return new Future.value(); | 38 return new Future.value(); |
28 }); | 39 }); |
29 } | 40 } |
30 | 41 |
31 /// Warn that dependencies should use the hosted source. | 42 /// Warn that dependencies should use the hosted source. |
32 Future _warnAboutSource(PackageDep dep) { | 43 Future _warnAboutSource(PackageDep dep) { |
33 return entrypoint.cache.sources['hosted'] | 44 return entrypoint.cache.sources['hosted'] |
34 .getVersions(dep.name, dep.name) | 45 .getVersions(dep.name, dep.name) |
35 .catchError((e) => <Version>[]) | 46 .catchError((e) => <Version>[]) |
(...skipping 20 matching lines...) Expand all Loading... |
56 '\n' | 67 '\n' |
57 'dependencies:\n' | 68 'dependencies:\n' |
58 ' ${dep.name}: $constraint\n' | 69 ' ${dep.name}: $constraint\n' |
59 '\n' | 70 '\n' |
60 'Using the hosted source ensures that everyone can download your ' | 71 'Using the hosted source ensures that everyone can download your ' |
61 'package\'s dependencies along with your package.'); | 72 'package\'s dependencies along with your package.'); |
62 }); | 73 }); |
63 } | 74 } |
64 | 75 |
65 /// Warn that dependencies should have version constraints. | 76 /// Warn that dependencies should have version constraints. |
66 void _warnAboutConstraint(PackageDep dep) { | 77 void _warnAboutNoConstraint(PackageDep dep) { |
67 var lockFile = entrypoint.loadLockFile(); | 78 var lockFile = entrypoint.loadLockFile(); |
68 var message = 'Your dependency on "${dep.name}" should have a version ' | 79 var message = 'Your dependency on "${dep.name}" should have a version ' |
69 'constraint.'; | 80 'constraint.'; |
70 var locked = lockFile.packages[dep.name]; | 81 var locked = lockFile.packages[dep.name]; |
71 if (locked != null) { | 82 if (locked != null) { |
72 message = '$message For example:\n' | 83 message = '$message For example:\n' |
73 '\n' | 84 '\n' |
74 'dependencies:\n' | 85 'dependencies:\n' |
75 ' ${dep.name}: ${_constraintForVersion(locked.version)}\n'; | 86 ' ${dep.name}: ${_constraintForVersion(locked.version)}\n'; |
76 } | 87 } |
77 warnings.add("$message\n" | 88 warnings.add("$message\n" |
78 "Without a constraint, you're promising to support all future " | 89 'Without a constraint, you\'re promising to support ${log.bold("all")} ' |
79 "versions of ${dep.name}."); | 90 'future versions of "${dep.name}".'); |
| 91 } |
| 92 |
| 93 // Warn that dependencies should allow more than a single version. |
| 94 void _warnAboutSingleVersionConstraint(PackageDep dep) { |
| 95 warnings.add( |
| 96 'Your dependency on "${dep.name}" should allow more than one version. ' |
| 97 'For example:\n' |
| 98 '\n' |
| 99 'dependencies:\n' |
| 100 ' ${dep.name}: ${_constraintForVersion(dep.constraint)}\n' |
| 101 '\n' |
| 102 'Constraints that are too tight will make it difficult for people to ' |
| 103 'use your package\n' |
| 104 'along with other packages that also depend on "${dep.name}".'); |
| 105 } |
| 106 |
| 107 // Warn that dependencies should have lower bounds on their constraints. |
| 108 void _warnAboutNoConstraintLowerBound(PackageDep dep) { |
| 109 var message = 'Your dependency on "${dep.name}" should have a lower bound.'; |
| 110 var locked = entrypoint.loadLockFile().packages[dep.name]; |
| 111 if (locked != null) { |
| 112 var constraint; |
| 113 if (locked.version == dep.constraint.max) { |
| 114 constraint = _constraintForVersion(locked.version); |
| 115 } else { |
| 116 constraint = '">=${locked.version} ${dep.constraint}"'; |
| 117 } |
| 118 |
| 119 message = '$message For example:\n' |
| 120 '\n' |
| 121 'dependencies:\n' |
| 122 ' ${dep.name}: $constraint\n'; |
| 123 } |
| 124 warnings.add("$message\n" |
| 125 'Without a constraint, you\'re promising to support ${log.bold("all")} ' |
| 126 'previous versions of "${dep.name}".'); |
| 127 } |
| 128 |
| 129 // Warn that dependencies should have upper bounds on their constraints. |
| 130 void _warnAboutNoConstraintUpperBound(PackageDep dep) { |
| 131 warnings.add( |
| 132 'Your dependency on "${dep.name}" should have an upper bound. For ' |
| 133 'example:\n' |
| 134 '\n' |
| 135 'dependencies:\n' |
| 136 ' ${dep.name}: "${dep.constraint} ' |
| 137 '${_upperBoundForVersion(dep.constraint.min)}"\n' |
| 138 '\n' |
| 139 'Without an upper bound, you\'re promising to support ' |
| 140 '${log.bold("all")} future versions of ${dep.name}.'); |
80 } | 141 } |
81 | 142 |
82 /// Returns the suggested version constraint for a dependency that was tested | 143 /// Returns the suggested version constraint for a dependency that was tested |
83 /// against [version]. | 144 /// against [version]. |
84 String _constraintForVersion(Version version) { | 145 String _constraintForVersion(Version version) => |
85 if (version.major != 0) return '">=$version <${version.major + 1}.0.0"'; | 146 '">=$version ${_upperBoundForVersion(version)}"'; |
86 return '">=$version <${version.major}.${version.minor}.' | 147 |
87 '${version.patch + 1}"'; | 148 /// Returns the suggested upper bound for a dependency that was tested against |
| 149 /// [version]. |
| 150 String _upperBoundForVersion(Version version) { |
| 151 if (version.major != 0) return '<${version.major + 1}.0.0'; |
| 152 return '<${version.major}.${version.minor + 1}.0'; |
88 } | 153 } |
89 } | 154 } |
OLD | NEW |