Chromium Code Reviews| 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 #library('system_cache'); | 5 #library('system_cache'); |
| 6 | 6 |
| 7 #import('io.dart'); | 7 #import('io.dart'); |
| 8 #import('package.dart'); | 8 #import('package.dart'); |
| 9 #import('source.dart'); | |
| 9 #import('source_registry.dart'); | 10 #import('source_registry.dart'); |
| 10 #import('utils.dart'); | 11 #import('utils.dart'); |
| 11 | 12 |
| 12 /** | 13 /** |
| 13 * The system-wide cache of installed packages. | 14 * The system-wide cache of installed packages. |
| 14 * | 15 * |
| 15 * This cache contains all packages that are downloaded from the internet. | 16 * This cache contains all packages that are downloaded from the internet. |
| 16 * Packages that are available locally (e.g. from the SDK) don't use this cache. | 17 * Packages that are available locally (e.g. from the SDK) don't use this cache. |
| 17 */ | 18 */ |
| 18 class SystemCache { | 19 class SystemCache { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 33 | 34 |
| 34 /** | 35 /** |
| 35 * Creates a new package cache which is backed by the given directory on the | 36 * Creates a new package cache which is backed by the given directory on the |
| 36 * user's file system. | 37 * user's file system. |
| 37 */ | 38 */ |
| 38 SystemCache(this.rootDir) | 39 SystemCache(this.rootDir) |
| 39 : _pendingInstalls = new Map<PackageId, Future<Package>>(), | 40 : _pendingInstalls = new Map<PackageId, Future<Package>>(), |
| 40 sources = new SourceRegistry(); | 41 sources = new SourceRegistry(); |
| 41 | 42 |
| 42 /** | 43 /** |
| 44 * Registers a new source. This source may not have the same name as a source | |
|
Bob Nystrom
2012/07/10 16:12:36
"may" -> "must"
nweiz
2012/07/10 18:13:01
Done.
| |
| 45 * that's already been registered. | |
| 46 */ | |
| 47 void register(Source source) { | |
| 48 source.bind(this); | |
| 49 sources.register(source); | |
| 50 } | |
| 51 | |
| 52 /** | |
| 43 * Loads all of the package ids in the cache and returns them. | 53 * Loads all of the package ids in the cache and returns them. |
| 44 */ | 54 */ |
| 45 Future<List<PackageId>> listAll() { | 55 Future<List<PackageId>> listAll() { |
| 46 return listDir(rootDir).chain((paths) { | 56 return listDir(rootDir).chain((paths) { |
| 47 final sources = paths.map((path) { | 57 final sources = paths.map((path) { |
| 48 final source = sources[basename(path)]; | 58 final source = sources[basename(path)]; |
| 49 return listDir(path).transform((subpaths) { | 59 return listDir(path).transform((subpaths) { |
| 50 // TODO(rnystrom): Once there are cached packages and this path is | 60 // TODO(rnystrom): Once there are cached packages and this path is |
| 51 // being used, figure out how version numbers should be acquired. | 61 // being used, figure out how version numbers should be acquired. |
| 52 return subpaths.map((subpath) => | 62 return subpaths.map((subpath) => |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 65 * == false` to the system cache. | 75 * == false` to the system cache. |
| 66 */ | 76 */ |
| 67 Future<Package> install(PackageId id) { | 77 Future<Package> install(PackageId id) { |
| 68 if (!id.source.shouldCache) { | 78 if (!id.source.shouldCache) { |
| 69 throw new IllegalArgumentException("Package $id is not cacheable."); | 79 throw new IllegalArgumentException("Package $id is not cacheable."); |
| 70 } | 80 } |
| 71 | 81 |
| 72 var pending = _pendingInstalls[id]; | 82 var pending = _pendingInstalls[id]; |
| 73 if (pending != null) return pending; | 83 if (pending != null) return pending; |
| 74 | 84 |
| 75 var sourceDir = join(rootDir, id.source.name); | 85 var path = id.source.systemCacheDirectory(id); |
| 76 var path = id.source.systemCacheDirectory(id, sourceDir); | |
| 77 var future = exists(path).chain((exists) { | 86 var future = exists(path).chain((exists) { |
| 78 // TODO(nweiz): better error handling | 87 // TODO(nweiz): better error handling |
| 79 if (exists) throw 'Package $id is already installed.'; | 88 if (exists) throw 'Package $id is already installed.'; |
| 80 return ensureDir(dirname(path)); | 89 return ensureDir(dirname(path)); |
| 81 }).chain((_) { | 90 }).chain((_) { |
| 82 return id.source.install(id, path); | 91 return id.source.install(id, path); |
| 83 }).chain((found) { | 92 }).chain((found) { |
| 84 if (!found) throw 'Package $id not found.'; | 93 if (!found) throw 'Package $id not found.'; |
| 85 return Package.load(path, sources); | 94 return Package.load(path, sources); |
| 86 }); | 95 }); |
| 87 | 96 |
| 88 always(future, () => _pendingInstalls.remove(id)); | 97 always(future, () => _pendingInstalls.remove(id)); |
| 89 _pendingInstalls[id] = future; | 98 _pendingInstalls[id] = future; |
| 90 return future; | 99 return future; |
| 91 } | 100 } |
| 92 } | 101 } |
| OLD | NEW |