| 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 /** | |
| 28 * Creates a new package cache which is backed by the given directory on the | 23 * Creates a new package cache which is backed by the given directory on the |
| 29 * user's file system. | 24 * user's file system. |
| 30 */ | 25 */ |
| 31 SystemCache(this.rootDir) | 26 SystemCache(this.rootDir) |
| 32 : _pendingInstalls = new Map<PackageId, Future<Package>>(), | 27 : _pendingInstalls = new Map<PackageId, Future<Package>>(); |
| 33 sources = new SourceRegistry(); | |
| 34 | 28 |
| 35 /** | 29 /** |
| 36 * Loads all of the package ids in the cache and returns them. | 30 * Loads all of the package ids in the cache and returns them. |
| 37 */ | 31 */ |
| 38 Future<List<PackageId>> listAll() { | 32 Future<List<PackageId>> listAll() { |
| 39 return listDir(rootDir).chain((paths) { | 33 return listDir(rootDir).chain((paths) { |
| 40 final sources = paths.map((path) { | 34 final sources = paths.map((path) { |
| 41 final source = sources[basename(path)]; | 35 final source = Source.fromName(basename(path)); |
| 42 return listDir(path).transform((subpaths) { | 36 return listDir(path).transform((subpaths) { |
| 43 return subpaths.map((subpath) => | 37 return subpaths.map((subpath) => |
| 44 new PackageId(basename(subpath), source)); | 38 new PackageId(basename(subpath), source)); |
| 45 }); | 39 }); |
| 46 }); | 40 }); |
| 47 return Futures.wait(sources).transform(flatten); | 41 return Futures.wait(sources).transform(flatten); |
| 48 }); | 42 }); |
| 49 } | 43 } |
| 50 | 44 |
| 51 /** | 45 /** |
| (...skipping 15 matching lines...) Expand all Loading... |
| 67 var future = exists(path).chain((exists) { | 61 var future = exists(path).chain((exists) { |
| 68 // TODO(nweiz): better error handling | 62 // TODO(nweiz): better error handling |
| 69 if (exists) throw 'Package $id is already installed.'; | 63 if (exists) throw 'Package $id is already installed.'; |
| 70 return ensureDir(dirname(path)); | 64 return ensureDir(dirname(path)); |
| 71 }).chain((_) { | 65 }).chain((_) { |
| 72 return id.source.install(id, path); | 66 return id.source.install(id, path); |
| 73 }).chain((found) { | 67 }).chain((found) { |
| 74 if (!found) { | 68 if (!found) { |
| 75 throw 'Package ${id.fullName} not found in source "${id.source.name}".'; | 69 throw 'Package ${id.fullName} not found in source "${id.source.name}".'; |
| 76 } | 70 } |
| 77 return Package.load(path, sources); | 71 return Package.load(path); |
| 78 }); | 72 }); |
| 79 | 73 |
| 80 always(future, () => _pendingInstalls.remove(id)); | 74 always(future, () => _pendingInstalls.remove(id)); |
| 81 _pendingInstalls[id] = future; | 75 _pendingInstalls[id] = future; |
| 82 return future; | 76 return future; |
| 83 } | 77 } |
| 84 } | 78 } |
| OLD | NEW |