| 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 | 8 |
| 9 import 'package:pathos/path.dart' as path; | 9 import 'package:pathos/path.dart' as path; |
| 10 | 10 |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 | 130 |
| 131 /// Removes the old packages directory, installs all dependencies listed in | 131 /// Removes the old packages directory, installs all dependencies listed in |
| 132 /// [packageVersions], and writes a [LockFile]. | 132 /// [packageVersions], and writes a [LockFile]. |
| 133 Future _installDependencies(List<PackageId> packageVersions) { | 133 Future _installDependencies(List<PackageId> packageVersions) { |
| 134 return new Future.of(() { | 134 return new Future.of(() { |
| 135 cleanDir(packagesDir); | 135 cleanDir(packagesDir); |
| 136 return Future.wait(packageVersions.map((id) { | 136 return Future.wait(packageVersions.map((id) { |
| 137 if (id.isRoot) return new Future.immediate(id); | 137 if (id.isRoot) return new Future.immediate(id); |
| 138 return install(id); | 138 return install(id); |
| 139 }).toList()); | 139 }).toList()); |
| 140 }).then(_saveLockFile) | 140 }).then((ids) { |
| 141 .then(_installSelfReference) | 141 _saveLockFile(ids); |
| 142 .then(_linkSecondaryPackageDirs); | 142 _installSelfReference(); |
| 143 _linkSecondaryPackageDirs(); |
| 144 }); |
| 143 } | 145 } |
| 144 | 146 |
| 145 /// Traverses the root's package dependency graph and loads each of the | 147 /// Traverses the root's package dependency graph and loads each of the |
| 146 /// reached packages. This should only be called after the lockfile has been | 148 /// reached packages. This should only be called after the lockfile has been |
| 147 /// successfully generated. | 149 /// successfully generated. |
| 148 Future<List<Pubspec>> walkDependencies() { | 150 Future<List<Pubspec>> walkDependencies() { |
| 149 return defer(() { | 151 return defer(() { |
| 150 var lockFile = loadLockFile(); | 152 var lockFile = loadLockFile(); |
| 151 var group = new FutureGroup<Pubspec>(); | 153 var group = new FutureGroup<Pubspec>(); |
| 152 var visited = new Set<String>(); | 154 var visited = new Set<String>(); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 for (var id in packageIds) { | 222 for (var id in packageIds) { |
| 221 if (!id.isRoot) lockFile.packages[id.name] = id; | 223 if (!id.isRoot) lockFile.packages[id.name] = id; |
| 222 } | 224 } |
| 223 | 225 |
| 224 var lockFilePath = path.join(root.dir, 'pubspec.lock'); | 226 var lockFilePath = path.join(root.dir, 'pubspec.lock'); |
| 225 writeTextFile(lockFilePath, lockFile.serialize()); | 227 writeTextFile(lockFilePath, lockFile.serialize()); |
| 226 } | 228 } |
| 227 | 229 |
| 228 /// Installs a self-referential symlink in the `packages` directory that will | 230 /// Installs a self-referential symlink in the `packages` directory that will |
| 229 /// allow a package to import its own files using `package:`. | 231 /// allow a package to import its own files using `package:`. |
| 230 Future _installSelfReference(_) { | 232 void _installSelfReference() { |
| 231 return defer(() { | 233 var linkPath = path.join(packagesDir, root.name); |
| 232 var linkPath = path.join(packagesDir, root.name); | 234 // Create the symlink if it doesn't exist. |
| 233 // Create the symlink if it doesn't exist. | 235 if (entryExists(linkPath)) return; |
| 234 if (entryExists(linkPath)) return; | 236 ensureDir(packagesDir); |
| 235 ensureDir(packagesDir); | 237 createPackageSymlink(root.name, root.dir, linkPath, |
| 236 return createPackageSymlink(root.name, root.dir, linkPath, | 238 isSelfLink: true, relative: true); |
| 237 isSelfLink: true, relative: true); | |
| 238 }); | |
| 239 } | 239 } |
| 240 | 240 |
| 241 /// If `bin/`, `test/`, or `example/` directories exist, symlink `packages/` | 241 /// If `bin/`, `test/`, or `example/` directories exist, symlink `packages/` |
| 242 /// into them so that their entrypoints can be run. Do the same for any | 242 /// into them so that their entrypoints can be run. Do the same for any |
| 243 /// subdirectories of `test/` and `example/`. | 243 /// subdirectories of `test/` and `example/`. |
| 244 Future _linkSecondaryPackageDirs(_) { | 244 void _linkSecondaryPackageDirs() { |
| 245 var binDir = path.join(root.dir, 'bin'); | 245 var binDir = path.join(root.dir, 'bin'); |
| 246 var exampleDir = path.join(root.dir, 'example'); | 246 var exampleDir = path.join(root.dir, 'example'); |
| 247 var testDir = path.join(root.dir, 'test'); | 247 var testDir = path.join(root.dir, 'test'); |
| 248 var toolDir = path.join(root.dir, 'tool'); | 248 var toolDir = path.join(root.dir, 'tool'); |
| 249 var webDir = path.join(root.dir, 'web'); | 249 var webDir = path.join(root.dir, 'web'); |
| 250 return defer(() { | 250 |
| 251 if (!dirExists(binDir)) return; | 251 if (dirExists(binDir)) _linkSecondaryPackageDir(binDir); |
| 252 return _linkSecondaryPackageDir(binDir); | 252 for (var dir in ['example', 'test', 'tool', 'web']) { |
| 253 }).then((_) => _linkSecondaryPackageDirsRecursively(exampleDir)) | 253 _linkSecondaryPackageDirsRecursively(path.join(root.dir, dir)); |
| 254 .then((_) => _linkSecondaryPackageDirsRecursively(testDir)) | 254 } |
| 255 .then((_) => _linkSecondaryPackageDirsRecursively(toolDir)) | 255 } |
| 256 .then((_) => _linkSecondaryPackageDirsRecursively(webDir)); | |
| 257 } | |
| 258 | 256 |
| 259 /// Creates a symlink to the `packages` directory in [dir] and all its | 257 /// Creates a symlink to the `packages` directory in [dir] and all its |
| 260 /// subdirectories. | 258 /// subdirectories. |
| 261 Future _linkSecondaryPackageDirsRecursively(String dir) { | 259 void _linkSecondaryPackageDirsRecursively(String dir) { |
| 262 return defer(() { | 260 if (!dirExists(dir)) return; |
| 263 if (!dirExists(dir)) return; | 261 _linkSecondaryPackageDir(dir); |
| 264 return _linkSecondaryPackageDir(dir) | 262 _listDirWithoutPackages(dir) |
| 265 .then((_) => _listDirWithoutPackages(dir)) | 263 .where(dirExists) |
| 266 .then((files) { | 264 .forEach(_linkSecondaryPackageDir); |
| 267 return Future.wait(files.map((file) { | |
| 268 return defer(() { | |
| 269 if (!dirExists(file)) return; | |
| 270 return _linkSecondaryPackageDir(file); | |
| 271 }); | |
| 272 }).toList()); | |
| 273 }); | |
| 274 }); | |
| 275 } | 265 } |
| 276 | 266 |
| 277 // TODO(nweiz): roll this into [listDir] in io.dart once issue 4775 is fixed. | 267 // TODO(nweiz): roll this into [listDir] in io.dart once issue 4775 is fixed. |
| 278 /// Recursively lists the contents of [dir], excluding hidden `.DS_Store` | 268 /// Recursively lists the contents of [dir], excluding hidden `.DS_Store` |
| 279 /// files and `package` files. | 269 /// files and `package` files. |
| 280 Future<List<String>> _listDirWithoutPackages(dir) { | 270 List<String> _listDirWithoutPackages(dir) { |
| 281 return listDir(dir).then((files) { | 271 return flatten(listDir(dir).map((file) { |
| 282 return Future.wait(files.map((file) { | 272 if (path.basename(file) == 'packages') return []; |
| 283 if (path.basename(file) == 'packages') return new Future.immediate([]); | 273 if (!dirExists(file)) return []; |
| 284 return defer(() { | 274 var fileAndSubfiles = [file]; |
| 285 if (!dirExists(file)) return []; | 275 fileAndSubfiles.addAll(_listDirWithoutPackages(file)); |
| 286 return _listDirWithoutPackages(file); | 276 return fileAndSubfiles; |
| 287 }).then((subfiles) { | 277 })); |
| 288 var fileAndSubfiles = [file]; | |
| 289 fileAndSubfiles.addAll(subfiles); | |
| 290 return fileAndSubfiles; | |
| 291 }); | |
| 292 }).toList()); | |
| 293 }).then(flatten); | |
| 294 } | 278 } |
| 295 | 279 |
| 296 /// Creates a symlink to the `packages` directory in [dir]. Will replace one | 280 /// Creates a symlink to the `packages` directory in [dir]. Will replace one |
| 297 /// if already there. | 281 /// if already there. |
| 298 Future _linkSecondaryPackageDir(String dir) { | 282 void _linkSecondaryPackageDir(String dir) { |
| 299 return defer(() { | 283 var symlink = path.join(dir, 'packages'); |
| 300 var symlink = path.join(dir, 'packages'); | 284 if (entryExists(symlink)) deleteEntry(symlink); |
| 301 if (entryExists(symlink)) deleteEntry(symlink); | 285 createSymlink(packagesDir, symlink, relative: true); |
| 302 return createSymlink(packagesDir, symlink, relative: true); | |
| 303 }); | |
| 304 } | 286 } |
| 305 } | 287 } |
| OLD | NEW |