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 'io.dart'; | 8 import 'io.dart'; |
8 import 'lock_file.dart'; | 9 import 'lock_file.dart'; |
9 import 'log.dart' as log; | 10 import 'log.dart' as log; |
10 import 'package.dart'; | 11 import 'package.dart'; |
11 import 'root_source.dart'; | 12 import 'root_source.dart'; |
12 import 'system_cache.dart'; | 13 import 'system_cache.dart'; |
13 import 'utils.dart'; | 14 import 'utils.dart'; |
14 import 'version.dart'; | 15 import 'version.dart'; |
15 import 'version_solver.dart'; | 16 import 'version_solver.dart'; |
16 | 17 |
(...skipping 22 matching lines...) Expand all Loading... |
39 | 40 |
40 /// Packages which are either currently being asynchronously installed to the | 41 /// Packages which are either currently being asynchronously installed to the |
41 /// directory, or have already been installed. | 42 /// directory, or have already been installed. |
42 final Map<PackageId, Future<PackageId>> _installs; | 43 final Map<PackageId, Future<PackageId>> _installs; |
43 | 44 |
44 Entrypoint(this.root, this.cache) | 45 Entrypoint(this.root, this.cache) |
45 : _installs = new Map<PackageId, Future<PackageId>>(); | 46 : _installs = new Map<PackageId, Future<PackageId>>(); |
46 | 47 |
47 /// Loads the entrypoint from a package at [rootDir]. | 48 /// Loads the entrypoint from a package at [rootDir]. |
48 static Future<Entrypoint> load(String rootDir, SystemCache cache) { | 49 static Future<Entrypoint> load(String rootDir, SystemCache cache) { |
49 return Package.load(null, rootDir, cache.sources).transform((package) => | 50 return Package.load(null, rootDir, cache.sources).then((package) => |
50 new Entrypoint(package, cache)); | 51 new Entrypoint(package, cache)); |
51 } | 52 } |
52 | 53 |
53 // TODO(rnystrom): Make this path configurable. | 54 // TODO(rnystrom): Make this path configurable. |
54 /// The path to this "packages" directory. | 55 /// The path to this "packages" directory. |
55 String get path => join(root.dir, 'packages'); | 56 String get path => join(root.dir, 'packages'); |
56 | 57 |
57 /// 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. |
58 /// Returns the resolved [PackageId]. | 59 /// Returns the resolved [PackageId]. |
59 /// | 60 /// |
60 /// If this completes successfully, the package is guaranteed to be importable | 61 /// If this completes successfully, the package is guaranteed to be importable |
61 /// using the `package:` scheme. | 62 /// using the `package:` scheme. |
62 /// | 63 /// |
63 /// 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 |
64 /// well if it requires network access to retrieve (specifically, if | 65 /// well if it requires network access to retrieve (specifically, if |
65 /// `id.source.shouldCache` is true). | 66 /// `id.source.shouldCache` is true). |
66 /// | 67 /// |
67 /// See also [installDependencies]. | 68 /// See also [installDependencies]. |
68 Future<PackageId> install(PackageId id) { | 69 Future<PackageId> install(PackageId id) { |
69 var pendingOrCompleted = _installs[id]; | 70 var pendingOrCompleted = _installs[id]; |
70 if (pendingOrCompleted != null) return pendingOrCompleted; | 71 if (pendingOrCompleted != null) return pendingOrCompleted; |
71 | 72 |
72 var packageDir = join(path, id.name); | 73 var packageDir = join(path, id.name); |
73 var future = ensureDir(dirname(packageDir)).chain((_) { | 74 var future = ensureDir(dirname(packageDir)).then((_) { |
74 return exists(packageDir); | 75 return exists(packageDir); |
75 }).chain((exists) { | 76 }).then((exists) { |
76 if (!exists) return new Future.immediate(null); | 77 if (!exists) return new Future.immediate(null); |
77 // 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 |
78 // we can just re-use the existing symlink. | 79 // we can just re-use the existing symlink. |
79 log.fine("Deleting package directory for ${id.name} before install."); | 80 log.fine("Deleting package directory for ${id.name} before install."); |
80 return deleteDir(packageDir); | 81 return deleteDir(packageDir); |
81 }).chain((_) { | 82 }).then((_) { |
82 if (id.source.shouldCache) { | 83 if (id.source.shouldCache) { |
83 return cache.install(id).chain( | 84 return cache.install(id).then( |
84 (pkg) => createPackageSymlink(id.name, pkg.dir, packageDir)); | 85 (pkg) => createPackageSymlink(id.name, pkg.dir, packageDir)); |
85 } else { | 86 } else { |
86 return id.source.install(id, packageDir).transform((found) { | 87 return id.source.install(id, packageDir).then((found) { |
87 if (found) return null; | 88 if (found) return null; |
88 // TODO(nweiz): More robust error-handling. | 89 // TODO(nweiz): More robust error-handling. |
89 throw 'Package ${id.name} not found in source "${id.source.name}".'; | 90 throw 'Package ${id.name} not found in source "${id.source.name}".'; |
90 }); | 91 }); |
91 } | 92 } |
92 }).chain((_) => id.resolved); | 93 }).then((_) => id.resolved); |
93 | 94 |
94 _installs[id] = future; | 95 _installs[id] = future; |
95 | 96 |
96 return future; | 97 return future; |
97 } | 98 } |
98 | 99 |
99 /// Installs all dependencies of the [root] package to its "packages" | 100 /// Installs all dependencies of the [root] package to its "packages" |
100 /// directory, respecting the [LockFile] if present. Returns a [Future] that | 101 /// directory, respecting the [LockFile] if present. Returns a [Future] that |
101 /// completes when all dependencies are installed. | 102 /// completes when all dependencies are installed. |
102 Future installDependencies() { | 103 Future installDependencies() { |
103 return loadLockFile() | 104 return loadLockFile() |
104 .chain((lockFile) => resolveVersions(cache.sources, root, lockFile)) | 105 .then((lockFile) => resolveVersions(cache.sources, root, lockFile)) |
105 .chain(_installDependencies); | 106 .then(_installDependencies); |
106 } | 107 } |
107 | 108 |
108 /// Installs the latest available versions of all dependencies of the [root] | 109 /// Installs the latest available versions of all dependencies of the [root] |
109 /// package to its "package" directory, writing a new [LockFile]. Returns a | 110 /// package to its "package" directory, writing a new [LockFile]. Returns a |
110 /// [Future] that completes when all dependencies are installed. | 111 /// [Future] that completes when all dependencies are installed. |
111 Future updateAllDependencies() { | 112 Future updateAllDependencies() { |
112 return resolveVersions(cache.sources, root, new LockFile.empty()) | 113 return resolveVersions(cache.sources, root, new LockFile.empty()) |
113 .chain(_installDependencies); | 114 .then(_installDependencies); |
114 } | 115 } |
115 | 116 |
116 /// Installs the latest available versions of [dependencies], while leaving | 117 /// Installs the latest available versions of [dependencies], while leaving |
117 /// other dependencies as specified by the [LockFile] if possible. Returns a | 118 /// other dependencies as specified by the [LockFile] if possible. Returns a |
118 /// [Future] that completes when all dependencies are installed. | 119 /// [Future] that completes when all dependencies are installed. |
119 Future updateDependencies(List<String> dependencies) { | 120 Future updateDependencies(List<String> dependencies) { |
120 return loadLockFile().chain((lockFile) { | 121 return loadLockFile().then((lockFile) { |
121 var versionSolver = new VersionSolver(cache.sources, root, lockFile); | 122 var versionSolver = new VersionSolver(cache.sources, root, lockFile); |
122 for (var dependency in dependencies) { | 123 for (var dependency in dependencies) { |
123 versionSolver.useLatestVersion(dependency); | 124 versionSolver.useLatestVersion(dependency); |
124 } | 125 } |
125 return versionSolver.solve(); | 126 return versionSolver.solve(); |
126 }).chain(_installDependencies); | 127 }).then(_installDependencies); |
127 } | 128 } |
128 | 129 |
129 /// Removes the old packages directory, installs all dependencies listed in | 130 /// Removes the old packages directory, installs all dependencies listed in |
130 /// [packageVersions], and writes a [LockFile]. | 131 /// [packageVersions], and writes a [LockFile]. |
131 Future _installDependencies(List<PackageId> packageVersions) { | 132 Future _installDependencies(List<PackageId> packageVersions) { |
132 return cleanDir(path).chain((_) { | 133 return cleanDir(path).then((_) { |
133 return Futures.wait(packageVersions.map((id) { | 134 return Futures.wait(packageVersions.mappedBy((id) { |
134 if (id.source is RootSource) return new Future.immediate(id); | 135 if (id.source is RootSource) return new Future.immediate(id); |
135 return install(id); | 136 return install(id); |
136 })); | 137 })); |
137 }).chain(_saveLockFile) | 138 }).then(_saveLockFile) |
138 .chain(_installSelfReference) | 139 .then(_installSelfReference) |
139 .chain(_linkSecondaryPackageDirs); | 140 .then(_linkSecondaryPackageDirs); |
140 } | 141 } |
141 | 142 |
142 /// 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 |
143 /// exists. If it doesn't, this completes to an empty [LockFile]. | 144 /// exists. If it doesn't, this completes to an empty [LockFile]. |
144 Future<LockFile> loadLockFile() { | 145 Future<LockFile> loadLockFile() { |
145 var lockFilePath = join(root.dir, 'pubspec.lock'); | 146 var lockFilePath = join(root.dir, 'pubspec.lock'); |
146 | 147 |
147 log.fine("Loading lockfile."); | 148 log.fine("Loading lockfile."); |
148 return fileExists(lockFilePath).chain((exists) { | 149 return fileExists(lockFilePath).then((exists) { |
149 if (!exists) { | 150 if (!exists) { |
150 log.fine("No lock file at $lockFilePath, creating empty one."); | 151 log.fine("No lock file at $lockFilePath, creating empty one."); |
151 return new Future<LockFile>.immediate(new LockFile.empty()); | 152 return new Future<LockFile>.immediate(new LockFile.empty()); |
152 } | 153 } |
153 | 154 |
154 return readTextFile(lockFilePath).transform((text) => | 155 return readTextFile(lockFilePath).then((text) => |
155 new LockFile.parse(text, cache.sources)); | 156 new LockFile.parse(text, cache.sources)); |
156 }); | 157 }); |
157 } | 158 } |
158 | 159 |
159 /// 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. |
160 Future _saveLockFile(List<PackageId> packageIds) { | 161 Future _saveLockFile(List<PackageId> packageIds) { |
161 var lockFile = new LockFile.empty(); | 162 var lockFile = new LockFile.empty(); |
162 for (var id in packageIds) { | 163 for (var id in packageIds) { |
163 if (id.source is! RootSource) lockFile.packages[id.name] = id; | 164 if (id.source is! RootSource) lockFile.packages[id.name] = id; |
164 } | 165 } |
165 | 166 |
166 var lockFilePath = join(root.dir, 'pubspec.lock'); | 167 var lockFilePath = join(root.dir, 'pubspec.lock'); |
167 log.fine("Saving lockfile."); | 168 log.fine("Saving lockfile."); |
168 return writeTextFile(lockFilePath, lockFile.serialize()); | 169 return writeTextFile(lockFilePath, lockFile.serialize()); |
169 } | 170 } |
170 | 171 |
171 /// Installs a self-referential symlink in the `packages` directory that will | 172 /// Installs a self-referential symlink in the `packages` directory that will |
172 /// allow a package to import its own files using `package:`. | 173 /// allow a package to import its own files using `package:`. |
173 Future _installSelfReference(_) { | 174 Future _installSelfReference(_) { |
174 var linkPath = join(path, root.name); | 175 var linkPath = join(path, root.name); |
175 return exists(linkPath).chain((exists) { | 176 return exists(linkPath).then((exists) { |
176 // Create the symlink if it doesn't exist. | 177 // Create the symlink if it doesn't exist. |
177 if (exists) return new Future.immediate(null); | 178 if (exists) return new Future.immediate(null); |
178 return ensureDir(path).chain( | 179 return ensureDir(path).then( |
179 (_) => createPackageSymlink(root.name, root.dir, linkPath, | 180 (_) => createPackageSymlink(root.name, root.dir, linkPath, |
180 isSelfLink: true)); | 181 isSelfLink: true)); |
181 }); | 182 }); |
182 } | 183 } |
183 | 184 |
184 /// If `bin/`, `test/`, or `example/` directories exist, symlink `packages/` | 185 /// If `bin/`, `test/`, or `example/` directories exist, symlink `packages/` |
185 /// 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 |
186 /// subdirectories of `test/` and `example/`. | 187 /// subdirectories of `test/` and `example/`. |
187 Future _linkSecondaryPackageDirs(_) { | 188 Future _linkSecondaryPackageDirs(_) { |
188 var binDir = join(root.dir, 'bin'); | 189 var binDir = join(root.dir, 'bin'); |
189 var exampleDir = join(root.dir, 'example'); | 190 var exampleDir = join(root.dir, 'example'); |
190 var testDir = join(root.dir, 'test'); | 191 var testDir = join(root.dir, 'test'); |
191 var toolDir = join(root.dir, 'tool'); | 192 var toolDir = join(root.dir, 'tool'); |
192 var webDir = join(root.dir, 'web'); | 193 var webDir = join(root.dir, 'web'); |
193 return dirExists(binDir).chain((exists) { | 194 return dirExists(binDir).then((exists) { |
194 if (!exists) return new Future.immediate(null); | 195 if (!exists) return new Future.immediate(null); |
195 return _linkSecondaryPackageDir(binDir); | 196 return _linkSecondaryPackageDir(binDir); |
196 }).chain((_) => _linkSecondaryPackageDirsRecursively(exampleDir)) | 197 }).then((_) => _linkSecondaryPackageDirsRecursively(exampleDir)) |
197 .chain((_) => _linkSecondaryPackageDirsRecursively(testDir)) | 198 .then((_) => _linkSecondaryPackageDirsRecursively(testDir)) |
198 .chain((_) => _linkSecondaryPackageDirsRecursively(toolDir)) | 199 .then((_) => _linkSecondaryPackageDirsRecursively(toolDir)) |
199 .chain((_) => _linkSecondaryPackageDirsRecursively(webDir)); | 200 .then((_) => _linkSecondaryPackageDirsRecursively(webDir)); |
200 } | 201 } |
201 | 202 |
202 /// 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 |
203 /// subdirectories. | 204 /// subdirectories. |
204 Future _linkSecondaryPackageDirsRecursively(String dir) { | 205 Future _linkSecondaryPackageDirsRecursively(String dir) { |
205 return dirExists(dir).chain((exists) { | 206 return dirExists(dir).then((exists) { |
206 if (!exists) return new Future.immediate(null); | 207 if (!exists) return new Future.immediate(null); |
207 return _linkSecondaryPackageDir(dir) | 208 return _linkSecondaryPackageDir(dir) |
208 .chain((_) => _listDirWithoutPackages(dir)) | 209 .then((_) => _listDirWithoutPackages(dir)) |
209 .chain((files) { | 210 .then((files) { |
210 return Futures.wait(files.map((file) { | 211 return Futures.wait(files.mappedBy((file) { |
211 return dirExists(file).chain((isDir) { | 212 return dirExists(file).then((isDir) { |
212 if (!isDir) return new Future.immediate(null); | 213 if (!isDir) return new Future.immediate(null); |
213 return _linkSecondaryPackageDir(file); | 214 return _linkSecondaryPackageDir(file); |
214 }); | 215 }); |
215 })); | 216 })); |
216 }); | 217 }); |
217 }); | 218 }); |
218 } | 219 } |
219 | 220 |
220 // 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. |
221 /// Recursively lists the contents of [dir], excluding hidden `.DS_Store` | 222 /// Recursively lists the contents of [dir], excluding hidden `.DS_Store` |
222 /// files and `package` files. | 223 /// files and `package` files. |
223 Future<List<String>> _listDirWithoutPackages(dir) { | 224 Future<List<String>> _listDirWithoutPackages(dir) { |
224 return listDir(dir).chain((files) { | 225 return listDir(dir).then((files) { |
225 return Futures.wait(files.map((file) { | 226 return Futures.wait(files.mappedBy((file) { |
226 if (basename(file) == 'packages') return new Future.immediate([]); | 227 if (basename(file) == 'packages') return new Future.immediate([]); |
227 return dirExists(file).chain((isDir) { | 228 return dirExists(file).then((isDir) { |
228 if (!isDir) return new Future.immediate([]); | 229 if (!isDir) return new Future.immediate([]); |
229 return _listDirWithoutPackages(file); | 230 return _listDirWithoutPackages(file); |
230 }).transform((subfiles) { | 231 }).then((subfiles) { |
231 var fileAndSubfiles = [file]; | 232 var fileAndSubfiles = [file]; |
232 fileAndSubfiles.addAll(subfiles); | 233 fileAndSubfiles.addAll(subfiles); |
233 return fileAndSubfiles; | 234 return fileAndSubfiles; |
234 }); | 235 }); |
235 })); | 236 })); |
236 }).transform(flatten); | 237 }).then(flatten); |
237 } | 238 } |
238 | 239 |
239 /// 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. |
240 Future _linkSecondaryPackageDir(String dir) { | 241 Future _linkSecondaryPackageDir(String dir) { |
241 var to = join(dir, 'packages'); | 242 var to = join(dir, 'packages'); |
242 return exists(to).chain((exists) { | 243 return exists(to).then((exists) { |
243 if (exists) return new Future.immediate(null); | 244 if (exists) return new Future.immediate(null); |
244 return createSymlink(path, to); | 245 return createSymlink(path, to); |
245 }); | 246 }); |
246 } | 247 } |
247 } | 248 } |
OLD | NEW |