[fuchsia] - migrate devicer discovery workflows to use ffx (#76241)

This commit is contained in:
omerlevran46
2021-03-01 13:16:13 -08:00
committed by GitHub
parent 54455c706d
commit f996033fcc
12 changed files with 483 additions and 54 deletions

View File

@@ -181,8 +181,11 @@ class FuchsiaDevices extends PollingDeviceDiscovery {
if (!_fuchsiaWorkflow.canListDevices) {
return <Device>[];
}
final List<String> text = (await _fuchsiaSdk.listDevices(timeout: timeout))
?.split('\n');
// TODO(omerlevran): Remove once soft transition is complete fxb/67602.
final List<String> text = (await _fuchsiaSdk.listDevices(
timeout: timeout,
useDeviceFinder: _fuchsiaWorkflow.shouldUseDeviceFinder,
))?.split('\n');
if (text == null || text.isEmpty) {
return <Device>[];
}
@@ -208,9 +211,18 @@ class FuchsiaDevices extends PollingDeviceDiscovery {
return null;
}
final String name = words[1];
final String resolvedHost = await _fuchsiaSdk.fuchsiaDevFinder.resolve(
name,
);
String resolvedHost;
// TODO(omerlevran): Remove once soft transition is complete fxb/67602.
if (_fuchsiaWorkflow.shouldUseDeviceFinder) {
// TODO(omerlevran): Add support for resolve on the FuchsiaSdk Object.
resolvedHost = await _fuchsiaSdk.fuchsiaDevFinder.resolve(
name,
);
} else {
// TODO(omerlevran): Add support for resolve on the FuchsiaSdk Object.
resolvedHost = await _fuchsiaSdk.fuchsiaFfx.resolve(name);
}
if (resolvedHost == null) {
_logger.printError('Failed to resolve host for Fuchsia device `$name`');
return null;

View File

@@ -0,0 +1,101 @@
// 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 'package:process/process.dart';
import '../base/common.dart';
import '../base/logger.dart';
import '../base/process.dart';
import 'fuchsia_sdk.dart';
// Usage: ffx [-c <config>] [-e <env>] [-t <target>] [-T <timeout>] [-v] [<command>] [<args>]
// Fuchsia's developer tool
// Options:
// -c, --config override default configuration
// -e, --env override default environment settings
// -t, --target apply operations across single or multiple targets
// -T, --timeout override default proxy timeout
// -v, --verbose use verbose output
// --help display usage information
// Commands:
// config View and switch default and user configurations
// daemon Interact with/control the ffx daemon
// target Interact with a target device or emulator
/// A simple wrapper for the Fuchsia SDK's 'ffx' tool.
class FuchsiaFfx {
FuchsiaFfx({
@required FuchsiaArtifacts fuchsiaArtifacts,
@required Logger logger,
@required ProcessManager processManager,
}) : _fuchsiaArtifacts = fuchsiaArtifacts,
_logger = logger,
_processUtils =
ProcessUtils(logger: logger, processManager: processManager);
final FuchsiaArtifacts _fuchsiaArtifacts;
final Logger _logger;
final ProcessUtils _processUtils;
/// Returns a list of attached devices as a list of strings with entries
/// formatted as follows:
///
/// abcd::abcd:abc:abcd:abcd%qemu scare-cable-skip-joy
Future<List<String>> list({Duration timeout}) async {
if (_fuchsiaArtifacts.ffx == null || !_fuchsiaArtifacts.ffx.existsSync()) {
throwToolExit('Fuchsia ffx tool not found.');
}
final List<String> command = <String>[
_fuchsiaArtifacts.ffx.path,
if (timeout != null)
...<String>['-T', '${timeout.inSeconds}'],
'target',
'list',
'--format',
's'
];
final RunResult result = await _processUtils.run(command);
if (result.exitCode != 0) {
_logger.printError('ffx failed: ${result.stderr}');
return null;
}
if (result.stderr.contains('No devices found')) {
return null;
}
return result.stdout.split('\n');
}
/// Returns the address of the named device.
///
/// The string [deviceName] should be the name of the device from the
/// 'list' command, e.g. 'scare-cable-skip-joy'.
Future<String> resolve(String deviceName) async {
if (_fuchsiaArtifacts.ffx == null || !_fuchsiaArtifacts.ffx.existsSync()) {
throwToolExit('Fuchsia ffx tool not found.');
}
final List<String> command = <String>[
_fuchsiaArtifacts.ffx.path,
'target',
'list',
'--format',
'a',
deviceName,
];
final RunResult result = await _processUtils.run(command);
if (result.exitCode != 0) {
_logger.printError('ffx failed: ${result.stderr}');
return null;
}
if (result.stderr.contains('No devices found')) {
return null;
}
return result.stdout.trim();
}
}

View File

@@ -106,7 +106,7 @@ class FuchsiaPM {
///
/// The argument [repoPath] should have previously been an argument to
/// [newrepo]. The [host] should be the host reported by
/// [FuchsiaDevFinder.resolve], and [port] should be an unused port for the
/// [FuchsiaDevFinder.resolve] or [FuchsiaFfx.resolve] and [port] should be an unused port for the
/// http server to bind.
Future<Process> serve(String repoPath, String host, int port) async {
if (globals.fuchsiaArtifacts.pm == null) {

View File

@@ -14,6 +14,7 @@ import '../convert.dart';
import '../globals.dart' as globals;
import 'fuchsia_dev_finder.dart';
import 'fuchsia_ffx.dart';
import 'fuchsia_kernel_compiler.dart';
import 'fuchsia_pm.dart';
@@ -48,18 +49,32 @@ class FuchsiaSdk {
FuchsiaKernelCompiler get fuchsiaKernelCompiler =>
_fuchsiaKernelCompiler ??= FuchsiaKernelCompiler();
/// Interface to the 'ffx' tool.
FuchsiaFfx _fuchsiaFfx;
FuchsiaFfx get fuchsiaFfx => _fuchsiaFfx ??= FuchsiaFfx(
fuchsiaArtifacts: globals.fuchsiaArtifacts,
logger: globals.logger,
processManager: globals.processManager,
);
/// Returns any attached devices is a newline-denominated String.
///
/// Example output:
///
/// $ device-finder list -full
/// > 192.168.42.56 paper-pulp-bush-angel
Future<String> listDevices({ Duration timeout }) async {
if (globals.fuchsiaArtifacts.devFinder == null ||
!globals.fuchsiaArtifacts.devFinder.existsSync()) {
return null;
/// Example output: abcd::abcd:abc:abcd:abcd%qemu scare-cable-skip-joy
Future<String> listDevices({Duration timeout, bool useDeviceFinder = false}) async {
List<String> devices;
if (useDeviceFinder) {
if (globals.fuchsiaArtifacts.devFinder == null ||
!globals.fuchsiaArtifacts.devFinder.existsSync()) {
return null;
}
devices = await fuchsiaDevFinder.list(timeout: timeout);
} else {
if (globals.fuchsiaArtifacts.ffx == null ||
!globals.fuchsiaArtifacts.ffx.existsSync()) {
return null;
}
devices = await fuchsiaFfx.list(timeout: timeout);
}
final List<String> devices = await fuchsiaDevFinder.list(timeout: timeout);
if (devices == null) {
return null;
}
@@ -112,6 +127,7 @@ class FuchsiaArtifacts {
FuchsiaArtifacts({
this.sshConfig,
this.devFinder,
this.ffx,
this.pm,
});
@@ -140,11 +156,13 @@ class FuchsiaArtifacts {
final String fuchsia = globals.cache.getArtifactDirectory('fuchsia').path;
final String tools = globals.fs.path.join(fuchsia, 'tools');
final File devFinder = globals.fs.file(globals.fs.path.join(tools, 'device-finder'));
final File ffx = globals.fs.file(globals.fs.path.join(tools, 'x64/ffx'));
final File pm = globals.fs.file(globals.fs.path.join(tools, 'pm'));
return FuchsiaArtifacts(
sshConfig: sshConfig,
devFinder: devFinder.existsSync() ? devFinder : null,
ffx: ffx.existsSync() ? ffx : null,
pm: pm.existsSync() ? pm : null,
);
}
@@ -160,6 +178,10 @@ class FuchsiaArtifacts {
/// Fuchsia devices.
final File devFinder;
/// The location of the ffx tool used to locate connected
/// Fuchsia devices.
final File ffx;
/// The pm tool.
final File pm;

View File

@@ -35,14 +35,29 @@ class FuchsiaWorkflow implements Workflow {
@override
bool get appliesToHostPlatform => _featureFlags.isFuchsiaEnabled && (_platform.isLinux || _platform.isMacOS);
bool get shouldUseDeviceFinder {
final String useDeviceFinder = _platform.environment.containsKey('FUCHSIA_DISABLED_ffx_discovery')
? _platform.environment['FUCHSIA_DISABLED_ffx_discovery'] : '0';
if (useDeviceFinder == '1') {
return true;
}
return false;
}
@override
bool get canListDevices {
return _fuchsiaArtifacts.devFinder != null;
if (shouldUseDeviceFinder) {
return _fuchsiaArtifacts.devFinder != null;
}
return _fuchsiaArtifacts.ffx != null;
}
@override
bool get canLaunchDevices {
return _fuchsiaArtifacts.devFinder != null && _fuchsiaArtifacts.sshConfig != null;
if (shouldUseDeviceFinder) {
return _fuchsiaArtifacts.devFinder != null && _fuchsiaArtifacts.sshConfig != null;
}
return _fuchsiaArtifacts.ffx != null && _fuchsiaArtifacts.sshConfig != null;
}
@override