OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /// Helper functionality to make working with IO easier. | 5 /// Helper functionality to make working with IO easier. |
6 library pub.io; | 6 library pub.io; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:collection'; | 9 import 'dart:collection'; |
10 import 'dart:convert'; | 10 import 'dart:convert'; |
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 {bool isSelfLink: false, bool relative: false}) { | 468 {bool isSelfLink: false, bool relative: false}) { |
469 // See if the package has a "lib" directory. If not, there's nothing to | 469 // See if the package has a "lib" directory. If not, there's nothing to |
470 // symlink to. | 470 // symlink to. |
471 target = path.join(target, 'lib'); | 471 target = path.join(target, 'lib'); |
472 if (!dirExists(target)) return; | 472 if (!dirExists(target)) return; |
473 | 473 |
474 log.fine("Creating ${isSelfLink ? "self" : ""}link for package '$name'."); | 474 log.fine("Creating ${isSelfLink ? "self" : ""}link for package '$name'."); |
475 createSymlink(target, symlink, relative: relative); | 475 createSymlink(target, symlink, relative: relative); |
476 } | 476 } |
477 | 477 |
| 478 /// Whether the current process is one of pub's test files. |
| 479 /// |
| 480 /// This works because an actual pub executable that imports this will always |
| 481 /// start with "pub". |
| 482 final bool runningAsTest = |
| 483 !path.url.basename(Platform.script.path).startsWith('pub.'); |
| 484 |
| 485 /// Whether the current process is a pub subprocess being run from a test. |
| 486 /// |
| 487 /// This works because when running from tests, pub always uses a snapshot named |
| 488 /// "pub.test.snapshot", which is not used outside of tests. |
| 489 final bool runningFromTest = Platform.script.path.endsWith('.test.snapshot'); |
| 490 |
478 /// Whether pub is running from within the Dart SDK, as opposed to from the Dart | 491 /// Whether pub is running from within the Dart SDK, as opposed to from the Dart |
479 /// source repository. | 492 /// source repository. |
480 final bool runningFromSdk = Platform.script.path.endsWith('.dart.snapshot'); | 493 final bool runningFromSdk = |
| 494 !runningFromTest && Platform.script.path.endsWith('.snapshot'); |
| 495 |
| 496 /// Whether pub is running from source in the Dart repo. |
| 497 /// |
| 498 /// This can happen when building Observatory, for example. |
| 499 final bool runningFromDartRepo = |
| 500 Platform.script.path.endsWith('/third_party/pkg/pub/bin/pub.dart'); |
481 | 501 |
482 /// Resolves [target] relative to the path to pub's `asset` directory. | 502 /// Resolves [target] relative to the path to pub's `asset` directory. |
483 String assetPath(String target) { | 503 String assetPath(String target) => runningFromSdk |
484 if (runningFromSdk) { | 504 ? sdkAssetPath(target) |
485 return path.join( | 505 : path.join(pubRoot, 'lib', 'src', 'asset', target); |
486 sdk.rootDirectory, 'lib', '_internal', 'pub', 'asset', target); | 506 |
487 } else { | 507 /// Resolves [target] relative to the Dart SDK's `asset` directory. |
488 return path.join(pubRoot, 'asset', target); | 508 /// |
| 509 /// Throws a [StateError] if called from within the Dart repo. |
| 510 String sdkAssetPath(String target) { |
| 511 if (runningFromDartRepo) { |
| 512 throw new StateError("Can't get SDK assets from within the Dart repo."); |
489 } | 513 } |
| 514 |
| 515 return path.join( |
| 516 sdk.rootDirectory, 'lib', '_internal', 'pub', 'asset', target); |
490 } | 517 } |
491 | 518 |
492 /// Returns the path to the root of pub's sources in the Dart repo. | 519 /// The path to the root of pub's sources in the pub repo. |
493 String get pubRoot => path.join(repoRoot, 'sdk', 'lib', '_internal', 'pub'); | |
494 | |
495 /// Returns the path to the root of the Dart repository. | |
496 /// | 520 /// |
497 /// This throws a [StateError] if it's called when running pub from the SDK. | 521 /// This throws a [StateError] if it's called when running pub from the SDK. |
498 String get repoRoot { | 522 final String pubRoot = (() { |
499 if (runningFromSdk) { | 523 if (runningFromSdk) { |
500 throw new StateError("Can't get the repo root from the SDK."); | 524 throw new StateError("Can't get pub's root from the SDK."); |
501 } | 525 } |
502 | 526 |
503 // Get the path to the directory containing this very file. | 527 var script = path.fromUri(Platform.script); |
504 var libDir = path.dirname(libraryPath('pub.io')); | 528 if (runningAsTest) { |
| 529 // Running from "test/../some_test.dart". |
| 530 var components = path.split(script); |
| 531 var testIndex = components.indexOf("test"); |
| 532 if (testIndex == -1) throw new StateError("Can't find pub's root."); |
| 533 return path.joinAll(components.take(testIndex)); |
| 534 } |
505 | 535 |
506 // Assume we're running directly from the source location in the repo: | 536 // Pub is either run from ".pub/pub.test.snapshot" or "bin/pub.dart". |
507 // | 537 return path.dirname(path.dirname(script)); |
508 // <repo>/sdk/lib/_internal/pub/lib/src | 538 })(); |
509 return path.normalize(path.join(libDir, '..', '..', '..', '..', '..', '..')); | 539 |
510 } | 540 /// The path to the root of the Dart repo. |
| 541 /// |
| 542 /// This throws a [StateError] if it's called when not running pub from source |
| 543 /// in the Dart repo. |
| 544 final String dartRepoRoot = (() { |
| 545 if (!runningFromDartRepo) { |
| 546 throw new StateError("Not running from source in the Dart repo."); |
| 547 } |
| 548 |
| 549 // In the Dart repo, the script is in "third_party/pkg/pub/bin". |
| 550 return path.dirname(path.dirname(path.dirname(path.dirname(path.dirname( |
| 551 path.fromUri(Platform.script)))))); |
| 552 })(); |
511 | 553 |
512 /// A line-by-line stream of standard input. | 554 /// A line-by-line stream of standard input. |
513 final Stream<String> stdinLines = streamToLines( | 555 final Stream<String> stdinLines = streamToLines( |
514 new ByteStream(stdin).toStringStream()); | 556 new ByteStream(stdin).toStringStream()); |
515 | 557 |
516 /// Displays a message and reads a yes/no confirmation from the user. | 558 /// Displays a message and reads a yes/no confirmation from the user. |
517 /// | 559 /// |
518 /// Returns a [Future] that completes to `true` if the user confirms or `false` | 560 /// Returns a [Future] that completes to `true` if the user confirms or `false` |
519 /// if they do not. | 561 /// if they do not. |
520 /// | 562 /// |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
866 | 908 |
867 var match = new RegExp(r"^tar \(GNU tar\) (\d+).(\d+)\n") | 909 var match = new RegExp(r"^tar \(GNU tar\) (\d+).(\d+)\n") |
868 .firstMatch(result.stdout); | 910 .firstMatch(result.stdout); |
869 if (match == null) return false; | 911 if (match == null) return false; |
870 | 912 |
871 var major = int.parse(match[1]); | 913 var major = int.parse(match[1]); |
872 var minor = int.parse(match[2]); | 914 var minor = int.parse(match[2]); |
873 return major >= 2 || (major == 1 && minor >= 23); | 915 return major >= 2 || (major == 1 && minor >= 23); |
874 } | 916 } |
875 | 917 |
876 String get pathTo7zip { | 918 final String pathTo7zip = (() { |
877 if (runningFromSdk) return assetPath(path.join('7zip', '7za.exe')); | 919 if (!runningFromDartRepo) return sdkAssetPath(path.join('7zip', '7za.exe')); |
878 return path.join(repoRoot, 'third_party', '7zip', '7za.exe'); | 920 return path.join(dartRepoRoot, 'third_party', '7zip', '7za.exe'); |
879 } | 921 })(); |
880 | 922 |
881 Future<bool> _extractTarGzWindows(Stream<List<int>> stream, | 923 Future<bool> _extractTarGzWindows(Stream<List<int>> stream, |
882 String destination) { | 924 String destination) { |
883 // TODO(rnystrom): In the repo's history, there is an older implementation of | 925 // TODO(rnystrom): In the repo's history, there is an older implementation of |
884 // this that does everything in memory by piping streams directly together | 926 // this that does everything in memory by piping streams directly together |
885 // instead of writing out temp files. The code is simpler, but unfortunately, | 927 // instead of writing out temp files. The code is simpler, but unfortunately, |
886 // 7zip seems to periodically fail when we invoke it from Dart and tell it to | 928 // 7zip seems to periodically fail when we invoke it from Dart and tell it to |
887 // read from stdin instead of a file. Consider resurrecting that version if | 929 // read from stdin instead of a file. Consider resurrecting that version if |
888 // we can figure out why it fails. | 930 // we can figure out why it fails. |
889 | 931 |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1010 | 1052 |
1011 // TODO(rnystrom): Remove this and change to returning one string. | 1053 // TODO(rnystrom): Remove this and change to returning one string. |
1012 static List<String> _toLines(String output) { | 1054 static List<String> _toLines(String output) { |
1013 var lines = splitLines(output); | 1055 var lines = splitLines(output); |
1014 if (!lines.isEmpty && lines.last == "") lines.removeLast(); | 1056 if (!lines.isEmpty && lines.last == "") lines.removeLast(); |
1015 return lines; | 1057 return lines; |
1016 } | 1058 } |
1017 | 1059 |
1018 bool get success => exitCode == exit_codes.SUCCESS; | 1060 bool get success => exitCode == exit_codes.SUCCESS; |
1019 } | 1061 } |
OLD | NEW |