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 local cache of previously installed packages. |
| 7 */ |
| 8 class PackageCache { |
| 9 /** |
| 10 * The root directory where this package cache is located. |
| 11 */ |
| 12 final String rootDir; |
| 13 |
| 14 // TODO(rnystrom): When packages are versioned, String here and elsewhere will |
| 15 // become a name/version/(source?) tuple. |
| 16 final Map<String, Package> _loadedPackages; |
| 17 |
| 18 /** |
| 19 * Packages which are currently being asynchronously loaded. |
| 20 */ |
| 21 final Map<String, Future<Package>> _pendingPackages; |
| 22 |
| 23 /** |
| 24 * Creates a new package cache which is backed by the given directory on the |
| 25 * user's file system. |
| 26 */ |
| 27 PackageCache(this.rootDir) |
| 28 : _loadedPackages = <Package>{}, |
| 29 _pendingPackages = <Future<Package>>{}; |
| 30 |
| 31 /** |
| 32 * Loads all of the packages in the cache and returns them. |
| 33 */ |
| 34 Future<List<Package>> listAll() { |
| 35 return listDir(rootDir).chain((paths) { |
| 36 final packages = paths.map((path) => find(basename(path))); |
| 37 return Futures.wait(packages); |
| 38 }); |
| 39 } |
| 40 |
| 41 /** |
| 42 * Loads the package named [name] from this cache, if present. |
| 43 */ |
| 44 // TODO(rnystrom): What happens if the package isn't cached? |
| 45 Future<Package> find(String name) { |
| 46 // Use the previously loaded one. |
| 47 final package = _loadedPackages[name]; |
| 48 if (package != null) { |
| 49 return new Future.immediate(package); |
| 50 } |
| 51 |
| 52 // If we are already in-progress loading it, re-use that one. |
| 53 final pending = _pendingPackages[name]; |
| 54 if (pending != null) { |
| 55 return pending; |
| 56 } |
| 57 |
| 58 return _loadPackage(name); |
| 59 } |
| 60 |
| 61 /** |
| 62 * Start loading the package. |
| 63 */ |
| 64 Future<Package> _loadPackage(String name) { |
| 65 final future = _parsePubspec(name).transform((dependencies) { |
| 66 final package = new Package._(this, name, dependencies); |
| 67 |
| 68 _pendingPackages.remove(name); |
| 69 _loadedPackages[name] = package; |
| 70 return package; |
| 71 }); |
| 72 |
| 73 _pendingPackages[name] = future; |
| 74 return future; |
| 75 } |
| 76 |
| 77 Future<List<String>> _parsePubspec(String name) { |
| 78 final completer = new Completer<List<String>>(); |
| 79 final pubspecPath = join(rootDir, name, 'pubspec'); |
| 80 |
| 81 // TODO(rnystrom): Handle the directory not existing. |
| 82 // TODO(rnystrom): Error-handling. |
| 83 final readFuture = readTextFile(pubspecPath); |
| 84 readFuture.handleException((error) { |
| 85 // If there is no pubspec, we implicitly treat that as a package with no |
| 86 // dependencies. |
| 87 // TODO(rnystrom): Distinguish file not found from other real errors. |
| 88 completer.complete(<String>[]); |
| 89 return true; |
| 90 }); |
| 91 |
| 92 readFuture.then((pubspec) { |
| 93 // TODO(rnystrom): Use YAML parser when ready. For now, it's just a flat |
| 94 // list of newline-separated strings. |
| 95 final dependencyNames = pubspec.split('\n'). |
| 96 map((name) => name.trim()). |
| 97 filter((name) => (name != null) && (name != '')); |
| 98 |
| 99 completer.complete(dependencyNames); |
| 100 }); |
| 101 |
| 102 return completer.future; |
| 103 } |
| 104 } |
OLD | NEW |