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 /** | 5 /** |
6 * Helper functionality to make working with IO easier. | 6 * Helper functionality to make working with IO easier. |
7 */ | 7 */ |
8 library io; | 8 library io; |
9 | 9 |
10 import 'dart:io'; | 10 import 'dart:io'; |
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
462 if (basename(utilDir) == '') throw 'Could not find path to pub.'; | 462 if (basename(utilDir) == '') throw 'Could not find path to pub.'; |
463 utilDir = dirname(utilDir); | 463 utilDir = dirname(utilDir); |
464 } | 464 } |
465 | 465 |
466 return path.normalize(join(utilDir, 'pub', target)); | 466 return path.normalize(join(utilDir, 'pub', target)); |
467 } | 467 } |
468 | 468 |
469 /// A StringInputStream reading from stdin. | 469 /// A StringInputStream reading from stdin. |
470 final _stringStdin = new StringInputStream(stdin); | 470 final _stringStdin = new StringInputStream(stdin); |
471 | 471 |
472 /// Displays a message and reads a yes/no confirmation from the user. Returns | |
473 /// a [Future] that completes to `true` if the user confirms or `false` if they | |
474 /// do not. | |
nweiz
2012/12/12 21:21:21
It would be nice to document that the message shou
Bob Nystrom
2012/12/12 21:45:36
Done.
| |
475 Future<bool> confirm(String message) { | |
476 log.fine('Showing confirm message: $message'); | |
477 stdout.writeString("$message (y/n)? "); | |
478 return readLine().transform((line) => new RegExp(r"^[yY]").hasMatch(line)); | |
479 } | |
480 | |
472 /// Returns a single line read from a [StringInputStream]. By default, reads | 481 /// Returns a single line read from a [StringInputStream]. By default, reads |
473 /// from stdin. | 482 /// from stdin. |
474 /// | 483 /// |
475 /// A [StringInputStream] passed to this should have no callbacks registered. | 484 /// A [StringInputStream] passed to this should have no callbacks registered. |
476 Future<String> readLine([StringInputStream stream]) { | 485 Future<String> readLine([StringInputStream stream]) { |
477 if (stream == null) stream = _stringStdin; | 486 if (stream == null) stream = _stringStdin; |
478 if (stream.closed) return new Future.immediate(''); | 487 if (stream.closed) return new Future.immediate(''); |
479 void removeCallbacks() { | 488 void removeCallbacks() { |
480 stream.onClosed = null; | 489 stream.onClosed = null; |
481 stream.onLine = null; | 490 stream.onLine = null; |
482 stream.onError = null; | 491 stream.onError = null; |
483 } | 492 } |
484 | 493 |
485 // TODO(nweiz): remove this when issue 4061 is fixed. | 494 // TODO(nweiz): remove this when issue 4061 is fixed. |
486 var stackTrace; | 495 var stackTrace; |
487 try { | 496 try { |
488 throw ""; | 497 throw ""; |
489 } catch (_, localStackTrace) { | 498 } catch (_, localStackTrace) { |
490 stackTrace = localStackTrace; | 499 stackTrace = localStackTrace; |
491 } | 500 } |
492 | 501 |
493 var completer = new Completer(); | 502 var completer = new Completer(); |
494 stream.onClosed = () { | 503 stream.onClosed = () { |
495 removeCallbacks(); | 504 removeCallbacks(); |
496 completer.complete(''); | 505 completer.complete(''); |
497 }; | 506 }; |
498 | 507 |
499 stream.onLine = () { | 508 stream.onLine = () { |
500 removeCallbacks(); | 509 removeCallbacks(); |
501 completer.complete(stream.readLine()); | 510 var line = stream.readLine(); |
511 log.io('Read line: $line'); | |
512 completer.complete(line); | |
502 }; | 513 }; |
503 | 514 |
504 stream.onError = (e) { | 515 stream.onError = (e) { |
505 removeCallbacks(); | 516 removeCallbacks(); |
506 completer.completeException(e, stackTrace); | 517 completer.completeException(e, stackTrace); |
507 }; | 518 }; |
508 | 519 |
509 return completer.future; | 520 return completer.future; |
510 } | 521 } |
511 | 522 |
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
945 }).transform((_) => true); | 956 }).transform((_) => true); |
946 } | 957 } |
947 | 958 |
948 /// Create a .tar.gz archive from a list of entries. Each entry can be a | 959 /// Create a .tar.gz archive from a list of entries. Each entry can be a |
949 /// [String], [Directory], or [File] object. The root of the archive is | 960 /// [String], [Directory], or [File] object. The root of the archive is |
950 /// considered to be [baseDir], which defaults to the current working directory. | 961 /// considered to be [baseDir], which defaults to the current working directory. |
951 /// Returns an [InputStream] that will emit the contents of the archive. | 962 /// Returns an [InputStream] that will emit the contents of the archive. |
952 InputStream createTarGz(List contents, {baseDir}) { | 963 InputStream createTarGz(List contents, {baseDir}) { |
953 var buffer = new StringBuffer(); | 964 var buffer = new StringBuffer(); |
954 buffer.add('Creating .tag.gz stream containing:\n'); | 965 buffer.add('Creating .tag.gz stream containing:\n'); |
955 contents.forEach(buffer.add); | 966 contents.forEach((file) => buffer.add('$file\n')); |
956 log.fine(buffer.toString()); | 967 log.fine(buffer.toString()); |
957 | 968 |
958 // TODO(nweiz): Propagate errors to the returned stream (including non-zero | 969 // TODO(nweiz): Propagate errors to the returned stream (including non-zero |
959 // exit codes). See issue 3657. | 970 // exit codes). See issue 3657. |
960 var stream = new ListInputStream(); | 971 var stream = new ListInputStream(); |
961 | 972 |
962 if (baseDir == null) baseDir = path.current; | 973 if (baseDir == null) baseDir = path.current; |
963 baseDir = getFullPath(baseDir); | 974 baseDir = getFullPath(baseDir); |
964 contents = contents.map((entry) { | 975 contents = contents.map((entry) { |
965 entry = getFullPath(entry); | 976 entry = getFullPath(entry); |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1110 return new Directory(entry); | 1121 return new Directory(entry); |
1111 } | 1122 } |
1112 | 1123 |
1113 /** | 1124 /** |
1114 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 1125 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
1115 */ | 1126 */ |
1116 Uri _getUri(uri) { | 1127 Uri _getUri(uri) { |
1117 if (uri is Uri) return uri; | 1128 if (uri is Uri) return uri; |
1118 return new Uri.fromString(uri); | 1129 return new Uri.fromString(uri); |
1119 } | 1130 } |
OLD | NEW |