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 pub.entrypoint; | 5 library pub.entrypoint; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:path/path.dart' as path; | 9 import 'package:path/path.dart' as path; |
10 import 'package:barback/barback.dart'; | 10 import 'package:barback/barback.dart'; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 Entrypoint(String rootDir, SystemCache cache, {bool packageSymlinks: true}) | 66 Entrypoint(String rootDir, SystemCache cache, {bool packageSymlinks: true}) |
67 : root = new Package.load(null, rootDir, cache.sources), | 67 : root = new Package.load(null, rootDir, cache.sources), |
68 cache = cache, | 68 cache = cache, |
69 _packageSymlinks = packageSymlinks; | 69 _packageSymlinks = packageSymlinks; |
70 | 70 |
71 /// Creates an entrypoint given package and lockfile objects. | 71 /// Creates an entrypoint given package and lockfile objects. |
72 Entrypoint.inMemory(this.root, this._lockFile, this.cache) | 72 Entrypoint.inMemory(this.root, this._lockFile, this.cache) |
73 : _packageSymlinks = false; | 73 : _packageSymlinks = false; |
74 | 74 |
75 /// The path to the entrypoint's "packages" directory. | 75 /// The path to the entrypoint's "packages" directory. |
76 String get packagesDir => path.join(root.dir, 'packages'); | 76 String get packagesDir => root.path('packages'); |
77 | 77 |
78 /// `true` if the entrypoint package currently has a lock file. | 78 /// `true` if the entrypoint package currently has a lock file. |
79 bool get lockFileExists => _lockFile != null || entryExists(lockFilePath); | 79 bool get lockFileExists => _lockFile != null || entryExists(lockFilePath); |
80 | 80 |
81 LockFile get lockFile { | 81 LockFile get lockFile { |
82 if (_lockFile != null) return _lockFile; | 82 if (_lockFile != null) return _lockFile; |
83 | 83 |
84 if (!lockFileExists) { | 84 if (!lockFileExists) { |
85 _lockFile = new LockFile.empty(); | 85 _lockFile = new LockFile.empty(); |
86 } else { | 86 } else { |
87 _lockFile = new LockFile.load(lockFilePath, cache.sources); | 87 _lockFile = new LockFile.load(lockFilePath, cache.sources); |
88 } | 88 } |
89 | 89 |
90 return _lockFile; | 90 return _lockFile; |
91 } | 91 } |
92 | 92 |
93 /// The path to the entrypoint package's pubspec. | 93 /// The path to the entrypoint package's pubspec. |
94 String get pubspecPath => path.join(root.dir, 'pubspec.yaml'); | 94 String get pubspecPath => root.path('pubspec.yaml'); |
95 | 95 |
96 /// The path to the entrypoint package's lockfile. | 96 /// The path to the entrypoint package's lockfile. |
97 String get lockFilePath => path.join(root.dir, 'pubspec.lock'); | 97 String get lockFilePath => root.path('pubspec.lock'); |
98 | 98 |
99 /// Gets all dependencies of the [root] package. | 99 /// Gets all dependencies of the [root] package. |
100 /// | 100 /// |
101 /// Performs version resolution according to [SolveType]. | 101 /// Performs version resolution according to [SolveType]. |
102 /// | 102 /// |
103 /// [useLatest], if provided, defines a list of packages that will be | 103 /// [useLatest], if provided, defines a list of packages that will be |
104 /// unlocked and forced to their latest versions. If [upgradeAll] is | 104 /// unlocked and forced to their latest versions. If [upgradeAll] is |
105 /// true, the previous lockfile is ignored and all packages are re-resolved | 105 /// true, the previous lockfile is ignored and all packages are re-resolved |
106 /// from scratch. Otherwise, it will attempt to preserve the versions of all | 106 /// from scratch. Otherwise, it will attempt to preserve the versions of all |
107 /// previously locked packages. | 107 /// previously locked packages. |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 changed); | 183 changed); |
184 }).map((package) => package.name).toSet(); | 184 }).map((package) => package.name).toSet(); |
185 | 185 |
186 if (dependenciesToPrecompile.isEmpty) return; | 186 if (dependenciesToPrecompile.isEmpty) return; |
187 | 187 |
188 await log.progress("Precompiling dependencies", () async { | 188 await log.progress("Precompiling dependencies", () async { |
189 var packagesToLoad = | 189 var packagesToLoad = |
190 unionAll(dependenciesToPrecompile.map(graph.transitiveDependencies)) | 190 unionAll(dependenciesToPrecompile.map(graph.transitiveDependencies)) |
191 .map((package) => package.name).toSet(); | 191 .map((package) => package.name).toSet(); |
192 | 192 |
| 193 for (var package in dependenciesToPrecompile) { |
| 194 deleteEntry(path.join(depsDir, package)); |
| 195 } |
| 196 |
193 var environment = await AssetEnvironment.create(this, BarbackMode.DEBUG, | 197 var environment = await AssetEnvironment.create(this, BarbackMode.DEBUG, |
194 packages: packagesToLoad, useDart2JS: false); | 198 packages: packagesToLoad, useDart2JS: false); |
195 | 199 |
196 /// Ignore barback errors since they'll be emitted via [getAllAssets] | 200 /// Ignore barback errors since they'll be emitted via [getAllAssets] |
197 /// below. | 201 /// below. |
198 environment.barback.errors.listen((_) {}); | 202 environment.barback.errors.listen((_) {}); |
199 | 203 |
200 for (var package in dependenciesToPrecompile) { | |
201 cleanDir(path.join(depsDir, package)); | |
202 } | |
203 | |
204 // TODO(nweiz): only get assets from [dependenciesToPrecompile] so as not | 204 // TODO(nweiz): only get assets from [dependenciesToPrecompile] so as not |
205 // to trigger unnecessary lazy transformers. | 205 // to trigger unnecessary lazy transformers. |
206 var assets = await environment.barback.getAllAssets(); | 206 var assets = await environment.barback.getAllAssets(); |
207 await waitAndPrintErrors(assets.map((asset) async { | 207 await waitAndPrintErrors(assets.map((asset) async { |
208 if (!dependenciesToPrecompile.contains(asset.id.package)) return; | 208 if (!dependenciesToPrecompile.contains(asset.id.package)) return; |
209 | 209 |
210 var destPath = path.join( | 210 var destPath = path.join( |
211 depsDir, asset.id.package, path.fromUri(asset.id.path)); | 211 depsDir, asset.id.package, path.fromUri(asset.id.path)); |
212 ensureDir(path.dirname(destPath)); | 212 ensureDir(path.dirname(destPath)); |
213 await createFileFromStream(asset.read(), destPath); | 213 await createFileFromStream(asset.read(), destPath); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 } | 285 } |
286 | 286 |
287 /// Returns the list of all executable assets for [packageName] that should be | 287 /// Returns the list of all executable assets for [packageName] that should be |
288 /// precompiled. | 288 /// precompiled. |
289 /// | 289 /// |
290 /// If [changed] isn't `null`, executables for [packageName] will only be | 290 /// If [changed] isn't `null`, executables for [packageName] will only be |
291 /// compiled if they might depend on a package in [changed]. | 291 /// compiled if they might depend on a package in [changed]. |
292 List<AssetId> _executablesForPackage(PackageGraph graph, String packageName, | 292 List<AssetId> _executablesForPackage(PackageGraph graph, String packageName, |
293 Set<String> changed) { | 293 Set<String> changed) { |
294 var package = graph.packages[packageName]; | 294 var package = graph.packages[packageName]; |
295 var binDir = path.join(package.dir, 'bin'); | 295 var binDir = package.path('bin'); |
296 if (!dirExists(binDir)) return []; | 296 if (!dirExists(binDir)) return []; |
297 if (graph.isPackageMutable(packageName)) return []; | 297 if (graph.isPackageMutable(packageName)) return []; |
298 | 298 |
299 var executables = package.executableIds; | 299 var executables = package.executableIds; |
300 | 300 |
301 // If we don't know which packages were changed, always precompile the | 301 // If we don't know which packages were changed, always precompile the |
302 // executables. | 302 // executables. |
303 if (changed == null) return executables; | 303 if (changed == null) return executables; |
304 | 304 |
305 // If any of the package's dependencies changed, recompile the executables. | 305 // If any of the package's dependencies changed, recompile the executables. |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 } | 459 } |
460 }).then((graph) { | 460 }).then((graph) { |
461 _packageGraph = graph; | 461 _packageGraph = graph; |
462 return graph; | 462 return graph; |
463 }); | 463 }); |
464 } | 464 } |
465 | 465 |
466 /// Saves a list of concrete package versions to the `pubspec.lock` file. | 466 /// Saves a list of concrete package versions to the `pubspec.lock` file. |
467 void _saveLockFile(List<PackageId> packageIds) { | 467 void _saveLockFile(List<PackageId> packageIds) { |
468 _lockFile = new LockFile(packageIds); | 468 _lockFile = new LockFile(packageIds); |
469 var lockFilePath = path.join(root.dir, 'pubspec.lock'); | 469 var lockFilePath = root.path('pubspec.lock'); |
470 writeTextFile(lockFilePath, _lockFile.serialize(root.dir, cache.sources)); | 470 writeTextFile(lockFilePath, _lockFile.serialize(root.dir, cache.sources)); |
471 } | 471 } |
472 | 472 |
473 /// Creates a self-referential symlink in the `packages` directory that allows | 473 /// Creates a self-referential symlink in the `packages` directory that allows |
474 /// a package to import its own files using `package:`. | 474 /// a package to import its own files using `package:`. |
475 void _linkSelf() { | 475 void _linkSelf() { |
476 var linkPath = path.join(packagesDir, root.name); | 476 var linkPath = path.join(packagesDir, root.name); |
477 // Create the symlink if it doesn't exist. | 477 // Create the symlink if it doesn't exist. |
478 if (entryExists(linkPath)) return; | 478 if (entryExists(linkPath)) return; |
479 ensureDir(packagesDir); | 479 ensureDir(packagesDir); |
480 createPackageSymlink(root.name, root.dir, linkPath, | 480 createPackageSymlink(root.name, root.dir, linkPath, |
481 isSelfLink: true, relative: true); | 481 isSelfLink: true, relative: true); |
482 } | 482 } |
483 | 483 |
484 /// If [packageSymlinks] is true, add "packages" directories to the whitelist | 484 /// If [packageSymlinks] is true, add "packages" directories to the whitelist |
485 /// of directories that may contain Dart entrypoints. | 485 /// of directories that may contain Dart entrypoints. |
486 /// | 486 /// |
487 /// Otherwise, delete any "packages" directories in the whitelist of | 487 /// Otherwise, delete any "packages" directories in the whitelist of |
488 /// directories that may contain Dart entrypoints. | 488 /// directories that may contain Dart entrypoints. |
489 void _linkOrDeleteSecondaryPackageDirs() { | 489 void _linkOrDeleteSecondaryPackageDirs() { |
490 // Only the main "bin" directory gets a "packages" directory, not its | 490 // Only the main "bin" directory gets a "packages" directory, not its |
491 // subdirectories. | 491 // subdirectories. |
492 var binDir = path.join(root.dir, 'bin'); | 492 var binDir = root.path('bin'); |
493 if (dirExists(binDir)) _linkOrDeleteSecondaryPackageDir(binDir); | 493 if (dirExists(binDir)) _linkOrDeleteSecondaryPackageDir(binDir); |
494 | 494 |
495 // The others get "packages" directories in subdirectories too. | 495 // The others get "packages" directories in subdirectories too. |
496 for (var dir in ['benchmark', 'example', 'test', 'tool', 'web']) { | 496 for (var dir in ['benchmark', 'example', 'test', 'tool', 'web']) { |
497 _linkOrDeleteSecondaryPackageDirsRecursively(path.join(root.dir, dir)); | 497 _linkOrDeleteSecondaryPackageDirsRecursively(root.path(dir)); |
498 } | 498 } |
499 } | 499 } |
500 | 500 |
501 /// If [packageSymlinks] is true, creates a symlink to the "packages" | 501 /// If [packageSymlinks] is true, creates a symlink to the "packages" |
502 /// directory in [dir] and all its subdirectories. | 502 /// directory in [dir] and all its subdirectories. |
503 /// | 503 /// |
504 /// Otherwise, deletes any "packages" directories in [dir] and all its | 504 /// Otherwise, deletes any "packages" directories in [dir] and all its |
505 /// subdirectories. | 505 /// subdirectories. |
506 void _linkOrDeleteSecondaryPackageDirsRecursively(String dir) { | 506 void _linkOrDeleteSecondaryPackageDirsRecursively(String dir) { |
507 if (!dirExists(dir)) return; | 507 if (!dirExists(dir)) return; |
(...skipping 19 matching lines...) Expand all Loading... |
527 /// If [packageSymlinks] is true, creates a symlink to the "packages" | 527 /// If [packageSymlinks] is true, creates a symlink to the "packages" |
528 /// directory in [dir]. | 528 /// directory in [dir]. |
529 /// | 529 /// |
530 /// Otherwise, deletes a "packages" directories in [dir] if one exists. | 530 /// Otherwise, deletes a "packages" directories in [dir] if one exists. |
531 void _linkOrDeleteSecondaryPackageDir(String dir) { | 531 void _linkOrDeleteSecondaryPackageDir(String dir) { |
532 var symlink = path.join(dir, 'packages'); | 532 var symlink = path.join(dir, 'packages'); |
533 if (entryExists(symlink)) deleteEntry(symlink); | 533 if (entryExists(symlink)) deleteEntry(symlink); |
534 if (_packageSymlinks) createSymlink(packagesDir, symlink, relative: true); | 534 if (_packageSymlinks) createSymlink(packagesDir, symlink, relative: true); |
535 } | 535 } |
536 } | 536 } |
OLD | NEW |