[flutter_tools] inject output preferences at the top level (#58875)
The tool was setting the output preferences in a sub-context. Originally these were not injected before the arg parsers were created, though that was fixed by the lazy command creation. Once local engine is removed, the inner flutter_command Zone can be removed.
This commit is contained in:
@@ -11,6 +11,7 @@ import 'src/base/template.dart';
|
||||
// The build_runner code generation is provided here to make it easier to
|
||||
// avoid introducing the dependency into google3. Not all build* packages
|
||||
// are synced internally.
|
||||
import 'src/base/terminal.dart';
|
||||
import 'src/build_runner/build_runner.dart';
|
||||
import 'src/build_runner/mustache_template.dart';
|
||||
import 'src/build_runner/resident_web_runner.dart';
|
||||
@@ -135,6 +136,11 @@ Future<void> main(List<String> args) async {
|
||||
stdio: globals.stdio,
|
||||
terminal: globals.terminal,
|
||||
outputPreferences: globals.outputPreferences,
|
||||
))
|
||||
)),
|
||||
OutputPreferences: () => OutputPreferences.fromArguments(
|
||||
args,
|
||||
stdio: globals.stdio,
|
||||
userMessages: globals.userMessages,
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -8,9 +8,12 @@ import 'package:meta/meta.dart';
|
||||
|
||||
import '../convert.dart';
|
||||
import '../globals.dart' as globals;
|
||||
import 'common.dart';
|
||||
import 'io.dart' as io;
|
||||
import 'io.dart';
|
||||
import 'logger.dart';
|
||||
import 'platform.dart';
|
||||
import 'user_messages.dart';
|
||||
|
||||
enum TerminalColor {
|
||||
red,
|
||||
@@ -47,6 +50,43 @@ class OutputPreferences {
|
||||
OutputPreferences.test({this.wrapText = false, int wrapColumn = kDefaultTerminalColumns, this.showColor = false})
|
||||
: _overrideWrapColumn = wrapColumn;
|
||||
|
||||
/// Create an [OutputPreferences] from a list of command line arguments.
|
||||
static OutputPreferences fromArguments(List<String> topLevelResults, {
|
||||
@required UserMessages userMessages,
|
||||
@required Stdio stdio,
|
||||
}) {
|
||||
// Don't set wrapColumns unless the user said to: if it's set, then all
|
||||
// wrapping will occur at this width explicitly, and won't adapt if the
|
||||
// terminal size changes during a run.
|
||||
int wrapColumn;
|
||||
final String rawWrapColumn = topLevelResults
|
||||
.firstWhere((String line) => line.startsWith('--wrap-column='), orElse: () => null);
|
||||
if (rawWrapColumn != null) {
|
||||
final List<String> parts = rawWrapColumn.split('=');
|
||||
if (parts.length > 1) {
|
||||
wrapColumn = int.tryParse(parts[1]);
|
||||
}
|
||||
if (wrapColumn == null) {
|
||||
throwToolExit(userMessages.runnerWrapColumnInvalid(rawWrapColumn));
|
||||
}
|
||||
}
|
||||
|
||||
// If we're not writing to a terminal with a defined width, then don't wrap
|
||||
// anything, unless the user explicitly said to.
|
||||
final bool useWrapping = !topLevelResults.contains('--no-wrap');
|
||||
|
||||
// If no default width was provided, first default to the terminal width. If
|
||||
// that is null, use `kDefaultTerminalColumns`.
|
||||
if (useWrapping && wrapColumn == null) {
|
||||
wrapColumn = stdio.terminalColumns ?? kDefaultTerminalColumns;
|
||||
}
|
||||
return OutputPreferences(
|
||||
wrapText: useWrapping,
|
||||
showColor: !topLevelResults.contains('--no-color'),
|
||||
wrapColumn: wrapColumn,
|
||||
);
|
||||
}
|
||||
|
||||
/// If [wrapText] is true, then any text sent to the context's [Logger]
|
||||
/// instance (e.g. from the [printError] or [printStatus] functions) will be
|
||||
/// wrapped (newlines added between words) to be no longer than the
|
||||
|
||||
@@ -15,7 +15,6 @@ import '../artifacts.dart';
|
||||
import '../base/common.dart';
|
||||
import '../base/context.dart';
|
||||
import '../base/file_system.dart';
|
||||
import '../base/terminal.dart';
|
||||
import '../base/user_messages.dart';
|
||||
import '../base/utils.dart';
|
||||
import '../cache.dart';
|
||||
@@ -235,32 +234,6 @@ class FlutterCommandRunner extends CommandRunner<void> {
|
||||
Future<void> runCommand(ArgResults topLevelResults) async {
|
||||
final Map<Type, dynamic> contextOverrides = <Type, dynamic>{};
|
||||
|
||||
// Don't set wrapColumns unless the user said to: if it's set, then all
|
||||
// wrapping will occur at this width explicitly, and won't adapt if the
|
||||
// terminal size changes during a run.
|
||||
int wrapColumn;
|
||||
if (topLevelResults.wasParsed('wrap-column')) {
|
||||
try {
|
||||
wrapColumn = int.parse(topLevelResults['wrap-column'] as String);
|
||||
if (wrapColumn < 0) {
|
||||
throwToolExit(userMessages.runnerWrapColumnInvalid(topLevelResults['wrap-column']));
|
||||
}
|
||||
} on FormatException {
|
||||
throwToolExit(userMessages.runnerWrapColumnParseError(topLevelResults['wrap-column']));
|
||||
}
|
||||
}
|
||||
|
||||
// If we're not writing to a terminal with a defined width, then don't wrap
|
||||
// anything, unless the user explicitly said to.
|
||||
final bool useWrapping = topLevelResults.wasParsed('wrap')
|
||||
? topLevelResults['wrap'] as bool
|
||||
: globals.stdio.terminalColumns != null && topLevelResults['wrap'] as bool;
|
||||
contextOverrides[OutputPreferences] = OutputPreferences(
|
||||
wrapText: useWrapping,
|
||||
showColor: topLevelResults['color'] as bool,
|
||||
wrapColumn: wrapColumn,
|
||||
);
|
||||
|
||||
if (topLevelResults['show-test-device'] as bool ||
|
||||
topLevelResults['device-id'] == FlutterTesterDevices.kTesterDeviceId) {
|
||||
FlutterTesterDevices.showFlutterTesterDevice = true;
|
||||
|
||||
Reference in New Issue
Block a user