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 final bool runningAsTest = | |
480 !path.url.basename(Platform.script.path).startsWith('pub.'); | |
Bob Nystrom
2015/05/20 22:00:57
Document why this works. It's because the only *no
nweiz
2015/05/21 00:43:10
Done.
| |
481 | |
482 /// Whether the current process is a pub subprocess being run from a test. | |
483 final bool runningFromTest = Platform.script.path.endsWith('.test.snapshot'); | |
484 | |
478 /// Whether pub is running from within the Dart SDK, as opposed to from the Dart | 485 /// Whether pub is running from within the Dart SDK, as opposed to from the Dart |
479 /// source repository. | 486 /// source repository. |
480 final bool runningFromSdk = Platform.script.path.endsWith('.dart.snapshot'); | 487 final bool runningFromSdk = |
488 !runningFromTest && Platform.script.path.endsWith('.snapshot'); | |
489 | |
490 /// Whether pub is running from source in the Dart repo. | |
491 /// | |
492 /// This can happen when building Observatory, for example. | |
493 final bool runningFromDartRepo = | |
494 Platform.script.path.endsWith('/third_party/pkg/pub/bin/pub.dart'); | |
481 | 495 |
482 /// Resolves [target] relative to the path to pub's `asset` directory. | 496 /// Resolves [target] relative to the path to pub's `asset` directory. |
483 String assetPath(String target) { | 497 String assetPath(String target) => runningFromSdk |
484 if (runningFromSdk) { | 498 ? sdkAssetPath(target) |
485 return path.join( | 499 : path.join(pubRoot, 'lib', 'src', 'asset', target); |
486 sdk.rootDirectory, 'lib', '_internal', 'pub', 'asset', target); | 500 |
487 } else { | 501 /// Resolves [target] relative to the Dart SDK's `asset` directory. |
488 return path.join(pubRoot, 'asset', target); | 502 /// |
503 /// Throws a [StateError] if called from within the Dart repo. | |
504 String sdkAssetPath(String target) { | |
505 if (runningFromDartRepo) { | |
506 throw new StateError("Can't get SDK assets from within the Dart repo."); | |
489 } | 507 } |
508 | |
509 return path.join( | |
510 sdk.rootDirectory, 'lib', '_internal', 'pub', 'asset', target); | |
490 } | 511 } |
491 | 512 |
492 /// Returns the path to the root of pub's sources in the Dart repo. | 513 /// The path to the root of pub's sources the pub repo. |
Bob Nystrom
2015/05/20 22:00:57
"sources the" -> "sources in the"
nweiz
2015/05/21 00:43:10
Done.
| |
493 String get pubRoot => path.join(repoRoot, 'sdk', 'lib', '_internal', 'pub'); | |
494 | |
495 /// Returns the path to the root of the Dart repository. | |
496 /// | 514 /// |
497 /// This throws a [StateError] if it's called when running pub from the SDK. | 515 /// This throws a [StateError] if it's called when running pub from the SDK. |
498 String get repoRoot { | 516 final String pubRoot = (() { |
499 if (runningFromSdk) { | 517 if (runningFromSdk) { |
500 throw new StateError("Can't get the repo root from the SDK."); | 518 throw new StateError("Can't get pub's root from the SDK."); |
501 } | 519 } |
502 | 520 |
503 // Get the path to the directory containing this very file. | 521 var script = path.fromUri(Platform.script); |
504 var libDir = path.dirname(libraryPath('pub.io')); | 522 if (runningAsTest) { |
523 // Running from "test/../some_test.dart". | |
524 while (path.basename(script) != 'test') { | |
Bob Nystrom
2015/05/20 22:00:57
Maybe use path.split() and .lastIndexOf("test")?
nweiz
2015/05/21 00:43:10
Done.
| |
525 var dirname = path.dirname(script); | |
526 if (dirname == script) throw new StateError("Can't find pub's root."); | |
527 script = dirname; | |
528 } | |
529 return path.dirname(script); | |
530 } | |
505 | 531 |
506 // Assume we're running directly from the source location in the repo: | 532 // Pub is either run from ".pub/pub.test.snapshot" or "bin/pub.dart". |
507 // | 533 return path.dirname(path.dirname(script)); |
508 // <repo>/sdk/lib/_internal/pub/lib/src | 534 })(); |
509 return path.normalize(path.join(libDir, '..', '..', '..', '..', '..', '..')); | 535 |
510 } | 536 /// The path to the root of the Dart repo. |
537 /// | |
538 /// This throws a [StateError] if it's called when not running pub from source | |
539 /// in the Dart repo. | |
540 final String dartRepoRoot = (() { | |
541 if (!runningFromDartRepo) { | |
542 throw new StateError("Not running from source in the Dart repo."); | |
543 } | |
544 | |
545 // In the Dart repo, the script is in "third_party/pkg/pub/bin". | |
546 return path.dirname(path.dirname(path.dirname(path.dirname(path.dirname( | |
547 path.fromUri(Platform.script)))))); | |
548 })(); | |
511 | 549 |
512 /// A line-by-line stream of standard input. | 550 /// A line-by-line stream of standard input. |
513 final Stream<String> stdinLines = streamToLines( | 551 final Stream<String> stdinLines = streamToLines( |
514 new ByteStream(stdin).toStringStream()); | 552 new ByteStream(stdin).toStringStream()); |
515 | 553 |
516 /// Displays a message and reads a yes/no confirmation from the user. | 554 /// Displays a message and reads a yes/no confirmation from the user. |
517 /// | 555 /// |
518 /// Returns a [Future] that completes to `true` if the user confirms or `false` | 556 /// Returns a [Future] that completes to `true` if the user confirms or `false` |
519 /// if they do not. | 557 /// if they do not. |
520 /// | 558 /// |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
866 | 904 |
867 var match = new RegExp(r"^tar \(GNU tar\) (\d+).(\d+)\n") | 905 var match = new RegExp(r"^tar \(GNU tar\) (\d+).(\d+)\n") |
868 .firstMatch(result.stdout); | 906 .firstMatch(result.stdout); |
869 if (match == null) return false; | 907 if (match == null) return false; |
870 | 908 |
871 var major = int.parse(match[1]); | 909 var major = int.parse(match[1]); |
872 var minor = int.parse(match[2]); | 910 var minor = int.parse(match[2]); |
873 return major >= 2 || (major == 1 && minor >= 23); | 911 return major >= 2 || (major == 1 && minor >= 23); |
874 } | 912 } |
875 | 913 |
876 String get pathTo7zip { | 914 final String pathTo7zip = (() { |
877 if (runningFromSdk) return assetPath(path.join('7zip', '7za.exe')); | 915 if (!runningFromDartRepo) return sdkAssetPath(path.join('7zip', '7za.exe')); |
878 return path.join(repoRoot, 'third_party', '7zip', '7za.exe'); | 916 return path.join(dartRepoRoot, 'third_party', '7zip', '7za.exe'); |
879 } | 917 })(); |
880 | 918 |
881 Future<bool> _extractTarGzWindows(Stream<List<int>> stream, | 919 Future<bool> _extractTarGzWindows(Stream<List<int>> stream, |
882 String destination) { | 920 String destination) { |
883 // TODO(rnystrom): In the repo's history, there is an older implementation of | 921 // 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 | 922 // this that does everything in memory by piping streams directly together |
885 // instead of writing out temp files. The code is simpler, but unfortunately, | 923 // 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 | 924 // 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 | 925 // read from stdin instead of a file. Consider resurrecting that version if |
888 // we can figure out why it fails. | 926 // we can figure out why it fails. |
889 | 927 |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1010 | 1048 |
1011 // TODO(rnystrom): Remove this and change to returning one string. | 1049 // TODO(rnystrom): Remove this and change to returning one string. |
1012 static List<String> _toLines(String output) { | 1050 static List<String> _toLines(String output) { |
1013 var lines = splitLines(output); | 1051 var lines = splitLines(output); |
1014 if (!lines.isEmpty && lines.last == "") lines.removeLast(); | 1052 if (!lines.isEmpty && lines.last == "") lines.removeLast(); |
1015 return lines; | 1053 return lines; |
1016 } | 1054 } |
1017 | 1055 |
1018 bool get success => exitCode == exit_codes.SUCCESS; | 1056 bool get success => exitCode == exit_codes.SUCCESS; |
1019 } | 1057 } |
OLD | NEW |