Skip to content

Commit dca6320

Browse files
author
Jonah Williams
authored
[flutter_tools] inject output preferences at the top level (flutter#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.
1 parent e7b4d2b commit dca6320

5 files changed

Lines changed: 167 additions & 74 deletions

File tree

‎packages/flutter_tools/lib/executable.dart‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'src/base/template.dart';
1111
// The build_runner code generation is provided here to make it easier to
1212
// avoid introducing the dependency into google3. Not all build* packages
1313
// are synced internally.
14+
import 'src/base/terminal.dart';
1415
import 'src/build_runner/build_runner.dart';
1516
import 'src/build_runner/mustache_template.dart';
1617
import 'src/build_runner/resident_web_runner.dart';
@@ -135,6 +136,11 @@ Future<void> main(List<String> args) async {
135136
stdio: globals.stdio,
136137
terminal: globals.terminal,
137138
outputPreferences: globals.outputPreferences,
138-
))
139+
)),
140+
OutputPreferences: () => OutputPreferences.fromArguments(
141+
args,
142+
stdio: globals.stdio,
143+
userMessages: globals.userMessages,
144+
),
139145
});
140146
}

‎packages/flutter_tools/lib/src/base/terminal.dart‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import 'package:meta/meta.dart';
88

99
import '../convert.dart';
1010
import '../globals.dart' as globals;
11+
import 'common.dart';
1112
import 'io.dart' as io;
13+
import 'io.dart';
1214
import 'logger.dart';
1315
import 'platform.dart';
16+
import 'user_messages.dart';
1417

1518
enum TerminalColor {
1619
red,
@@ -47,6 +50,43 @@ class OutputPreferences {
4750
OutputPreferences.test({this.wrapText = false, int wrapColumn = kDefaultTerminalColumns, this.showColor = false})
4851
: _overrideWrapColumn = wrapColumn;
4952

53+
/// Create an [OutputPreferences] from a list of command line arguments.
54+
static OutputPreferences fromArguments(List<String> topLevelResults, {
55+
@required UserMessages userMessages,
56+
@required Stdio stdio,
57+
}) {
58+
// Don't set wrapColumns unless the user said to: if it's set, then all
59+
// wrapping will occur at this width explicitly, and won't adapt if the
60+
// terminal size changes during a run.
61+
int wrapColumn;
62+
final String rawWrapColumn = topLevelResults
63+
.firstWhere((String line) => line.startsWith('--wrap-column='), orElse: () => null);
64+
if (rawWrapColumn != null) {
65+
final List<String> parts = rawWrapColumn.split('=');
66+
if (parts.length > 1) {
67+
wrapColumn = int.tryParse(parts[1]);
68+
}
69+
if (wrapColumn == null) {
70+
throwToolExit(userMessages.runnerWrapColumnInvalid(rawWrapColumn));
71+
}
72+
}
73+
74+
// If we're not writing to a terminal with a defined width, then don't wrap
75+
// anything, unless the user explicitly said to.
76+
final bool useWrapping = !topLevelResults.contains('--no-wrap');
77+
78+
// If no default width was provided, first default to the terminal width. If
79+
// that is null, use `kDefaultTerminalColumns`.
80+
if (useWrapping && wrapColumn == null) {
81+
wrapColumn = stdio.terminalColumns ?? kDefaultTerminalColumns;
82+
}
83+
return OutputPreferences(
84+
wrapText: useWrapping,
85+
showColor: !topLevelResults.contains('--no-color'),
86+
wrapColumn: wrapColumn,
87+
);
88+
}
89+
5090
/// If [wrapText] is true, then any text sent to the context's [Logger]
5191
/// instance (e.g. from the [printError] or [printStatus] functions) will be
5292
/// wrapped (newlines added between words) to be no longer than the

‎packages/flutter_tools/lib/src/runner/flutter_command_runner.dart‎

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import '../artifacts.dart';
1515
import '../base/common.dart';
1616
import '../base/context.dart';
1717
import '../base/file_system.dart';
18-
import '../base/terminal.dart';
1918
import '../base/user_messages.dart';
2019
import '../base/utils.dart';
2120
import '../cache.dart';
@@ -235,32 +234,6 @@ class FlutterCommandRunner extends CommandRunner<void> {
235234
Future<void> runCommand(ArgResults topLevelResults) async {
236235
final Map<Type, dynamic> contextOverrides = <Type, dynamic>{};
237236

238-
// Don't set wrapColumns unless the user said to: if it's set, then all
239-
// wrapping will occur at this width explicitly, and won't adapt if the
240-
// terminal size changes during a run.
241-
int wrapColumn;
242-
if (topLevelResults.wasParsed('wrap-column')) {
243-
try {
244-
wrapColumn = int.parse(topLevelResults['wrap-column'] as String);
245-
if (wrapColumn < 0) {
246-
throwToolExit(userMessages.runnerWrapColumnInvalid(topLevelResults['wrap-column']));
247-
}
248-
} on FormatException {
249-
throwToolExit(userMessages.runnerWrapColumnParseError(topLevelResults['wrap-column']));
250-
}
251-
}
252-
253-
// If we're not writing to a terminal with a defined width, then don't wrap
254-
// anything, unless the user explicitly said to.
255-
final bool useWrapping = topLevelResults.wasParsed('wrap')
256-
? topLevelResults['wrap'] as bool
257-
: globals.stdio.terminalColumns != null && topLevelResults['wrap'] as bool;
258-
contextOverrides[OutputPreferences] = OutputPreferences(
259-
wrapText: useWrapping,
260-
showColor: topLevelResults['color'] as bool,
261-
wrapColumn: wrapColumn,
262-
);
263-
264237
if (topLevelResults['show-test-device'] as bool ||
265238
topLevelResults['device-id'] == FlutterTesterDevices.kTesterDeviceId) {
266239
FlutterTesterDevices.showFlutterTesterDevice = true;
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_tools/src/base/common.dart';
6+
import 'package:flutter_tools/src/base/io.dart';
7+
import 'package:flutter_tools/src/base/terminal.dart';
8+
import 'package:flutter_tools/src/base/user_messages.dart';
9+
import 'package:mockito/mockito.dart';
10+
11+
import '../src/common.dart';
12+
13+
void main() {
14+
testWithoutContext('Defaults to a wrapped terminal columns with color if no '
15+
'args are provided', () {
16+
final MockUserMessage userMessages = MockUserMessage();
17+
final MockStdio stdio = MockStdio();
18+
when(stdio.terminalColumns).thenReturn(80);
19+
20+
final OutputPreferences preferences = OutputPreferences.fromArguments(
21+
<String>[],
22+
userMessages: userMessages,
23+
stdio: stdio,
24+
);
25+
26+
expect(preferences.showColor, true);
27+
expect(preferences.wrapText, true);
28+
expect(preferences.wrapColumn, 80);
29+
});
30+
31+
testWithoutContext('Can be configured with --no-color', () {
32+
final MockUserMessage userMessages = MockUserMessage();
33+
final MockStdio stdio = MockStdio();
34+
when(stdio.terminalColumns).thenReturn(80);
35+
36+
final OutputPreferences preferences = OutputPreferences.fromArguments(
37+
<String>['--no-color'],
38+
userMessages: userMessages,
39+
stdio: stdio,
40+
);
41+
42+
expect(preferences.showColor, false);
43+
expect(preferences.wrapText, true);
44+
expect(preferences.wrapColumn, 80);
45+
});
46+
47+
testWithoutContext('Can be configured with a specific wrap length', () {
48+
final MockUserMessage userMessages = MockUserMessage();
49+
final MockStdio stdio = MockStdio();
50+
when(stdio.terminalColumns).thenReturn(80);
51+
52+
final OutputPreferences preferences = OutputPreferences.fromArguments(
53+
<String>['--wrap-column=123'],
54+
userMessages: userMessages,
55+
stdio: stdio,
56+
);
57+
58+
expect(preferences.showColor, true);
59+
expect(preferences.wrapText, true);
60+
expect(preferences.wrapColumn, 123);
61+
});
62+
63+
testWithoutContext('Will wrap to 100 when there is no terminal columns available', () {
64+
final MockUserMessage userMessages = MockUserMessage();
65+
final MockStdio stdio = MockStdio();
66+
when(stdio.terminalColumns).thenReturn(null);
67+
68+
final OutputPreferences preferences = OutputPreferences.fromArguments(
69+
<String>['--wrap'],
70+
userMessages: userMessages,
71+
stdio: stdio,
72+
);
73+
74+
expect(preferences.showColor, true);
75+
expect(preferences.wrapText, true);
76+
expect(preferences.wrapColumn, 100);
77+
});
78+
79+
testWithoutContext('Can be configured to disable wrapping', () {
80+
final MockUserMessage userMessages = MockUserMessage();
81+
final MockStdio stdio = MockStdio();
82+
when(stdio.terminalColumns).thenReturn(80);
83+
84+
final OutputPreferences preferences = OutputPreferences.fromArguments(
85+
<String>['--no-wrap'],
86+
userMessages: userMessages,
87+
stdio: stdio,
88+
);
89+
90+
expect(preferences.showColor, true);
91+
expect(preferences.wrapText, false);
92+
});
93+
94+
testWithoutContext('Throws a tool exit when an invalid wrap number is given', () {
95+
final MockUserMessage userMessages = MockUserMessage();
96+
final MockStdio stdio = MockStdio();
97+
when(stdio.terminalColumns).thenReturn(80);
98+
99+
expect(() => OutputPreferences.fromArguments(
100+
<String>['--wrap-column=a'],
101+
userMessages: userMessages,
102+
stdio: stdio,
103+
), throwsA(isA<ToolExit>()));
104+
});
105+
106+
testWithoutContext('Throws a tool exit when wrap is given without a number', () {
107+
final MockUserMessage userMessages = MockUserMessage();
108+
final MockStdio stdio = MockStdio();
109+
when(stdio.terminalColumns).thenReturn(80);
110+
111+
expect(() => OutputPreferences.fromArguments(
112+
<String>['--wrap-column='],
113+
userMessages: userMessages,
114+
stdio: stdio,
115+
), throwsA(isA<ToolExit>()));
116+
});
117+
}
118+
119+
class MockUserMessage extends Mock implements UserMessages {}
120+
class MockStdio extends Mock implements Stdio {}

‎packages/flutter_tools/test/general.shard/runner/flutter_command_runner_test.dart‎

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -239,52 +239,6 @@ void main() {
239239
Platform: () => platform,
240240
}, initializeFlutterRoot: false);
241241
});
242-
243-
group('wrapping', () {
244-
testUsingContext('checks that output wrapping is turned on when writing to a terminal', () async {
245-
final FakeFlutterCommand fakeCommand = FakeFlutterCommand();
246-
runner.addCommand(fakeCommand);
247-
await runner.run(<String>['fake']);
248-
expect(fakeCommand.preferences.wrapText, isTrue);
249-
}, overrides: <Type, Generator>{
250-
FileSystem: () => fs,
251-
ProcessManager: () => FakeProcessManager.any(),
252-
Stdio: () => FakeStdio(hasFakeTerminal: true),
253-
}, initializeFlutterRoot: false);
254-
255-
testUsingContext('checks that output wrapping is turned off when not writing to a terminal', () async {
256-
final FakeFlutterCommand fakeCommand = FakeFlutterCommand();
257-
runner.addCommand(fakeCommand);
258-
await runner.run(<String>['fake']);
259-
expect(fakeCommand.preferences.wrapText, isFalse);
260-
}, overrides: <Type, Generator>{
261-
FileSystem: () => fs,
262-
ProcessManager: () => FakeProcessManager.any(),
263-
Stdio: () => FakeStdio(hasFakeTerminal: false),
264-
}, initializeFlutterRoot: false);
265-
266-
testUsingContext('checks that output wrapping is turned off when set on the command line and writing to a terminal', () async {
267-
final FakeFlutterCommand fakeCommand = FakeFlutterCommand();
268-
runner.addCommand(fakeCommand);
269-
await runner.run(<String>['--no-wrap', 'fake']);
270-
expect(fakeCommand.preferences.wrapText, isFalse);
271-
}, overrides: <Type, Generator>{
272-
FileSystem: () => fs,
273-
ProcessManager: () => FakeProcessManager.any(),
274-
Stdio: () => FakeStdio(hasFakeTerminal: true),
275-
}, initializeFlutterRoot: false);
276-
277-
testUsingContext('checks that output wrapping is turned on when set on the command line, but not writing to a terminal', () async {
278-
final FakeFlutterCommand fakeCommand = FakeFlutterCommand();
279-
runner.addCommand(fakeCommand);
280-
await runner.run(<String>['--wrap', 'fake']);
281-
expect(fakeCommand.preferences.wrapText, isTrue);
282-
}, overrides: <Type, Generator>{
283-
FileSystem: () => fs,
284-
ProcessManager: () => FakeProcessManager.any(),
285-
Stdio: () => FakeStdio(hasFakeTerminal: false),
286-
}, initializeFlutterRoot: false);
287-
});
288242
});
289243
}
290244
class MockProcessManager extends Mock implements ProcessManager {}

0 commit comments

Comments
 (0)