| 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 "packages" directory for an application or library. |
| 7 * |
| 8 * This directory contains symlinks to all packages used by an app. These links |
| 9 * point either to the [SystemCache] or to some other location on the local |
| 10 * filesystem. |
| 11 */ |
| 12 class PackagesDir { |
| 13 /** |
| 14 * The package containing this directory. |
| 15 */ |
| 16 final Package owner; |
| 17 |
| 18 /** |
| 19 * The system-wide cache which caches packages that need to be fetched over |
| 20 * the network. |
| 21 */ |
| 22 final SystemCache cache; |
| 23 |
| 24 /** |
| 25 * Packages which have already been loaded into memory. |
| 26 */ |
| 27 final Map<PackageId, Package> _loadedPackages; |
| 28 |
| 29 /** |
| 30 * Packages which are currently being asynchronously installed to the |
| 31 * directory. |
| 32 */ |
| 33 final Map<PackageId, Future<Package>> _pendingInstalls; |
| 34 |
| 35 PackagesDir(this.owner, this.cache) |
| 36 : _loadedPackages = new Map<PackageId, Package>(), |
| 37 _pendingInstalls = new Map<PackageId, Future<Package>>(); |
| 38 |
| 39 /** |
| 40 * Returns the path to the "packages" directory. |
| 41 */ |
| 42 // TODO(rnystrom): Make this path configurable. |
| 43 String get path() => join(owner.dir, 'packages'); |
| 44 |
| 45 /** |
| 46 * Ensures that the package identified by [id] is installed to the directory, |
| 47 * loads it, and returns it. |
| 48 * |
| 49 * If this completes successfully, the package is guaranteed to be importable |
| 50 * using the `package:` scheme. |
| 51 * |
| 52 * This will automatically install the package to the system-wide cache as |
| 53 * well if it requires network access to retrieve (specifically, if |
| 54 * `id.source.shouldCache` is true). |
| 55 * |
| 56 * See also [installTransitively]. |
| 57 */ |
| 58 Future<Package> install(PackageId id) { |
| 59 var package = _loadedPackages[id]; |
| 60 if (package != null) return new Future<Package>.immediate(package); |
| 61 |
| 62 var pending = _pendingInstalls[id]; |
| 63 if (pending != null) return new Future<Package>.immediate(package); |
| 64 |
| 65 var packageDir = join(path, id.name); |
| 66 var future = ensureDir(dirname(packageDir)).chain((_) { |
| 67 return exists(packageDir); |
| 68 }).chain((exists) { |
| 69 // If the package already exists in the directory, no need to re-install. |
| 70 if (exists) return new Future.immediate(null); |
| 71 |
| 72 if (id.source.shouldCache) { |
| 73 return cache.install(id).chain( |
| 74 (pkg) => createSymlink(pkg.dir, packageDir)); |
| 75 } else { |
| 76 return id.source.install(id, packageDir).transform((found) { |
| 77 if (found) return null; |
| 78 // TODO(nweiz): More robust error-handling. |
| 79 throw 'Package ${id.fullName} not found in source ' |
| 80 '"${id.source.name}".'; |
| 81 }); |
| 82 } |
| 83 }).chain((_) => Package.load(packageDir)); |
| 84 |
| 85 future.then((pkg) => _loadedPackages[id] = pkg); |
| 86 always(future, () => _pendingInstalls.remove(id)); |
| 87 _pendingInstalls[id] = future; |
| 88 |
| 89 return future; |
| 90 } |
| 91 |
| 92 /** |
| 93 * Installs the package identified by [id] and all its transitive |
| 94 * dependencies. |
| 95 */ |
| 96 Future<Package> installTransitively(PackageId id) { |
| 97 var seen = new Set<PackageId>(); |
| 98 Future<Package> helper(id) { |
| 99 if (seen.contains(id)) return new Future.immediate(null); |
| 100 seen.add(id); |
| 101 |
| 102 return install(id).chain((package) { |
| 103 return Futures.wait(package.dependencies.map(helper)). |
| 104 transform((_) => package); |
| 105 }); |
| 106 } |
| 107 |
| 108 return helper(id); |
| 109 } |
| 110 |
| 111 /** |
| 112 * Installs all dependencies of [owner] to the "packages" directory. Returns a |
| 113 * [Future] that completes when all dependencies are installed. |
| 114 */ |
| 115 Future installDependencies() { |
| 116 return Futures.wait(owner.dependencies.map(installTransitively)). |
| 117 transform((_) => null); |
| 118 } |
| 119 } |
| OLD | NEW |