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 475 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
486 /// | 486 /// |
487 /// This works because when running from tests, pub always uses a snapshot named | 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. | 488 /// "pub.test.snapshot", which is not used outside of tests. |
489 final bool runningFromTest = Platform.script.path.endsWith('.test.snapshot'); | 489 final bool runningFromTest = Platform.script.path.endsWith('.test.snapshot'); |
490 | 490 |
491 /// 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 |
492 /// source repository. | 492 /// source repository. |
493 final bool runningFromSdk = | 493 final bool runningFromSdk = |
494 !runningFromTest && Platform.script.path.endsWith('.snapshot'); | 494 !runningFromTest && Platform.script.path.endsWith('.snapshot'); |
495 | 495 |
496 /// A regular expression to match the script path of a pub script running from | |
497 /// source in the Dart repo. | |
498 final _dartRepoRegExp = new RegExp( | |
499 r"/third_party/pkg_tested/pub/(bin/pub.dart|test/.*_test.dart)$"); | |
Bob Nystrom
2015/05/29 20:15:55
".dart" -> "\.dart".
nweiz
2015/05/29 20:33:32
Done.
| |
500 | |
496 /// Whether pub is running from source in the Dart repo. | 501 /// Whether pub is running from source in the Dart repo. |
497 /// | 502 /// |
498 /// This can happen when building Observatory, for example. | 503 /// This can happen when building Observatory, for example. |
499 final bool runningFromDartRepo = | 504 final bool runningFromDartRepo = |
500 Platform.script.path.endsWith('/third_party/pkg_tested/pub/bin/pub.dart'); | 505 Platform.script.path.contains(_dartRepoRegExp); |
501 | 506 |
502 /// Resolves [target] relative to the path to pub's `asset` directory. | 507 /// Resolves [target] relative to the path to pub's `asset` directory. |
503 String assetPath(String target) => runningFromSdk | 508 String assetPath(String target) => runningFromSdk |
504 ? sdkAssetPath(target) | 509 ? sdkAssetPath(target) |
505 : path.join(pubRoot, 'lib', 'src', 'asset', target); | 510 : path.join(pubRoot, 'lib', 'src', 'asset', target); |
506 | 511 |
507 /// Resolves [target] relative to the Dart SDK's `asset` directory. | 512 /// Resolves [target] relative to the Dart SDK's `asset` directory. |
508 /// | 513 /// |
509 /// Throws a [StateError] if called from within the Dart repo. | 514 /// Throws a [StateError] if called from within the Dart repo. |
510 String sdkAssetPath(String target) { | 515 String sdkAssetPath(String target) { |
(...skipping 28 matching lines...) Expand all Loading... | |
539 | 544 |
540 /// The path to the root of the Dart repo. | 545 /// The path to the root of the Dart repo. |
541 /// | 546 /// |
542 /// This throws a [StateError] if it's called when not running pub from source | 547 /// This throws a [StateError] if it's called when not running pub from source |
543 /// in the Dart repo. | 548 /// in the Dart repo. |
544 final String dartRepoRoot = (() { | 549 final String dartRepoRoot = (() { |
545 if (!runningFromDartRepo) { | 550 if (!runningFromDartRepo) { |
546 throw new StateError("Not running from source in the Dart repo."); | 551 throw new StateError("Not running from source in the Dart repo."); |
547 } | 552 } |
548 | 553 |
549 // In the Dart repo, the script is in "third_party/pkg_tested/pub/bin". | 554 // Get the URL of the repo root in a way that works when either both running |
550 return path.dirname(path.dirname(path.dirname(path.dirname(path.dirname( | 555 // as a test or as a pub executable. |
551 path.fromUri(Platform.script)))))); | 556 var url = Platform.script.replace( |
557 path: Platform.script.path.replaceAll(_dartRepoRegExp, '')); | |
558 return path.fromUri(url); | |
552 })(); | 559 })(); |
553 | 560 |
554 /// A line-by-line stream of standard input. | 561 /// A line-by-line stream of standard input. |
555 final Stream<String> stdinLines = streamToLines( | 562 final Stream<String> stdinLines = streamToLines( |
556 new ByteStream(stdin).toStringStream()); | 563 new ByteStream(stdin).toStringStream()); |
557 | 564 |
558 /// Displays a message and reads a yes/no confirmation from the user. | 565 /// Displays a message and reads a yes/no confirmation from the user. |
559 /// | 566 /// |
560 /// Returns a [Future] that completes to `true` if the user confirms or `false` | 567 /// Returns a [Future] that completes to `true` if the user confirms or `false` |
561 /// if they do not. | 568 /// if they do not. |
(...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1052 | 1059 |
1053 // TODO(rnystrom): Remove this and change to returning one string. | 1060 // TODO(rnystrom): Remove this and change to returning one string. |
1054 static List<String> _toLines(String output) { | 1061 static List<String> _toLines(String output) { |
1055 var lines = splitLines(output); | 1062 var lines = splitLines(output); |
1056 if (!lines.isEmpty && lines.last == "") lines.removeLast(); | 1063 if (!lines.isEmpty && lines.last == "") lines.removeLast(); |
1057 return lines; | 1064 return lines; |
1058 } | 1065 } |
1059 | 1066 |
1060 bool get success => exitCode == exit_codes.SUCCESS; | 1067 bool get success => exitCode == exit_codes.SUCCESS; |
1061 } | 1068 } |
OLD | NEW |