| 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 version_solver; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:json' as json; | |
| 9 | |
| 10 import '../lock_file.dart'; | |
| 11 import '../log.dart' as log; | |
| 12 import '../package.dart'; | |
| 13 import '../pubspec.dart'; | |
| 14 import '../source.dart'; | |
| 15 import '../source_registry.dart'; | |
| 16 import '../version.dart'; | |
| 17 import 'backtracking_solver.dart'; | |
| 18 | |
| 19 /// Attempts to select the best concrete versions for all of the transitive | |
| 20 /// dependencies of [root] taking into account all of the [VersionConstraint]s | |
| 21 /// that those dependencies place on each other and the requirements imposed by | |
| 22 /// [lockFile]. | |
| 23 /// | |
| 24 /// If [useLatest] is given, then only the latest versions of the referenced | |
| 25 /// packages will be used. This is for forcing an update to one or more | |
| 26 /// packages. | |
| 27 Future<SolveResult> resolveVersions(SourceRegistry sources, Package root, | |
| 28 {LockFile lockFile, List<String> useLatest}) { | |
| 29 log.message('Resolving dependencies...'); | |
| 30 | |
| 31 if (lockFile == null) lockFile = new LockFile.empty(); | |
| 32 if (useLatest == null) useLatest = []; | |
| 33 | |
| 34 return new BacktrackingSolver(sources, root, lockFile, useLatest).solve(); | |
| 35 } | |
| 36 | |
| 37 /// The result of a version resolution. | |
| 38 class SolveResult { | |
| 39 /// Whether the solver found a complete solution or failed. | |
| 40 bool get succeeded => error == null; | |
| 41 | |
| 42 /// The list of concrete package versions that were selected for each package | |
| 43 /// reachable from the root, or `null` if the solver failed. | |
| 44 final List<PackageId> packages; | |
| 45 | |
| 46 /// The error that prevented the solver from finding a solution or `null` if | |
| 47 /// it was successful. | |
| 48 final SolveFailure error; | |
| 49 | |
| 50 /// The number of solutions that were attempted before either finding a | |
| 51 /// successful solution or exhausting all options. In other words, one more | |
| 52 /// than the number of times it had to backtrack because it found an invalid | |
| 53 /// solution. | |
| 54 final int attemptedSolutions; | |
| 55 | |
| 56 SolveResult(this.packages, this.error, this.attemptedSolutions); | |
| 57 | |
| 58 String toString() { | |
| 59 if (!succeeded) { | |
| 60 return 'Failed to solve after $attemptedSolutions attempts:\n' | |
| 61 '$error'; | |
| 62 } | |
| 63 | |
| 64 return 'Took $attemptedSolutions tries to resolve to\n' | |
| 65 '- ${packages.join("\n- ")}'; | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 /// Maintains a cache of previously-requested data: pubspecs and version lists. | |
| 70 /// Used to avoid requesting the same pubspec from the server repeatedly. | |
| 71 class PubspecCache { | |
| 72 final SourceRegistry _sources; | |
| 73 final _versions = new Map<PackageId, List<PackageId>>(); | |
| 74 final _pubspecs = new Map<PackageId, Pubspec>(); | |
| 75 | |
| 76 /// The number of times a version list was requested and it wasn't cached and | |
| 77 /// had to be requested from the source. | |
| 78 int versionCacheMisses = 0; | |
| 79 | |
| 80 /// The number of times a version list was requested and the cached version | |
| 81 /// was returned. | |
| 82 int versionCacheHits = 0; | |
| 83 | |
| 84 /// The number of times a pubspec was requested and it wasn't cached and had | |
| 85 /// to be requested from the source. | |
| 86 int pubspecCacheMisses = 0; | |
| 87 | |
| 88 /// The number of times a pubspec was requested and the cached version was | |
| 89 /// returned. | |
| 90 int pubspecCacheHits = 0; | |
| 91 | |
| 92 PubspecCache(this._sources); | |
| 93 | |
| 94 /// Caches [pubspec] as the [Pubspec] for the package identified by [id]. | |
| 95 void cache(PackageId id, Pubspec pubspec) { | |
| 96 _pubspecs[id] = pubspec; | |
| 97 } | |
| 98 | |
| 99 /// Loads the pubspec for the package identified by [id]. | |
| 100 Future<Pubspec> getPubspec(PackageId id) { | |
| 101 // Complete immediately if it's already cached. | |
| 102 if (_pubspecs.containsKey(id)) { | |
| 103 pubspecCacheHits++; | |
| 104 return new Future<Pubspec>.value(_pubspecs[id]); | |
| 105 } | |
| 106 | |
| 107 pubspecCacheMisses++; | |
| 108 return id.describe().then((pubspec) { | |
| 109 log.solver('requested $id pubspec'); | |
| 110 | |
| 111 // Cache it. | |
| 112 _pubspecs[id] = pubspec; | |
| 113 return pubspec; | |
| 114 }); | |
| 115 } | |
| 116 | |
| 117 /// Returns the previously cached pubspec for the package identified by [id] | |
| 118 /// or returns `null` if not in the cache. | |
| 119 Pubspec getCachedPubspec(PackageId id) => _pubspecs[id]; | |
| 120 | |
| 121 /// Gets the list of versions for [package] in descending order. | |
| 122 Future<List<PackageId>> getVersions(String package, Source source, | |
| 123 description) { | |
| 124 // Create a fake ID to use as a key. | |
| 125 // TODO(rnystrom): Create a separate type for (name, source, description) | |
| 126 // without a version. | |
| 127 var id = new PackageId(package, source, Version.none, description); | |
| 128 | |
| 129 // See if we have it cached. | |
| 130 var versions = _versions[id]; | |
| 131 if (versions != null) { | |
| 132 versionCacheHits++; | |
| 133 return new Future.value(versions); | |
| 134 } | |
| 135 | |
| 136 versionCacheMisses++; | |
| 137 return source.getVersions(package, description).then((versions) { | |
| 138 var ids = versions | |
| 139 .map((version) => new PackageId(package, source, version, | |
| 140 description)) | |
| 141 .toList(); | |
| 142 | |
| 143 // Sort by descending version so we try newer versions first. | |
| 144 ids.sort((a, b) => b.version.compareTo(a.version)); | |
| 145 | |
| 146 log.solver('requested $package version list'); | |
| 147 _versions[id] = ids; | |
| 148 return ids; | |
| 149 }); | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 /// A reference from a depending package to a package that it depends on. | |
| 154 class Dependency { | |
| 155 /// The name of the package that has this dependency. | |
| 156 final String depender; | |
| 157 | |
| 158 /// The referenced dependent package. | |
| 159 final PackageRef ref; | |
| 160 | |
| 161 Dependency(this.depender, this.ref); | |
| 162 | |
| 163 String toString() => '$depender -> $ref'; | |
| 164 } | |
| 165 | |
| 166 /// Base class for all failures that can occur while trying to resolve versions. | |
| 167 class SolveFailure implements Exception { | |
| 168 /// The name of the package whose version could not be solved. Will be `null` | |
| 169 /// if the failure is not specific to one package. | |
| 170 final String package; | |
| 171 | |
| 172 /// The known dependencies on [package] at the time of the failure. Will be | |
| 173 /// an empty collection if the failure is not specific to one package. | |
| 174 final Iterable<Dependency> dependencies; | |
| 175 | |
| 176 SolveFailure(this.package, Iterable<Dependency> dependencies) | |
| 177 : dependencies = dependencies != null ? dependencies : <Dependency>[]; | |
| 178 | |
| 179 /// Writes [dependencies] to [buffer] as a bullet list. If [describe] is | |
| 180 /// passed, it will be called for each dependency and the result will be | |
| 181 /// written next to the dependency. | |
| 182 void writeDependencies(StringBuffer buffer, | |
| 183 [String describe(PackageRef ref)]) { | |
| 184 var map = {}; | |
| 185 for (var dep in dependencies) { | |
| 186 map[dep.depender] = dep.ref; | |
| 187 } | |
| 188 | |
| 189 var names = map.keys.toList(); | |
| 190 names.sort(); | |
| 191 | |
| 192 for (var name in names) { | |
| 193 buffer.writeln("- '$name' "); | |
| 194 if (describe != null) { | |
| 195 buffer.writeln(describe(map[name])); | |
| 196 } else { | |
| 197 buffer.writeln("depends on version ${map[name].constraint}"); | |
| 198 } | |
| 199 } | |
| 200 } | |
| 201 | |
| 202 String toString() { | |
| 203 if (dependencies.isEmpty) return _message; | |
| 204 | |
| 205 var buffer = new StringBuffer(); | |
| 206 buffer.writeln("$_message:"); | |
| 207 | |
| 208 var map = {}; | |
| 209 for (var dep in dependencies) { | |
| 210 map[dep.depender] = dep.ref; | |
| 211 } | |
| 212 | |
| 213 var names = map.keys.toList(); | |
| 214 names.sort(); | |
| 215 | |
| 216 for (var name in names) { | |
| 217 buffer.writeln("- '$name' ${_describeDependency(map[name])}"); | |
| 218 } | |
| 219 | |
| 220 return buffer.toString(); | |
| 221 } | |
| 222 | |
| 223 /// A message describing the specific kind of solve failure. | |
| 224 String get _message; | |
| 225 | |
| 226 /// Describes a dependencie's reference in the output message. Override this | |
| 227 /// to highlight which aspect of [ref] led to the failure. | |
| 228 String _describeDependency(PackageRef ref) => | |
| 229 "depends on version ${ref.constraint}"; | |
| 230 } | |
| 231 | |
| 232 /// Exception thrown when the [VersionSolver] fails to find a solution after a | |
| 233 /// certain number of iterations. | |
| 234 class CouldNotSolveException extends SolveFailure { | |
| 235 CouldNotSolveException([String message]) | |
| 236 : super(null, null), | |
| 237 _message = (message != null) ? message : | |
| 238 "Could not find a solution that met all constraints."; | |
| 239 | |
| 240 /// A message describing the specific kind of solve failure. | |
| 241 final String _message; | |
| 242 } | |
| 243 | |
| 244 /// Exception thrown when the [VersionConstraint] used to match a package is | |
| 245 /// valid (i.e. non-empty), but there are no available versions of the package | |
| 246 /// that fit that constraint. | |
| 247 class NoVersionException extends SolveFailure { | |
| 248 final VersionConstraint constraint; | |
| 249 | |
| 250 NoVersionException(String package, this.constraint, | |
| 251 Iterable<Dependency> dependencies) | |
| 252 : super(package, dependencies); | |
| 253 | |
| 254 String get _message => "Package '$package' has no versions that match " | |
| 255 "$constraint derived from"; | |
| 256 } | |
| 257 | |
| 258 // TODO(rnystrom): Report the list of depending packages and their constraints. | |
| 259 /// Exception thrown when the most recent version of [package] must be selected, | |
| 260 /// but doesn't match the [VersionConstraint] imposed on the package. | |
| 261 class CouldNotUpdateException extends SolveFailure { | |
| 262 final VersionConstraint constraint; | |
| 263 final Version best; | |
| 264 | |
| 265 CouldNotUpdateException(String package, this.constraint, this.best) | |
| 266 : super(package, null); | |
| 267 | |
| 268 String get _message => | |
| 269 "The latest version of '$package', $best, does not match $constraint."; | |
| 270 } | |
| 271 | |
| 272 /// Exception thrown when the [VersionConstraint] used to match a package is | |
| 273 /// the empty set: in other words, multiple packages depend on it and have | |
| 274 /// conflicting constraints that have no overlap. | |
| 275 class DisjointConstraintException extends SolveFailure { | |
| 276 DisjointConstraintException(String package, Iterable<Dependency> dependencies) | |
| 277 : super(package, dependencies); | |
| 278 | |
| 279 String get _message => "Incompatible version constraints on '$package'"; | |
| 280 } | |
| 281 | |
| 282 /// Exception thrown when two packages with the same name but different sources | |
| 283 /// are depended upon. | |
| 284 class SourceMismatchException extends SolveFailure { | |
| 285 | |
| 286 SourceMismatchException(String package, Iterable<Dependency> dependencies) | |
| 287 : super(package, dependencies); | |
| 288 | |
| 289 String get _message => "Incompatible dependencies on '$package'"; | |
| 290 | |
| 291 String _describeDependency(PackageRef ref) => | |
| 292 "depends on it from source ${ref.source}"; | |
| 293 } | |
| 294 | |
| 295 /// Exception thrown when two packages with the same name and source but | |
| 296 /// different descriptions are depended upon. | |
| 297 class DescriptionMismatchException extends SolveFailure { | |
| 298 DescriptionMismatchException(String package, | |
| 299 Iterable<Dependency> dependencies) | |
| 300 : super(package, dependencies); | |
| 301 | |
| 302 String get _message => "Incompatible dependencies on '$package'"; | |
| 303 | |
| 304 String _describeDependency(PackageRef ref) { | |
| 305 // TODO(nweiz): Dump descriptions to YAML when that's supported. | |
| 306 return "depends on it with description ${json.stringify(ref.description)}"; | |
| 307 } | |
| 308 } | |
| OLD | NEW |