| 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 dependency_validator; | 5 library dependency_validator; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import '../entrypoint.dart'; | 9 import '../entrypoint.dart'; |
| 10 import '../hosted_source.dart'; | 10 import '../hosted_source.dart'; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 return new Future.value(); | 34 return new Future.value(); |
| 35 } | 35 } |
| 36 | 36 |
| 37 if (dependency.constraint.isAny) _warnAboutConstraint(dependency); | 37 if (dependency.constraint.isAny) _warnAboutConstraint(dependency); |
| 38 | 38 |
| 39 return new Future.value(); | 39 return new Future.value(); |
| 40 }); | 40 }); |
| 41 } | 41 } |
| 42 | 42 |
| 43 /// Warn that dependencies should use the hosted source. | 43 /// Warn that dependencies should use the hosted source. |
| 44 Future _warnAboutSource(PackageRef ref) { | 44 Future _warnAboutSource(PackageDep dep) { |
| 45 return entrypoint.cache.sources['hosted'] | 45 return entrypoint.cache.sources['hosted'] |
| 46 .getVersions(ref.name, ref.name) | 46 .getVersions(dep.name, dep.name) |
| 47 .catchError((e) => <Version>[]) | 47 .catchError((e) => <Version>[]) |
| 48 .then((versions) { | 48 .then((versions) { |
| 49 var constraint; | 49 var constraint; |
| 50 var primary = Version.primary(versions); | 50 var primary = Version.primary(versions); |
| 51 if (primary != null) { | 51 if (primary != null) { |
| 52 constraint = _constraintForVersion(primary); | 52 constraint = _constraintForVersion(primary); |
| 53 } else { | 53 } else { |
| 54 constraint = ref.constraint.toString(); | 54 constraint = dep.constraint.toString(); |
| 55 if (!ref.constraint.isAny && ref.constraint is! Version) { | 55 if (!dep.constraint.isAny && dep.constraint is! Version) { |
| 56 constraint = '"$constraint"'; | 56 constraint = '"$constraint"'; |
| 57 } | 57 } |
| 58 } | 58 } |
| 59 | 59 |
| 60 // Path sources are errors. Other sources are just warnings. | 60 // Path sources are errors. Other sources are just warnings. |
| 61 var messages = warnings; | 61 var messages = warnings; |
| 62 if (ref.source is PathSource) { | 62 if (dep.source is PathSource) { |
| 63 messages = errors; | 63 messages = errors; |
| 64 } | 64 } |
| 65 | 65 |
| 66 messages.add('Don\'t depend on "${ref.name}" from the ${ref.source.name} ' | 66 messages.add('Don\'t depend on "${dep.name}" from the ${dep.source.name} ' |
| 67 'source. Use the hosted source instead. For example:\n' | 67 'source. Use the hosted source instead. For example:\n' |
| 68 '\n' | 68 '\n' |
| 69 'dependencies:\n' | 69 'dependencies:\n' |
| 70 ' ${ref.name}: $constraint\n' | 70 ' ${dep.name}: $constraint\n' |
| 71 '\n' | 71 '\n' |
| 72 'Using the hosted source ensures that everyone can download your ' | 72 'Using the hosted source ensures that everyone can download your ' |
| 73 'package\'s dependencies along with your package.'); | 73 'package\'s dependencies along with your package.'); |
| 74 }); | 74 }); |
| 75 } | 75 } |
| 76 | 76 |
| 77 /// Warn that dependencies should have version constraints. | 77 /// Warn that dependencies should have version constraints. |
| 78 void _warnAboutConstraint(PackageRef ref) { | 78 void _warnAboutConstraint(PackageDep dep) { |
| 79 var lockFile = entrypoint.loadLockFile(); | 79 var lockFile = entrypoint.loadLockFile(); |
| 80 var message = 'Your dependency on "${ref.name}" should have a version ' | 80 var message = 'Your dependency on "${dep.name}" should have a version ' |
| 81 'constraint.'; | 81 'constraint.'; |
| 82 var locked = lockFile.packages[ref.name]; | 82 var locked = lockFile.packages[dep.name]; |
| 83 if (locked != null) { | 83 if (locked != null) { |
| 84 message = '$message For example:\n' | 84 message = '$message For example:\n' |
| 85 '\n' | 85 '\n' |
| 86 'dependencies:\n' | 86 'dependencies:\n' |
| 87 ' ${ref.name}: ${_constraintForVersion(locked.version)}\n'; | 87 ' ${dep.name}: ${_constraintForVersion(locked.version)}\n'; |
| 88 } | 88 } |
| 89 warnings.add("$message\n" | 89 warnings.add("$message\n" |
| 90 "Without a constraint, you're promising to support all future " | 90 "Without a constraint, you're promising to support all future " |
| 91 "versions of ${ref.name}."); | 91 "versions of ${dep.name}."); |
| 92 } | 92 } |
| 93 | 93 |
| 94 /// Returns the suggested version constraint for a dependency that was tested | 94 /// Returns the suggested version constraint for a dependency that was tested |
| 95 /// against [version]. | 95 /// against [version]. |
| 96 String _constraintForVersion(Version version) { | 96 String _constraintForVersion(Version version) { |
| 97 if (version.major != 0) return '">=$version <${version.major + 1}.0.0"'; | 97 if (version.major != 0) return '">=$version <${version.major + 1}.0.0"'; |
| 98 return '">=$version <${version.major}.${version.minor}.' | 98 return '">=$version <${version.major}.${version.minor}.' |
| 99 '${version.patch + 1}"'; | 99 '${version.patch + 1}"'; |
| 100 } | 100 } |
| 101 } | 101 } |
| OLD | NEW |