Chromium Code Reviews| 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 /** | |
| 6 * The system-wide cache of installed packages. | |
| 7 * | |
| 8 * This cache contains all packages that are downloaded from the internet. | |
| 9 * Packages that are available locally (e.g. from the SDK) don't use this cache. | |
| 10 */ | |
| 11 class SystemCache { | |
| 12 /** | |
| 13 * The root directory where this package cache is located. | |
| 14 */ | |
| 15 final String rootDir; | |
| 16 | |
| 17 /** | |
| 18 * Packages which are currently being asynchronously installed to the cache. | |
| 19 */ | |
| 20 final Map<PackageId, Future<Package>> _pendingInstalls; | |
| 21 | |
| 22 /** | |
| 23 * Creates a new package cache which is backed by the given directory on the | |
| 24 * user's file system. | |
| 25 */ | |
| 26 SystemCache(this.rootDir) | |
| 27 : _pendingInstalls = new Map<PackageId, Future<Package>>(); | |
| 28 | |
| 29 /** | |
| 30 * Loads all of the package ids in the cache and returns them. | |
| 31 */ | |
| 32 Future<List<PackageId>> listAll() { | |
| 33 return listDir(rootDir).chain((paths) { | |
| 34 final sources = paths.map((path) { | |
| 35 final source = Source.fromName(basename(path)); | |
| 36 return listDir(path).transform((subpaths) { | |
| 37 return subpaths.map((subpath) => | |
| 38 new PackageId(basename(subpath), source)); | |
| 39 }); | |
| 40 }); | |
| 41 return Futures.wait(sources).transform(flatten); | |
| 42 }); | |
| 43 } | |
| 44 | |
| 45 /** | |
| 46 * Ensures that the package identified by [id] is installed to the cache, | |
| 47 * loads it, and returns it. | |
| 48 * | |
| 49 * It is an error to try installing a package from a source with `shouldCache | |
| 50 * == false` to the system cache. | |
| 51 */ | |
| 52 Future<Package> install(PackageId id) { | |
| 53 if (!id.source.shouldCache) throw "[PUB BUG] Package $id is not cacheable."; | |
|
Bob Nystrom
2012/05/03 00:15:49
Throw an IllegalArgumentException here and remove
nweiz
2012/05/04 01:03:43
Done.
| |
| 54 | |
|
Bob Nystrom
2012/05/03 00:15:49
Should this have a cache of already-loaded-into-me
nweiz
2012/05/04 01:03:43
I don't think so. Most if not all of the actual Pa
| |
| 55 var pending = _pendingInstalls[id]; | |
| 56 if (pending != null) return pending; | |
| 57 | |
| 58 var path = join(rootDir, id.source.name, id.source.packageName(id)); | |
| 59 var future = exists(path).chain((exists) { | |
| 60 if (exists) throw 'Package ${id.name} is already installed.'; | |
|
Bob Nystrom
2012/05/03 00:15:49
Add a TODO for better error-handling. Anytime we'r
nweiz
2012/05/04 01:03:43
Done.
| |
| 61 return ensureNestedDir(dirname(path)); | |
| 62 }).chain((_) { | |
| 63 return id.source.install(id, path); | |
| 64 }).chain((found) { | |
| 65 if (!found) { | |
| 66 throw 'Package ${id.name} not found in source "${id.source.name}".'; | |
| 67 } | |
| 68 return Package.load(path); | |
| 69 }); | |
| 70 | |
| 71 always(future, () => _pendingInstalls.remove(id)); | |
| 72 _pendingInstalls[id] = future; | |
| 73 return future; | |
| 74 } | |
| 75 } | |
| OLD | NEW |