Chromium Code Reviews| 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 import 'io.dart'; | 8 import 'io.dart'; |
| 9 import 'lock_file.dart'; | 9 import 'lock_file.dart'; |
| 10 import 'log.dart' as log; | 10 import 'log.dart' as log; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 67 /// | 67 /// |
| 68 /// See also [installDependencies]. | 68 /// See also [installDependencies]. |
| 69 Future<PackageId> install(PackageId id) { | 69 Future<PackageId> install(PackageId id) { |
| 70 var pendingOrCompleted = _installs[id]; | 70 var pendingOrCompleted = _installs[id]; |
| 71 if (pendingOrCompleted != null) return pendingOrCompleted; | 71 if (pendingOrCompleted != null) return pendingOrCompleted; |
| 72 | 72 |
| 73 var packageDir = join(path, id.name); | 73 var packageDir = join(path, id.name); |
| 74 var future = ensureDir(dirname(packageDir)).then((_) { | 74 var future = ensureDir(dirname(packageDir)).then((_) { |
| 75 return exists(packageDir); | 75 return exists(packageDir); |
| 76 }).then((exists) { | 76 }).then((exists) { |
| 77 if (!exists) return new Future.immediate(null); | 77 if (!exists) return; |
|
Alan Knight
2013/01/24 00:03:20
Being fussy, but seeing as this isn't a void funct
Bob Nystrom
2013/01/24 00:57:42
The intent here is to exit the function and not ex
| |
| 78 // 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 |
| 79 // we can just re-use the existing symlink. | 79 // we can just re-use the existing symlink. |
| 80 log.fine("Deleting package directory for ${id.name} before install."); | 80 log.fine("Deleting package directory for ${id.name} before install."); |
| 81 return deleteDir(packageDir); | 81 return deleteDir(packageDir); |
| 82 }).then((_) { | 82 }).then((_) { |
| 83 if (id.source.shouldCache) { | 83 if (id.source.shouldCache) { |
| 84 return cache.install(id).then( | 84 return cache.install(id).then( |
| 85 (pkg) => createPackageSymlink(id.name, pkg.dir, packageDir)); | 85 (pkg) => createPackageSymlink(id.name, pkg.dir, packageDir)); |
| 86 } else { | 86 } else { |
| 87 return id.source.install(id, packageDir).then((found) { | 87 return id.source.install(id, packageDir).then((found) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 | 142 |
| 143 /// Loads the list of concrete package versions from the `pubspec.lock`, if it | 143 /// Loads the list of concrete package versions from the `pubspec.lock`, if it |
| 144 /// exists. If it doesn't, this completes to an empty [LockFile]. | 144 /// exists. If it doesn't, this completes to an empty [LockFile]. |
| 145 Future<LockFile> loadLockFile() { | 145 Future<LockFile> loadLockFile() { |
| 146 var lockFilePath = join(root.dir, 'pubspec.lock'); | 146 var lockFilePath = join(root.dir, 'pubspec.lock'); |
| 147 | 147 |
| 148 log.fine("Loading lockfile."); | 148 log.fine("Loading lockfile."); |
| 149 return fileExists(lockFilePath).then((exists) { | 149 return fileExists(lockFilePath).then((exists) { |
| 150 if (!exists) { | 150 if (!exists) { |
| 151 log.fine("No lock file at $lockFilePath, creating empty one."); | 151 log.fine("No lock file at $lockFilePath, creating empty one."); |
| 152 return new Future<LockFile>.immediate(new LockFile.empty()); | 152 return new LockFile.empty(); |
| 153 } | 153 } |
| 154 | 154 |
| 155 return readTextFile(lockFilePath).then((text) => | 155 return readTextFile(lockFilePath).then((text) => |
| 156 new LockFile.parse(text, cache.sources)); | 156 new LockFile.parse(text, cache.sources)); |
| 157 }); | 157 }); |
| 158 } | 158 } |
| 159 | 159 |
| 160 /// Saves a list of concrete package versions to the `pubspec.lock` file. | 160 /// Saves a list of concrete package versions to the `pubspec.lock` file. |
| 161 Future _saveLockFile(List<PackageId> packageIds) { | 161 Future _saveLockFile(List<PackageId> packageIds) { |
| 162 var lockFile = new LockFile.empty(); | 162 var lockFile = new LockFile.empty(); |
| 163 for (var id in packageIds) { | 163 for (var id in packageIds) { |
| 164 if (id.source is! RootSource) lockFile.packages[id.name] = id; | 164 if (id.source is! RootSource) lockFile.packages[id.name] = id; |
| 165 } | 165 } |
| 166 | 166 |
| 167 var lockFilePath = join(root.dir, 'pubspec.lock'); | 167 var lockFilePath = join(root.dir, 'pubspec.lock'); |
| 168 log.fine("Saving lockfile."); | 168 log.fine("Saving lockfile."); |
| 169 return writeTextFile(lockFilePath, lockFile.serialize()); | 169 return writeTextFile(lockFilePath, lockFile.serialize()); |
| 170 } | 170 } |
| 171 | 171 |
| 172 /// Installs a self-referential symlink in the `packages` directory that will | 172 /// Installs a self-referential symlink in the `packages` directory that will |
| 173 /// allow a package to import its own files using `package:`. | 173 /// allow a package to import its own files using `package:`. |
| 174 Future _installSelfReference(_) { | 174 Future _installSelfReference(_) { |
| 175 var linkPath = join(path, root.name); | 175 var linkPath = join(path, root.name); |
| 176 return exists(linkPath).then((exists) { | 176 return exists(linkPath).then((exists) { |
| 177 // Create the symlink if it doesn't exist. | 177 // Create the symlink if it doesn't exist. |
| 178 if (exists) return new Future.immediate(null); | 178 if (exists) return; |
| 179 return ensureDir(path).then( | 179 return ensureDir(path).then( |
| 180 (_) => createPackageSymlink(root.name, root.dir, linkPath, | 180 (_) => createPackageSymlink(root.name, root.dir, linkPath, |
| 181 isSelfLink: true)); | 181 isSelfLink: true)); |
| 182 }); | 182 }); |
| 183 } | 183 } |
| 184 | 184 |
| 185 /// If `bin/`, `test/`, or `example/` directories exist, symlink `packages/` | 185 /// If `bin/`, `test/`, or `example/` directories exist, symlink `packages/` |
| 186 /// into them so that their entrypoints can be run. Do the same for any | 186 /// into them so that their entrypoints can be run. Do the same for any |
| 187 /// subdirectories of `test/` and `example/`. | 187 /// subdirectories of `test/` and `example/`. |
| 188 Future _linkSecondaryPackageDirs(_) { | 188 Future _linkSecondaryPackageDirs(_) { |
| 189 var binDir = join(root.dir, 'bin'); | 189 var binDir = join(root.dir, 'bin'); |
| 190 var exampleDir = join(root.dir, 'example'); | 190 var exampleDir = join(root.dir, 'example'); |
| 191 var testDir = join(root.dir, 'test'); | 191 var testDir = join(root.dir, 'test'); |
| 192 var toolDir = join(root.dir, 'tool'); | 192 var toolDir = join(root.dir, 'tool'); |
| 193 var webDir = join(root.dir, 'web'); | 193 var webDir = join(root.dir, 'web'); |
| 194 return dirExists(binDir).then((exists) { | 194 return dirExists(binDir).then((exists) { |
| 195 if (!exists) return new Future.immediate(null); | 195 if (!exists) return; |
| 196 return _linkSecondaryPackageDir(binDir); | 196 return _linkSecondaryPackageDir(binDir); |
| 197 }).then((_) => _linkSecondaryPackageDirsRecursively(exampleDir)) | 197 }).then((_) => _linkSecondaryPackageDirsRecursively(exampleDir)) |
| 198 .then((_) => _linkSecondaryPackageDirsRecursively(testDir)) | 198 .then((_) => _linkSecondaryPackageDirsRecursively(testDir)) |
| 199 .then((_) => _linkSecondaryPackageDirsRecursively(toolDir)) | 199 .then((_) => _linkSecondaryPackageDirsRecursively(toolDir)) |
| 200 .then((_) => _linkSecondaryPackageDirsRecursively(webDir)); | 200 .then((_) => _linkSecondaryPackageDirsRecursively(webDir)); |
| 201 } | 201 } |
| 202 | 202 |
| 203 /// Creates a symlink to the `packages` directory in [dir] and all its | 203 /// Creates a symlink to the `packages` directory in [dir] and all its |
| 204 /// subdirectories. | 204 /// subdirectories. |
| 205 Future _linkSecondaryPackageDirsRecursively(String dir) { | 205 Future _linkSecondaryPackageDirsRecursively(String dir) { |
| 206 return dirExists(dir).then((exists) { | 206 return dirExists(dir).then((exists) { |
| 207 if (!exists) return new Future.immediate(null); | 207 if (!exists) return; |
| 208 return _linkSecondaryPackageDir(dir) | 208 return _linkSecondaryPackageDir(dir) |
| 209 .then((_) => _listDirWithoutPackages(dir)) | 209 .then((_) => _listDirWithoutPackages(dir)) |
| 210 .then((files) { | 210 .then((files) { |
| 211 return Future.wait(files.mappedBy((file) { | 211 return Future.wait(files.mappedBy((file) { |
| 212 return dirExists(file).then((isDir) { | 212 return dirExists(file).then((isDir) { |
| 213 if (!isDir) return new Future.immediate(null); | 213 if (!isDir) return; |
| 214 return _linkSecondaryPackageDir(file); | 214 return _linkSecondaryPackageDir(file); |
| 215 }); | 215 }); |
| 216 })); | 216 })); |
| 217 }); | 217 }); |
| 218 }); | 218 }); |
| 219 } | 219 } |
| 220 | 220 |
| 221 // TODO(nweiz): roll this into [listDir] in io.dart once issue 4775 is fixed. | 221 // TODO(nweiz): roll this into [listDir] in io.dart once issue 4775 is fixed. |
| 222 /// Recursively lists the contents of [dir], excluding hidden `.DS_Store` | 222 /// Recursively lists the contents of [dir], excluding hidden `.DS_Store` |
| 223 /// files and `package` files. | 223 /// files and `package` files. |
| 224 Future<List<String>> _listDirWithoutPackages(dir) { | 224 Future<List<String>> _listDirWithoutPackages(dir) { |
| 225 return listDir(dir).then((files) { | 225 return listDir(dir).then((files) { |
| 226 return Future.wait(files.mappedBy((file) { | 226 return Future.wait(files.mappedBy((file) { |
| 227 if (basename(file) == 'packages') return new Future.immediate([]); | 227 if (basename(file) == 'packages') return new Future.immediate([]); |
| 228 return dirExists(file).then((isDir) { | 228 return dirExists(file).then((isDir) { |
| 229 if (!isDir) return new Future.immediate([]); | 229 if (!isDir) return []; |
| 230 return _listDirWithoutPackages(file); | 230 return _listDirWithoutPackages(file); |
| 231 }).then((subfiles) { | 231 }).then((subfiles) { |
| 232 var fileAndSubfiles = [file]; | 232 var fileAndSubfiles = [file]; |
| 233 fileAndSubfiles.addAll(subfiles); | 233 fileAndSubfiles.addAll(subfiles); |
| 234 return fileAndSubfiles; | 234 return fileAndSubfiles; |
| 235 }); | 235 }); |
| 236 })); | 236 })); |
| 237 }).then(flatten); | 237 }).then(flatten); |
| 238 } | 238 } |
| 239 | 239 |
| 240 /// Creates a symlink to the `packages` directory in [dir] if none exists. | 240 /// Creates a symlink to the `packages` directory in [dir] if none exists. |
| 241 Future _linkSecondaryPackageDir(String dir) { | 241 Future _linkSecondaryPackageDir(String dir) { |
| 242 var to = join(dir, 'packages'); | 242 var to = join(dir, 'packages'); |
| 243 return exists(to).then((exists) { | 243 return exists(to).then((exists) { |
| 244 if (exists) return new Future.immediate(null); | 244 if (exists) return; |
| 245 return createSymlink(path, to); | 245 return createSymlink(path, to); |
| 246 }); | 246 }); |
| 247 } | 247 } |
| 248 } | 248 } |
| OLD | NEW |