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 dependency_validator; |
| 6 |
| 7 import '../entrypoint.dart'; |
| 8 import '../hosted_source.dart'; |
| 9 import '../http.dart'; |
| 10 import '../package.dart'; |
| 11 import '../utils.dart'; |
| 12 import '../validator.dart'; |
| 13 import '../version.dart'; |
| 14 |
| 15 /// A validator that validates a package's dependencies. |
| 16 class DependencyValidator extends Validator { |
| 17 DependencyValidator(Entrypoint entrypoint) |
| 18 : super(entrypoint); |
| 19 |
| 20 Future validate() { |
| 21 return Futures.forEach(entrypoint.root.pubspec.dependencies, (dependency) { |
| 22 if (dependency.source is! HostedSource) { |
| 23 return _warnAboutSource(dependency); |
| 24 } |
| 25 |
| 26 if (dependency.constraint.isAny && |
| 27 // TODO(nweiz): once we have development dependencies (issue 5358), we |
| 28 // should warn about unittest. Until then, it's reasonable not to put |
| 29 // a constraint on it. |
| 30 dependency.name != 'unittest') { |
| 31 return _warnAboutConstraint(dependency); |
| 32 } |
| 33 |
| 34 return new Future.immediate(null); |
| 35 }); |
| 36 } |
| 37 |
| 38 /// Warn that dependencies should use the hosted source. |
| 39 Future _warnAboutSource(PackageRef ref) { |
| 40 return entrypoint.cache.sources['hosted'] |
| 41 .getVersions(ref.name, ref.name) |
| 42 .transformException((e) => <Version>[]) |
| 43 .transform((versions) { |
| 44 var constraint; |
| 45 var primary = Version.primary(versions); |
| 46 if (primary != null) { |
| 47 constraint = _constraintForVersion(primary); |
| 48 } else { |
| 49 constraint = ref.constraint.toString(); |
| 50 if (!ref.constraint.isAny && ref.constraint is! Version) { |
| 51 constraint = '"$constraint"'; |
| 52 } |
| 53 } |
| 54 |
| 55 warnings.add('Don\'t depend on "${ref.name}" from the ${ref.source.name} ' |
| 56 'source. Use the hosted source instead. For example:\n' |
| 57 '\n' |
| 58 'dependencies:\n' |
| 59 ' ${ref.name}: $constraint\n' |
| 60 '\n' |
| 61 'Using the hosted source ensures that everyone can download your ' |
| 62 'package\'s dependencies along with your package.'); |
| 63 }); |
| 64 } |
| 65 |
| 66 /// Warn that dependencies should have version constraints. |
| 67 Future _warnAboutConstraint(PackageRef ref) { |
| 68 return entrypoint.loadLockFile().transform((lockFile) { |
| 69 var message = 'Your dependency on "${ref.name}" should have a version ' |
| 70 'constraint.'; |
| 71 var locked = lockFile.packages[ref.name]; |
| 72 if (locked != null) { |
| 73 message = '$message For example:\n' |
| 74 '\n' |
| 75 'dependencies:\n' |
| 76 ' ${ref.name}: ${_constraintForVersion(locked.version)}\n'; |
| 77 } |
| 78 warnings.add("$message\n" |
| 79 "Without a constraint, you're promising to support all future " |
| 80 "versions of ${ref.name}."); |
| 81 }); |
| 82 } |
| 83 |
| 84 /// Returns the suggested version constraint for a dependency that was tested |
| 85 /// against [version]. |
| 86 String _constraintForVersion(Version version) { |
| 87 if (version.major != 0) return '">=$version <${version.major + 1}.0.0"'; |
| 88 return '">=$version <${version.major}.${version.minor}.' |
| 89 '${version.patch + 1}"'; |
| 90 } |
| 91 } |
OLD | NEW |