Flutter attach (#18677)
Add `flutter attach` command. This command allows Flutter to connect to applications that haven't been launched using `flutter run`.
This commit is contained in:
@@ -6,6 +6,7 @@ import 'dart:async';
|
||||
|
||||
import 'runner.dart' as runner;
|
||||
import 'src/commands/analyze.dart';
|
||||
import 'src/commands/attach.dart';
|
||||
import 'src/commands/build.dart';
|
||||
import 'src/commands/channel.dart';
|
||||
import 'src/commands/clean.dart';
|
||||
@@ -48,6 +49,7 @@ Future<Null> main(List<String> args) async {
|
||||
|
||||
await runner.run(args, <FlutterCommand>[
|
||||
new AnalyzeCommand(verboseHelp: verboseHelp),
|
||||
new AttachCommand(verboseHelp: verboseHelp),
|
||||
new BuildCommand(verboseHelp: verboseHelp),
|
||||
new ChannelCommand(verboseHelp: verboseHelp),
|
||||
new CleanCommand(),
|
||||
|
||||
105
packages/flutter_tools/lib/src/commands/attach.dart
Normal file
105
packages/flutter_tools/lib/src/commands/attach.dart
Normal file
@@ -0,0 +1,105 @@
|
||||
// Copyright 2018 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import '../base/common.dart';
|
||||
import '../base/io.dart';
|
||||
import '../cache.dart';
|
||||
import '../device.dart';
|
||||
import '../protocol_discovery.dart';
|
||||
import '../resident_runner.dart';
|
||||
import '../run_hot.dart';
|
||||
import '../runner/flutter_command.dart';
|
||||
|
||||
final String ipv4Loopback = InternetAddress.loopbackIPv4.address;
|
||||
|
||||
/// A Flutter-command that attaches to applications that have been launched
|
||||
/// without `flutter run`.
|
||||
///
|
||||
/// With an application already running, a HotRunner can be attached to it
|
||||
/// with:
|
||||
/// ```
|
||||
/// $ flutter attach --debug-port 12345
|
||||
/// ```
|
||||
///
|
||||
/// Alternatively, the attach command can start listening and scan for new
|
||||
/// programs that become active:
|
||||
/// ```
|
||||
/// $ flutter attach
|
||||
/// ```
|
||||
/// As soon as a new observatory is detected the command attaches to it and
|
||||
/// enables hot reloading.
|
||||
class AttachCommand extends FlutterCommand {
|
||||
AttachCommand({bool verboseHelp = false}) {
|
||||
addBuildModeFlags(defaultToRelease: false);
|
||||
argParser.addOption(
|
||||
'debug-port',
|
||||
help: 'Local port where the observatory is listening.',
|
||||
);
|
||||
argParser.addFlag(
|
||||
'preview-dart-2',
|
||||
defaultsTo: true,
|
||||
hide: !verboseHelp,
|
||||
help: 'Preview Dart 2.0 functionality.',
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
final String name = 'attach';
|
||||
|
||||
@override
|
||||
final String description = 'Attach to a running application.';
|
||||
|
||||
int get observatoryPort {
|
||||
if (argResults['debug-port'] == null)
|
||||
return null;
|
||||
try {
|
||||
return int.parse(argResults['debug-port']);
|
||||
} catch (error) {
|
||||
throwToolExit('Invalid port for `--debug-port`: $error');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Null> runCommand() async {
|
||||
Cache.releaseLockEarly();
|
||||
|
||||
await _validateArguments();
|
||||
|
||||
final Device device = await findTargetDevice();
|
||||
final int devicePort = observatoryPort;
|
||||
Uri observatoryUri;
|
||||
if (devicePort == null) {
|
||||
ProtocolDiscovery observatoryDiscovery;
|
||||
try {
|
||||
observatoryDiscovery = new ProtocolDiscovery.observatory(
|
||||
device.getLogReader(), portForwarder: device.portForwarder);
|
||||
print('Listening.');
|
||||
observatoryUri = await observatoryDiscovery.uri;
|
||||
} finally {
|
||||
await observatoryDiscovery?.cancel();
|
||||
}
|
||||
} else {
|
||||
final int localPort = await device.portForwarder.forward(devicePort);
|
||||
observatoryUri = Uri.parse('http://$ipv4Loopback:$localPort/');
|
||||
}
|
||||
try {
|
||||
final FlutterDevice flutterDevice =
|
||||
new FlutterDevice(device, trackWidgetCreation: false, previewDart2: argResults['preview-dart-2']);
|
||||
flutterDevice.observatoryUris = <Uri>[ observatoryUri ];
|
||||
final HotRunner hotRunner = new HotRunner(
|
||||
<FlutterDevice>[flutterDevice],
|
||||
debuggingOptions: new DebuggingOptions.enabled(getBuildInfo()),
|
||||
packagesFilePath: globalResults['packages'],
|
||||
);
|
||||
await hotRunner.attach();
|
||||
} finally {
|
||||
device.portForwarder.forwardedPorts.forEach(device.portForwarder.unforward);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _validateArguments() async {}
|
||||
}
|
||||
74
packages/flutter_tools/test/commands/attach_test.dart
Normal file
74
packages/flutter_tools/test/commands/attach_test.dart
Normal file
@@ -0,0 +1,74 @@
|
||||
// Copyright 2018 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_tools/src/cache.dart';
|
||||
import 'package:flutter_tools/src/commands/attach.dart';
|
||||
import 'package:flutter_tools/src/device.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../src/common.dart';
|
||||
import '../src/context.dart';
|
||||
import '../src/mocks.dart';
|
||||
|
||||
void main() {
|
||||
group('attach', () {
|
||||
setUpAll(() {
|
||||
Cache.disableLocking();
|
||||
});
|
||||
|
||||
testUsingContext('finds observatory port and forwards', () async {
|
||||
const int devicePort = 499;
|
||||
const int hostPort = 42;
|
||||
final MockDeviceLogReader mockLogReader = new MockDeviceLogReader();
|
||||
final MockPortForwarder portForwarder = new MockPortForwarder();
|
||||
final MockAndroidDevice device = new MockAndroidDevice();
|
||||
when(device.getLogReader()).thenAnswer((_) {
|
||||
// Now that the reader is used, start writing messages to it.
|
||||
Timer.run(() {
|
||||
mockLogReader.addLine('Foo');
|
||||
mockLogReader.addLine('Observatory listening on http://127.0.0.1:$devicePort');
|
||||
});
|
||||
|
||||
return mockLogReader;
|
||||
});
|
||||
when(device.portForwarder).thenReturn(portForwarder);
|
||||
when(portForwarder.forward(devicePort, hostPort: anyNamed('hostPort'))).thenAnswer((_) async => hostPort);
|
||||
when(portForwarder.forwardedPorts).thenReturn(<ForwardedPort>[new ForwardedPort(hostPort, devicePort)]);
|
||||
when(portForwarder.unforward).thenReturn((ForwardedPort _) async => null);
|
||||
testDeviceManager.addDevice(device);
|
||||
|
||||
final AttachCommand command = new AttachCommand();
|
||||
|
||||
await createTestCommandRunner(command).run(<String>['attach']);
|
||||
|
||||
verify(portForwarder.forward(devicePort, hostPort: anyNamed('hostPort'))).called(1);
|
||||
|
||||
mockLogReader.dispose();
|
||||
});
|
||||
|
||||
testUsingContext('forwards to given port', () async {
|
||||
const int devicePort = 499;
|
||||
const int hostPort = 42;
|
||||
final MockPortForwarder portForwarder = new MockPortForwarder();
|
||||
final MockAndroidDevice device = new MockAndroidDevice();
|
||||
|
||||
when(device.portForwarder).thenReturn(portForwarder);
|
||||
when(portForwarder.forward(devicePort)).thenAnswer((_) async => hostPort);
|
||||
when(portForwarder.forwardedPorts).thenReturn(<ForwardedPort>[new ForwardedPort(hostPort, devicePort)]);
|
||||
when(portForwarder.unforward).thenReturn((ForwardedPort _) async => null);
|
||||
testDeviceManager.addDevice(device);
|
||||
|
||||
final AttachCommand command = new AttachCommand();
|
||||
|
||||
await createTestCommandRunner(command).run(<String>['attach', '--debug-port', '$devicePort']);
|
||||
|
||||
verify(portForwarder.forward(devicePort)).called(1);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class MockPortForwarder extends Mock implements DevicePortForwarder {}
|
||||
Reference in New Issue
Block a user