Return host and port from devtools launcher (#63795)
This commit is contained in:
@@ -256,7 +256,11 @@ The returned `params` will contain:
|
||||
|
||||
#### devtools.serve
|
||||
|
||||
The `serve()` command starts a DevTools server if one isn't already running and returns the host and port of the server.
|
||||
The `serve()` command starts a DevTools server if one isn't already running. The return value will contain:
|
||||
|
||||
- `success` - whether the server started.
|
||||
- `host` - the address host if the server successfully started.
|
||||
- `port` - the port if the server successfully started.
|
||||
|
||||
## 'flutter run --machine' and 'flutter attach --machine'
|
||||
|
||||
|
||||
@@ -897,11 +897,11 @@ class DevToolsDomain extends Domain {
|
||||
|
||||
Future<Map<String, dynamic>> serve([ Map<String, dynamic> args ]) async {
|
||||
_devtoolsLauncher ??= DevtoolsLauncher.instance;
|
||||
final HttpServer server = await _devtoolsLauncher.serve();
|
||||
final DevToolsServerAddress server = await _devtoolsLauncher.serve();
|
||||
|
||||
return<String, dynamic>{
|
||||
'host': server.address.host,
|
||||
'port': server.port,
|
||||
'host': server?.host,
|
||||
'port': server?.port,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1723,15 +1723,16 @@ class DevtoolsLauncher {
|
||||
}
|
||||
}
|
||||
|
||||
Future<io.HttpServer> serve() async {
|
||||
Future<DevToolsServerAddress> serve() async {
|
||||
try {
|
||||
_devtoolsServer ??= await devtools_server.serveDevTools(
|
||||
enableStdinCommands: false,
|
||||
);
|
||||
return DevToolsServerAddress(_devtoolsServer.address.host, _devtoolsServer.port);
|
||||
} on Exception catch (e, st) {
|
||||
globals.printTrace('Failed to serve DevTools: $e\n$st');
|
||||
return null;
|
||||
}
|
||||
return _devtoolsServer;
|
||||
}
|
||||
|
||||
Future<void> close() async {
|
||||
@@ -1741,3 +1742,10 @@ class DevtoolsLauncher {
|
||||
|
||||
static DevtoolsLauncher get instance => context.get<DevtoolsLauncher>() ?? DevtoolsLauncher();
|
||||
}
|
||||
|
||||
class DevToolsServerAddress {
|
||||
DevToolsServerAddress(this.host, this.port);
|
||||
|
||||
final String host;
|
||||
final int port;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter_tools/src/android/android_workflow.dart';
|
||||
import 'package:flutter_tools/src/base/common.dart';
|
||||
@@ -304,7 +303,7 @@ void main() {
|
||||
await input.close();
|
||||
});
|
||||
|
||||
testUsingContext('devtools.serve command should return host and port', () async {
|
||||
testUsingContext('devtools.serve command should return host and port on success', () async {
|
||||
final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>();
|
||||
final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>();
|
||||
daemon = Daemon(
|
||||
@@ -312,19 +311,34 @@ void main() {
|
||||
responses.add,
|
||||
notifyingLogger: notifyingLogger,
|
||||
);
|
||||
final HttpServer mockDevToolsServer = MockDevToolsServer();
|
||||
final InternetAddress mockInternetAddress = MockInternetAddress();
|
||||
when(mockDevToolsServer.address).thenReturn(mockInternetAddress);
|
||||
when(mockInternetAddress.host).thenReturn('127.0.0.1');
|
||||
when(mockDevToolsServer.port).thenReturn(1234);
|
||||
|
||||
when(mockDevToolsLauncher.serve()).thenAnswer((_) async => mockDevToolsServer);
|
||||
when(mockDevToolsLauncher.serve()).thenAnswer((_) async => DevToolsServerAddress('127.0.0.1', 1234));
|
||||
|
||||
commands.add(<String, dynamic>{'id': 0, 'method': 'devtools.serve'});
|
||||
final Map<String, dynamic> response = await responses.stream.firstWhere((Map<String, dynamic> response) => response['id'] == 0);
|
||||
expect(response['result'], isNotEmpty);
|
||||
expect(response['result']['host'], equals('127.0.0.1'));
|
||||
expect(response['result']['port'], equals(1234));
|
||||
expect(response['result']['host'], '127.0.0.1');
|
||||
expect(response['result']['port'], 1234);
|
||||
await responses.close();
|
||||
await commands.close();
|
||||
}, overrides: <Type, Generator>{
|
||||
DevtoolsLauncher: () => mockDevToolsLauncher,
|
||||
});
|
||||
|
||||
testUsingContext('devtools.serve command should return null fields if null returned', () async {
|
||||
final StreamController<Map<String, dynamic>> commands = StreamController<Map<String, dynamic>>();
|
||||
final StreamController<Map<String, dynamic>> responses = StreamController<Map<String, dynamic>>();
|
||||
daemon = Daemon(
|
||||
commands.stream,
|
||||
responses.add,
|
||||
notifyingLogger: notifyingLogger,
|
||||
);
|
||||
when(mockDevToolsLauncher.serve()).thenAnswer((_) async => null);
|
||||
|
||||
commands.add(<String, dynamic>{'id': 0, 'method': 'devtools.serve'});
|
||||
final Map<String, dynamic> response = await responses.stream.firstWhere((Map<String, dynamic> response) => response['id'] == 0);
|
||||
expect(response['result'], isNotEmpty);
|
||||
expect(response['result']['host'], null);
|
||||
expect(response['result']['port'], null);
|
||||
await responses.close();
|
||||
await commands.close();
|
||||
}, overrides: <Type, Generator>{
|
||||
|
||||
Reference in New Issue
Block a user