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 import 'greedy_solver.dart'; | |
19 | |
20 /// Attempts to select the best concrete versions for all of the transitive | |
21 /// dependencies of [root] taking into account all of the [VersionConstraint]s | |
22 /// that those dependencies place on each other and the requirements imposed by | |
23 /// [lockFile]. If successful, completes to a list of selected [PackageId]s. If | |
24 /// it fails, the future will complete with a [SolverFailure]. | |
25 /// | |
26 /// If [useLatest] is given, then only the latest versions of the referenced | |
27 /// packages will be used. This is for forcing an update to one or more | |
28 /// packages. | |
29 /// | |
30 /// If [allowBacktracking] is `true` (or omitted), the backtracking version | |
31 /// solver will be used. Otherwise, the non-backtracking one will be. | |
32 Future<SolveResult> resolveVersions(SourceRegistry sources, Package root, | |
33 {LockFile lockFile, bool allowBacktracking, List<PackageRef> useLatest}) { | |
34 log.message('Resolving dependencies...'); | |
35 | |
36 if (allowBacktracking == null) allowBacktracking = true; | |
37 if (lockFile == null) lockFile = new LockFile.empty(); | |
38 if (useLatest == null) useLatest = []; | |
39 | |
40 var solver; | |
41 if (allowBacktracking) { | |
42 solver = new BacktrackingVersionSolver(sources, root, lockFile, useLatest); | |
43 } else { | |
44 solver = new GreedyVersionSolver(sources, root, lockFile, useLatest); | |
45 } | |
46 | |
47 return solver.solve(); | |
48 } | |
49 | |
50 /// Base class for an implementation of the version constraint solver. | |
51 class VersionSolver { | |
52 final SourceRegistry sources; | |
53 final Package root; | |
54 final LockFile lockFile; | |
55 final PubspecCache cache; | |
56 | |
57 VersionSolver(SourceRegistry sources, this.root, this.lockFile, | |
58 List<String> useLatest) | |
59 : sources = sources, | |
60 cache = new PubspecCache(sources) { | |
61 for (var package in useLatest) { | |
62 forceLatestVersion(package); | |
63 lockFile.packages.remove(package); | |
64 } | |
65 } | |
66 | |
67 /// Force the solver to upgrade [package] to the latest available version. | |
68 void forceLatestVersion(String package); | |
69 | |
70 /// Run the solver. Completes with a list of specific package versions if | |
71 /// successful or an error if it failed to find a solution. | |
72 Future<SolveResult> solve() { | |
73 var stopwatch = new Stopwatch(); | |
74 stopwatch.start(); | |
75 | |
76 // Pre-cache the root package's known pubspec. | |
77 cache.cache(new PackageId.root(root), root.pubspec); | |
78 | |
79 // Gather some solving metrics. | |
80 return runSolver().whenComplete(() { | |
81 var buffer = new StringBuffer(); | |
82 buffer.writeln('${runtimeType} took ${stopwatch.elapsed} seconds.'); | |
83 buffer.writeln( | |
84 '- Requested ${cache.versionCacheMisses} version lists'); | |
85 buffer.writeln( | |
86 '- Looked up ${cache.versionCacheHits} cached version lists'); | |
87 buffer.writeln( | |
88 '- Requested ${cache.pubspecCacheMisses} pubspecs'); | |
89 buffer.writeln( | |
90 '- Looked up ${cache.pubspecCacheHits} cached pubspecs'); | |
91 log.solver(buffer); | |
92 }); | |
93 } | |
94 | |
95 /// Entrypoint for subclasses to actually begin solving. External code should | |
96 /// call [solve()]. | |
97 Future<SolveResult> runSolver(); | |
98 } | |
99 | |
100 class SolveResult { | |
nweiz
2013/04/03 00:28:43
Document this.
Bob Nystrom
2013/04/08 22:13:00
Done.
| |
101 final List<PackageId> packages; | |
102 final int attemptedSolutions; | |
103 | |
104 SolveResult(this.packages, this.attemptedSolutions); | |
105 | |
106 String toString() => | |
107 'Took $attemptedSolutions tries to resolve to\n' | |
108 '- ${packages.join("\n- ")}'; | |
109 } | |
110 | |
111 /// Maintains a cache of previously-requested data: pubspecs and version lists. | |
112 /// Used to avoid requesting the same pubspec from the server repeatedly. | |
113 class PubspecCache { | |
114 final SourceRegistry _sources; | |
115 final _versions = new Map<PackageId, List<PackageId>>(); | |
116 final _pubspecs = new Map<PackageId, Pubspec>(); | |
117 | |
118 /// The number of times a version list was requested and it wasn't cached and | |
119 /// had to be requested from the source. | |
120 int versionCacheMisses = 0; | |
121 | |
122 /// The number of times a version list was requested and the cached version | |
123 /// was returned. | |
124 int versionCacheHits = 0; | |
125 | |
126 /// The number of times a pubspec was requested and it wasn't cached and had | |
127 /// to be requested from the source. | |
128 int pubspecCacheMisses = 0; | |
129 | |
130 /// The number of times a pubspec was requested and the cached version was | |
131 /// returned. | |
132 int pubspecCacheHits = 0; | |
133 | |
134 PubspecCache(this._sources); | |
135 | |
136 /// Caches [pubspec] as the [Pubspec] for the package identified by [id]. | |
137 void cache(PackageId id, Pubspec pubspec) { | |
138 _pubspecs[id] = pubspec; | |
139 } | |
140 | |
141 /// Loads the pubspec for the package identified by [id]. | |
142 Future<Pubspec> getPubspec(PackageId id) { | |
143 // Complete immediately if it's already cached. | |
144 if (_pubspecs.containsKey(id)) { | |
145 pubspecCacheHits++; | |
146 return new Future<Pubspec>.immediate(_pubspecs[id]); | |
147 } | |
148 | |
149 pubspecCacheMisses++; | |
150 return id.describe().then((pubspec) { | |
151 log.solver('requested $id pubspec'); | |
152 | |
153 // Cache it. | |
154 _pubspecs[id] = pubspec; | |
155 return pubspec; | |
156 }); | |
157 } | |
158 | |
159 /// Gets the list of versions for [package] in descending order. | |
160 Future<List<PackageId>> getVersions(String package, Source source, | |
161 description) { | |
162 // Create a fake ID to use as a key. | |
163 // TODO(rnystrom): Create a separate type for (name, source, description) | |
164 // without a version. | |
165 var id = new PackageId(package, source, Version.none, description); | |
166 | |
167 // See if we have it cached. | |
168 var versions = _versions[id]; | |
169 if (versions != null) { | |
170 versionCacheHits++; | |
171 return new Future.immediate(versions); | |
172 } | |
173 | |
174 versionCacheMisses++; | |
175 return source.getVersions(package, description).then((versions) { | |
176 var ids = versions | |
177 .map((version) => new PackageId(package, source, version, | |
178 description)) | |
179 .toList(); | |
180 | |
181 // Sort by descending version so we try newer versions first. | |
182 ids.sort((a, b) => b.version.compareTo(a.version)); | |
183 | |
184 log.solver('requested $package version list'); | |
185 _versions[id] = ids; | |
186 return ids; | |
187 }); | |
188 } | |
189 } | |
190 | |
191 /// A reference from a depending package to a package that it depends on. | |
192 class Dependency { | |
193 /// The name of the package that has this dependency. | |
194 final String depender; | |
195 | |
196 /// The referenced dependent package. | |
197 final PackageRef ref; | |
198 | |
199 Dependency(this.depender, this.ref); | |
200 } | |
201 | |
202 /// Base class for all failures that can occur while trying to resolve versions. | |
203 class SolverFailure implements Exception { | |
204 /// Writes [deps] to [buffer] as a bullet list. | |
205 void _writeDependencies(StringBuffer buffer, List<Dependency> deps) { | |
206 var map = {}; | |
207 for (var dep in deps) { | |
208 map[dep.depender] = dep.ref; | |
209 } | |
210 | |
211 var names = map.keys.toList(); | |
212 names.sort(); | |
213 | |
214 for (var name in names) { | |
215 buffer.writeln("- '$name' depends on version ${map[name].constraint}"); | |
216 } | |
217 } | |
218 } | |
219 | |
220 /// Exception thrown when the [VersionSolver] fails to find a solution after a | |
221 /// certain number of iterations. | |
222 class CouldNotSolveException extends SolverFailure { | |
223 CouldNotSolveException(); | |
224 | |
225 String toString() => | |
226 "Could not find a solution that met all version constraints."; | |
227 } | |
228 | |
229 /// Exception thrown when the [VersionConstraint] used to match a package is | |
230 /// valid (i.e. non-empty), but there are no available versions of the package | |
231 /// that fit that constraint. | |
232 class NoVersionException extends SolverFailure { | |
233 final String package; | |
234 final VersionConstraint constraint; | |
235 final List<Dependency> _dependencies; | |
236 | |
237 NoVersionException(this.package, this.constraint, this._dependencies); | |
238 | |
239 String toString() { | |
240 var buffer = new StringBuffer(); | |
241 buffer.write("Package '$package' has no versions that match $constraint " | |
242 "derived from:\n"); | |
243 _writeDependencies(buffer, _dependencies); | |
244 return buffer.toString(); | |
245 } | |
246 } | |
247 | |
248 // TODO(rnystrom): Report the list of depending packages and their constraints. | |
249 /// Exception thrown when the most recent version of [package] must be selected, | |
250 /// but doesn't match the [VersionConstraint] imposed on the package. | |
251 class CouldNotUpdateException extends SolverFailure { | |
252 final String package; | |
253 final VersionConstraint constraint; | |
254 final Version best; | |
255 | |
256 CouldNotUpdateException(this.package, this.constraint, this.best); | |
257 | |
258 String toString() => | |
259 "The latest version of '$package', $best, does not match $constraint."; | |
260 } | |
261 | |
262 /// Exception thrown when the [VersionConstraint] used to match a package is | |
263 /// the empty set: in other words, multiple packages depend on it and have | |
264 /// conflicting constraints that have no overlap. | |
265 class DisjointConstraintException extends SolverFailure { | |
266 final String package; | |
267 final List<Dependency> _dependencies; | |
268 | |
269 DisjointConstraintException(this.package, this._dependencies); | |
270 | |
271 String toString() { | |
272 var buffer = new StringBuffer(); | |
273 buffer.write("Incompatible version constraints on '$package':\n"); | |
274 _writeDependencies(buffer, _dependencies); | |
275 return buffer.toString(); | |
276 } | |
277 } | |
278 | |
279 /// Exception thrown when two packages with the same name but different sources | |
280 /// are depended upon. | |
281 class SourceMismatchException extends SolverFailure { | |
282 final String package; | |
283 final String depender1; | |
284 final Source source1; | |
285 final String depender2; | |
286 final Source source2; | |
287 | |
288 SourceMismatchException(this.package, this.depender1, this.source1, | |
289 this.depender2, this.source2); | |
290 | |
291 String toString() { | |
292 return "Incompatible dependencies on '$package':\n" | |
293 "- '$depender1' depends on it from source '$source1'\n" | |
294 "- '$depender2' depends on it from source '$source2'"; | |
295 } | |
296 } | |
297 | |
298 /// Exception thrown when two packages with the same name and source but | |
299 /// different descriptions are depended upon. | |
300 class DescriptionMismatchException extends SolverFailure { | |
301 final String package; | |
302 final String depender1; | |
303 final description1; | |
304 final String depender2; | |
305 final description2; | |
306 | |
307 DescriptionMismatchException(this.package, this.depender1, this.description1, | |
308 this.depender2, this.description2); | |
309 | |
310 String toString() { | |
311 // TODO(nweiz): Dump descriptions to YAML when that's supported. | |
312 return "Incompatible dependencies on '$package':\n" | |
313 "- '$depender1' depends on it with description " | |
314 "${json.stringify(description1)}\n" | |
315 "- '$depender2' depends on it with description " | |
316 "${json.stringify(description2)}"; | |
317 } | |
318 } | |
OLD | NEW |