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 entrypoint; | 5 library entrypoint; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 |
| 9 import '../../pkg/path/lib/path.dart' as path; |
| 10 |
8 import 'io.dart'; | 11 import 'io.dart'; |
9 import 'lock_file.dart'; | 12 import 'lock_file.dart'; |
10 import 'log.dart' as log; | 13 import 'log.dart' as log; |
11 import 'package.dart'; | 14 import 'package.dart'; |
12 import 'pubspec.dart'; | 15 import 'pubspec.dart'; |
13 import 'sdk.dart' as sdk; | 16 import 'sdk.dart' as sdk; |
14 import 'system_cache.dart'; | 17 import 'system_cache.dart'; |
15 import 'utils.dart'; | 18 import 'utils.dart'; |
16 import 'version.dart'; | 19 import 'version.dart'; |
17 import 'version_solver.dart'; | 20 import 'version_solver.dart'; |
(...skipping 24 matching lines...) Expand all Loading... |
42 /// Packages which are either currently being asynchronously installed to the | 45 /// Packages which are either currently being asynchronously installed to the |
43 /// directory, or have already been installed. | 46 /// directory, or have already been installed. |
44 final _installs = new Map<PackageId, Future<PackageId>>(); | 47 final _installs = new Map<PackageId, Future<PackageId>>(); |
45 | 48 |
46 /// Loads the entrypoint from a package at [rootDir]. | 49 /// Loads the entrypoint from a package at [rootDir]. |
47 Entrypoint(String rootDir, SystemCache cache) | 50 Entrypoint(String rootDir, SystemCache cache) |
48 : root = new Package.load(null, rootDir, cache.sources), | 51 : root = new Package.load(null, rootDir, cache.sources), |
49 cache = cache; | 52 cache = cache; |
50 | 53 |
51 // TODO(rnystrom): Make this path configurable. | 54 // TODO(rnystrom): Make this path configurable. |
52 /// The path to this "packages" directory. | 55 /// The path to the entrypoint's "packages" directory. |
53 String get path => join(root.dir, 'packages'); | 56 String get packagesDir => join(root.dir, 'packages'); |
54 | 57 |
55 /// Ensures that the package identified by [id] is installed to the directory. | 58 /// Ensures that the package identified by [id] is installed to the directory. |
56 /// Returns the resolved [PackageId]. | 59 /// Returns the resolved [PackageId]. |
57 /// | 60 /// |
58 /// If this completes successfully, the package is guaranteed to be importable | 61 /// If this completes successfully, the package is guaranteed to be importable |
59 /// using the `package:` scheme. | 62 /// using the `package:` scheme. |
60 /// | 63 /// |
61 /// This will automatically install the package to the system-wide cache as | 64 /// This will automatically install the package to the system-wide cache as |
62 /// well if it requires network access to retrieve (specifically, if | 65 /// well if it requires network access to retrieve (specifically, if |
63 /// `id.source.shouldCache` is true). | 66 /// `id.source.shouldCache` is true). |
64 /// | 67 /// |
65 /// See also [installDependencies]. | 68 /// See also [installDependencies]. |
66 Future<PackageId> install(PackageId id) { | 69 Future<PackageId> install(PackageId id) { |
67 var pendingOrCompleted = _installs[id]; | 70 var pendingOrCompleted = _installs[id]; |
68 if (pendingOrCompleted != null) return pendingOrCompleted; | 71 if (pendingOrCompleted != null) return pendingOrCompleted; |
69 | 72 |
70 var packageDir = join(path, id.name); | 73 var packageDir = join(packagesDir, id.name); |
71 var future = defer(() { | 74 var future = defer(() { |
72 ensureDir(dirname(packageDir)); | 75 ensureDir(path.dirname(packageDir)); |
73 if (!dirExists(packageDir)) return; | 76 if (!dirExists(packageDir)) return; |
74 | 77 |
75 // TODO(nweiz): figure out when to actually delete the directory, and when | 78 // TODO(nweiz): figure out when to actually delete the directory, and when |
76 // we can just re-use the existing symlink. | 79 // we can just re-use the existing symlink. |
77 log.fine("Deleting package directory for ${id.name} before install."); | 80 log.fine("Deleting package directory for ${id.name} before install."); |
78 return deleteDir(packageDir); | 81 return deleteDir(packageDir); |
79 }).then((_) { | 82 }).then((_) { |
80 if (id.source.shouldCache) { | 83 if (id.source.shouldCache) { |
81 return cache.install(id).then( | 84 return cache.install(id).then( |
82 (pkg) => createPackageSymlink(id.name, pkg.dir, packageDir)); | 85 (pkg) => createPackageSymlink(id.name, pkg.dir, packageDir)); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 for (var dependency in dependencies) { | 123 for (var dependency in dependencies) { |
121 solver.useLatestVersion(dependency); | 124 solver.useLatestVersion(dependency); |
122 } | 125 } |
123 return solver.solve(); | 126 return solver.solve(); |
124 }).then(_installDependencies); | 127 }).then(_installDependencies); |
125 } | 128 } |
126 | 129 |
127 /// Removes the old packages directory, installs all dependencies listed in | 130 /// Removes the old packages directory, installs all dependencies listed in |
128 /// [packageVersions], and writes a [LockFile]. | 131 /// [packageVersions], and writes a [LockFile]. |
129 Future _installDependencies(List<PackageId> packageVersions) { | 132 Future _installDependencies(List<PackageId> packageVersions) { |
130 return cleanDir(path).then((_) { | 133 return cleanDir(packagesDir).then((_) { |
131 return Future.wait(packageVersions.map((id) { | 134 return Future.wait(packageVersions.map((id) { |
132 if (id.isRoot) return new Future.immediate(id); | 135 if (id.isRoot) return new Future.immediate(id); |
133 return install(id); | 136 return install(id); |
134 })); | 137 })); |
135 }).then(_saveLockFile) | 138 }).then(_saveLockFile) |
136 .then(_installSelfReference) | 139 .then(_installSelfReference) |
137 .then(_linkSecondaryPackageDirs); | 140 .then(_linkSecondaryPackageDirs); |
138 } | 141 } |
139 | 142 |
140 /// Traverses the root's package dependency graph and loads each of the | 143 /// Traverses the root's package dependency graph and loads each of the |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 } | 220 } |
218 | 221 |
219 var lockFilePath = join(root.dir, 'pubspec.lock'); | 222 var lockFilePath = join(root.dir, 'pubspec.lock'); |
220 writeTextFile(lockFilePath, lockFile.serialize()); | 223 writeTextFile(lockFilePath, lockFile.serialize()); |
221 } | 224 } |
222 | 225 |
223 /// Installs a self-referential symlink in the `packages` directory that will | 226 /// Installs a self-referential symlink in the `packages` directory that will |
224 /// allow a package to import its own files using `package:`. | 227 /// allow a package to import its own files using `package:`. |
225 Future _installSelfReference(_) { | 228 Future _installSelfReference(_) { |
226 return defer(() { | 229 return defer(() { |
227 var linkPath = join(path, root.name); | 230 var linkPath = join(packagesDir, root.name); |
228 // Create the symlink if it doesn't exist. | 231 // Create the symlink if it doesn't exist. |
229 if (entryExists(linkPath)) return; | 232 if (entryExists(linkPath)) return; |
230 ensureDir(path); | 233 ensureDir(packagesDir); |
231 return createPackageSymlink(root.name, root.dir, linkPath, | 234 return createPackageSymlink(root.name, root.dir, linkPath, |
232 isSelfLink: true); | 235 isSelfLink: true); |
233 }); | 236 }); |
234 } | 237 } |
235 | 238 |
236 /// If `bin/`, `test/`, or `example/` directories exist, symlink `packages/` | 239 /// If `bin/`, `test/`, or `example/` directories exist, symlink `packages/` |
237 /// into them so that their entrypoints can be run. Do the same for any | 240 /// into them so that their entrypoints can be run. Do the same for any |
238 /// subdirectories of `test/` and `example/`. | 241 /// subdirectories of `test/` and `example/`. |
239 Future _linkSecondaryPackageDirs(_) { | 242 Future _linkSecondaryPackageDirs(_) { |
240 var binDir = join(root.dir, 'bin'); | 243 var binDir = join(root.dir, 'bin'); |
(...skipping 27 matching lines...) Expand all Loading... |
268 }); | 271 }); |
269 }); | 272 }); |
270 } | 273 } |
271 | 274 |
272 // TODO(nweiz): roll this into [listDir] in io.dart once issue 4775 is fixed. | 275 // TODO(nweiz): roll this into [listDir] in io.dart once issue 4775 is fixed. |
273 /// Recursively lists the contents of [dir], excluding hidden `.DS_Store` | 276 /// Recursively lists the contents of [dir], excluding hidden `.DS_Store` |
274 /// files and `package` files. | 277 /// files and `package` files. |
275 Future<List<String>> _listDirWithoutPackages(dir) { | 278 Future<List<String>> _listDirWithoutPackages(dir) { |
276 return listDir(dir).then((files) { | 279 return listDir(dir).then((files) { |
277 return Future.wait(files.map((file) { | 280 return Future.wait(files.map((file) { |
278 if (basename(file) == 'packages') return new Future.immediate([]); | 281 if (path.basename(file) == 'packages') return new Future.immediate([]); |
279 return defer(() { | 282 return defer(() { |
280 if (!dirExists(file)) return []; | 283 if (!dirExists(file)) return []; |
281 return _listDirWithoutPackages(file); | 284 return _listDirWithoutPackages(file); |
282 }).then((subfiles) { | 285 }).then((subfiles) { |
283 var fileAndSubfiles = [file]; | 286 var fileAndSubfiles = [file]; |
284 fileAndSubfiles.addAll(subfiles); | 287 fileAndSubfiles.addAll(subfiles); |
285 return fileAndSubfiles; | 288 return fileAndSubfiles; |
286 }); | 289 }); |
287 })); | 290 })); |
288 }).then(flatten); | 291 }).then(flatten); |
289 } | 292 } |
290 | 293 |
291 /// Creates a symlink to the `packages` directory in [dir] if none exists. | 294 /// Creates a symlink to the `packages` directory in [dir] if none exists. |
292 Future _linkSecondaryPackageDir(String dir) { | 295 Future _linkSecondaryPackageDir(String dir) { |
293 return defer(() { | 296 return defer(() { |
294 var to = join(dir, 'packages'); | 297 var to = join(dir, 'packages'); |
295 if (entryExists(to)) return; | 298 if (entryExists(to)) return; |
296 return createSymlink(path, to); | 299 return createSymlink(packagesDir, to); |
297 }); | 300 }); |
298 } | 301 } |
299 } | 302 } |
OLD | NEW |