From 72ff553af31231758011d561ccbe48ef5cce9e32 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Tue, 23 Mar 2021 10:10:37 -0700 Subject: [PATCH] [flutter_tools] migrate io, process, logger, terminal (#78816) --- packages/flutter_tools/lib/src/base/io.dart | 46 ++-- .../flutter_tools/lib/src/base/logger.dart | 253 +++++++++--------- .../flutter_tools/lib/src/base/process.dart | 59 ++-- .../flutter_tools/lib/src/base/terminal.dart | 57 ++-- 4 files changed, 205 insertions(+), 210 deletions(-) diff --git a/packages/flutter_tools/lib/src/base/io.dart b/packages/flutter_tools/lib/src/base/io.dart index 28045cbc8b..1138ee9da6 100644 --- a/packages/flutter_tools/lib/src/base/io.dart +++ b/packages/flutter_tools/lib/src/base/io.dart @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// @dart = 2.8 - /// This file serves as the single point of entry into the `dart:io` APIs /// within Flutter tools. /// @@ -140,7 +138,7 @@ bool _inUnitTest() { /// Sets the [exit] function to a function that throws an exception rather /// than exiting the process; this is intended for testing purposes. @visibleForTesting -void setExitFunctionForTests([ ExitFunction exitFunction ]) { +void setExitFunctionForTests([ ExitFunction? exitFunction ]) { _exitFunction = exitFunction ?? (int exitCode) { throw ProcessExit(exitCode, immediate: true); }; @@ -235,12 +233,12 @@ class Stdio { /// dart:io. @visibleForTesting Stdio.test({ - @required io.Stdout stdout, - @required io.IOSink stderr, + required io.Stdout stdout, + required io.IOSink stderr, }) : _stdoutOverride = stdout, _stderrOverride = stderr; - io.Stdout _stdoutOverride; - io.IOSink _stderrOverride; + io.Stdout? _stdoutOverride; + io.IOSink? _stderrOverride; // These flags exist to remember when the done Futures on stdout and stderr // complete to avoid trying to write to a closed stream sink, which would @@ -253,34 +251,34 @@ class Stdio { @visibleForTesting io.Stdout get stdout { if (_stdout != null) { - return _stdout; + return _stdout!; } _stdout = _stdoutOverride ?? io.stdout; - _stdout.done.then( + _stdout!.done.then( (void _) { _stdoutDone = true; }, onError: (Object err, StackTrace st) { _stdoutDone = true; }, ); - return _stdout; + return _stdout!; } - io.Stdout _stdout; + io.Stdout? _stdout; @visibleForTesting io.IOSink get stderr { if (_stderr != null) { - return _stderr; + return _stderr!; } _stderr = _stderrOverride ?? io.stderr; - _stderr.done.then( + _stderr!.done.then( (void _) { _stderrDone = true; }, onError: (Object err, StackTrace st) { _stderrDone = true; }, ); - return _stderr; + return _stderr!; } - io.IOSink _stderr; + io.IOSink? _stderr; bool get hasTerminal => io.stdout.hasTerminal; - static bool _stdinHasTerminal; + static bool? _stdinHasTerminal; /// Determines whether there is a terminal attached. /// @@ -290,7 +288,7 @@ class Stdio { /// runtime errors such as "inappropriate ioctl for device" if not handled. bool get stdinHasTerminal { if (_stdinHasTerminal != null) { - return _stdinHasTerminal; + return _stdinHasTerminal!; } if (stdin is! io.Stdin) { return _stdinHasTerminal = false; @@ -309,15 +307,15 @@ class Stdio { return _stdinHasTerminal = true; } - int get terminalColumns => hasTerminal ? stdout.terminalColumns : null; - int get terminalLines => hasTerminal ? stdout.terminalLines : null; + int? get terminalColumns => hasTerminal ? stdout.terminalColumns : null; + int? get terminalLines => hasTerminal ? stdout.terminalLines : null; bool get supportsAnsiEscapes => hasTerminal && stdout.supportsAnsiEscapes; /// Writes [message] to [stderr], falling back on [fallback] if the write /// throws any exception. The default fallback calls [print] on [message]. void stderrWrite( String message, { - void Function(String, dynamic, StackTrace) fallback, + void Function(String, dynamic, StackTrace)? fallback, }) { if (!_stderrDone) { _stdioWrite(stderr, message, fallback: fallback); @@ -334,7 +332,7 @@ class Stdio { /// throws any exception. The default fallback calls [print] on [message]. void stdoutWrite( String message, { - void Function(String, dynamic, StackTrace) fallback, + void Function(String, dynamic, StackTrace)? fallback, }) { if (!_stdoutDone) { _stdioWrite(stdout, message, fallback: fallback); @@ -349,7 +347,7 @@ class Stdio { // Helper for [stderrWrite] and [stdoutWrite]. void _stdioWrite(io.IOSink sink, String message, { - void Function(String, dynamic, StackTrace) fallback, + void Function(String, dynamic, StackTrace)? fallback, }) { asyncGuard(() async { sink.write(message); @@ -447,7 +445,7 @@ typedef NetworkInterfaceLister = Future> Function({ io.InternetAddressType type, }); -NetworkInterfaceLister _networkInterfaceListerOverride; +NetworkInterfaceLister? _networkInterfaceListerOverride; // Tests can set up a non-default network interface lister. @visibleForTesting @@ -469,7 +467,7 @@ Future> listNetworkInterfaces({ io.InternetAddressType type = io.InternetAddressType.any, }) async { if (_networkInterfaceListerOverride != null) { - return _networkInterfaceListerOverride( + return _networkInterfaceListerOverride!.call( includeLoopback: includeLoopback, includeLinkLocal: includeLinkLocal, type: type, diff --git a/packages/flutter_tools/lib/src/base/logger.dart b/packages/flutter_tools/lib/src/base/logger.dart index 6a634eef8e..47ac70a722 100644 --- a/packages/flutter_tools/lib/src/base/logger.dart +++ b/packages/flutter_tools/lib/src/base/logger.dart @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// @dart = 2.8 - import 'dart:async'; import 'package:meta/meta.dart'; @@ -65,12 +63,12 @@ abstract class Logger { /// `outputPreferences.wrapText` setting. void printError( String message, { - StackTrace stackTrace, - bool emphasis, - TerminalColor color, - int indent, - int hangingIndent, - bool wrap, + StackTrace? stackTrace, + bool? emphasis, + TerminalColor? color, + int? indent, + int? hangingIndent, + bool? wrap, }); /// Display normal output of the command. This should be used for things like @@ -103,12 +101,12 @@ abstract class Logger { /// `outputPreferences.wrapText` setting. void printStatus( String message, { - bool emphasis, - TerminalColor color, - bool newline, - int indent, - int hangingIndent, - bool wrap, + bool? emphasis, + TerminalColor? color, + bool? newline, + int? indent, + int? hangingIndent, + bool? wrap, }); /// Use this for verbose tracing output. Users can turn this output on in order @@ -126,20 +124,20 @@ abstract class Logger { /// between the `message` and the progress indicator, if any. Status startProgress( String message, { - String progressId, + String? progressId, bool multilineOutput = false, int progressIndicatorPadding = kDefaultStatusPadding, }); /// A [SilentStatus] or an [AnsiSpinner] (depending on whether the /// terminal is fancy enough), already started. - Status startSpinner({ VoidCallback onFinish }); + Status startSpinner({ VoidCallback? onFinish }); /// Send an event to be emitted. /// /// Only surfaces a value in machine modes, Loggers may ignore this message in /// non-machine modes. - void sendEvent(String name, [Map args]) { } + void sendEvent(String name, [Map? args]) { } /// Clears all output. void clear(); @@ -174,7 +172,14 @@ class DelegatingLogger implements Logger { bool get isVerbose => _delegate.isVerbose; @override - void printError(String message, {StackTrace stackTrace, bool emphasis, TerminalColor color, int indent, int hangingIndent, bool wrap}) { + void printError(String message, { + StackTrace? stackTrace, + bool? emphasis, + TerminalColor? color, + int? indent, + int? hangingIndent, + bool? wrap, + }) { _delegate.printError( message, stackTrace: stackTrace, @@ -187,7 +192,14 @@ class DelegatingLogger implements Logger { } @override - void printStatus(String message, {bool emphasis, TerminalColor color, bool newline, int indent, int hangingIndent, bool wrap}) { + void printStatus(String message, { + bool? emphasis, + TerminalColor? color, + bool? newline, + int? indent, + int? hangingIndent, + bool? wrap, + }) { _delegate.printStatus(message, emphasis: emphasis, color: color, @@ -204,12 +216,16 @@ class DelegatingLogger implements Logger { } @override - void sendEvent(String name, [Map args]) { + void sendEvent(String name, [Map? args]) { _delegate.sendEvent(name, args); } @override - Status startProgress(String message, {String progressId, bool multilineOutput = false, int progressIndicatorPadding = kDefaultStatusPadding}) { + Status startProgress(String message, { + String? progressId, + bool multilineOutput = false, + int progressIndicatorPadding = kDefaultStatusPadding, + }) { return _delegate.startProgress(message, progressId: progressId, multilineOutput: multilineOutput, @@ -218,7 +234,7 @@ class DelegatingLogger implements Logger { } @override - Status startSpinner({VoidCallback onFinish}) { + Status startSpinner({VoidCallback? onFinish}) { return _delegate.startSpinner(onFinish: onFinish); } @@ -240,7 +256,7 @@ T asLogger(Logger logger) { if (logger is T) { return logger; } else if (logger is DelegatingLogger) { - logger = (logger as DelegatingLogger)._delegate; + logger = logger._delegate; } else { throw StateError('$original has no ancestor delegate of type $T'); } @@ -249,9 +265,9 @@ T asLogger(Logger logger) { class StdoutLogger extends Logger { StdoutLogger({ - @required this.terminal, - @required Stdio stdio, - @required OutputPreferences outputPreferences, + required this.terminal, + required Stdio stdio, + required OutputPreferences outputPreferences, StopwatchFactory stopwatchFactory = const StopwatchFactory(), }) : _stdio = stdio, @@ -265,7 +281,7 @@ class StdoutLogger extends Logger { final Stdio _stdio; final StopwatchFactory _stopwatchFactory; - Status _status; + Status? _status; @override bool get isVerbose => false; @@ -279,15 +295,14 @@ class StdoutLogger extends Logger { @override void printError( String message, { - StackTrace stackTrace, - bool emphasis, - TerminalColor color, - int indent, - int hangingIndent, - bool wrap, + StackTrace? stackTrace, + bool? emphasis, + TerminalColor? color, + int? indent, + int? hangingIndent, + bool? wrap, }) { _status?.pause(); - message ??= ''; message = wrapText(message, indent: indent, hangingIndent: hangingIndent, @@ -308,15 +323,14 @@ class StdoutLogger extends Logger { @override void printStatus( String message, { - bool emphasis, - TerminalColor color, - bool newline, - int indent, - int hangingIndent, - bool wrap, + bool? emphasis, + TerminalColor? color, + bool? newline, + int? indent, + int? hangingIndent, + bool? wrap, }) { _status?.pause(); - message ??= ''; message = wrapText(message, indent: indent, hangingIndent: hangingIndent, @@ -348,11 +362,10 @@ class StdoutLogger extends Logger { @override Status startProgress( String message, { - String progressId, + String? progressId, bool multilineOutput = false, int progressIndicatorPadding = kDefaultStatusPadding, }) { - assert(progressIndicatorPadding != null); if (_status != null) { // Ignore nested progresses; return a no-op status object. return SilentStatus( @@ -379,11 +392,11 @@ class StdoutLogger extends Logger { stopwatch: _stopwatchFactory.createStopwatch(), )..start(); } - return _status; + return _status!; } @override - Status startSpinner({ VoidCallback onFinish }) { + Status startSpinner({ VoidCallback? onFinish }) { if (terminal.supportsColor) { return AnsiSpinner( onFinish: onFinish, @@ -403,7 +416,7 @@ class StdoutLogger extends Logger { } @override - void sendEvent(String name, [Map args]) { } + void sendEvent(String name, [Map? args]) { } @override void clear() { @@ -423,9 +436,9 @@ class StdoutLogger extends Logger { /// they will show up as the unrepresentable character symbol '�'. class WindowsStdoutLogger extends StdoutLogger { WindowsStdoutLogger({ - @required Terminal terminal, - @required Stdio stdio, - @required OutputPreferences outputPreferences, + required Terminal terminal, + required Stdio stdio, + required OutputPreferences outputPreferences, StopwatchFactory stopwatchFactory = const StopwatchFactory(), }) : super( terminal: terminal, @@ -451,16 +464,16 @@ class WindowsStdoutLogger extends StdoutLogger { class BufferLogger extends Logger { BufferLogger({ - @required this.terminal, - @required OutputPreferences outputPreferences, + required this.terminal, + required OutputPreferences outputPreferences, StopwatchFactory stopwatchFactory = const StopwatchFactory(), }) : _outputPreferences = outputPreferences, _stopwatchFactory = stopwatchFactory; /// Create a [BufferLogger] with test preferences. BufferLogger.test({ - Terminal terminal, - OutputPreferences outputPreferences, + Terminal? terminal, + OutputPreferences? outputPreferences, }) : terminal = terminal ?? Terminal.test(), _outputPreferences = outputPreferences ?? OutputPreferences.test(), _stopwatchFactory = const StopwatchFactory(); @@ -496,12 +509,12 @@ class BufferLogger extends Logger { @override void printError( String message, { - StackTrace stackTrace, - bool emphasis, - TerminalColor color, - int indent, - int hangingIndent, - bool wrap, + StackTrace? stackTrace, + bool? emphasis, + TerminalColor? color, + int? indent, + int? hangingIndent, + bool? wrap, }) { _error.writeln(terminal.color( wrapText(message, @@ -517,12 +530,12 @@ class BufferLogger extends Logger { @override void printStatus( String message, { - bool emphasis, - TerminalColor color, - bool newline, - int indent, - int hangingIndent, - bool wrap, + bool? emphasis, + TerminalColor? color, + bool? newline, + int? indent, + int? hangingIndent, + bool? wrap, }) { if (newline != false) { _status.writeln(wrapText(message, @@ -547,7 +560,7 @@ class BufferLogger extends Logger { @override Status startProgress( String message, { - String progressId, + String? progressId, bool multilineOutput = false, int progressIndicatorPadding = kDefaultStatusPadding, }) { @@ -559,7 +572,7 @@ class BufferLogger extends Logger { } @override - Status startSpinner({VoidCallback onFinish}) { + Status startSpinner({VoidCallback? onFinish}) { return SilentStatus( stopwatch: _stopwatchFactory.createStopwatch(), onFinish: onFinish, @@ -575,8 +588,8 @@ class BufferLogger extends Logger { } @override - void sendEvent(String name, [Map args]) { - _events.write(json.encode({ + void sendEvent(String name, [Map? args]) { + _events.write(json.encode({ 'name': name, 'args': args })); @@ -602,12 +615,12 @@ class VerboseLogger extends DelegatingLogger { @override void printError( String message, { - StackTrace stackTrace, - bool emphasis, - TerminalColor color, - int indent, - int hangingIndent, - bool wrap, + StackTrace? stackTrace, + bool? emphasis, + TerminalColor? color, + int? indent, + int? hangingIndent, + bool? wrap, }) { _emit( _LogType.error, @@ -624,12 +637,12 @@ class VerboseLogger extends DelegatingLogger { @override void printStatus( String message, { - bool emphasis, - TerminalColor color, - bool newline, - int indent, - int hangingIndent, - bool wrap, + bool? emphasis, + TerminalColor? color, + bool? newline, + int? indent, + int? hangingIndent, + bool? wrap, }) { _emit(_LogType.status, wrapText(message, indent: indent, @@ -647,7 +660,7 @@ class VerboseLogger extends DelegatingLogger { @override Status startProgress( String message, { - String progressId, + String? progressId, bool multilineOutput = false, int progressIndicatorPadding = kDefaultStatusPadding, }) { @@ -669,7 +682,7 @@ class VerboseLogger extends DelegatingLogger { )..start(); } - void _emit(_LogType type, String message, [ StackTrace stackTrace ]) { + void _emit(_LogType type, String message, [ StackTrace? stackTrace ]) { if (message.trim().isEmpty) { return; } @@ -705,7 +718,7 @@ class VerboseLogger extends DelegatingLogger { } @override - void sendEvent(String name, [Map args]) { } + void sendEvent(String name, [Map? args]) { } } class PrefixedErrorLogger extends DelegatingLogger { @@ -714,14 +727,14 @@ class PrefixedErrorLogger extends DelegatingLogger { @override void printError( String message, { - StackTrace stackTrace, - bool emphasis, - TerminalColor color, - int indent, - int hangingIndent, - bool wrap, + StackTrace? stackTrace, + bool? emphasis, + TerminalColor? color, + int? indent, + int? hangingIndent, + bool? wrap, }) { - if (message?.trim()?.isNotEmpty == true) { + if (message.trim().isNotEmpty == true) { message = 'ERROR: $message'; } super.printError( @@ -760,10 +773,10 @@ typedef SlowWarningCallback = String Function(); abstract class Status { Status({ this.onFinish, - @required Stopwatch stopwatch, + required Stopwatch stopwatch, }) : _stopwatch = stopwatch; - final VoidCallback onFinish; + final VoidCallback? onFinish; @protected final Stopwatch _stopwatch; @@ -802,17 +815,15 @@ abstract class Status { void finish() { assert(_stopwatch.isRunning); _stopwatch.stop(); - if (onFinish != null) { - onFinish(); - } + onFinish?.call(); } } /// A [SilentStatus] shows nothing. class SilentStatus extends Status { SilentStatus({ - @required Stopwatch stopwatch, - VoidCallback onFinish, + required Stopwatch stopwatch, + VoidCallback? onFinish, }) : super( onFinish: onFinish, stopwatch: stopwatch, @@ -820,9 +831,7 @@ class SilentStatus extends Status { @override void finish() { - if (onFinish != null) { - onFinish(); - } + onFinish?.call(); } } @@ -831,13 +840,11 @@ class SilentStatus extends Status { class SummaryStatus extends Status { SummaryStatus({ this.message = '', - @required Stopwatch stopwatch, + required Stopwatch stopwatch, this.padding = kDefaultStatusPadding, - VoidCallback onFinish, - @required Stdio stdio, - }) : assert(message != null), - assert(padding != null), - _stdio = stdio, + VoidCallback? onFinish, + required Stdio stdio, + }) : _stdio = stdio, super( onFinish: onFinish, stopwatch: stopwatch, @@ -899,10 +906,10 @@ class SummaryStatus extends Status { /// terminal spinner. When stopped or canceled, the animation erases itself. class AnsiSpinner extends Status { AnsiSpinner({ - @required Stopwatch stopwatch, - @required Terminal terminal, - VoidCallback onFinish, - @required Stdio stdio, + required Stopwatch stopwatch, + required Terminal terminal, + VoidCallback? onFinish, + required Stdio stdio, }) : _stdio = stdio, _terminal = terminal, super( @@ -918,7 +925,7 @@ class AnsiSpinner extends Status { bool timedOut = false; int ticks = 0; - Timer timer; + Timer? timer; // Windows console font has a limited set of Unicode characters. List get _animation => !_terminal.supportsEmoji @@ -945,7 +952,7 @@ class AnsiSpinner extends Status { void _startSpinner() { _writeToStdOut(_clear); // for _callback to backspace over timer = Timer.periodic(const Duration(milliseconds: 100), _callback); - _callback(timer); + _callback(timer!); } void _callback(Timer timer) { @@ -960,8 +967,8 @@ class AnsiSpinner extends Status { @override void finish() { assert(timer != null); - assert(timer.isActive); - timer.cancel(); + assert(timer!.isActive); + timer?.cancel(); timer = null; _clearSpinner(); super.finish(); @@ -974,15 +981,15 @@ class AnsiSpinner extends Status { @override void pause() { assert(timer != null); - assert(timer.isActive); + assert(timer!.isActive); _clearSpinner(); - timer.cancel(); + timer?.cancel(); } @override void resume() { assert(timer != null); - assert(!timer.isActive); + assert(!timer!.isActive); _startSpinner(); } } @@ -1000,10 +1007,10 @@ class AnsiStatus extends AnsiSpinner { this.message = '', this.multilineOutput = false, this.padding = kDefaultStatusPadding, - @required Stopwatch stopwatch, - @required Terminal terminal, - VoidCallback onFinish, - Stdio stdio, + required Stopwatch stopwatch, + required Terminal terminal, + VoidCallback? onFinish, + required Stdio stdio, }) : assert(message != null), assert(multilineOutput != null), assert(padding != null), @@ -1023,7 +1030,7 @@ class AnsiStatus extends AnsiSpinner { @override int get spinnerIndent => _kTimePadding - 1; - int _totalMessageLength; + int _totalMessageLength = 0; @override void start() { diff --git a/packages/flutter_tools/lib/src/base/process.dart b/packages/flutter_tools/lib/src/base/process.dart index 7f9c3ffcb9..fe053b14bd 100644 --- a/packages/flutter_tools/lib/src/base/process.dart +++ b/packages/flutter_tools/lib/src/base/process.dart @@ -2,11 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// @dart = 2.8 - import 'dart:async'; -import 'package:meta/meta.dart'; import 'package:process/process.dart'; import '../convert.dart'; @@ -27,7 +24,7 @@ typedef ShutdownHook = FutureOr Function(); abstract class ShutdownHooks { factory ShutdownHooks({ - @required Logger logger, + required Logger logger, }) => _DefaultShutdownHooks( logger: logger, ); @@ -49,7 +46,7 @@ abstract class ShutdownHooks { class _DefaultShutdownHooks implements ShutdownHooks { _DefaultShutdownHooks({ - @required Logger logger, + required Logger logger, }) : _logger = logger; final Logger _logger; @@ -137,8 +134,8 @@ typedef RunResultChecker = bool Function(int); abstract class ProcessUtils { factory ProcessUtils({ - @required ProcessManager processManager, - @required Logger logger, + required ProcessManager processManager, + required Logger logger, }) => _DefaultProcessUtils( processManager: processManager, logger: logger, @@ -236,8 +233,8 @@ abstract class ProcessUtils { class _DefaultProcessUtils implements ProcessUtils { _DefaultProcessUtils({ - @required ProcessManager processManager, - @required Logger logger, + required ProcessManager processManager, + required Logger logger, }) : _processManager = processManager, _logger = logger; @@ -249,11 +246,11 @@ class _DefaultProcessUtils implements ProcessUtils { Future run( List cmd, { bool throwOnError = false, - RunResultChecker allowedFailures, - String workingDirectory, + RunResultChecker? allowedFailures, + String? workingDirectory, bool allowReentrantFlutter = false, - Map environment, - Duration timeout, + Map? environment, + Duration? timeout, int timeoutRetries = 0, }) async { if (cmd == null || cmd.isEmpty) { @@ -305,8 +302,8 @@ class _DefaultProcessUtils implements ProcessUtils { .listen(stderrBuffer.write) .asFuture(null); - int exitCode; - exitCode = await process.exitCode.timeout(timeout, onTimeout: () { + int? exitCode; + exitCode = await process.exitCode.then((int x) => x).timeout(timeout, onTimeout: () { // The process timed out. Kill it. _processManager.killPid(process.pid); return null; @@ -324,7 +321,7 @@ class _DefaultProcessUtils implements ProcessUtils { stdioFuture = stdioFuture.timeout(const Duration(seconds: 1)); } await stdioFuture; - } on Exception catch (_) { + } on Exception { // Ignore errors on the process' stdout and stderr streams. Just capture // whatever we got, and use the exit code } @@ -365,10 +362,10 @@ class _DefaultProcessUtils implements ProcessUtils { List cmd, { bool throwOnError = false, bool verboseExceptions = false, - RunResultChecker allowedFailures, + RunResultChecker? allowedFailures, bool hideStdout = false, - String workingDirectory, - Map environment, + String? workingDirectory, + Map? environment, bool allowReentrantFlutter = false, Encoding encoding = systemEncoding, }) { @@ -420,9 +417,9 @@ class _DefaultProcessUtils implements ProcessUtils { @override Future start( List cmd, { - String workingDirectory, + String? workingDirectory, bool allowReentrantFlutter = false, - Map environment, + Map? environment, }) { _traceCommand(cmd, workingDirectory: workingDirectory); return _processManager.start( @@ -435,14 +432,14 @@ class _DefaultProcessUtils implements ProcessUtils { @override Future stream( List cmd, { - String workingDirectory, + String? workingDirectory, bool allowReentrantFlutter = false, String prefix = '', bool trace = false, - RegExp filter, - RegExp stdoutErrorMatcher, - StringConverter mapFunction, - Map environment, + RegExp? filter, + RegExp? stdoutErrorMatcher, + StringConverter? mapFunction, + Map? environment, }) async { final Process process = await start( cmd, @@ -503,7 +500,7 @@ class _DefaultProcessUtils implements ProcessUtils { @override bool exitsHappySync( List cli, { - Map environment, + Map? environment, }) { _traceCommand(cli); if (!_processManager.canRun(cli.first)) { @@ -522,7 +519,7 @@ class _DefaultProcessUtils implements ProcessUtils { @override Future exitsHappy( List cli, { - Map environment, + Map? environment, }) async { _traceCommand(cli); if (!_processManager.canRun(cli.first)) { @@ -538,8 +535,8 @@ class _DefaultProcessUtils implements ProcessUtils { } } - Map _environment(bool allowReentrantFlutter, [ - Map environment, + Map? _environment(bool allowReentrantFlutter, [ + Map? environment, ]) { if (allowReentrantFlutter) { if (environment == null) { @@ -552,7 +549,7 @@ class _DefaultProcessUtils implements ProcessUtils { return environment; } - void _traceCommand(List args, { String workingDirectory }) { + void _traceCommand(List args, { String? workingDirectory }) { final String argsText = args.join(' '); if (workingDirectory == null) { _logger.printTrace('executing: $argsText'); diff --git a/packages/flutter_tools/lib/src/base/terminal.dart b/packages/flutter_tools/lib/src/base/terminal.dart index e440b2df48..7fd6fac148 100644 --- a/packages/flutter_tools/lib/src/base/terminal.dart +++ b/packages/flutter_tools/lib/src/base/terminal.dart @@ -2,10 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// @dart = 2.8 - -import 'package:meta/meta.dart'; - import '../convert.dart'; import 'io.dart' as io; import 'logger.dart'; @@ -25,12 +21,12 @@ enum TerminalColor { /// console. class OutputPreferences { OutputPreferences({ - bool wrapText, - int wrapColumn, - bool showColor, - io.Stdio stdio, + bool? wrapText, + int? wrapColumn, + bool? showColor, + io.Stdio? stdio, }) : _stdio = stdio, - wrapText = wrapText ?? stdio.hasTerminal, + wrapText = wrapText ?? stdio?.hasTerminal ?? false, _overrideWrapColumn = wrapColumn, showColor = showColor ?? false; @@ -38,7 +34,7 @@ class OutputPreferences { OutputPreferences.test({this.wrapText = false, int wrapColumn = kDefaultTerminalColumns, this.showColor = false}) : _overrideWrapColumn = wrapColumn, _stdio = null; - final io.Stdio _stdio; + final io.Stdio? _stdio; /// If [wrapText] is true, then any text sent to the context's [Logger] /// instance (e.g. from the [printError] or [printStatus] functions) will be @@ -56,7 +52,7 @@ class OutputPreferences { /// (e.g. from the [printError] or [printStatus] functions) will be wrapped. /// Ignored if [wrapText] is false. Defaults to the width of the output /// terminal, or to [kDefaultTerminalColumns] if not writing to a terminal. - final int _overrideWrapColumn; + final int? _overrideWrapColumn; int get wrapColumn { return _overrideWrapColumn ?? _stdio?.terminalColumns ?? kDefaultTerminalColumns; } @@ -136,17 +132,17 @@ abstract class Terminal { /// If [usesTerminalUi] is false, throws a [StateError]. Future promptForCharInput( List acceptedCharacters, { - @required Logger logger, - String prompt, - int defaultChoiceIndex, + required Logger logger, + String? prompt, + int? defaultChoiceIndex, bool displayAcceptedCharacters = true, }); } class AnsiTerminal implements Terminal { AnsiTerminal({ - @required io.Stdio stdio, - @required Platform platform, + required io.Stdio stdio, + required Platform platform, }) : _stdio = stdio, _platform = platform; @@ -178,10 +174,10 @@ class AnsiTerminal implements Terminal { TerminalColor.grey: grey, }; - static String colorCode(TerminalColor color) => _colorMap[color]; + static String colorCode(TerminalColor color) => _colorMap[color]!; @override - bool get supportsColor => _platform?.stdoutSupportsAnsi ?? false; + bool get supportsColor => _platform.stdoutSupportsAnsi; // Assume unicode emojis are supported when not on Windows. // If we are on Windows, unicode emojis are supported in Windows Terminal, @@ -236,7 +232,7 @@ class AnsiTerminal implements Terminal { return message; } final StringBuffer buffer = StringBuffer(); - final String colorCodes = _colorMap[color]; + final String colorCodes = _colorMap[color]!; for (String line in message.split('\n')) { // If there were resets in the string before, then keep them, but // restart the color right after. This prevents embedded resets from @@ -273,26 +269,23 @@ class AnsiTerminal implements Terminal { @override bool get stdinHasTerminal => _stdio.stdinHasTerminal; - Stream _broadcastStdInString; + Stream? _broadcastStdInString; @override Stream get keystrokes { - _broadcastStdInString ??= _stdio.stdin.transform(const AsciiDecoder(allowInvalid: true)).asBroadcastStream(); - return _broadcastStdInString; + return _broadcastStdInString ??= _stdio.stdin.transform(const AsciiDecoder(allowInvalid: true)).asBroadcastStream(); } @override Future promptForCharInput( List acceptedCharacters, { - @required Logger logger, - String prompt, - int defaultChoiceIndex, + required Logger logger, + String? prompt, + int? defaultChoiceIndex, bool displayAcceptedCharacters = true, }) async { - assert(acceptedCharacters != null); assert(acceptedCharacters.isNotEmpty); assert(prompt == null || prompt.isNotEmpty); - assert(displayAcceptedCharacters != null); if (!usesTerminalUi) { throw StateError('cannot prompt without a terminal ui'); } @@ -303,7 +296,7 @@ class AnsiTerminal implements Terminal { charactersToDisplay[defaultChoiceIndex] = bolden(charactersToDisplay[defaultChoiceIndex]); acceptedCharacters.add(''); } - String choice; + String? choice; singleCharMode = true; while (choice == null || choice.length > 1 || !acceptedCharacters.contains(choice)) { if (prompt != null) { @@ -329,7 +322,7 @@ class _TestTerminal implements Terminal { _TestTerminal({this.supportsColor = false, this.supportsEmoji = false}); @override - bool usesTerminalUi; + bool usesTerminalUi = false; @override String bolden(String message) => message; @@ -345,9 +338,9 @@ class _TestTerminal implements Terminal { @override Future promptForCharInput(List acceptedCharacters, { - @required Logger logger, - String prompt, - int defaultChoiceIndex, + required Logger logger, + String? prompt, + int? defaultChoiceIndex, bool displayAcceptedCharacters = true, }) { throw UnsupportedError('promptForCharInput not supported in the test terminal.');