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