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