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