| 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 * The system-wide cache of installed packages. | 6 * The system-wide cache of installed packages. |
| 7 * | 7 * |
| 8 * This cache contains all packages that are downloaded from the internet. | 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. | 9 * Packages that are available locally (e.g. from the SDK) don't use this cache. |
| 10 */ | 10 */ |
| 11 class SystemCache { | 11 class SystemCache { |
| 12 /** | 12 /** |
| 13 * The root directory where this package cache is located. | 13 * The root directory where this package cache is located. |
| 14 */ | 14 */ |
| 15 final String rootDir; | 15 final String rootDir; |
| 16 | 16 |
| 17 /** | 17 /** |
| 18 * Packages which are currently being asynchronously installed to the cache. | 18 * Packages which are currently being asynchronously installed to the cache. |
| 19 */ | 19 */ |
| 20 final Map<PackageId, Future<Package>> _pendingInstalls; | 20 final Map<PackageId, Future<Package>> _pendingInstalls; |
| 21 | 21 |
| 22 /** | 22 /** |
| 23 * The sources from which to install packages. |
| 24 */ |
| 25 final SourceRegistry sources; |
| 26 |
| 27 /** |
| 23 * Creates a new package cache which is backed by the given directory on the | 28 * Creates a new package cache which is backed by the given directory on the |
| 24 * user's file system. | 29 * user's file system. |
| 25 */ | 30 */ |
| 26 SystemCache(this.rootDir) | 31 SystemCache(this.rootDir) |
| 27 : _pendingInstalls = new Map<PackageId, Future<Package>>(); | 32 : _pendingInstalls = new Map<PackageId, Future<Package>>(), |
| 33 sources = new SourceRegistry(); |
| 28 | 34 |
| 29 /** | 35 /** |
| 30 * Loads all of the package ids in the cache and returns them. | 36 * Loads all of the package ids in the cache and returns them. |
| 31 */ | 37 */ |
| 32 Future<List<PackageId>> listAll() { | 38 Future<List<PackageId>> listAll() { |
| 33 return listDir(rootDir).chain((paths) { | 39 return listDir(rootDir).chain((paths) { |
| 34 final sources = paths.map((path) { | 40 final sources = paths.map((path) { |
| 35 final source = Source.fromName(basename(path)); | 41 final source = sources[basename(path)]; |
| 36 return listDir(path).transform((subpaths) { | 42 return listDir(path).transform((subpaths) { |
| 37 return subpaths.map((subpath) => | 43 return subpaths.map((subpath) => |
| 38 new PackageId(basename(subpath), source)); | 44 new PackageId(basename(subpath), source)); |
| 39 }); | 45 }); |
| 40 }); | 46 }); |
| 41 return Futures.wait(sources).transform(flatten); | 47 return Futures.wait(sources).transform(flatten); |
| 42 }); | 48 }); |
| 43 } | 49 } |
| 44 | 50 |
| 45 /** | 51 /** |
| (...skipping 15 matching lines...) Expand all Loading... |
| 61 var future = exists(path).chain((exists) { | 67 var future = exists(path).chain((exists) { |
| 62 // TODO(nweiz): better error handling | 68 // TODO(nweiz): better error handling |
| 63 if (exists) throw 'Package $id is already installed.'; | 69 if (exists) throw 'Package $id is already installed.'; |
| 64 return ensureDir(dirname(path)); | 70 return ensureDir(dirname(path)); |
| 65 }).chain((_) { | 71 }).chain((_) { |
| 66 return id.source.install(id, path); | 72 return id.source.install(id, path); |
| 67 }).chain((found) { | 73 }).chain((found) { |
| 68 if (!found) { | 74 if (!found) { |
| 69 throw 'Package ${id.fullName} not found in source "${id.source.name}".'; | 75 throw 'Package ${id.fullName} not found in source "${id.source.name}".'; |
| 70 } | 76 } |
| 71 return Package.load(path); | 77 return Package.load(path, sources); |
| 72 }); | 78 }); |
| 73 | 79 |
| 74 always(future, () => _pendingInstalls.remove(id)); | 80 always(future, () => _pendingInstalls.remove(id)); |
| 75 _pendingInstalls[id] = future; | 81 _pendingInstalls[id] = future; |
| 76 return future; | 82 return future; |
| 77 } | 83 } |
| 78 } | 84 } |
| OLD | NEW |