Unverified Commit 30c0fc1b authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] remove globals from compile and devices (#67485)

Remove global variables from compile.dart and devices.dart (except for dds which needs more changes).

#47161
parent 0cd380f5
......@@ -96,6 +96,14 @@ abstract class Terminal {
bool get usesTerminalUi;
set usesTerminalUi(bool value);
/// Whether there is a terminal attached to stdin.
///
/// If true, this usually indicates that a user is using the CLI as
/// opposed to using an IDE. This can be used to determine
/// whether it is appropriate to show a terminal prompt,
/// or whether an automatic selection should be made instead.
bool get stdinHasTerminal;
String bolden(String message);
String color(String message, TerminalColor color);
......@@ -251,6 +259,9 @@ class AnsiTerminal implements Terminal {
}
}
@override
bool get stdinHasTerminal => _stdio.stdinHasTerminal;
Stream<String> _broadcastStdInString;
@override
......@@ -339,4 +350,7 @@ class _TestTerminal implements Terminal {
@override
bool get supportsEmoji => false;
@override
bool get stdinHasTerminal => false;
}
......@@ -80,7 +80,7 @@ class DevicesCommand extends FlutterCommand {
globals.printStatus(status.toString());
} else {
globals.printStatus('${devices.length} connected ${pluralize('device', devices.length)}:\n');
await Device.printDevices(devices);
await Device.printDevices(devices, globals.logger);
}
await _printDiagnostics();
}
......
......@@ -427,7 +427,7 @@ Future<Device> findTargetDevice({ @required Duration timeout }) async {
}
if (devices.length > 1) {
globals.printStatus("Found ${devices.length} devices with name or id matching '${deviceManager.specifiedDeviceId}':");
await Device.printDevices(devices);
await Device.printDevices(devices, globals.logger);
return null;
}
return devices.first;
......@@ -438,7 +438,7 @@ Future<Device> findTargetDevice({ @required Duration timeout }) async {
return null;
} else if (devices.length > 1) {
globals.printStatus('Found multiple connected devices:');
await Device.printDevices(devices);
await Device.printDevices(devices, globals.logger);
}
globals.printStatus('Using device ${devices.first.name}.');
return devices.first;
......
......@@ -17,7 +17,6 @@ import 'base/logger.dart';
import 'base/platform.dart';
import 'build_info.dart';
import 'convert.dart';
import 'globals.dart' as globals;
/// The target model describes the set of core libraries that are available within
/// the SDK.
......@@ -416,8 +415,6 @@ abstract class ResidentCompiler {
List<String> dartDefines,
String librariesSpec,
@required Platform platform,
// Deprecated
List<String> experimentalFlags,
}) = DefaultResidentCompiler;
// TODO(jonahwilliams): find a better way to configure additional file system
......@@ -517,12 +514,10 @@ class DefaultResidentCompiler implements ResidentCompiler {
this.platformDill,
List<String> dartDefines,
this.librariesSpec,
// Deprecated
List<String> experimentalFlags, // ignore: avoid_unused_constructor_parameters
}) : assert(sdkRoot != null),
_logger = logger ?? globals.logger,
_processManager = processManager ?? globals.processManager,
_artifacts = artifacts ?? globals.artifacts,
_logger = logger,
_processManager = processManager,
_artifacts = artifacts,
_stdoutHandler = StdoutHandler(logger: logger),
_platform = platform,
dartDefines = dartDefines ?? const <String>[],
......
......@@ -152,6 +152,7 @@ Future<T> runInContext<T>(
featureFlags: featureFlags,
),
operatingSystemUtils: globals.os,
terminal: globals.terminal,
),
Doctor: () => Doctor(logger: globals.logger),
DoctorValidatorsProvider: () => DoctorValidatorsProvider.defaultInstance,
......
......@@ -23,14 +23,15 @@ import 'base/io.dart';
import 'base/logger.dart';
import 'base/os.dart';
import 'base/platform.dart';
import 'base/user_messages.dart';
import 'base/terminal.dart';
import 'base/user_messages.dart' hide userMessages;
import 'base/utils.dart';
import 'build_info.dart';
import 'features.dart';
import 'fuchsia/fuchsia_device.dart';
import 'fuchsia/fuchsia_sdk.dart';
import 'fuchsia/fuchsia_workflow.dart';
import 'globals.dart' as globals;
import 'globals.dart' as globals show logger;
import 'ios/devices.dart';
import 'ios/ios_workflow.dart';
import 'ios/simulators.dart';
......@@ -81,6 +82,17 @@ class PlatformType {
/// A disovery mechanism for flutter-supported development devices.
abstract class DeviceManager {
DeviceManager({
@required Logger logger,
@required Terminal terminal,
@required UserMessages userMessages,
}) : _logger = logger,
_terminal = terminal,
_userMessages = userMessages;
final Logger _logger;
final Terminal _terminal;
final UserMessages _userMessages;
/// Constructing DeviceManagers is cheap; they only do expensive work if some
/// of their methods are called.
......@@ -139,7 +151,7 @@ abstract class DeviceManager {
return null;
}, onError: (dynamic error, StackTrace stackTrace) {
// Return matches from other discoverers even if one fails.
globals.printTrace('Ignored error discovering $deviceId: $error');
_logger.printTrace('Ignored error discovering $deviceId: $error');
})
];
......@@ -275,11 +287,11 @@ abstract class DeviceManager {
// has two active Android devices running, then we request the user to
// choose one. If the user has two nonEphemeral devices running, we also
// request input to choose one.
if (devices.length > 1 && globals.stdio.stdinHasTerminal) {
globals.printStatus(globals.userMessages.flutterMultipleDevicesFound);
await Device.printDevices(devices);
if (devices.length > 1 && _terminal.stdinHasTerminal) {
_logger.printStatus(_userMessages.flutterMultipleDevicesFound);
await Device.printDevices(devices, _logger);
final Device chosenDevice = await _chooseOneOfAvailableDevices(devices);
globals.deviceManager.specifiedDeviceId = chosenDevice.id;
specifiedDeviceId = chosenDevice.id;
devices = <Device>[chosenDevice];
}
}
......@@ -298,18 +310,19 @@ abstract class DeviceManager {
void _displayDeviceOptions(List<Device> devices) {
int count = 0;
for (final Device device in devices) {
globals.printStatus(userMessages.flutterChooseDevice(count, device.name, device.id));
_logger.printStatus(_userMessages.flutterChooseDevice(count, device.name, device.id));
count++;
}
}
Future<String> _readUserInput(int deviceCount) async {
globals.terminal.usesTerminalUi = true;
final String result = await globals.terminal.promptForCharInput(
<String>[ for (int i = 0; i < deviceCount; i++) '$i', 'q', 'Q'],
displayAcceptedCharacters: false,
logger: globals.logger,
prompt: userMessages.flutterChooseOne);
_terminal.usesTerminalUi = true;
final String result = await _terminal.promptForCharInput(
<String>[ for (int i = 0; i < deviceCount; i++) '$i', 'q', 'Q'],
displayAcceptedCharacters: false,
logger: _logger,
prompt: _userMessages.flutterChooseOne,
);
return result;
}
......@@ -345,6 +358,7 @@ class FlutterDeviceManager extends DeviceManager {
@required UserMessages userMessages,
@required OperatingSystemUtils operatingSystemUtils,
@required WindowsWorkflow windowsWorkflow,
@required Terminal terminal,
}) : deviceDiscoverers = <DeviceDiscovery>[
AndroidDevices(
logger: logger,
......@@ -408,7 +422,11 @@ class FlutterDeviceManager extends DeviceManager {
processManager: processManager,
logger: logger,
),
];
], super(
logger: logger,
terminal: terminal,
userMessages: userMessages,
);
@override
final List<DeviceDiscovery> deviceDiscoverers;
......@@ -465,7 +483,7 @@ abstract class PollingDeviceDiscovery extends DeviceDiscovery {
final List<Device> devices = await pollingGetDevices(timeout: pollingTimeout);
deviceNotifier.updateWithNewList(devices);
} on TimeoutException {
globals.printTrace('Device poll timed out. Will retry.');
// Do nothing on a timeout.
}
// Subsequent timeouts after initial population should wait longer.
_timer = _initTimer(_pollingTimeout);
......@@ -736,8 +754,8 @@ abstract class Device {
}
}
static Future<void> printDevices(List<Device> devices) async {
await descriptions(devices).forEach(globals.printStatus);
static Future<void> printDevices(List<Device> devices, Logger logger) async {
await descriptions(devices).forEach(logger.printStatus);
}
static List<String> devicesPlatformTypes(List<Device> devices) {
......
......@@ -1063,7 +1063,7 @@ abstract class FlutterCommand extends Command<void> {
devices = await deviceManager.getAllConnectedDevices();
}
globals.printStatus('');
await Device.printDevices(devices);
await Device.printDevices(devices, globals.logger);
return null;
}
return devices;
......@@ -1087,7 +1087,7 @@ abstract class FlutterCommand extends Command<void> {
globals.printStatus(userMessages.flutterSpecifyDevice);
deviceList = await globals.deviceManager.getAllConnectedDevices();
globals.printStatus('');
await Device.printDevices(deviceList);
await Device.printDevices(deviceList, globals.logger);
return null;
}
return deviceList.single;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment