Unverified Commit eee154af authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

Replace netls and netaddr with dev_finder (#26090)

parent a67a2f62
...@@ -41,6 +41,7 @@ Future<void> main(List<String> args) async { ...@@ -41,6 +41,7 @@ Future<void> main(List<String> args) async {
final String buildDirectory = argResults['build-dir']; final String buildDirectory = argResults['build-dir'];
final File frontendServer = fs.file('$buildDirectory/host_x64/gen/third_party/flutter/frontend_server/frontend_server_tool.snapshot'); final File frontendServer = fs.file('$buildDirectory/host_x64/gen/third_party/flutter/frontend_server/frontend_server_tool.snapshot');
final File sshConfig = fs.file('$buildDirectory/ssh-keys/ssh_config'); final File sshConfig = fs.file('$buildDirectory/ssh-keys/ssh_config');
final File devFinder = fs.file('$buildDirectory/host_x64/dev_finder');
final File platformKernelDill = fs.file('$buildDirectory/flutter_runner_patched_sdk/platform_strong.dill'); final File platformKernelDill = fs.file('$buildDirectory/flutter_runner_patched_sdk/platform_strong.dill');
final File flutterPatchedSdk = fs.file('$buildDirectory/flutter_runner_patched_sdk'); final File flutterPatchedSdk = fs.file('$buildDirectory/flutter_runner_patched_sdk');
final String packages = '$buildDirectory/dartlang/gen/$path/${name}_dart_library.packages'; final String packages = '$buildDirectory/dartlang/gen/$path/${name}_dart_library.packages';
...@@ -91,7 +92,7 @@ Future<void> main(List<String> args) async { ...@@ -91,7 +92,7 @@ Future<void> main(List<String> args) async {
muteCommandLogging: false, muteCommandLogging: false,
verboseHelp: false, verboseHelp: false,
overrides: <Type, Generator>{ overrides: <Type, Generator>{
FuchsiaArtifacts: () => FuchsiaArtifacts(sshConfig: sshConfig), FuchsiaArtifacts: () => FuchsiaArtifacts(sshConfig: sshConfig, devFinder: devFinder),
Artifacts: () => OverrideArtifacts( Artifacts: () => OverrideArtifacts(
parent: CachedArtifacts(), parent: CachedArtifacts(),
frontendServer: frontendServer, frontendServer: frontendServer,
......
...@@ -108,12 +108,8 @@ class FuchsiaDevices extends PollingDeviceDiscovery { ...@@ -108,12 +108,8 @@ class FuchsiaDevices extends PollingDeviceDiscovery {
if (!fuchsiaWorkflow.canListDevices) { if (!fuchsiaWorkflow.canListDevices) {
return <Device>[]; return <Device>[];
} }
final String text = await fuchsiaSdk.netls(); final String text = await fuchsiaSdk.listDevices();
final List<FuchsiaDevice> devices = <FuchsiaDevice>[]; final List<FuchsiaDevice> devices = parseListDevices(text);
for (String name in parseFuchsiaDeviceOutput(text)) {
final String id = await fuchsiaSdk.netaddr();
devices.add(FuchsiaDevice(id, name: name));
}
return devices; return devices;
} }
...@@ -121,24 +117,18 @@ class FuchsiaDevices extends PollingDeviceDiscovery { ...@@ -121,24 +117,18 @@ class FuchsiaDevices extends PollingDeviceDiscovery {
Future<List<String>> getDiagnostics() async => const <String>[]; Future<List<String>> getDiagnostics() async => const <String>[];
} }
/// Parses output from the netls tool into fuchsia devices names.
///
/// Example output:
/// $ ./netls
/// > device liliac-shore-only-last (fe80::82e4:da4d:fe81:227d/3)
@visibleForTesting @visibleForTesting
List<String> parseFuchsiaDeviceOutput(String text) { List<FuchsiaDevice> parseListDevices(String text) {
final List<String> names = <String>[]; final List<FuchsiaDevice> devices = <FuchsiaDevice>[];
for (String rawLine in text.trim().split('\n')) { for (String rawLine in text.trim().split('\n')) {
final String line = rawLine.trim(); final String line = rawLine.trim();
if (!line.startsWith('device')) // ['ip', 'device name']
continue;
// ['device', 'device name', '(id)']
final List<String> words = line.split(' '); final List<String> words = line.split(' ');
final String name = words[1]; final String name = words[1];
names.add(name); final String id = words[0];
devices.add(FuchsiaDevice(id, name: name));
} }
return names; return devices;
} }
class FuchsiaDevice extends Device { class FuchsiaDevice extends Device {
......
...@@ -24,21 +24,15 @@ FuchsiaArtifacts get fuchsiaArtifacts => context[FuchsiaArtifacts]; ...@@ -24,21 +24,15 @@ FuchsiaArtifacts get fuchsiaArtifacts => context[FuchsiaArtifacts];
/// This workflow assumes development within the fuchsia source tree, /// This workflow assumes development within the fuchsia source tree,
/// including a working fx command-line tool in the user's PATH. /// including a working fx command-line tool in the user's PATH.
class FuchsiaSdk { class FuchsiaSdk {
static const List<String> _netaddrCommand = <String>['fx', 'netaddr', '--fuchsia', '--nowait'];
static const List<String> _netlsCommand = <String>['fx', 'netls', '--nowait'];
static const List<String> _syslogCommand = <String>['fx', 'syslog', '--clock', 'Local']; static const List<String> _syslogCommand = <String>['fx', 'syslog', '--clock', 'Local'];
/// Invokes the `netaddr` command.
///
/// This returns the network address of an attached fuchsia device. Does
/// not currently support multiple attached devices.
///
/// Example output: /// Example output:
/// $ fx netaddr --fuchsia --nowait /// $ dev_finder list -full
/// > fe80::9aaa:fcff:fe60:d3af%eth1 /// > 192.168.42.56 paper-pulp-bush-angel
Future<String> netaddr() async { Future<String> listDevices() async {
try { try {
final RunResult process = await runAsync(_netaddrCommand); final String path = fuchsiaArtifacts.devFinder.absolute.path;
final RunResult process = await runAsync(<String>[path, 'list', '-full']);
return process.stdout.trim(); return process.stdout.trim();
} on ArgumentError catch (exception) { } on ArgumentError catch (exception) {
throwToolExit('$exception'); throwToolExit('$exception');
...@@ -69,36 +63,19 @@ class FuchsiaSdk { ...@@ -69,36 +63,19 @@ class FuchsiaSdk {
} }
return null; return null;
} }
/// Invokes the `netls` command.
///
/// This lists attached fuchsia devices with their name and address. Does
/// not currently support multiple attached devices.
///
/// Example output:
/// $ fx netls --nowait
/// > device liliac-shore-only-last (fe80::82e4:da4d:fe81:227d/3)
Future<String> netls() async {
try {
final RunResult process = await runAsync(_netlsCommand);
return process.stdout;
} on ArgumentError catch (exception) {
throwToolExit('$exception');
}
return null;
}
} }
/// Fuchsia-specific artifacts used to interact with a device. /// Fuchsia-specific artifacts used to interact with a device.
class FuchsiaArtifacts { class FuchsiaArtifacts {
/// Creates a new [FuchsiaArtifacts]. /// Creates a new [FuchsiaArtifacts].
/// ///
/// May optionally provide a file `sshConfig` file. /// May optionally provide a file `sshConfig` file and `devFinder` file.
FuchsiaArtifacts({File sshConfig}) FuchsiaArtifacts({File sshConfig, File devFinder})
: _sshConfig = sshConfig; : _sshConfig = sshConfig,
_devFinder = devFinder;
/// The location of the SSH configuration file used to interact with a /// The location of the SSH configuration file used to interact with a
/// fuchsia device. /// Fuchsia device.
/// ///
/// Requires the env variable `BUILD_DIR` to be set if not provided by /// Requires the env variable `BUILD_DIR` to be set if not provided by
/// the constructor. /// the constructor.
...@@ -114,4 +91,22 @@ class FuchsiaArtifacts { ...@@ -114,4 +91,22 @@ class FuchsiaArtifacts {
return _sshConfig; return _sshConfig;
} }
File _sshConfig; File _sshConfig;
/// The location of the dev finder tool used to locate connected
/// Fuchsia devices.
///
/// Requires the env variable `BUILD_DIR` to be set if not provided by
/// the constructor.
File get devFinder {
if (_devFinder == null) {
final String buildDirectory = platform.environment['BUILD_DIR'];
if (buildDirectory == null) {
throwToolExit('BUILD_DIR must be supplied to dev_finder. For example:\n'
' export BUILD_DIR=path/to/fuchsia/out/x64\n');
}
_devFinder = fs.file('$buildDirectory/host_x64/dev_finder');
}
return _devFinder;
}
File _devFinder;
} }
...@@ -29,12 +29,13 @@ void main() { ...@@ -29,12 +29,13 @@ void main() {
expect(device.name, name); expect(device.name, name);
}); });
test('parse netls log output', () { test('parse dev_finder output', () {
const String example = 'device lilia-shore-only-last (fe80::0000:a00a:f00f:2002/3)'; const String example = '192.168.42.56 paper-pulp-bush-angel';
final List<String> names = parseFuchsiaDeviceOutput(example); final List<FuchsiaDevice> names = parseListDevices(example);
expect(names.length, 1); expect(names.length, 1);
expect(names.first, 'lilia-shore-only-last'); expect(names.first.name, 'paper-pulp-bush-angel');
expect(names.first.id, '192.168.42.56');
}); });
test('default capabilities', () async { test('default capabilities', () async {
......
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