Support legacy behavior for --host-vmservice-port and --observatory-port with DDS (#70336)

Implements the following:
  - If both --host-vmservice-port and --dds-port are specified, use the current behavior
  - If only --host-vmservice-port is specified and dds is enabled, use that for the dds port
  - If only --dds-port is specified, use that for the dds port

Fixes https://github.com/flutter/flutter/issues/70332
This commit is contained in:
Ben Konyi
2020-11-17 16:03:02 -08:00
committed by GitHub
parent 9b238a1ff8
commit f6bedddd9f
3 changed files with 147 additions and 9 deletions

View File

@@ -341,10 +341,27 @@ abstract class FlutterCommand extends Command<void> {
bool get disableDds => boolArg('disable-dds');
bool get _hostVmServicePortProvided => argResults.wasParsed('observatory-port') ||
argResults.wasParsed('host-vmservice-port');
int _tryParseHostVmservicePort() {
try {
return int.parse(stringArg('observatory-port') ?? stringArg('host-vmservice-port'));
} on FormatException catch (error) {
throwToolExit('Invalid port for `--observatory-port/--host-vmservice-port`: $error');
}
return null;
}
int get ddsPort {
if (argResults.wasParsed('dds-port')) {
if (!argResults.wasParsed('dds-port') && _hostVmServicePortProvided) {
// If an explicit DDS port is _not_ provided, use the host-vmservice-port for DDS.
return _tryParseHostVmservicePort();
} else if (argResults.wasParsed('dds-port')) {
// If an explicit DDS port is provided, use dds-port for DDS.
return int.tryParse(stringArg('dds-port')) ?? 0;
}
// Otherwise, DDS can bind to a random port.
return 0;
}
@@ -356,9 +373,7 @@ abstract class FlutterCommand extends Command<void> {
///
/// If no port is set, returns null.
int get hostVmservicePort {
if (!_usesPortOption ||
(argResults['observatory-port'] == null &&
argResults['host-vmservice-port'] == null)) {
if (!_usesPortOption || !_hostVmServicePortProvided) {
return null;
}
if (argResults.wasParsed('observatory-port') &&
@@ -366,12 +381,13 @@ abstract class FlutterCommand extends Command<void> {
throwToolExit('Only one of "--observatory-port" and '
'"--host-vmservice-port" may be specified.');
}
try {
return int.parse(stringArg('observatory-port') ?? stringArg('host-vmservice-port'));
} on FormatException catch (error) {
throwToolExit('Invalid port for `--observatory-port/--host-vmservice-port`: $error');
// If DDS is enabled and no explicit DDS port is provided, use the
// host-vmservice-port for DDS instead and bind the VM service to a random
// port.
if (!disableDds && !argResults.wasParsed('dds-port')) {
return null;
}
return null;
return _tryParseHostVmservicePort();
}
/// Gets the vmservice port provided to in the 'device-vmservice-port' option.