| 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) { |
| 54 throw new IllegalArgumentException("Package $id is not cacheable."); |
| 55 } |
| 56 |
| 57 var pending = _pendingInstalls[id]; |
| 58 if (pending != null) return pending; |
| 59 |
| 60 var path = join(rootDir, id.source.name, id.source.packageName(id)); |
| 61 var future = exists(path).chain((exists) { |
| 62 // TODO(nweiz): better error handling |
| 63 if (exists) throw 'Package $id is already installed.'; |
| 64 return ensureDir(dirname(path)); |
| 65 }).chain((_) { |
| 66 return id.source.install(id, path); |
| 67 }).chain((found) { |
| 68 if (!found) { |
| 69 throw 'Package ${id.fullName} not found in source "${id.source.name}".'; |
| 70 } |
| 71 return Package.load(path); |
| 72 }); |
| 73 |
| 74 always(future, () => _pendingInstalls.remove(id)); |
| 75 _pendingInstalls[id] = future; |
| 76 return future; |
| 77 } |
| 78 } |
| OLD | NEW |