Skip to content

Commit 32041c0

Browse files
Reland: Switch to dev_finder (flutter#26250)
1 parent 6f1d10b commit 32041c0

File tree

6 files changed

+51
-104
lines changed

6 files changed

+51
-104
lines changed

packages/flutter_tools/bin/fuchsia_attach.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Future<void> main(List<String> args) async {
4141
final String buildDirectory = argResults['build-dir'];
4242
final File frontendServer = fs.file('$buildDirectory/host_x64/gen/third_party/flutter/frontend_server/frontend_server_tool.snapshot');
4343
final File sshConfig = fs.file('$buildDirectory/ssh-keys/ssh_config');
44+
final File devFinder = fs.file('$buildDirectory/host_x64/dev_finder');
4445
final File platformKernelDill = fs.file('$buildDirectory/flutter_runner_patched_sdk/platform_strong.dill');
4546
final File flutterPatchedSdk = fs.file('$buildDirectory/flutter_runner_patched_sdk');
4647
final String packages = '$buildDirectory/dartlang/gen/$path/${name}_dart_library.packages';
@@ -91,7 +92,7 @@ Future<void> main(List<String> args) async {
9192
muteCommandLogging: false,
9293
verboseHelp: false,
9394
overrides: <Type, Generator>{
94-
FuchsiaArtifacts: () => FuchsiaArtifacts(sshConfig: sshConfig),
95+
FuchsiaArtifacts: () => FuchsiaArtifacts(sshConfig: sshConfig, devFinder: devFinder),
9596
Artifacts: () => OverrideArtifacts(
9697
parent: CachedArtifacts(),
9798
frontendServer: frontendServer,

packages/flutter_tools/lib/src/fuchsia/fuchsia_device.dart

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -108,37 +108,27 @@ class FuchsiaDevices extends PollingDeviceDiscovery {
108108
if (!fuchsiaWorkflow.canListDevices) {
109109
return <Device>[];
110110
}
111-
final String text = await fuchsiaSdk.netls();
112-
final List<FuchsiaDevice> devices = <FuchsiaDevice>[];
113-
for (String name in parseFuchsiaDeviceOutput(text)) {
114-
final String id = await fuchsiaSdk.netaddr();
115-
devices.add(FuchsiaDevice(id, name: name));
116-
}
111+
final String text = await fuchsiaSdk.listDevices();
112+
final List<FuchsiaDevice> devices = parseListDevices(text);
117113
return devices;
118114
}
119115

120116
@override
121117
Future<List<String>> getDiagnostics() async => const <String>[];
122118
}
123119

124-
/// Parses output from the netls tool into fuchsia devices names.
125-
///
126-
/// Example output:
127-
/// $ ./netls
128-
/// > device liliac-shore-only-last (fe80::82e4:da4d:fe81:227d/3)
129120
@visibleForTesting
130-
List<String> parseFuchsiaDeviceOutput(String text) {
131-
final List<String> names = <String>[];
121+
List<FuchsiaDevice> parseListDevices(String text) {
122+
final List<FuchsiaDevice> devices = <FuchsiaDevice>[];
132123
for (String rawLine in text.trim().split('\n')) {
133124
final String line = rawLine.trim();
134-
if (!line.startsWith('device'))
135-
continue;
136-
// ['device', 'device name', '(id)']
125+
// ['ip', 'device name']
137126
final List<String> words = line.split(' ');
138127
final String name = words[1];
139-
names.add(name);
128+
final String id = words[0];
129+
devices.add(FuchsiaDevice(id, name: name));
140130
}
141-
return names;
131+
return devices;
142132
}
143133

144134
class FuchsiaDevice extends Device {

packages/flutter_tools/lib/src/fuchsia/fuchsia_sdk.dart

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import '../base/common.dart';
99
import '../base/context.dart';
1010
import '../base/file_system.dart';
1111
import '../base/io.dart';
12-
import '../base/platform.dart';
1312
import '../base/process.dart';
1413
import '../base/process_manager.dart';
1514

@@ -24,21 +23,15 @@ FuchsiaArtifacts get fuchsiaArtifacts => context[FuchsiaArtifacts];
2423
/// This workflow assumes development within the fuchsia source tree,
2524
/// including a working fx command-line tool in the user's PATH.
2625
class FuchsiaSdk {
27-
static const List<String> _netaddrCommand = <String>['fx', 'netaddr', '--fuchsia', '--nowait'];
28-
static const List<String> _netlsCommand = <String>['fx', 'netls', '--nowait'];
2926
static const List<String> _syslogCommand = <String>['fx', 'syslog', '--clock', 'Local'];
3027

31-
/// Invokes the `netaddr` command.
32-
///
33-
/// This returns the network address of an attached fuchsia device. Does
34-
/// not currently support multiple attached devices.
35-
///
3628
/// Example output:
37-
/// $ fx netaddr --fuchsia --nowait
38-
/// > fe80::9aaa:fcff:fe60:d3af%eth1
39-
Future<String> netaddr() async {
29+
/// $ dev_finder list -full
30+
/// > 192.168.42.56 paper-pulp-bush-angel
31+
Future<String> listDevices() async {
4032
try {
41-
final RunResult process = await runAsync(_netaddrCommand);
33+
final String path = fuchsiaArtifacts.devFinder.absolute.path;
34+
final RunResult process = await runAsync(<String>[path, 'list', '-full']);
4235
return process.stdout.trim();
4336
} on ArgumentError catch (exception) {
4437
throwToolExit('$exception');
@@ -69,49 +62,18 @@ class FuchsiaSdk {
6962
}
7063
return null;
7164
}
72-
73-
/// Invokes the `netls` command.
74-
///
75-
/// This lists attached fuchsia devices with their name and address. Does
76-
/// not currently support multiple attached devices.
77-
///
78-
/// Example output:
79-
/// $ fx netls --nowait
80-
/// > device liliac-shore-only-last (fe80::82e4:da4d:fe81:227d/3)
81-
Future<String> netls() async {
82-
try {
83-
final RunResult process = await runAsync(_netlsCommand);
84-
return process.stdout;
85-
} on ArgumentError catch (exception) {
86-
throwToolExit('$exception');
87-
}
88-
return null;
89-
}
9065
}
9166

9267
/// Fuchsia-specific artifacts used to interact with a device.
9368
class FuchsiaArtifacts {
9469
/// Creates a new [FuchsiaArtifacts].
95-
///
96-
/// May optionally provide a file `sshConfig` file.
97-
FuchsiaArtifacts({File sshConfig})
98-
: _sshConfig = sshConfig;
70+
FuchsiaArtifacts({this.sshConfig, this.devFinder});
9971

10072
/// The location of the SSH configuration file used to interact with a
101-
/// fuchsia device.
102-
///
103-
/// Requires the env variable `BUILD_DIR` to be set if not provided by
104-
/// the constructor.
105-
File get sshConfig {
106-
if (_sshConfig == null) {
107-
final String buildDirectory = platform.environment['BUILD_DIR'];
108-
if (buildDirectory == null) {
109-
throwToolExit('BUILD_DIR must be supplied to locate SSH keys. For example:\n'
110-
' export BUILD_DIR=path/to/fuchsia/out/x64\n');
111-
}
112-
_sshConfig = fs.file('$buildDirectory/ssh-keys/ssh_config');
113-
}
114-
return _sshConfig;
115-
}
116-
File _sshConfig;
73+
/// Fuchsia device.
74+
final File sshConfig;
75+
76+
/// The location of the dev finder tool used to locate connected
77+
/// Fuchsia devices.
78+
final File devFinder;
11779
}

packages/flutter_tools/lib/src/fuchsia/fuchsia_workflow.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// found in the LICENSE file.
44

55
import '../base/context.dart';
6-
import '../base/os.dart';
76
import '../base/platform.dart';
87
import '../doctor.dart';
8+
import 'fuchsia_sdk.dart';
99

1010
/// The [FuchsiaWorkflow] instance.
1111
FuchsiaWorkflow get fuchsiaWorkflow => context[FuchsiaWorkflow];
@@ -21,12 +21,12 @@ class FuchsiaWorkflow implements Workflow {
2121

2222
@override
2323
bool get canListDevices {
24-
return os.which('fx') != null;
24+
return fuchsiaArtifacts.devFinder != null;
2525
}
2626

2727
@override
2828
bool get canLaunchDevices {
29-
return os.which('fx') != null;
29+
return fuchsiaArtifacts.devFinder != null && fuchsiaArtifacts.sshConfig != null;
3030
}
3131

3232
@override

packages/flutter_tools/test/fuchsia/fuchsa_device_test.dart

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ void main() {
2929
expect(device.name, name);
3030
});
3131

32-
test('parse netls log output', () {
33-
const String example = 'device lilia-shore-only-last (fe80::0000:a00a:f00f:2002/3)';
34-
final List<String> names = parseFuchsiaDeviceOutput(example);
32+
test('parse dev_finder output', () {
33+
const String example = '192.168.42.56 paper-pulp-bush-angel';
34+
final List<FuchsiaDevice> names = parseListDevices(example);
3535

3636
expect(names.length, 1);
37-
expect(names.first, 'lilia-shore-only-last');
37+
expect(names.first.name, 'paper-pulp-bush-angel');
38+
expect(names.first.id, '192.168.42.56');
3839
});
3940

4041
test('default capabilities', () async {
@@ -50,7 +51,6 @@ void main() {
5051
group('displays friendly error when', () {
5152
final MockProcessManager mockProcessManager = MockProcessManager();
5253
final MockProcessResult mockProcessResult = MockProcessResult();
53-
final MockFuchsiaArtifacts mockFuchsiaArtifacts = MockFuchsiaArtifacts();
5454
final MockFile mockFile = MockFile();
5555
when(mockProcessManager.run(
5656
any,
@@ -60,23 +60,9 @@ void main() {
6060
when(mockProcessResult.exitCode).thenReturn(1);
6161
when<String>(mockProcessResult.stdout).thenReturn('');
6262
when<String>(mockProcessResult.stderr).thenReturn('');
63-
when(mockFuchsiaArtifacts.sshConfig).thenReturn(mockFile);
6463
when(mockFile.absolute).thenReturn(mockFile);
6564
when(mockFile.path).thenReturn('');
6665

67-
testUsingContext('No BUILD_DIR set', () async {
68-
final FuchsiaDevice device = FuchsiaDevice('id');
69-
ToolExit toolExit;
70-
try {
71-
await device.servicePorts();
72-
} on ToolExit catch (err) {
73-
toolExit = err;
74-
}
75-
expect(toolExit.message, contains('BUILD_DIR must be supplied to locate SSH keys'));
76-
}, overrides: <Type, Generator>{
77-
ProcessManager: () => mockProcessManager,
78-
});
79-
8066
final MockProcessManager emptyStdoutProcessManager = MockProcessManager();
8167
final MockProcessResult emptyStdoutProcessResult = MockProcessResult();
8268
when(emptyStdoutProcessManager.run(
@@ -99,7 +85,10 @@ void main() {
9985
expect(toolExit.message, contains('No Dart Observatories found. Are you running a debug build?'));
10086
}, overrides: <Type, Generator>{
10187
ProcessManager: () => emptyStdoutProcessManager,
102-
FuchsiaArtifacts: () => mockFuchsiaArtifacts,
88+
FuchsiaArtifacts: () => FuchsiaArtifacts(
89+
sshConfig: mockFile,
90+
devFinder: mockFile,
91+
),
10392
});
10493

10594
group('device logs', () {
@@ -211,8 +200,6 @@ void main() {
211200
});
212201
}
213202

214-
class MockFuchsiaArtifacts extends Mock implements FuchsiaArtifacts {}
215-
216203
class MockProcessManager extends Mock implements ProcessManager {}
217204

218205
class MockProcessResult extends Mock implements ProcessResult {}

packages/flutter_tools/test/fuchsia/fuchsia_workflow_test.dart

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:flutter_tools/src/base/file_system.dart';
66
import 'package:flutter_tools/src/base/os.dart';
7+
import 'package:flutter_tools/src/fuchsia/fuchsia_sdk.dart';
78
import 'package:flutter_tools/src/fuchsia/fuchsia_workflow.dart';
89
import 'package:mockito/mockito.dart';
910

@@ -14,28 +15,34 @@ class MockOperatingSystemUtils extends Mock implements OperatingSystemUtils {}
1415
class MockFile extends Mock implements File {}
1516

1617
void main() {
17-
bool fxPresent = false;
18-
final MockOperatingSystemUtils utils = MockOperatingSystemUtils();
19-
final MockFile file = MockFile();
20-
when(utils.which('fx')).thenAnswer((Invocation _) => fxPresent ? file : null);
21-
2218
group('android workflow', () {
23-
testUsingContext('can not list and launch devices if there is no `fx` command', () {
24-
fxPresent = false;
19+
final MockFile devFinder = MockFile();
20+
final MockFile sshConfig = MockFile();
21+
when(devFinder.absolute).thenReturn(devFinder);
22+
when(sshConfig.absolute).thenReturn(sshConfig);
23+
24+
testUsingContext('can not list and launch devices if there is not ssh config and dev finder', () {
2525
expect(fuchsiaWorkflow.canLaunchDevices, false);
2626
expect(fuchsiaWorkflow.canListDevices, false);
2727
expect(fuchsiaWorkflow.canListEmulators, false);
2828
}, overrides: <Type, Generator>{
29-
OperatingSystemUtils: () => utils,
29+
FuchsiaArtifacts: () => FuchsiaArtifacts(devFinder: null, sshConfig: null),
30+
});
31+
32+
testUsingContext('can not list and launch devices if there is not ssh config and dev finder', () {
33+
expect(fuchsiaWorkflow.canLaunchDevices, false);
34+
expect(fuchsiaWorkflow.canListDevices, true);
35+
expect(fuchsiaWorkflow.canListEmulators, false);
36+
}, overrides: <Type, Generator>{
37+
FuchsiaArtifacts: () => FuchsiaArtifacts(devFinder: devFinder, sshConfig: null),
3038
});
3139

3240
testUsingContext('can list and launch devices supported if there is a `fx` command', () {
33-
fxPresent = true;
3441
expect(fuchsiaWorkflow.canLaunchDevices, true);
3542
expect(fuchsiaWorkflow.canListDevices, true);
3643
expect(fuchsiaWorkflow.canListEmulators, false);
3744
}, overrides: <Type, Generator>{
38-
OperatingSystemUtils: () => utils,
45+
FuchsiaArtifacts: () => FuchsiaArtifacts(devFinder: devFinder, sshConfig: sshConfig),
3946
});
4047
});
4148
}

0 commit comments

Comments
 (0)