Change flutter tool to not require Fuchsia build args (#22382)

* Change flutter tool to not require Fuchsia build args

* Restore code that was commented out.

* Fix style nits.
This commit is contained in:
shrike69 2018-09-28 15:13:47 -07:00 committed by GitHub
parent 54c10f44b2
commit c496751ae8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -23,10 +23,20 @@ import '../runner/flutter_command.dart';
import '../vmservice.dart'; import '../vmservice.dart';
// Usage: // Usage:
// With e.g. flutter_gallery already running, a HotRunner can be attached to it // With e.g. hello_mod already running, a HotRunner can be attached to it.
// with: //
// $ flutter fuchsia_reload -f ~/fuchsia -a 192.168.1.39 \ // From a Fuchsia in-tree build:
// -g //lib/flutter/examples/flutter_gallery:flutter_gallery // $ flutter fuchsia_reload --address 192.168.1.39 \
// --build-dir ~/fuchsia/out/x64 \
// --gn-target //topaz/examples/ui/hello_mod:hello_mod
//
// From out of tree:
// $ flutter fuchsia_reload --address 192.168.1.39 \
// --mod_name hello_mod \
// --path /path/to/hello_mod
// --dot-packages /path/to/hello_mod_out/app.packages \
// --ssh-config /path/to/ssh_config \
// --target /path/to/hello_mod/lib/main.dart
final String ipv4Loopback = InternetAddress.loopbackIPv4.address; final String ipv4Loopback = InternetAddress.loopbackIPv4.address;
@ -40,6 +50,11 @@ class FuchsiaReloadCommand extends FlutterCommand {
abbr: 'b', abbr: 'b',
defaultsTo: null, defaultsTo: null,
help: 'Fuchsia build directory, e.g. out/release-x86-64.'); help: 'Fuchsia build directory, e.g. out/release-x86-64.');
argParser.addOption('dot-packages',
abbr: 'd',
defaultsTo: null,
help: 'Path to the mod\'s .packages file. Required if no'
'GN target specified.');
argParser.addOption('gn-target', argParser.addOption('gn-target',
abbr: 'g', abbr: 'g',
help: 'GN target of the application, e.g //path/to/app:app.'); help: 'GN target of the application, e.g //path/to/app:app.');
@ -51,14 +66,23 @@ class FuchsiaReloadCommand extends FlutterCommand {
abbr: 'l', abbr: 'l',
defaultsTo: false, defaultsTo: false,
help: 'Lists the running modules. '); help: 'Lists the running modules. ');
argParser.addOption('name-override', argParser.addOption('mod-name',
abbr: 'n', abbr: 'm',
help: 'On-device name of the application binary.'); help: 'Name of the flutter mod. If used with -g, overrides the name '
'inferred from the GN target.');
argParser.addOption('path',
abbr: 'p',
defaultsTo: null,
help: 'Path to the flutter mod project.');
argParser.addOption('ssh-config',
abbr: 's',
defaultsTo: null,
help: 'Path to the Fuchsia target\'s ssh config file.');
argParser.addOption('target', argParser.addOption('target',
abbr: 't', abbr: 't',
defaultsTo: bundle.defaultMainPath, defaultsTo: bundle.defaultMainPath,
help: 'Target app path / main entry-point file. ' help: 'Target app path / main entry-point file. '
'Relative to --gn-target path, e.g. lib/main.dart.'); 'Relative to --path or --gn-target path, e.g. lib/main.dart.');
} }
@override @override
@ -67,15 +91,13 @@ class FuchsiaReloadCommand extends FlutterCommand {
@override @override
final String description = 'Hot reload on Fuchsia.'; final String description = 'Hot reload on Fuchsia.';
String _buildDir; String _modName;
String _projectRoot;
String _projectName;
String _binaryName;
String _isolateNumber; String _isolateNumber;
String _fuchsiaProjectPath; String _fuchsiaProjectPath;
String _target; String _target;
String _address; String _address;
String _dotPackagesPath; String _dotPackagesPath;
String _sshConfig;
bool _list; bool _list;
@ -113,13 +135,13 @@ class FuchsiaReloadCommand extends FlutterCommand {
// Check that there are running VM services on the returned // Check that there are running VM services on the returned
// ports, and find the Isolates that are running the target app. // ports, and find the Isolates that are running the target app.
final String isolateName = '$_binaryName\$main$_isolateNumber'; final String isolateName = '$_modName\$main$_isolateNumber';
final List<int> targetPorts = await _filterPorts( final List<int> targetPorts = await _filterPorts(
servicePorts, isolateName); servicePorts, isolateName);
if (targetPorts.isEmpty) if (targetPorts.isEmpty)
throwToolExit('No VMs found running $_binaryName.'); throwToolExit('No VMs found running $_modName.');
for (int port in targetPorts) for (int port in targetPorts)
printTrace('Found $_binaryName at $port'); printTrace('Found $_modName at $port');
// Set up a device and hot runner and attach the hot runner to the first // Set up a device and hot runner and attach the hot runner to the first
// vm service we found. // vm service we found.
@ -143,7 +165,7 @@ class FuchsiaReloadCommand extends FlutterCommand {
projectRootPath: _fuchsiaProjectPath, projectRootPath: _fuchsiaProjectPath,
packagesFilePath: _dotPackagesPath packagesFilePath: _dotPackagesPath
); );
printStatus('Connecting to $_binaryName'); printStatus('Connecting to $_modName');
await hotRunner.attach(viewFilter: isolateName); await hotRunner.attach(viewFilter: isolateName);
} finally { } finally {
await Future.wait(forwardedPorts.map((_PortForwarder pf) => pf.stop())); await Future.wait(forwardedPorts.map((_PortForwarder pf) => pf.stop()));
@ -274,19 +296,28 @@ class FuchsiaReloadCommand extends FlutterCommand {
} }
Future<Null> _validateArguments() async { Future<Null> _validateArguments() async {
_buildDir = argResults['build-dir']; final String fuchsiaBuildDir = argResults['build-dir'];
if (_buildDir == null) { final String gnTarget = argResults['gn-target'];
final ProcessResult result = await processManager.run(<String>['fx', 'get-build-dir']);
if (result.exitCode == 0) if (fuchsiaBuildDir != null) {
_buildDir = result.stdout.trim(); if (gnTarget == null)
else throwToolExit('Must provide --gn-target when specifying --build-dir.');
printStatus('get-build-dir failed:\nstdout: ${result.stdout}\nstderr: ${result.stderr}');
if (!_directoryExists(fuchsiaBuildDir))
throwToolExit('Specified --build-dir "$fuchsiaBuildDir" does not exist.');
_sshConfig = '$fuchsiaBuildDir/ssh-keys/ssh_config';
} }
if (!_directoryExists(_buildDir))
throwToolExit('Specified --build-dir "$_buildDir" does not exist.'); // If sshConfig path not available from the fuchsiaBuildDir, get from command line.
_sshConfig ??= argResults['ssh-config'];
if (_sshConfig == null)
throwToolExit('Provide the path to the ssh config file with --ssh-config.');
if (!_fileExists(_sshConfig))
throwToolExit('Couldn\'t find ssh config file at $_sshConfig.');
_address = argResults['address']; _address = argResults['address'];
if (_address == null) { if (_address == null && fuchsiaBuildDir != null) {
final ProcessResult result = await processManager.run(<String>['fx', 'netaddr', '--fuchsia']); final ProcessResult result = await processManager.run(<String>['fx', 'netaddr', '--fuchsia']);
if (result.exitCode == 0) if (result.exitCode == 0)
_address = result.stdout.trim(); _address = result.stdout.trim();
@ -298,21 +329,27 @@ class FuchsiaReloadCommand extends FlutterCommand {
_list = argResults['list']; _list = argResults['list'];
if (_list) { if (_list) {
// For --list, we only need the device address and the Fuchsia tree root. // For --list, we only need the ssh config and device address.
return; return;
} }
final String gnTarget = argResults['gn-target']; String projectRoot;
if (gnTarget == null) if (gnTarget != null) {
throwToolExit('Give the GN target with --gn-target(-g).'); if (fuchsiaBuildDir == null)
final List<String> targetInfo = _extractPathAndName(gnTarget); throwToolExit('Must provide --build-dir when specifying --gn-target.');
_projectRoot = targetInfo[0];
printTrace('_projectRoot is $_projectRoot'); final List<String> targetInfo = _extractPathAndName(gnTarget);
_projectName = targetInfo[1]; projectRoot = targetInfo[0];
_fuchsiaProjectPath = '$_buildDir/../../$_projectRoot'; _modName = targetInfo[1];
printTrace('_fuchsiaProjectPath is $_fuchsiaProjectPath'); _fuchsiaProjectPath = '$fuchsiaBuildDir/../../$projectRoot';
} else if (argResults['path'] != null) {
_fuchsiaProjectPath = argResults['path'];
}
if (_fuchsiaProjectPath == null)
throwToolExit('Provide the mod project path with --path.');
if (!_directoryExists(_fuchsiaProjectPath)) if (!_directoryExists(_fuchsiaProjectPath))
throwToolExit('Target does not exist in the Fuchsia tree: $_fuchsiaProjectPath.'); throwToolExit('Cannot locate project at $_fuchsiaProjectPath.');
final String relativeTarget = argResults['target']; final String relativeTarget = argResults['target'];
if (relativeTarget == null) if (relativeTarget == null)
@ -321,18 +358,19 @@ class FuchsiaReloadCommand extends FlutterCommand {
if (!_fileExists(_target)) if (!_fileExists(_target))
throwToolExit('Couldn\'t find application entry point at $_target.'); throwToolExit('Couldn\'t find application entry point at $_target.');
final String nameOverride = argResults['name-override']; if (argResults['mod-name'] != null)
if (nameOverride == null) { _modName = argResults['mod-name'];
_binaryName = _projectName; if (_modName == null)
} else { throwToolExit('Provide the mod name with --mod-name.');
_binaryName = nameOverride;
}
// When there's an override of the on-device binary name, use that name if (argResults['dot-packages'] != null) {
// to locate the .packages file. _dotPackagesPath = argResults['dot-packages'];
final String packagesFileName = '${_binaryName}_dart_library.packages'; } else if (fuchsiaBuildDir != null) {
_dotPackagesPath = '$_buildDir/dartlang/gen/$_projectRoot/$packagesFileName'; final String packagesFileName = '${_modName}_dart_library.packages';
printTrace('_dotPackagesPath is $_dotPackagesPath'); _dotPackagesPath = '$fuchsiaBuildDir/dartlang/gen/$projectRoot/$packagesFileName';
}
if (_dotPackagesPath == null)
throwToolExit('Provide the .packages path with --dot-packages.');
if (!_fileExists(_dotPackagesPath)) if (!_fileExists(_dotPackagesPath))
throwToolExit('Couldn\'t find .packages file at $_dotPackagesPath.'); throwToolExit('Couldn\'t find .packages file at $_dotPackagesPath.');
@ -361,11 +399,10 @@ class FuchsiaReloadCommand extends FlutterCommand {
} }
Future<List<_PortForwarder>> _forwardPorts(List<int> remotePorts) async { Future<List<_PortForwarder>> _forwardPorts(List<int> remotePorts) async {
final String config = '$_buildDir/ssh-keys/ssh_config';
final List<_PortForwarder> forwarders = <_PortForwarder>[]; final List<_PortForwarder> forwarders = <_PortForwarder>[];
for (int port in remotePorts) { for (int port in remotePorts) {
final _PortForwarder f = final _PortForwarder f =
await _PortForwarder.start(config, _address, port); await _PortForwarder.start(_sshConfig, _address, port);
forwarders.add(f); forwarders.add(f);
} }
return forwarders; return forwarders;
@ -373,7 +410,7 @@ class FuchsiaReloadCommand extends FlutterCommand {
Future<List<int>> _getServicePorts() async { Future<List<int>> _getServicePorts() async {
final FuchsiaDeviceCommandRunner runner = final FuchsiaDeviceCommandRunner runner =
FuchsiaDeviceCommandRunner(_address, _buildDir); FuchsiaDeviceCommandRunner(_address, _sshConfig);
final List<String> lsOutput = await runner.run('ls /tmp/dart.services'); final List<String> lsOutput = await runner.run('ls /tmp/dart.services');
final List<int> ports = <int>[]; final List<int> ports = <int>[];
if (lsOutput != null) { if (lsOutput != null) {
@ -483,13 +520,12 @@ class _PortForwarder {
class FuchsiaDeviceCommandRunner { class FuchsiaDeviceCommandRunner {
final String _address; final String _address;
final String _buildDir; final String _sshConfig;
FuchsiaDeviceCommandRunner(this._address, this._buildDir); FuchsiaDeviceCommandRunner(this._address, this._sshConfig);
Future<List<String>> run(String command) async { Future<List<String>> run(String command) async {
final String config = '$_buildDir/ssh-keys/ssh_config'; final List<String> args = <String>['ssh', '-F', _sshConfig, _address, command];
final List<String> args = <String>['ssh', '-F', config, _address, command];
printTrace(args.join(' ')); printTrace(args.join(' '));
final ProcessResult result = await processManager.run(args); final ProcessResult result = await processManager.run(args);
if (result.exitCode != 0) { if (result.exitCode != 0) {