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 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 | 395 |
396 return source.descriptionsEqual(package.description, locked.description); | 396 return source.descriptionsEqual(package.description, locked.description); |
397 }); | 397 }); |
398 } | 398 } |
399 | 399 |
400 /// Determines whether all of the packages in the lockfile are already | 400 /// Determines whether all of the packages in the lockfile are already |
401 /// installed and available. | 401 /// installed and available. |
402 /// | 402 /// |
403 /// Note: this assumes [isLockFileUpToDate] has already been called and | 403 /// Note: this assumes [isLockFileUpToDate] has already been called and |
404 /// returned `true`. | 404 /// returned `true`. |
405 Future<bool> _arePackagesAvailable(LockFile lockFile) { | 405 bool _arePackagesAvailable(LockFile lockFile) { |
406 return Future.wait(lockFile.packages.values.map((package) { | 406 return lockFile.packages.values.every((package) { |
407 var source = cache.sources[package.source]; | 407 var source = cache.sources[package.source]; |
408 | 408 |
409 // This should only be called after [_isLockFileUpToDate] has returned | 409 // This should only be called after [_isLockFileUpToDate] has returned |
410 // `true`, which ensures all of the sources in the lock file are valid. | 410 // `true`, which ensures all of the sources in the lock file are valid. |
411 assert(source != null); | 411 assert(source != null); |
412 | 412 |
413 // We only care about cached sources. Uncached sources aren't "installed". | 413 // We only care about cached sources. Uncached sources aren't "installed". |
414 // If one of those is missing, we want to show the user the file not | 414 // If one of those is missing, we want to show the user the file not |
415 // found error later since installing won't accomplish anything. | 415 // found error later since installing won't accomplish anything. |
416 if (source is! CachedSource) return new Future.value(true); | 416 if (source is! CachedSource) return true; |
417 | 417 |
418 // Get the directory. | 418 // Get the directory. |
419 return source.getDirectory(package).then((dir) { | 419 var dir = source.getDirectory(package); |
420 // See if the directory is there and looks like a package. | 420 // See if the directory is there and looks like a package. |
421 return dirExists(dir) || fileExists(path.join(dir, "pubspec.yaml")); | 421 return dirExists(dir) || fileExists(path.join(dir, "pubspec.yaml")); |
422 }); | |
423 })).then((results) { | |
424 // Make sure they are all true. | |
425 return results.every((result) => result); | |
426 }); | 422 }); |
427 } | 423 } |
428 | 424 |
429 /// Gets dependencies if the lockfile is out of date with respect to the | 425 /// Gets dependencies if the lockfile is out of date with respect to the |
430 /// pubspec. | 426 /// pubspec. |
431 Future ensureLockFileIsUpToDate() async { | 427 Future ensureLockFileIsUpToDate() async { |
432 if (!lockFileExists) { | 428 if (!lockFileExists) { |
433 log.message( | 429 log.message( |
434 "You don't have a lockfile, so we need to generate that:"); | 430 "You don't have a lockfile, so we need to generate that:"); |
435 } else if (_isLockFileUpToDate(lockFile)) { | 431 } else if (_isLockFileUpToDate(lockFile)) { |
436 // If we do have a lock file, we still need to make sure the packages are | 432 // If we do have a lock file, we still need to make sure the packages are |
437 // actually installed. The user may have just gotten a package that | 433 // actually installed. The user may have just gotten a package that |
438 // includes a lockfile. | 434 // includes a lockfile. |
439 if (await _arePackagesAvailable(lockFile)) return; | 435 if (_arePackagesAvailable(lockFile)) return; |
440 | 436 |
441 // If we don't have a current lock file, we definitely need to install. | 437 // If we don't have a current lock file, we definitely need to install. |
442 log.message( | 438 log.message( |
443 "You are missing some dependencies, so we need to install them " | 439 "You are missing some dependencies, so we need to install them " |
444 "first:"); | 440 "first:"); |
445 } else { | 441 } else { |
446 log.message( | 442 log.message( |
447 "Your pubspec has changed, so we need to update your lockfile:"); | 443 "Your pubspec has changed, so we need to update your lockfile:"); |
448 } | 444 } |
449 | 445 |
450 await acquireDependencies(SolveType.GET); | 446 await acquireDependencies(SolveType.GET); |
451 } | 447 } |
452 | 448 |
453 /// Loads the package graph for the application and all of its transitive | 449 /// Loads the package graph for the application and all of its transitive |
454 /// dependencies. | 450 /// dependencies. |
455 /// | 451 /// |
456 /// If [result] is passed, this loads the graph from it without re-parsing the | 452 /// If [result] is passed, this loads the graph from it without re-parsing the |
457 /// lockfile or any pubspecs. Otherwise, before loading, this makes sure the | 453 /// lockfile or any pubspecs. Otherwise, before loading, this makes sure the |
458 /// lockfile and dependencies are installed and up to date. | 454 /// lockfile and dependencies are installed and up to date. |
459 Future<PackageGraph> loadPackageGraph([SolveResult result]) async { | 455 Future<PackageGraph> loadPackageGraph([SolveResult result]) async { |
460 if (_packageGraph != null) return _packageGraph; | 456 if (_packageGraph != null) return _packageGraph; |
461 | 457 |
462 var graph = await log.progress("Loading package graph", () async { | 458 var graph = await log.progress("Loading package graph", () async { |
463 if (result != null) { | 459 if (result != null) { |
464 var packages = await Future.wait(result.packages.map((id) async { | 460 var packages = new Map.fromIterable(result.packages, |
| 461 key: (id) => id.name, |
| 462 value: (id) { |
465 if (id.name == root.name) return root; | 463 if (id.name == root.name) return root; |
466 | 464 |
467 var dir = await cache.sources[id.source].getDirectory(id); | 465 return new Package(result.pubspecs[id.name], |
468 return new Package(result.pubspecs[id.name], dir); | 466 cache.sources[id.source].getDirectory(id)); |
469 })); | 467 }); |
470 | 468 |
471 return new PackageGraph(this, new LockFile(result.packages), | 469 return new PackageGraph(this, new LockFile(result.packages), packages); |
472 new Map.fromIterable(packages, key: (package) => package.name)); | |
473 } | 470 } |
474 | 471 |
475 await ensureLockFileIsUpToDate(); | 472 await ensureLockFileIsUpToDate(); |
476 var packages = await Future.wait(lockFile.packages.values.map((id) async { | 473 var packages = new Map.fromIterable(lockFile.packages.values, |
477 var source = cache.sources[id.source]; | 474 key: (id) => id.name, |
478 var dir = await source.getDirectory(id); | 475 value: (id) { |
| 476 var dir = cache.sources[id.source].getDirectory(id); |
479 return new Package.load(id.name, dir, cache.sources); | 477 return new Package.load(id.name, dir, cache.sources); |
480 })); | 478 }); |
481 | 479 packages[root.name] = root; |
482 var packageMap = new Map.fromIterable(packages, key: (p) => p.name); | 480 return new PackageGraph(this, lockFile, packages); |
483 packageMap[root.name] = root; | |
484 return new PackageGraph(this, lockFile, packageMap); | |
485 }, fine: true); | 481 }, fine: true); |
486 | 482 |
487 _packageGraph = graph; | 483 _packageGraph = graph; |
488 return graph; | 484 return graph; |
489 } | 485 } |
490 | 486 |
491 /// Saves a list of concrete package versions to the `pubspec.lock` file. | 487 /// Saves a list of concrete package versions to the `pubspec.lock` file. |
492 void _saveLockFile(List<PackageId> packageIds) { | 488 void _saveLockFile(List<PackageId> packageIds) { |
493 _lockFile = new LockFile(packageIds); | 489 _lockFile = new LockFile(packageIds); |
494 var lockFilePath = root.path('pubspec.lock'); | 490 var lockFilePath = root.path('pubspec.lock'); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
552 /// If [packageSymlinks] is true, creates a symlink to the "packages" | 548 /// If [packageSymlinks] is true, creates a symlink to the "packages" |
553 /// directory in [dir]. | 549 /// directory in [dir]. |
554 /// | 550 /// |
555 /// Otherwise, deletes a "packages" directories in [dir] if one exists. | 551 /// Otherwise, deletes a "packages" directories in [dir] if one exists. |
556 void _linkOrDeleteSecondaryPackageDir(String dir) { | 552 void _linkOrDeleteSecondaryPackageDir(String dir) { |
557 var symlink = path.join(dir, 'packages'); | 553 var symlink = path.join(dir, 'packages'); |
558 if (entryExists(symlink)) deleteEntry(symlink); | 554 if (entryExists(symlink)) deleteEntry(symlink); |
559 if (_packageSymlinks) createSymlink(packagesDir, symlink, relative: true); | 555 if (_packageSymlinks) createSymlink(packagesDir, symlink, relative: true); |
560 } | 556 } |
561 } | 557 } |
OLD | NEW |