Chromium Code Reviews| Index: utils/pub/system_cache.dart |
| diff --git a/utils/pub/system_cache.dart b/utils/pub/system_cache.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..99e25e3adffc4bf24c8429c634afc86e05acb82f |
| --- /dev/null |
| +++ b/utils/pub/system_cache.dart |
| @@ -0,0 +1,75 @@ |
| +// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +/** |
| + * The system-wide cache of installed packages. |
| + * |
| + * This cache contains all packages that are downloaded from the internet. |
| + * Packages that are available locally (e.g. from the SDK) don't use this cache. |
| + */ |
| +class SystemCache { |
| + /** |
| + * The root directory where this package cache is located. |
| + */ |
| + final String rootDir; |
| + |
| + /** |
| + * Packages which are currently being asynchronously installed to the cache. |
| + */ |
| + final Map<PackageId, Future<Package>> _pendingInstalls; |
| + |
| + /** |
| + * Creates a new package cache which is backed by the given directory on the |
| + * user's file system. |
| + */ |
| + SystemCache(this.rootDir) |
| + : _pendingInstalls = new Map<PackageId, Future<Package>>(); |
| + |
| + /** |
| + * Loads all of the package ids in the cache and returns them. |
| + */ |
| + Future<List<PackageId>> listAll() { |
| + return listDir(rootDir).chain((paths) { |
| + final sources = paths.map((path) { |
| + final source = Source.fromName(basename(path)); |
| + return listDir(path).transform((subpaths) { |
| + return subpaths.map((subpath) => |
| + new PackageId(basename(subpath), source)); |
| + }); |
| + }); |
| + return Futures.wait(sources).transform(flatten); |
| + }); |
| + } |
| + |
| + /** |
| + * Ensures that the package identified by [id] is installed to the cache, |
| + * loads it, and returns it. |
| + * |
| + * It is an error to try installing a package from a source with `shouldCache |
| + * == false` to the system cache. |
| + */ |
| + Future<Package> install(PackageId id) { |
| + 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.
|
| + |
|
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
|
| + var pending = _pendingInstalls[id]; |
| + if (pending != null) return pending; |
| + |
| + var path = join(rootDir, id.source.name, id.source.packageName(id)); |
| + var future = exists(path).chain((exists) { |
| + 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.
|
| + return ensureNestedDir(dirname(path)); |
| + }).chain((_) { |
| + return id.source.install(id, path); |
| + }).chain((found) { |
| + if (!found) { |
| + throw 'Package ${id.name} not found in source "${id.source.name}".'; |
| + } |
| + return Package.load(path); |
| + }); |
| + |
| + always(future, () => _pendingInstalls.remove(id)); |
| + _pendingInstalls[id] = future; |
| + return future; |
| + } |
| +} |