Use the dart:io IOSink API in pub.

Review URL: https://codereview.chromium.org//14266003

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@21525 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
nweiz@google.com 2013-04-15 22:59:30 +00:00
parent dec3937950
commit ee5f6fc653
2 changed files with 14 additions and 41 deletions

View file

@ -279,32 +279,6 @@ String relativeToPub(String target) {
return path.normalize(path.join(utilDir, 'pub', target));
}
// TODO(nweiz): add a ByteSink wrapper to make writing strings to stdout/stderr
// nicer.
/// A sink that writes to standard output. Errors piped to this stream will be
/// surfaced to the top-level error handler.
// TODO: Unrequired wrapper, stdout is now an EventSink<List<int>>.
final EventSink<List<int>> stdoutSink = _wrapStdio(stdout, "stdout");
/// A sink that writes to standard error. Errors piped to this stream will be
/// surfaced to the top-level error handler.
// TODO: Unrequired wrapper, stdout is now an EventSink<List<int>>.
final EventSink<List<int>> stderrSink = _wrapStdio(stderr, "stderr");
/// Wrap the standard output or error [stream] in a [EventSink]. Any errors are
/// logged, and then the program is terminated. [name] is used for debugging.
EventSink<List<int>> _wrapStdio(IOSink sink, String name) {
var pair = consumerToSink(sink);
pair.last.catchError((e) {
// This log may or may not work, depending on how the stream failed. Not
// much we can do about that.
log.error("Error writing to $name", e);
exit(exit_codes.IO);
});
return pair.first;
}
/// A line-by-line stream of standard input.
final Stream<String> stdinLines = streamToLines(
new ByteStream(stdin).toStringStream());
@ -317,7 +291,7 @@ final Stream<String> stdinLines = streamToLines(
/// should just be a fragment like, "Are you sure you want to proceed".
Future<bool> confirm(String message) {
log.fine('Showing confirm message: $message');
stdoutSink.add("$message (y/n)? ".codeUnits);
stdout.write("$message (y/n)? ");
return streamFirst(stdinLines)
.then((line) => new RegExp(r"^[yY]").hasMatch(line));
}
@ -571,8 +545,8 @@ Future<bool> extractTarGz(Stream<List<int>> stream, String destination) {
// Ignore errors on process.std{out,err}. They'll be passed to
// process.exitCode, and we don't want them being top-levelled by
// std{out,err}Sink.
store(process.stdout.handleError((_) {}), stdoutSink, closeSink: false);
store(process.stderr.handleError((_) {}), stderrSink, closeSink: false);
store(process.stdout.handleError((_) {}), stdout, closeSink: false);
store(process.stderr.handleError((_) {}), stderr, closeSink: false);
return Future.wait([
store(stream, process.stdin),
process.exitCode

View file

@ -5,6 +5,7 @@
/// Message logging.
library log;
import 'dart:io';
import 'dart:async';
import 'io.dart';
@ -164,11 +165,11 @@ void recordTranscript() {
void dumpTranscript() {
if (_transcript == null) return;
stderrSink.add('---- Log transcript ----\n'.codeUnits);
stderr.writeln('---- Log transcript ----');
for (var entry in _transcript) {
_logToStderrWithLabel(entry);
}
stderrSink.add('---- End log transcript ----\n'.codeUnits);
stderr.writeln('---- End log transcript ----');
}
/// Sets the verbosity to "normal", which shows errors, warnings, and messages.
@ -201,38 +202,36 @@ void showAll() {
/// Log function that prints the message to stdout.
void _logToStdout(Entry entry) {
_logToStream(stdoutSink, entry, showLabel: false);
_logToStream(stdout, entry, showLabel: false);
}
/// Log function that prints the message to stdout with the level name.
void _logToStdoutWithLabel(Entry entry) {
_logToStream(stdoutSink, entry, showLabel: true);
_logToStream(stdout, entry, showLabel: true);
}
/// Log function that prints the message to stderr.
void _logToStderr(Entry entry) {
_logToStream(stderrSink, entry, showLabel: false);
_logToStream(stderr, entry, showLabel: false);
}
/// Log function that prints the message to stderr with the level name.
void _logToStderrWithLabel(Entry entry) {
_logToStream(stderrSink, entry, showLabel: true);
_logToStream(stderr, entry, showLabel: true);
}
void _logToStream(EventSink<List<int>> sink, Entry entry, {bool showLabel}) {
void _logToStream(IOSink sink, Entry entry, {bool showLabel}) {
bool firstLine = true;
for (var line in entry.lines) {
if (showLabel) {
if (firstLine) {
sink.add(entry.level.name.codeUnits);
sink.add(': '.codeUnits);
sink.write('${entry.level.name}: ');
} else {
sink.add(' | '.codeUnits);
sink.write(' | ');
}
}
sink.add(line.codeUnits);
sink.add('\n'.codeUnits);
sink.writeln(line);
firstLine = false;
}