Run pub in interactive mode in flutter packages pub (#11700)

This commit is contained in:
Mikkel Nygaard Ravn
2017-09-06 11:53:39 +02:00
committed by GitHub
parent f430a45a5b
commit 8303fff8f9
8 changed files with 306 additions and 79 deletions

View File

@@ -26,10 +26,11 @@
/// increase the API surface that we have to test in Flutter tools, and the APIs
/// in `dart:io` can sometimes be hard to use in tests.
import 'dart:async';
import 'dart:io' as io show exit, ProcessSignal;
import 'dart:io' as io show exit, IOSink, ProcessSignal, stderr, stdin, stdout;
import 'package:meta/meta.dart';
import 'context.dart';
import 'platform.dart';
import 'process.dart';
@@ -62,10 +63,11 @@ export 'dart:io'
ProcessStartMode,
// RandomAccessFile NO! Use `file_system.dart`
ServerSocket,
stderr,
stdin,
// stderr, NO! Use `io.dart`
// stdin, NO! Use `io.dart`
Stdin,
StdinException,
stdout,
// stdout, NO! Use `io.dart`
Socket,
SocketException,
SYSTEM_ENCODING,
@@ -143,3 +145,32 @@ class _PosixProcessSignal extends ProcessSignal {
return super.watch();
}
}
class Stdio {
const Stdio();
Stream<List<int>> get stdin => io.stdin;
io.IOSink get stdout => io.stdout;
io.IOSink get stderr => io.stderr;
}
io.IOSink get stderr {
if (context == null)
return io.stderr;
final Stdio contextStreams = context[Stdio];
return contextStreams.stderr;
}
Stream<List<int>> get stdin {
if (context == null)
return io.stdin;
final Stdio contextStreams = context[Stdio];
return contextStreams.stdin;
}
io.IOSink get stdout {
if (context == null)
return io.stdout;
final Stdio contextStreams = context[Stdio];
return contextStreams.stdout;
}

View File

@@ -166,6 +166,30 @@ Future<int> runCommandAndStreamOutput(List<String> cmd, {
return await process.exitCode;
}
/// Runs the [command] interactively, connecting the stdin/stdout/stderr
/// streams of this process to those of the child process. Completes with
/// the exit code of the child process.
Future<int> runInteractively(List<String> command, {
String workingDirectory,
bool allowReentrantFlutter: false,
Map<String, String> environment
}) async {
final Process process = await runCommand(
command,
workingDirectory: workingDirectory,
allowReentrantFlutter: allowReentrantFlutter,
environment: environment,
);
process.stdin.addStream(stdin);
// Wait for stdout and stderr to be fully processed, because process.exitCode
// may complete first.
Future.wait<dynamic>(<Future<dynamic>>[
stdout.addStream(process.stdout),
stderr.addStream(process.stderr),
]);
return await process.exitCode;
}
Future<Null> runAndKill(List<String> cmd, Duration timeout) {
final Future<Process> proc = runDetached(cmd);
return new Future<Null>.delayed(timeout, () async {

View File

@@ -9,7 +9,7 @@ import 'package:quiver/strings.dart';
import '../globals.dart';
import 'context.dart';
import 'io.dart';
import 'io.dart' as io;
import 'platform.dart';
final AnsiTerminal _kAnsiTerminal = new AnsiTerminal();
@@ -61,18 +61,21 @@ class AnsiTerminal {
// [_ENOTTY] or [_INVALID_HANDLE], we should check beforehand if stdin is
// connected to a terminal or not.
// (Requires https://github.com/dart-lang/sdk/issues/29083 to be resolved.)
try {
// The order of setting lineMode and echoMode is important on Windows.
if (value) {
stdin.echoMode = false;
stdin.lineMode = false;
} else {
stdin.lineMode = true;
stdin.echoMode = true;
final Stream<List<int>> stdin = io.stdin;
if (stdin is io.Stdin) {
try {
// The order of setting lineMode and echoMode is important on Windows.
if (value) {
stdin.echoMode = false;
stdin.lineMode = false;
} else {
stdin.lineMode = true;
stdin.echoMode = true;
}
} on io.StdinException catch (error) {
if (!_lineModeIgnorableErrors.contains(error.osError?.errorCode))
rethrow;
}
} on StdinException catch (error) {
if (!_lineModeIgnorableErrors.contains(error.osError?.errorCode))
rethrow;
}
}
@@ -83,7 +86,7 @@ class AnsiTerminal {
/// Useful when the console is in [singleCharMode].
Stream<String> get onCharInput {
if (_broadcastStdInString == null)
_broadcastStdInString = stdin.transform(ASCII.decoder).asBroadcastStream();
_broadcastStdInString = io.stdin.transform(ASCII.decoder).asBroadcastStream();
return _broadcastStdInString;
}

View File

@@ -124,5 +124,5 @@ class PackagesPassthroughCommand extends FlutterCommand {
}
@override
Future<Null> runCommand() => pub(argResults.rest, retry: false);
Future<Null> runCommand() => pubInteractively(argResults.rest);
}

View File

@@ -78,23 +78,25 @@ Future<Null> pubGet({
typedef String MessageFilter(String message);
/// Runs pub in 'batch' mode, forwarding complete lines written by pub to its
/// stdout/stderr streams to the corresponding stream of this process, optionally
/// applying filtering. The pub process will not receive anything on its stdin stream.
Future<Null> pub(List<String> arguments, {
String directory,
MessageFilter filter,
String failureMessage: 'pub failed',
@required bool retry,
}) async {
final List<String> command = <String>[ sdkBinaryName('pub') ]..addAll(arguments);
int attempts = 0;
int duration = 1;
int code;
while (true) {
attempts += 1;
code = await runCommandAndStreamOutput(
command,
_pubCommand(arguments),
workingDirectory: directory,
mapFunction: filter,
environment: <String, String>{ 'FLUTTER_ROOT': Cache.flutterRoot, _pubEnvironmentKey: _getPubEnvironmentValue() }
environment: _pubEnvironment,
);
if (code != 69) // UNAVAILABLE in https://github.com/dart-lang/pub/blob/master/lib/src/exit_codes.dart
break;
@@ -108,6 +110,32 @@ Future<Null> pub(List<String> arguments, {
throwToolExit('$failureMessage ($code)', exitCode: code);
}
/// Runs pub in 'interactive' mode, directly piping the stdin stream of this
/// process to that of pub, and the stdout/stderr stream of pub to the corresponding
/// streams of this process.
Future<Null> pubInteractively(List<String> arguments, {
String directory,
}) async {
final int code = await runInteractively(
_pubCommand(arguments),
workingDirectory: directory,
environment: _pubEnvironment,
);
if (code != 0)
throwToolExit('pub finished with exit code $code', exitCode: code);
}
/// The command used for running pub.
List<String> _pubCommand(List<String> arguments) {
return <String>[ sdkBinaryName('pub') ]..addAll(arguments);
}
/// The full environment used when running pub.
Map<String, String> get _pubEnvironment => <String, String>{
'FLUTTER_ROOT': Cache.flutterRoot,
_pubEnvironmentKey: _getPubEnvironmentValue(),
};
final RegExp _analyzerWarning = new RegExp(r'^! \w+ [^ ]+ from path \.\./\.\./bin/cache/dart-sdk/lib/\w+$');
/// The console environment key used by the pub tool.