| 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import "dart:convert"; | 6 import "dart:convert"; |
| 7 | 7 |
| 8 import 'package:pub_semver/pub_semver.dart'; | 8 import 'package:pub_semver/pub_semver.dart'; |
| 9 import 'package:stack_trace/stack_trace.dart'; | 9 import 'package:stack_trace/stack_trace.dart'; |
| 10 | 10 |
| 11 import '../exceptions.dart'; | 11 import '../exceptions.dart'; |
| 12 import '../lock_file.dart'; | 12 import '../lock_file.dart'; |
| 13 import '../log.dart' as log; | 13 import '../log.dart' as log; |
| 14 import '../package.dart'; | 14 import '../package.dart'; |
| 15 import '../pubspec.dart'; | 15 import '../pubspec.dart'; |
| 16 import '../system_cache.dart'; |
| 16 import '../source_registry.dart'; | 17 import '../source_registry.dart'; |
| 17 import '../utils.dart'; | 18 import '../utils.dart'; |
| 18 import 'backtracking_solver.dart'; | 19 import 'backtracking_solver.dart'; |
| 19 import 'solve_report.dart'; | 20 import 'solve_report.dart'; |
| 20 | 21 |
| 21 /// Attempts to select the best concrete versions for all of the transitive | 22 /// Attempts to select the best concrete versions for all of the transitive |
| 22 /// dependencies of [root] taking into account all of the [VersionConstraint]s | 23 /// dependencies of [root] taking into account all of the [VersionConstraint]s |
| 23 /// that those dependencies place on each other and the requirements imposed by | 24 /// that those dependencies place on each other and the requirements imposed by |
| 24 /// [lockFile]. | 25 /// [lockFile]. |
| 25 /// | 26 /// |
| 26 /// If [useLatest] is given, then only the latest versions of the referenced | 27 /// If [useLatest] is given, then only the latest versions of the referenced |
| 27 /// packages will be used. This is for forcing an upgrade to one or more | 28 /// packages will be used. This is for forcing an upgrade to one or more |
| 28 /// packages. | 29 /// packages. |
| 29 /// | 30 /// |
| 30 /// If [upgradeAll] is true, the contents of [lockFile] are ignored. | 31 /// If [upgradeAll] is true, the contents of [lockFile] are ignored. |
| 31 Future<SolveResult> resolveVersions(SolveType type, SourceRegistry sources, | 32 Future<SolveResult> resolveVersions(SolveType type, SystemCache cache, |
| 32 Package root, {LockFile lockFile, List<String> useLatest}) { | 33 Package root, {LockFile lockFile, List<String> useLatest}) { |
| 33 if (lockFile == null) lockFile = new LockFile.empty(sources); | 34 if (lockFile == null) lockFile = new LockFile.empty(cache.sources); |
| 34 if (useLatest == null) useLatest = []; | 35 if (useLatest == null) useLatest = []; |
| 35 | 36 |
| 36 return log.progress('Resolving dependencies', () { | 37 return log.progress('Resolving dependencies', () { |
| 37 return new BacktrackingSolver(type, sources, root, lockFile, useLatest) | 38 return new BacktrackingSolver(type, cache, root, lockFile, useLatest) |
| 38 .solve(); | 39 .solve(); |
| 39 }); | 40 }); |
| 40 } | 41 } |
| 41 | 42 |
| 42 /// The result of a version resolution. | 43 /// The result of a version resolution. |
| 43 class SolveResult { | 44 class SolveResult { |
| 44 /// Whether the solver found a complete solution or failed. | 45 /// Whether the solver found a complete solution or failed. |
| 45 bool get succeeded => error == null; | 46 bool get succeeded => error == null; |
| 46 | 47 |
| 47 /// The list of concrete package versions that were selected for each package | 48 /// The list of concrete package versions that were selected for each package |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 '$error'; | 139 '$error'; |
| 139 } | 140 } |
| 140 | 141 |
| 141 return 'Took $attemptedSolutions tries to resolve to\n' | 142 return 'Took $attemptedSolutions tries to resolve to\n' |
| 142 '- ${packages.join("\n- ")}'; | 143 '- ${packages.join("\n- ")}'; |
| 143 } | 144 } |
| 144 } | 145 } |
| 145 | 146 |
| 146 /// Maintains a cache of previously-requested version lists. | 147 /// Maintains a cache of previously-requested version lists. |
| 147 class SolverCache { | 148 class SolverCache { |
| 148 final SourceRegistry _sources; | 149 final SystemCache _cache; |
| 149 | 150 |
| 150 /// The already-requested cached version lists. | 151 /// The already-requested cached version lists. |
| 151 final _versions = new Map<PackageRef, List<PackageId>>(); | 152 final _versions = new Map<PackageRef, List<PackageId>>(); |
| 152 | 153 |
| 153 /// The errors from failed version list requests. | 154 /// The errors from failed version list requests. |
| 154 final _versionErrors = new Map<PackageRef, Pair<Object, Chain>>(); | 155 final _versionErrors = new Map<PackageRef, Pair<Object, Chain>>(); |
| 155 | 156 |
| 156 /// The type of version resolution that was run. | 157 /// The type of version resolution that was run. |
| 157 final SolveType _type; | 158 final SolveType _type; |
| 158 | 159 |
| 159 /// The number of times a version list was requested and it wasn't cached and | 160 /// The number of times a version list was requested and it wasn't cached and |
| 160 /// had to be requested from the source. | 161 /// had to be requested from the source. |
| 161 int _versionCacheMisses = 0; | 162 int _versionCacheMisses = 0; |
| 162 | 163 |
| 163 /// The number of times a version list was requested and the cached version | 164 /// The number of times a version list was requested and the cached version |
| 164 /// was returned. | 165 /// was returned. |
| 165 int _versionCacheHits = 0; | 166 int _versionCacheHits = 0; |
| 166 | 167 |
| 167 SolverCache(this._type, this._sources); | 168 SolverCache(this._type, this._cache); |
| 168 | 169 |
| 169 /// Gets the list of versions for [package]. | 170 /// Gets the list of versions for [package]. |
| 170 /// | 171 /// |
| 171 /// Packages are sorted in descending version order with all "stable" | 172 /// Packages are sorted in descending version order with all "stable" |
| 172 /// versions (i.e. ones without a prerelease suffix) before pre-release | 173 /// versions (i.e. ones without a prerelease suffix) before pre-release |
| 173 /// versions. This ensures that the solver prefers stable packages over | 174 /// versions. This ensures that the solver prefers stable packages over |
| 174 /// unstable ones. | 175 /// unstable ones. |
| 175 Future<List<PackageId>> getVersions(PackageRef package) async { | 176 Future<List<PackageId>> getVersions(PackageRef package) async { |
| 176 if (package.isRoot) { | 177 if (package.isRoot) { |
| 177 throw new StateError("Cannot get versions for root package $package."); | 178 throw new StateError("Cannot get versions for root package $package."); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 188 | 189 |
| 189 // See if we cached a failure. | 190 // See if we cached a failure. |
| 190 var error = _versionErrors[package]; | 191 var error = _versionErrors[package]; |
| 191 if (error != null) { | 192 if (error != null) { |
| 192 _versionCacheHits++; | 193 _versionCacheHits++; |
| 193 await new Future.error(error.first, error.last); | 194 await new Future.error(error.first, error.last); |
| 194 } | 195 } |
| 195 | 196 |
| 196 _versionCacheMisses++; | 197 _versionCacheMisses++; |
| 197 | 198 |
| 198 var source = _sources[package.source]; | 199 var source = _cache.liveSource(package.source); |
| 199 var ids; | 200 var ids; |
| 200 try { | 201 try { |
| 201 ids = await source.getVersions(package); | 202 ids = await source.getVersions(package); |
| 202 } catch (error, stackTrace) { | 203 } catch (error, stackTrace) { |
| 203 // If an error occurs, cache that too. We only want to do one request | 204 // If an error occurs, cache that too. We only want to do one request |
| 204 // for any given package, successful or not. | 205 // for any given package, successful or not. |
| 205 var chain = new Chain.forTrace(stackTrace); | 206 var chain = new Chain.forTrace(stackTrace); |
| 206 log.solver("Could not get versions for $package:\n$error\n\n" + | 207 log.solver("Could not get versions for $package:\n$error\n\n" + |
| 207 chain.terse.toString()); | 208 chain.terse.toString()); |
| 208 _versionErrors[package] = new Pair(error, chain); | 209 _versionErrors[package] = new Pair(error, chain); |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 | 437 |
| 437 DependencyNotFoundException(String package, this._innerException, | 438 DependencyNotFoundException(String package, this._innerException, |
| 438 Iterable<Dependency> dependencies) | 439 Iterable<Dependency> dependencies) |
| 439 : super(package, dependencies); | 440 : super(package, dependencies); |
| 440 | 441 |
| 441 /// The failure isn't because of the version of description of the package, | 442 /// The failure isn't because of the version of description of the package, |
| 442 /// it's the package itself that can't be found, so just show the name and no | 443 /// it's the package itself that can't be found, so just show the name and no |
| 443 /// descriptive details. | 444 /// descriptive details. |
| 444 String _describeDependency(PackageDep dep) => ""; | 445 String _describeDependency(PackageDep dep) => ""; |
| 445 } | 446 } |
| OLD | NEW |