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