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 /** | 5 /** |
6 * Attempts to resolve a set of version constraints for a package dependency | 6 * Attempts to resolve a set of version constraints for a package dependency |
7 * graph and select an appropriate set of best specific versions for all | 7 * graph and select an appropriate set of best specific versions for all |
8 * dependent packages. It works iteratively and tries to reach a stable | 8 * dependent packages. It works iteratively and tries to reach a stable |
9 * solution where the constraints of all dependencies are met. If it fails to | 9 * solution where the constraints of all dependencies are met. If it fails to |
10 * reach a solution after a certain number of iterations, it assumes the | 10 * reach a solution after a certain number of iterations, it assumes the |
(...skipping 22 matching lines...) Expand all Loading... |
33 * picked version for that package may fall outside of the new constraint. If | 33 * picked version for that package may fall outside of the new constraint. If |
34 * that happens, we find the new best version that meets the updated constraint | 34 * that happens, we find the new best version that meets the updated constraint |
35 * and then the change the package to use that version. That cycles back up to | 35 * and then the change the package to use that version. That cycles back up to |
36 * the beginning again. | 36 * the beginning again. |
37 */ | 37 */ |
38 library version_solver; | 38 library version_solver; |
39 | 39 |
40 import 'dart:json'; | 40 import 'dart:json'; |
41 import 'dart:math'; | 41 import 'dart:math'; |
42 import 'lock_file.dart'; | 42 import 'lock_file.dart'; |
| 43 import 'log.dart' as log; |
43 import 'package.dart'; | 44 import 'package.dart'; |
44 import 'pubspec.dart'; | 45 import 'pubspec.dart'; |
45 import 'root_source.dart'; | 46 import 'root_source.dart'; |
46 import 'source.dart'; | 47 import 'source.dart'; |
47 import 'source_registry.dart'; | 48 import 'source_registry.dart'; |
48 import 'utils.dart'; | 49 import 'utils.dart'; |
49 import 'version.dart'; | 50 import 'version.dart'; |
50 | 51 |
51 /** | 52 /** |
52 * Attempts to select the best concrete versions for all of the transitive | 53 * Attempts to select the best concrete versions for all of the transitive |
53 * dependencies of [root] taking into account all of the [VersionConstraint]s | 54 * dependencies of [root] taking into account all of the [VersionConstraint]s |
54 * that those dependencies place on each other and the requirements imposed by | 55 * that those dependencies place on each other and the requirements imposed by |
55 * [lockFile]. If successful, completes to a [Map] that maps package names to | 56 * [lockFile]. If successful, completes to a [Map] that maps package names to |
56 * the selected version for that package. If it fails, the future will complete | 57 * the selected version for that package. If it fails, the future will complete |
57 * with a [NoVersionException], [DisjointConstraintException], or | 58 * with a [NoVersionException], [DisjointConstraintException], or |
58 * [CouldNotSolveException]. | 59 * [CouldNotSolveException]. |
59 */ | 60 */ |
60 Future<List<PackageId>> resolveVersions(SourceRegistry sources, Package root, | 61 Future<List<PackageId>> resolveVersions(SourceRegistry sources, Package root, |
61 LockFile lockFile) { | 62 LockFile lockFile) { |
62 print('Resolving dependencies...'); | 63 log.message('Resolving dependencies...'); |
63 return new VersionSolver(sources, root, lockFile).solve(); | 64 return new VersionSolver(sources, root, lockFile).solve(); |
64 } | 65 } |
65 | 66 |
66 class VersionSolver { | 67 class VersionSolver { |
67 final SourceRegistry _sources; | 68 final SourceRegistry _sources; |
68 final Package _root; | 69 final Package _root; |
69 final LockFile lockFile; | 70 final LockFile lockFile; |
70 final PubspecCache _pubspecs; | 71 final PubspecCache _pubspecs; |
71 final Map<String, Dependency> _packages; | 72 final Map<String, Dependency> _packages; |
72 final Queue<WorkItem> _work; | 73 final Queue<WorkItem> _work; |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 /** | 261 /** |
261 * The new selected version. | 262 * The new selected version. |
262 */ | 263 */ |
263 final Version version; | 264 final Version version; |
264 | 265 |
265 ChangeVersion(this.package, this.source, this.description, this.version) { | 266 ChangeVersion(this.package, this.source, this.description, this.version) { |
266 if (source == null) throw "null source"; | 267 if (source == null) throw "null source"; |
267 } | 268 } |
268 | 269 |
269 Future process(VersionSolver solver) { | 270 Future process(VersionSolver solver) { |
| 271 log.fine("Changing $package to version $version."); |
| 272 |
270 var dependency = solver.getDependency(package); | 273 var dependency = solver.getDependency(package); |
271 var oldVersion = dependency.version; | 274 var oldVersion = dependency.version; |
272 solver.setVersion(package, version); | 275 solver.setVersion(package, version); |
273 | 276 |
274 // The dependencies between the old and new version may be different. Walk | 277 // The dependencies between the old and new version may be different. Walk |
275 // them both and update any constraints that differ between the two. | 278 // them both and update any constraints that differ between the two. |
276 return Futures.wait([ | 279 return Futures.wait([ |
277 getDependencyRefs(solver, oldVersion), | 280 getDependencyRefs(solver, oldVersion), |
278 getDependencyRefs(solver, version)]).transform((list) { | 281 getDependencyRefs(solver, version)]).transform((list) { |
279 var oldDependencyRefs = list[0]; | 282 var oldDependencyRefs = list[0]; |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 /** | 414 /** |
412 * The package being depended on and the constraints being placed on it. The | 415 * The package being depended on and the constraints being placed on it. The |
413 * source, version, and description in this ref are all considered constraints | 416 * source, version, and description in this ref are all considered constraints |
414 * on the dependent package. | 417 * on the dependent package. |
415 */ | 418 */ |
416 final PackageRef ref; | 419 final PackageRef ref; |
417 | 420 |
418 AddConstraint(this.depender, this.ref); | 421 AddConstraint(this.depender, this.ref); |
419 | 422 |
420 Future process(VersionSolver solver) { | 423 Future process(VersionSolver solver) { |
| 424 log.fine("Adding $depender's constraint $ref."); |
| 425 |
421 var dependency = solver.getDependency(ref.name); | 426 var dependency = solver.getDependency(ref.name); |
422 var oldDependency = dependency.clone(); | 427 var oldDependency = dependency.clone(); |
423 dependency.placeConstraint(depender, ref); | 428 dependency.placeConstraint(depender, ref); |
424 return _processChange(solver, oldDependency, dependency); | 429 return _processChange(solver, oldDependency, dependency); |
425 } | 430 } |
426 | 431 |
427 void undo(VersionSolver solver) { | 432 void undo(VersionSolver solver) { |
428 solver.getDependency(ref.name).removeConstraint(depender); | 433 solver.getDependency(ref.name).removeConstraint(depender); |
429 } | 434 } |
430 } | 435 } |
(...skipping 11 matching lines...) Expand all Loading... |
442 * The package that was being depended on. | 447 * The package that was being depended on. |
443 */ | 448 */ |
444 String dependent; | 449 String dependent; |
445 | 450 |
446 /** The constraint that was removed. */ | 451 /** The constraint that was removed. */ |
447 PackageRef _removed; | 452 PackageRef _removed; |
448 | 453 |
449 RemoveConstraint(this.depender, this.dependent); | 454 RemoveConstraint(this.depender, this.dependent); |
450 | 455 |
451 Future process(VersionSolver solver) { | 456 Future process(VersionSolver solver) { |
| 457 log.fine("Removing $depender's constraint ($_removed) on $dependent."); |
| 458 |
452 var dependency = solver.getDependency(dependent); | 459 var dependency = solver.getDependency(dependent); |
453 var oldDependency = dependency.clone(); | 460 var oldDependency = dependency.clone(); |
454 _removed = dependency.removeConstraint(depender); | 461 _removed = dependency.removeConstraint(depender); |
455 return _processChange(solver, oldDependency, dependency); | 462 return _processChange(solver, oldDependency, dependency); |
456 } | 463 } |
457 | 464 |
458 void undo(VersionSolver solver) { | 465 void undo(VersionSolver solver) { |
459 solver.getDependency(dependent).placeConstraint(depender, _removed); | 466 solver.getDependency(dependent).placeConstraint(depender, _removed); |
460 } | 467 } |
461 } | 468 } |
462 | 469 |
463 /** [package]'s version is no longer constrained by the lockfile. */ | 470 /** [package]'s version is no longer constrained by the lockfile. */ |
464 class UnlockPackage implements WorkItem { | 471 class UnlockPackage implements WorkItem { |
465 /** The package being unlocked. */ | 472 /** The package being unlocked. */ |
466 Dependency package; | 473 Dependency package; |
467 | 474 |
468 UnlockPackage(this.package); | 475 UnlockPackage(this.package); |
469 | 476 |
470 Future process(VersionSolver solver) { | 477 Future process(VersionSolver solver) { |
| 478 log.fine("Unlocking ${package.name}."); |
| 479 |
471 solver.lockFile.packages.remove(package.name); | 480 solver.lockFile.packages.remove(package.name); |
472 return solver.getBestVersion(package).transform((best) { | 481 return solver.getBestVersion(package).transform((best) { |
473 if (best == null) return null; | 482 if (best == null) return null; |
474 solver.enqueue(new ChangeVersion( | 483 solver.enqueue(new ChangeVersion( |
475 package.name, package.source, package.description, best)); | 484 package.name, package.source, package.description, best)); |
476 }); | 485 }); |
477 } | 486 } |
478 } | 487 } |
479 | 488 |
480 // TODO(rnystrom): Instead of always pulling from the source (which will mean | 489 // TODO(rnystrom): Instead of always pulling from the source (which will mean |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
765 | 774 |
766 String toString() { | 775 String toString() { |
767 // TODO(nweiz): Dump descriptions to YAML when that's supported. | 776 // TODO(nweiz): Dump descriptions to YAML when that's supported. |
768 return "Incompatible dependencies on '$package':\n" | 777 return "Incompatible dependencies on '$package':\n" |
769 "- '$depender1' depends on it with description " | 778 "- '$depender1' depends on it with description " |
770 "${JSON.stringify(description1)}\n" | 779 "${JSON.stringify(description1)}\n" |
771 "- '$depender2' depends on it with description " | 780 "- '$depender2' depends on it with description " |
772 "${JSON.stringify(description2)}"; | 781 "${JSON.stringify(description2)}"; |
773 } | 782 } |
774 } | 783 } |
OLD | NEW |