[flutter_tools] Add support for custom devices (#78113)

This commit is contained in:
Hannes Winkler
2021-04-01 03:09:02 +02:00
committed by GitHub
parent c1ca47b4b4
commit e8ad03099f
16 changed files with 1859 additions and 7 deletions

View File

@@ -28,11 +28,12 @@ class Config {
required FileSystem fileSystem,
required Logger logger,
required Platform platform,
bool deleteFileOnFormatException = true
}) {
final String filePath = _configPath(platform, fileSystem, name);
final File file = fileSystem.file(filePath);
file.parent.createSync(recursive: true);
return Config.createForTesting(file, logger);
return Config.createForTesting(file, logger, deleteFileOnFormatException: deleteFileOnFormatException);
}
/// Constructs a new [Config] object from a file called [name] in
@@ -43,14 +44,19 @@ class Config {
String name = 'test',
Directory? directory,
Logger? logger,
bool deleteFileOnFormatException = true
}) {
directory ??= MemoryFileSystem.test().directory('/');
return Config.createForTesting(directory.childFile('.${kConfigDir}_$name'), logger ?? BufferLogger.test());
return Config.createForTesting(
directory.childFile('.${kConfigDir}_$name'),
logger ?? BufferLogger.test(),
deleteFileOnFormatException: deleteFileOnFormatException
);
}
/// Test only access to the Config constructor.
@visibleForTesting
Config.createForTesting(File file, Logger logger) : _file = file, _logger = logger {
Config.createForTesting(File file, Logger logger, {bool deleteFileOnFormatException = true}) : _file = file, _logger = logger {
if (!_file.existsSync()) {
return;
}
@@ -65,7 +71,10 @@ class Config {
'You may need to reapply any previously saved configuration '
'with the "flutter config" command.',
);
_file.deleteSync();
if (deleteFileOnFormatException) {
_file.deleteSync();
}
} on Exception catch (err) {
_logger
..printError('Could not read preferences in ${file.path}.\n$err')

View File

@@ -389,3 +389,55 @@ bool isWhitespace(_AnsiRun run) {
rune == 0x3000 ||
rune == 0xFEFF;
}
final RegExp _interpolationRegex = RegExp(r'\$\{([^}]*)\}');
/// Given a string that possibly contains string interpolation sequences
/// (so for example, something like `ping -n 1 ${host}`), replace all those
/// interpolation sequences with the matching value given in [replacementValues].
///
/// If the value could not be found inside [replacementValues], an empty
/// string will be substituted instead.
///
/// However, if the dollar sign inside the string is preceded with a backslash,
/// the sequences won't be substituted at all.
///
/// Example:
/// ```dart
/// final interpolated = _interpolateString(r'ping -n 1 ${host}', {'host': 'raspberrypi'});
/// print(interpolated); // will print 'ping -n 1 raspberrypi'
///
/// final interpolated2 = _interpolateString(r'ping -n 1 ${_host}', {'host': 'raspberrypi'});
/// print(interpolated2); // will print 'ping -n 1 '
/// ```
String interpolateString(String toInterpolate, Map<String, String> replacementValues) {
return toInterpolate.replaceAllMapped(_interpolationRegex, (Match match) {
/// The name of the variable to be inserted into the string.
/// Example: If the source string is 'ping -n 1 ${host}',
/// `name` would be 'host'
final String name = match.group(1)!;
return replacementValues.containsKey(name) ? replacementValues[name]! : '';
});
}
/// Given a list of strings possibly containing string interpolation sequences
/// (so for example, something like `['ping', '-n', '1', '${host}']`), replace
/// all those interpolation sequences with the matching value given in [replacementValues].
///
/// If the value could not be found inside [replacementValues], an empty
/// string will be substituted instead.
///
/// However, if the dollar sign inside the string is preceded with a backslash,
/// the sequences won't be substituted at all.
///
/// Example:
/// ```dart
/// final interpolated = _interpolateString(['ping', '-n', '1', r'${host}'], {'host': 'raspberrypi'});
/// print(interpolated); // will print '[ping, -n, 1, raspberrypi]'
///
/// final interpolated2 = _interpolateString(['ping', '-n', '1', r'${_host}'], {'host': 'raspberrypi'});
/// print(interpolated2); // will print '[ping, -n, 1, ]'
/// ```
List<String> interpolateStringList(List<String> toInterpolate, Map<String, String> replacementValues) {
return toInterpolate.map((String s) => interpolateString(s, replacementValues)).toList();
}

View File

@@ -391,6 +391,9 @@ class DaemonDomain extends Domain {
if (featureFlags.isFuchsiaEnabled && flutterProject.fuchsia.existsSync()) {
result.add('fuchsia');
}
if (featureFlags.areCustomDevicesEnabled) {
result.add('custom');
}
return <String, Object>{
'platforms': result,
};

View File

@@ -30,6 +30,7 @@ import 'base/user_messages.dart';
import 'build_info.dart';
import 'build_system/build_system.dart';
import 'cache.dart';
import 'custom_devices/custom_devices_config.dart';
import 'dart/pub.dart';
import 'devfs.dart';
import 'device.dart';
@@ -197,6 +198,11 @@ Future<T> runInContext<T>(
),
operatingSystemUtils: globals.os,
terminal: globals.terminal,
customDevicesConfig: CustomDevicesConfig(
fileSystem: globals.fs,
logger: globals.logger,
platform: globals.platform
),
),
DevtoolsLauncher: () => DevtoolsServerLauncher(
processManager: globals.processManager,

View File

@@ -0,0 +1,751 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// 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 '../application_package.dart';
import '../base/common.dart';
import '../base/io.dart';
import '../base/logger.dart';
import '../base/process.dart';
import '../base/utils.dart';
import '../build_info.dart';
import '../bundle.dart';
import '../convert.dart';
import '../device.dart';
import '../device_port_forwader.dart';
import '../features.dart';
import '../project.dart';
import '../protocol_discovery.dart';
import 'custom_device_config.dart';
import 'custom_device_workflow.dart';
import 'custom_devices_config.dart';
/// Replace all ocurrences of `${someName}` with the value found for that
/// name inside replacementValues or additionalReplacementValues.
///
/// The replacement value is first looked for in [replacementValues] and then
/// [additionalReplacementValues]. If no value is found, an empty string will be
/// substituted instead.
List<String> interpolateCommand(
List<String> command,
Map<String, String> replacementValues, {
Map<String, String> additionalReplacementValues = const <String, String>{}
}) {
return interpolateStringList(
command,
Map<String, String>.of(additionalReplacementValues)
..addAll(replacementValues)
);
}
/// A log reader that can listen to a process' stdout / stderr or another log line
/// Stream.
class CustomDeviceLogReader extends DeviceLogReader {
CustomDeviceLogReader(this.name);
/// The name of the device this log reader is associated with.
@override
final String name;
final StreamController<String> _logLinesController = StreamController<String>.broadcast();
/// Listen to [process]' stdout and stderr, decode them using [SystemEncoding]
/// and add each decoded line to [logLines].
///
/// However, [logLines] will not be done when the [process]' stdout and stderr
/// streams are done. So [logLines] will still be alive after the process has
/// finished.
///
/// See [CustomDeviceLogReader.dispose] to end the [logLines] stream.
void listenToProcessOutput(Process process, {Encoding encoding = systemEncoding}) {
final Converter<List<int>, String> decoder = encoding.decoder;
process.stdout.transform<String>(decoder)
.transform<String>(const LineSplitter())
.listen(_logLinesController.add);
process.stderr.transform<String>(decoder)
.transform<String>(const LineSplitter())
.listen(_logLinesController.add);
}
/// Add all lines emitted by [lines] to this [CustomDeviceLogReader]s [logLines]
/// stream.
///
/// Similiar to [listenToProcessOutput], [logLines] will not be marked as done
/// when the argument stream is done.
///
/// Useful when you want to combine the contents of multiple log readers.
void listenToLinesStream(Stream<String> lines) {
_logLinesController.addStream(lines);
}
/// Dispose this log reader, freeing all associated resources and marking
/// [logLines] as done.
@override
void dispose() {
_logLinesController.close();
}
@override
Stream<String> get logLines => _logLinesController.stream;
}
/// A [DevicePortForwarder] that uses commands to forward / unforward a port.
class CustomDevicePortForwarder extends DevicePortForwarder {
CustomDevicePortForwarder({
@required String deviceName,
@required List<String> forwardPortCommand,
@required RegExp forwardPortSuccessRegex,
this.numTries/*?*/,
@required ProcessManager processManager,
@required Logger logger,
Map<String, String> additionalReplacementValues = const <String, String>{}
}) : _deviceName = deviceName,
_forwardPortCommand = forwardPortCommand,
_forwardPortSuccessRegex = forwardPortSuccessRegex,
_processManager = processManager,
_processUtils = ProcessUtils(
processManager: processManager,
logger: logger
),
_additionalReplacementValues = additionalReplacementValues;
final String _deviceName;
final List<String> _forwardPortCommand;
final RegExp _forwardPortSuccessRegex;
final ProcessManager _processManager;
final ProcessUtils _processUtils;
final int numTries;
final Map<String, String> _additionalReplacementValues;
final List<ForwardedPort> _forwardedPorts = <ForwardedPort>[];
@override
Future<void> dispose() async {
// copy the list so we don't modify it concurrently
return Future.wait(List<ForwardedPort>.of(_forwardedPorts).map(unforward));
}
Future<ForwardedPort> _tryForward(int devicePort, int hostPort) async {
final List<String> interpolated = interpolateCommand(
_forwardPortCommand,
<String, String>{
'devicePort': '$devicePort',
'hostPort': '$hostPort'
},
additionalReplacementValues: _additionalReplacementValues
);
// launch the forwarding command
final Process process = await _processUtils.start(interpolated);
final Completer<ForwardedPort> completer = Completer<ForwardedPort>();
// read the outputs of the process, if we find a line that matches
// the configs forwardPortSuccessRegex, we complete with a successfully
// forwarded port
// Note that if that regex never matches, this will potentially run forever
// and the forwarding will never complete
final CustomDeviceLogReader reader = CustomDeviceLogReader(_deviceName)..listenToProcessOutput(process);
final StreamSubscription<String> logLinesSubscription = reader.logLines.listen((String line) {
if (_forwardPortSuccessRegex.hasMatch(line) && !completer.isCompleted) {
completer.complete(
ForwardedPort.withContext(hostPort, devicePort, process)
);
}
});
// if the process exits (even with exitCode == 0), that is considered
// a port forwarding failure and we complete with a null value.
unawaited(process.exitCode.whenComplete(() {
if (!completer.isCompleted) {
completer.complete(null);
}
}));
unawaited(completer.future.whenComplete(() {
logLinesSubscription.cancel();
reader.dispose();
}));
return completer.future;
}
@override
Future<int> forward(int devicePort, {int hostPort}) async {
int actualHostPort = (hostPort == 0 || hostPort == null) ? devicePort : hostPort;
int tries = 0;
while ((numTries == null) || (tries < numTries)) {
// when the desired host port is already forwarded by this Forwarder,
// choose another one
while (_forwardedPorts.any((ForwardedPort port) => port.hostPort == actualHostPort)) {
actualHostPort += 1;
}
final ForwardedPort port = await _tryForward(devicePort, actualHostPort);
if (port != null) {
_forwardedPorts.add(port);
return actualHostPort;
} else {
// null value means the forwarding failed (for whatever reason)
// increase port by one and try again
actualHostPort += 1;
tries += 1;
}
}
throw ToolExit('Forwarding port for custom device $_deviceName failed after $tries tries.');
}
@override
List<ForwardedPort> get forwardedPorts => List<ForwardedPort>.unmodifiable(_forwardedPorts);
@override
Future<void> unforward(ForwardedPort forwardedPort) async {
assert(_forwardedPorts.contains(forwardedPort));
// since a forwarded port represents a running process launched with
// the forwardPortCommand, unforwarding is as easy as killing the proces
_processManager.killPid(forwardedPort.context.pid);
_forwardedPorts.remove(forwardedPort);
}
}
/// A combination of [ApplicationPackage] and a [CustomDevice]. Can only start,
/// stop this specific app package with this specific device. Useful because we
/// often need to store additional context to an app that is running on a device,
/// like any forwarded ports we need to unforward later, the process we need to
/// kill to stop the app, maybe other things in the future.
class CustomDeviceAppSession {
CustomDeviceAppSession({
@required this.name,
@required CustomDevice device,
@required ApplicationPackage appPackage,
@required Logger logger,
@required ProcessManager processManager
}) : _appPackage = appPackage,
_device = device,
_logger = logger,
_processManager = processManager,
logReader = CustomDeviceLogReader(name);
final String name;
final CustomDevice _device;
final ApplicationPackage _appPackage;
final Logger _logger;
final ProcessManager _processManager;
final CustomDeviceLogReader logReader;
Process _process;
int _forwardedHostPort;
Future<LaunchResult> start({
String mainPath,
String route,
DebuggingOptions debuggingOptions,
Map<String, dynamic> platformArgs,
bool prebuiltApplication = false,
bool ipv6 = false,
String userIdentifier
}) async {
final List<String> interpolated = interpolateCommand(
_device._config.runDebugCommand,
<String, String>{
'remotePath': '/tmp/',
'appName': _appPackage.name
}
);
final Process process = await _processManager.start(interpolated);
assert(_process == null);
_process = process;
final ProtocolDiscovery discovery = ProtocolDiscovery.observatory(
logReader,
portForwarder: _device._config.usesPortForwarding ? _device.portForwarder : null,
hostPort: null, devicePort: null,
logger: _logger,
ipv6: ipv6,
);
// We need to make the discovery listen to the logReader before the logReader
// listens to the process output since logReader.lines is a broadcast stream
// and events may be discarded.
// Whether that actually happens is another thing since this is all executed
// in the same microtask AFAICT but this way we're on the safe side.
logReader.listenToProcessOutput(process);
final Uri observatoryUri = await discovery.uri;
await discovery.cancel();
if (_device._config.usesPortForwarding) {
_forwardedHostPort = observatoryUri.port;
}
return LaunchResult.succeeded(observatoryUri: observatoryUri);
}
void _maybeUnforwardPort() {
if (_forwardedHostPort != null) {
final ForwardedPort forwardedPort = _device.portForwarder.forwardedPorts.singleWhere((ForwardedPort forwardedPort) {
return forwardedPort.hostPort == _forwardedHostPort;
});
_forwardedHostPort = null;
_device.portForwarder.unforward(forwardedPort);
}
}
Future<bool> stop() async {
if (_process == null) {
return false;
}
_maybeUnforwardPort();
final bool result = _processManager.killPid(_process.pid);
_process = null;
return result;
}
void dispose() {
if (_process != null) {
_maybeUnforwardPort();
_processManager.killPid(_process.pid);
_process = null;
}
logReader.dispose();
}
}
/// A device that uses user-configured actions for the common device methods.
/// The exact actions are defined by the contents of the [CustomDeviceConfig]
/// used to construct it.
class CustomDevice extends Device {
CustomDevice({
@required CustomDeviceConfig config,
@required Logger logger,
@required ProcessManager processManager,
}) : _config = config,
_logger = logger,
_processManager = processManager,
_processUtils = ProcessUtils(
processManager: processManager,
logger: logger
),
_globalLogReader = CustomDeviceLogReader(config.label),
portForwarder = config.usesPortForwarding ?
CustomDevicePortForwarder(
deviceName: config.label,
forwardPortCommand: config.forwardPortCommand,
forwardPortSuccessRegex: config.forwardPortSuccessRegex,
processManager: processManager,
logger: logger,
) : const NoOpDevicePortForwarder(),
super(
config.id,
category: Category.mobile,
ephemeral: true,
platformType: PlatformType.custom
);
final CustomDeviceConfig _config;
final Logger _logger;
final ProcessManager _processManager;
final ProcessUtils _processUtils;
final Map<ApplicationPackage, CustomDeviceAppSession> _sessions = <ApplicationPackage, CustomDeviceAppSession>{};
final CustomDeviceLogReader _globalLogReader;
@override
final DevicePortForwarder portForwarder;
CustomDeviceAppSession _getOrCreateAppSession(covariant ApplicationPackage app) {
return _sessions.putIfAbsent(
app,
() {
/// create a new session and add its logging to the global log reader.
/// (needed bc it's possible the infra requests a global log in [getLogReader]
final CustomDeviceAppSession session = CustomDeviceAppSession(
name: name,
device: this,
appPackage: app,
logger: _logger,
processManager: _processManager
);
_globalLogReader.listenToLinesStream(session.logReader.logLines);
return session;
}
);
}
/// Tries to ping the device using the ping command given in the config.
/// All string interpolation occurrences inside the ping command will be replaced
/// using the entries in [replacementValues].
///
/// If the process finishes with an exit code != 0, false will be returned and
/// the error (with the process' stdout and stderr) will be logged using
/// [_logger.printError].
///
/// If [timeout] is not null and the process doesn't finish in time,
/// it will be killed with a SIGTERM, false will be returned and the timeout
/// will be reported in the log using [_logger.printError]. If [timeout]
/// is null, it's treated as if it's an infinite timeout.
Future<bool> _tryPing({
Duration timeout,
Map<String, String> replacementValues = const <String, String>{}
}) async {
final List<String> interpolated = interpolateCommand(
_config.pingCommand,
replacementValues
);
try {
final RunResult result = await _processUtils.run(
interpolated,
throwOnError: true,
timeout: timeout
);
// If the user doesn't configure a ping success regex, any ping with exitCode zero
// is good enough. Otherwise we check if either stdout or stderr have a match of
// the pingSuccessRegex.
return _config.pingSuccessRegex == null
|| _config.pingSuccessRegex.hasMatch(result.stdout)
|| _config.pingSuccessRegex.hasMatch(result.stderr);
} on ProcessException catch (e) {
_logger.printError('Error executing ping command for custom device $id: $e');
return false;
}
}
/// Tries to execute the configs postBuild command using [appName] for the
/// `${appName}` and [localPath] for the `${localPath}` interpolations,
/// any additional string interpolation occurrences will be replaced using the
/// entries in [additionalReplacementValues].
///
/// Calling this when the config doesn't have a configured postBuild command
/// is an error.
///
/// If [timeout] is not null and the process doesn't finish in time, it
/// will be killed with a SIGTERM, false will be returned and the timeout
/// will be reported in the log using [_logger.printError]. If [timeout]
/// is null, it's treated as if it's an infinite timeout.
Future<bool> _tryPostBuild({
@required String appName,
@required String localPath,
Duration timeout,
Map<String, String> additionalReplacementValues = const <String, String>{}
}) async {
assert(_config.postBuildCommand != null);
final List<String> interpolated = interpolateCommand(
_config.postBuildCommand,
<String, String>{
'appName': appName,
'localPath': localPath
},
additionalReplacementValues: additionalReplacementValues
);
try {
await _processUtils.run(
interpolated,
throwOnError: true,
timeout: timeout
);
return true;
} on ProcessException catch (e) {
_logger.printError('Error executing postBuild command for custom device $id: $e');
return false;
}
}
/// Tries to execute the configs uninstall command.
///
/// [appName] is the name of the app to be installed.
///
/// If [timeout] is not null and the process doesn't finish in time, it
/// will be killed with a SIGTERM, false will be returned and the timeout
/// will be reported in the log using [_logger.printError]. If [timeout]
/// is null, it's treated as if it's an infinite timeout.
Future<bool> _tryUninstall({
@required String appName,
Duration timeout,
Map<String, String> additionalReplacementValues = const <String, String>{}
}) async {
final List<String> interpolated = interpolateCommand(
_config.uninstallCommand,
<String, String>{
'appName': appName
},
additionalReplacementValues: additionalReplacementValues
);
try {
await _processUtils.run(
interpolated,
throwOnError: true,
timeout: timeout
);
return true;
} on ProcessException catch (e) {
_logger.printError('Error executing uninstall command for custom device $id: $e');
return false;
}
}
/// Tries to install the app to the custom device.
///
/// [localPath] is the file / directory on the local device that will be
/// copied over to the target custom device. This is substituted for any occurrence
/// of `${localPath}` in the custom device configs `install` command.
///
/// [appName] is the name of the app to be installed. Substituted for any occurrence
/// of `${appName}` in the custom device configs `install` command.
Future<bool> _tryInstall({
@required String localPath,
@required String appName,
Duration timeout,
Map<String, String> additionalReplacementValues = const <String, String>{}
}) async {
final List<String> interpolated = interpolateCommand(
_config.installCommand,
<String, String>{
'localPath': localPath,
'appName': appName
},
additionalReplacementValues: additionalReplacementValues
);
try {
await _processUtils.run(
interpolated,
throwOnError: true,
timeout: timeout
);
return true;
} on ProcessException catch (e) {
_logger.printError('Error executing install command for custom device $id: $e');
return false;
}
}
@override
void clearLogs() {}
@override
Future<void> dispose() async {
_sessions
..forEach((_, CustomDeviceAppSession session) => session.dispose())
..clear();
}
@override
Future<String> get emulatorId async => null;
@override
FutureOr<DeviceLogReader> getLogReader({
covariant ApplicationPackage app,
bool includePastLogs = false
}) {
if (app != null) {
return _getOrCreateAppSession(app).logReader;
}
return _globalLogReader;
}
@override
Future<bool> installApp(covariant ApplicationPackage app, {String userIdentifier}) async {
if (!await _tryUninstall(appName: app.name)) {
return false;
}
final bool result = await _tryInstall(
localPath: getAssetBuildDirectory(),
appName: app.name
);
return result;
}
@override
Future<bool> isAppInstalled(covariant ApplicationPackage app, {String userIdentifier}) async {
return false;
}
@override
Future<bool> isLatestBuildInstalled(covariant ApplicationPackage app) async {
return false;
}
@override
Future<bool> get isLocalEmulator async => false;
@override
bool isSupported() {
return true;
}
@override
bool isSupportedForProject(FlutterProject flutterProject) {
return true;
}
@override
FutureOr<bool> supportsRuntimeMode(BuildMode buildMode) {
return buildMode == BuildMode.debug;
}
@override
String get name => _config.label;
@override
Future<String> get sdkNameAndVersion => Future<String>.value(_config.sdkNameAndVersion);
@override
Future<LaunchResult> startApp(
covariant ApplicationPackage package, {
String mainPath,
String route,
DebuggingOptions debuggingOptions,
Map<String, dynamic> platformArgs,
bool prebuiltApplication = false,
bool ipv6 = false,
String userIdentifier,
BundleBuilder bundleBuilder
}) async {
if (!prebuiltApplication) {
final String assetBundleDir = getAssetBuildDirectory();
bundleBuilder ??= BundleBuilder();
// this just builds the asset bundle, it's the same as `flutter build bundle`
await bundleBuilder.build(
platform: await targetPlatform,
buildInfo: debuggingOptions.buildInfo,
mainPath: mainPath,
depfilePath: defaultDepfilePath,
assetDirPath: assetBundleDir,
treeShakeIcons: false,
);
// if we have a post build step (needed for some embedders), execute it
if (_config.postBuildCommand != null) {
await _tryPostBuild(
appName: package.name,
localPath: assetBundleDir,
);
}
}
// install the app on the device
// (will invoke the uninstall and then the install command internally)
await installApp(package, userIdentifier: userIdentifier);
// finally launch the app
return _getOrCreateAppSession(package).start(
mainPath: mainPath,
route: route,
debuggingOptions: debuggingOptions,
platformArgs: platformArgs,
prebuiltApplication: prebuiltApplication,
ipv6: ipv6,
userIdentifier: userIdentifier,
);
}
@override
Future<bool> stopApp(covariant ApplicationPackage app, {String userIdentifier}) {
return _getOrCreateAppSession(app).stop();
}
@override
// TODO(ardera): Allow configuring or auto-detecting the target platform, https://github.com/flutter/flutter/issues/78151
Future<TargetPlatform> get targetPlatform async => TargetPlatform.linux_arm64;
@override
Future<bool> uninstallApp(covariant ApplicationPackage app, {String userIdentifier}) {
return _tryUninstall(appName: app.name);
}
}
/// A [PollingDeviceDiscovery] that'll try to ping all enabled devices in the argument
/// [CustomDevicesConfig] and report the ones that were actually reachable.
class CustomDevices extends PollingDeviceDiscovery {
/// Create a custom device discovery that pings all enabled devices in the
/// given [CustomDevicesConfig].
CustomDevices({
@required FeatureFlags featureFlags,
@required ProcessManager processManager,
@required Logger logger,
@required CustomDevicesConfig config
}) : _customDeviceWorkflow = CustomDeviceWorkflow(
featureFlags: featureFlags,
),
_logger = logger,
_processManager = processManager,
_config = config,
super('custom devices');
final CustomDeviceWorkflow _customDeviceWorkflow;
final ProcessManager _processManager;
final Logger _logger;
final CustomDevicesConfig _config;
@override
bool get supportsPlatform => true;
@override
bool get canListAnything => _customDeviceWorkflow.canListDevices;
CustomDevicesConfig get _customDevicesConfig => _config;
List<CustomDevice> get enabledCustomDevices {
return _customDevicesConfig.devices
.where((CustomDeviceConfig element) => !element.disabled)
.map(
(CustomDeviceConfig config) => CustomDevice(
config: config,
logger: _logger,
processManager: _processManager
)
).toList();
}
@override
Future<List<Device>> pollingGetDevices({Duration timeout}) async {
if (!canListAnything) {
return const <Device>[];
}
final List<CustomDevice> devices = enabledCustomDevices;
// maps any custom device to whether its reachable or not.
final Map<CustomDevice, bool> pingedDevices = Map<CustomDevice, bool>.fromIterables(
devices,
await Future.wait(devices.map((CustomDevice e) => e._tryPing(timeout: timeout)))
);
// remove all the devices we couldn't reach.
pingedDevices.removeWhere((_, bool value) => value == false);
// return only the devices.
return pingedDevices.keys.toList();
}
@override
Future<List<String>> getDiagnostics() async => const <String>[];
}

View File

@@ -0,0 +1,153 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:meta/meta.dart';
/// A single configured custom device.
///
/// In the custom devices config file on disk, there may be multiple custom
/// devices configured.
@immutable
class CustomDeviceConfig {
const CustomDeviceConfig({
required this.id,
required this.label,
required this.sdkNameAndVersion,
required this.disabled,
required this.pingCommand,
this.pingSuccessRegex,
required this.postBuildCommand,
required this.installCommand,
required this.uninstallCommand,
required this.runDebugCommand,
this.forwardPortCommand,
this.forwardPortSuccessRegex
}) : assert(forwardPortCommand == null || forwardPortSuccessRegex != null);
factory CustomDeviceConfig.fromJson(dynamic json) {
final Map<String, Object> typedMap = (json as Map<dynamic, dynamic>).cast<String, Object>();
return CustomDeviceConfig(
id: typedMap[_kId]! as String,
label: typedMap[_kLabel]! as String,
sdkNameAndVersion: typedMap[_kSdkNameAndVersion]! as String,
disabled: typedMap[_kDisabled]! as bool,
pingCommand: _castStringList(typedMap[_kPingCommand]!),
pingSuccessRegex: _convertToRegexOrNull(typedMap[_kPingSuccessRegex]),
postBuildCommand: _castStringListOrNull(typedMap[_kPostBuildCommand]),
installCommand: _castStringList(typedMap[_kInstallCommand]!),
uninstallCommand: _castStringList(typedMap[_kUninstallCommand]!),
runDebugCommand: _castStringList(typedMap[_kRunDebugCommand]!),
forwardPortCommand: _castStringListOrNull(typedMap[_kForwardPortCommand]),
forwardPortSuccessRegex: _convertToRegexOrNull(typedMap[_kForwardPortSuccessRegex])
);
}
static const String _kId = 'id';
static const String _kLabel = 'label';
static const String _kSdkNameAndVersion = 'sdkNameAndVersion';
static const String _kDisabled = 'disabled';
static const String _kPingCommand = 'ping';
static const String _kPingSuccessRegex = 'pingSuccessRegex';
static const String _kPostBuildCommand = 'postBuild';
static const String _kInstallCommand = 'install';
static const String _kUninstallCommand = 'uninstall';
static const String _kRunDebugCommand = 'runDebug';
static const String _kForwardPortCommand = 'forwardPort';
static const String _kForwardPortSuccessRegex = 'forwardPortSuccessRegex';
/// An example device config used for creating the default config file.
static final CustomDeviceConfig example = CustomDeviceConfig(
id: 'test1',
label: 'Test Device',
sdkNameAndVersion: 'Test Device 4 Model B+',
disabled: true,
pingCommand: const <String>['ping', '-w', '500', '-n', '1', 'raspberrypi'],
pingSuccessRegex: RegExp('ms TTL='),
postBuildCommand: null,
installCommand: const <String>['scp', '-r', r'${localPath}', r'pi@raspberrypi:/tmp/${appName}'],
uninstallCommand: const <String>['ssh', 'pi@raspberrypi', r'rm -rf "/tmp/${appName}"'],
runDebugCommand: const <String>['ssh', 'pi@raspberrypi', r'flutter-pi "/tmp/${appName}"'],
forwardPortCommand: const <String>['ssh', '-o', 'ExitOnForwardFailure=yes', '-L', r'127.0.0.1:${hostPort}:127.0.0.1:${devicePort}', 'pi@raspberrypi'],
forwardPortSuccessRegex: RegExp('Linux')
);
final String id;
final String label;
final String sdkNameAndVersion;
final bool disabled;
final List<String> pingCommand;
final RegExp? pingSuccessRegex;
final List<String>? postBuildCommand;
final List<String> installCommand;
final List<String> uninstallCommand;
final List<String> runDebugCommand;
final List<String>? forwardPortCommand;
final RegExp? forwardPortSuccessRegex;
bool get usesPortForwarding => forwardPortCommand != null;
static List<String> _castStringList(Object object) {
return (object as List<dynamic>).cast<String>();
}
static List<String>? _castStringListOrNull(Object? object) {
return object == null ? null : _castStringList(object);
}
static RegExp? _convertToRegexOrNull(Object? object) {
return object == null ? null : RegExp(object as String);
}
dynamic toJson() {
return <String, Object?>{
_kId: id,
_kLabel: label,
_kSdkNameAndVersion: sdkNameAndVersion,
_kDisabled: disabled,
_kPingCommand: pingCommand,
_kPingSuccessRegex: pingSuccessRegex?.pattern,
_kPostBuildCommand: postBuildCommand,
_kInstallCommand: installCommand,
_kUninstallCommand: uninstallCommand,
_kRunDebugCommand: runDebugCommand,
_kForwardPortCommand: forwardPortCommand,
_kForwardPortSuccessRegex: forwardPortSuccessRegex?.pattern
};
}
CustomDeviceConfig copyWith({
String? id,
String? label,
String? sdkNameAndVersion,
bool? disabled,
List<String>? pingCommand,
bool explicitPingSuccessRegex = false,
RegExp? pingSuccessRegex,
bool explicitPostBuildCommand = false,
List<String>? postBuildCommand,
List<String>? installCommand,
List<String>? uninstallCommand,
List<String>? runDebugCommand,
bool explicitForwardPortCommand = false,
List<String>? forwardPortCommand,
bool explicitForwardPortSuccessRegex = false,
RegExp? forwardPortSuccessRegex
}) {
return CustomDeviceConfig(
id: id ?? this.id,
label: label ?? this.label,
sdkNameAndVersion: sdkNameAndVersion ?? this.sdkNameAndVersion,
disabled: disabled ?? this.disabled,
pingCommand: pingCommand ?? this.pingCommand,
pingSuccessRegex: explicitPingSuccessRegex ? pingSuccessRegex : (pingSuccessRegex ?? this.pingSuccessRegex),
postBuildCommand: explicitPostBuildCommand ? postBuildCommand : (postBuildCommand ?? this.postBuildCommand),
installCommand: installCommand ?? this.installCommand,
uninstallCommand: uninstallCommand ?? this.uninstallCommand,
runDebugCommand: runDebugCommand ?? this.runDebugCommand,
forwardPortCommand: explicitForwardPortCommand ? forwardPortCommand : (forwardPortCommand ?? this.forwardPortCommand),
forwardPortSuccessRegex: explicitForwardPortSuccessRegex ? forwardPortSuccessRegex : (forwardPortSuccessRegex ?? this.forwardPortSuccessRegex)
);
}
}

View File

@@ -0,0 +1,37 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// 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 '../doctor.dart';
import '../features.dart';
/// The custom-devices-specific implementation of a [Workflow].
///
/// Will apply to the host platform / be able to launch & list devices only if
/// the custom devices feature is enabled in the featureFlags argument.
///
/// Can't list emulators at all.
@immutable
class CustomDeviceWorkflow implements Workflow {
const CustomDeviceWorkflow({
@required FeatureFlags featureFlags
}) : _featureFlags = featureFlags;
final FeatureFlags _featureFlags;
@override
bool get appliesToHostPlatform => _featureFlags.areCustomDevicesEnabled;
@override
bool get canLaunchDevices => _featureFlags.areCustomDevicesEnabled;
@override
bool get canListDevices => _featureFlags.areCustomDevicesEnabled;
@override
bool get canListEmulators => false;
}

View File

@@ -0,0 +1,113 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// 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 '../base/config.dart';
import '../base/file_system.dart';
import '../base/logger.dart';
import '../base/platform.dart';
import '../cache.dart';
import 'custom_device_config.dart';
/// Represents the custom devices config file on disk which in turn
/// contains a list of individual custom device configs.
class CustomDevicesConfig {
/// Load a [CustomDevicesConfig] from a (possibly non-existent) location on disk.
///
/// The config is loaded on construction. Any error while loading will be logged
/// but will not result in an exception being thrown. The file will not be deleted
/// when it's not valid JSON (which other configurations do) and will not
/// be implicitly created when it doesn't exist.
CustomDevicesConfig({
@required FileSystem fileSystem,
@required Logger logger,
@required Platform platform
}) : _fileSystem = fileSystem,
_config = Config(
_kCustomDevicesConfigName,
fileSystem: fileSystem,
logger: logger,
platform: platform,
deleteFileOnFormatException: false
)
{
ensureFileExists();
}
@visibleForTesting
CustomDevicesConfig.test({
@required FileSystem fileSystem,
Directory directory,
@required Logger logger
}) : _fileSystem = fileSystem,
_config = Config.test(
name: _kCustomDevicesConfigName,
directory: directory,
logger: logger,
deleteFileOnFormatException: false
)
{
ensureFileExists();
}
static const String _kCustomDevicesConfigName = 'custom_devices.json';
static const String _kCustomDevicesConfigKey = 'custom-devices';
static const String _kSchema = r'$schema';
static const String _kCustomDevices = 'custom-devices';
final FileSystem _fileSystem;
final Config _config;
String get _defaultSchema {
final Uri uri = _fileSystem
.directory(Cache.flutterRoot)
.childDirectory('packages')
.childDirectory('flutter_tools')
.childDirectory('static')
.childFile('custom-devices.schema.json')
.uri;
// otherwise it won't contain the Uri schema, so the file:// at the start
// will be missing
assert(uri.isAbsolute);
return uri.toString();
}
/// Ensure the config file exists on disk by creating one with default values
/// if it doesn't exist yet.
///
/// The config file should always be present so we can give the user a path
/// to a file they can edit.
void ensureFileExists() {
if (!_fileSystem.file(_config.configPath).existsSync()) {
_config.setValue(_kSchema, _defaultSchema);
_config.setValue(_kCustomDevices, <dynamic>[CustomDeviceConfig.example.toJson()]);
}
}
/// Get the list of [CustomDeviceConfig]s that are listed in the config file
/// including disabled ones.
///
/// Returns an empty list when the config could not be loaded.
List<CustomDeviceConfig> get devices {
final dynamic json = _config.getValue(_kCustomDevicesConfigKey);
if (json == null) {
return <CustomDeviceConfig>[];
}
final List<dynamic> typedList = json as List<dynamic>;
return typedList.map((dynamic e) => CustomDeviceConfig.fromJson(e)).toList();
}
// We don't have a setter for devices here because we don't need it and
// it also may overwrite any things done by the user that aren't explicitly
// tracked by the JSON-representation. For example comments (not possible right now,
// but they'd be useful so maybe in the future) or formatting.
}

View File

@@ -52,6 +52,7 @@ class PlatformType {
static const PlatformType macos = PlatformType._('macos');
static const PlatformType windows = PlatformType._('windows');
static const PlatformType fuchsia = PlatformType._('fuchsia');
static const PlatformType custom = PlatformType._('custom');
final String value;

View File

@@ -48,6 +48,9 @@ abstract class FeatureFlags {
/// Whether fuchsia is enabled.
bool get isFuchsiaEnabled => true;
/// Whether custom devices are enabled.
bool get areCustomDevicesEnabled => false;
/// Whether fast single widget reloads are enabled.
bool get isSingleWidgetReloadEnabled => false;
@@ -97,6 +100,9 @@ class FlutterFeatureFlags implements FeatureFlags {
@override
bool get isFuchsiaEnabled => isEnabled(flutterFuchsiaFeature);
@override
bool get areCustomDevicesEnabled => isEnabled(flutterCustomDevicesFeature);
@override
bool get isSingleWidgetReloadEnabled => isEnabled(singleWidgetReload);
@@ -140,6 +146,7 @@ const List<Feature> allFeatures = <Feature>[
flutterAndroidFeature,
flutterIOSFeature,
flutterFuchsiaFeature,
flutterCustomDevicesFeature,
experimentalInvalidationStrategy,
];
@@ -297,6 +304,28 @@ const Feature flutterFuchsiaFeature = Feature(
),
);
const Feature flutterCustomDevicesFeature = Feature(
name: 'Early support for custom device types',
configSetting: 'enable-custom-devices',
environmentOverride: 'FLUTTER_CUSTOM_DEVICES',
master: FeatureChannelSetting(
available: true,
enabledByDefault: false,
),
dev: FeatureChannelSetting(
available: true,
enabledByDefault: false,
),
beta: FeatureChannelSetting(
available: false,
enabledByDefault: false,
),
stable: FeatureChannelSetting(
available: false,
enabledByDefault: false,
)
);
/// The fast hot reload feature for https://github.com/flutter/flutter/issues/61407.
const Feature singleWidgetReload = Feature(
name: 'Hot reload optimization for changes to class body of a single widget',
@@ -371,7 +400,7 @@ class Feature {
this.master = const FeatureChannelSetting(),
this.dev = const FeatureChannelSetting(),
this.beta = const FeatureChannelSetting(),
this.stable = const FeatureChannelSetting(),
this.stable = const FeatureChannelSetting()
});
/// The user visible name for this feature.

View File

@@ -18,6 +18,8 @@ import 'base/os.dart';
import 'base/platform.dart';
import 'base/terminal.dart';
import 'base/user_messages.dart' hide userMessages;
import 'custom_devices/custom_device.dart';
import 'custom_devices/custom_devices_config.dart';
import 'device.dart';
import 'features.dart';
import 'fuchsia/fuchsia_device.dart';
@@ -58,6 +60,7 @@ class FlutterDeviceManager extends DeviceManager {
@required OperatingSystemUtils operatingSystemUtils,
@required WindowsWorkflow windowsWorkflow,
@required Terminal terminal,
@required CustomDevicesConfig customDevicesConfig,
}) : deviceDiscoverers = <DeviceDiscovery>[
AndroidDevices(
logger: logger,
@@ -123,6 +126,12 @@ class FlutterDeviceManager extends DeviceManager {
processManager: processManager,
logger: logger,
),
CustomDevices(
featureFlags: featureFlags,
processManager: processManager,
logger: logger,
config: customDevicesConfig
),
], super(
logger: logger,
terminal: terminal,

View File

@@ -149,8 +149,7 @@ class ProtocolDiscovery {
hostUri = deviceUri.replace(port: actualHostPort);
}
assert(InternetAddress(hostUri.host).isLoopback);
if (ipv6) {
if (InternetAddress(hostUri.host).isLoopback && ipv6) {
hostUri = hostUri.replace(host: InternetAddress.loopbackIPv6.host);
}
return hostUri;