Merge pull request #2123 from devoncarew/device_commands
additional validation for device commands
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:io' as dart_io;
|
||||
|
||||
import 'package:file/io.dart';
|
||||
import 'package:file/sync_io.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
@@ -83,6 +83,8 @@ class DriveCommand extends RunCommand {
|
||||
|
||||
DriveCommand() : this.custom();
|
||||
|
||||
bool get requiresDevice => true;
|
||||
|
||||
@override
|
||||
Future<int> runInProject() async {
|
||||
String testFile = _getTestFile();
|
||||
|
||||
@@ -3,41 +3,29 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import '../application_package.dart';
|
||||
import '../device.dart';
|
||||
import '../ios/simulators.dart';
|
||||
import '../runner/flutter_command.dart';
|
||||
|
||||
class InstallCommand extends FlutterCommand {
|
||||
final String name = 'install';
|
||||
final String description = 'Install Flutter apps on attached devices.';
|
||||
|
||||
InstallCommand() {
|
||||
argParser.addFlag('boot', help: 'Boot the iOS Simulator if it isn\'t already running.');
|
||||
}
|
||||
bool get requiresDevice => true;
|
||||
|
||||
@override
|
||||
Future<int> runInProject() async {
|
||||
await downloadApplicationPackagesAndConnectToDevices();
|
||||
bool installedAny = await installApp(
|
||||
devices,
|
||||
applicationPackages,
|
||||
boot: argResults['boot']
|
||||
);
|
||||
bool installedAny = await installApp(devices, applicationPackages);
|
||||
return installedAny ? 0 : 2;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> installApp(
|
||||
DeviceStore devices,
|
||||
ApplicationPackageStore applicationPackages, {
|
||||
bool boot: false
|
||||
}) async {
|
||||
if (boot && Platform.isMacOS)
|
||||
await SimControl.boot();
|
||||
|
||||
ApplicationPackageStore applicationPackages
|
||||
) async {
|
||||
bool installedSomewhere = false;
|
||||
|
||||
for (Device device in devices.all) {
|
||||
|
||||
@@ -22,6 +22,10 @@ class ListenCommand extends RunCommandBase {
|
||||
|
||||
ListenCommand({ this.singleRun: false });
|
||||
|
||||
bool get androidOnly => true;
|
||||
|
||||
bool get requiresDevice => true;
|
||||
|
||||
@override
|
||||
Future<int> runInProject() async {
|
||||
await downloadApplicationPackagesAndConnectToDevices();
|
||||
|
||||
@@ -22,6 +22,8 @@ class LogsCommand extends FlutterCommand {
|
||||
|
||||
bool get requiresProjectRoot => false;
|
||||
|
||||
bool get requiresDevice => true;
|
||||
|
||||
Future<int> runInProject() async {
|
||||
List<Device> devices = await deviceManager.getDevices();
|
||||
|
||||
|
||||
@@ -23,6 +23,10 @@ class RefreshCommand extends FlutterCommand {
|
||||
);
|
||||
}
|
||||
|
||||
bool get androidOnly => true;
|
||||
|
||||
bool get requiresDevice => true;
|
||||
|
||||
@override
|
||||
Future<int> runInProject() async {
|
||||
printTrace('Downloading toolchain.');
|
||||
|
||||
@@ -55,8 +55,7 @@ abstract class RunCommandBase extends FlutterCommand {
|
||||
|
||||
class RunCommand extends RunCommandBase {
|
||||
final String name = 'run';
|
||||
final String description =
|
||||
'Run your Flutter app on an attached device (defaults to checked/debug mode).';
|
||||
final String description = 'Run your Flutter app on an attached device.';
|
||||
final List<String> aliases = <String>['start'];
|
||||
|
||||
RunCommand() {
|
||||
@@ -78,6 +77,8 @@ class RunCommand extends RunCommandBase {
|
||||
help: 'Listen to the given port for a debug connection.');
|
||||
}
|
||||
|
||||
bool get requiresDevice => true;
|
||||
|
||||
@override
|
||||
Future<int> run() async {
|
||||
if (argResults['pub'])
|
||||
|
||||
@@ -51,12 +51,17 @@ class TestCommand extends FlutterCommand {
|
||||
}
|
||||
|
||||
TestCommand() {
|
||||
argParser.addFlag('flutter-repo', help: 'Run tests from the \'flutter\' package in the Flutter repository instead of the current directory.', defaultsTo: false);
|
||||
argParser.addFlag(
|
||||
'flutter-repo',
|
||||
help: 'Run tests from the \'flutter\' package in the Flutter repository instead of the current directory.',
|
||||
defaultsTo: false
|
||||
);
|
||||
}
|
||||
|
||||
Iterable<String> _findTests(Directory directory) {
|
||||
return directory.listSync(recursive: true, followLinks: false)
|
||||
.where((FileSystemEntity entity) => entity.path.endsWith('_test.dart') && FileSystemEntity.isFileSync(entity.path))
|
||||
.where((FileSystemEntity entity) => entity.path.endsWith('_test.dart') &&
|
||||
FileSystemEntity.isFileSync(entity.path))
|
||||
.map((FileSystemEntity entity) => path.absolute(entity.path));
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,10 @@ class TraceCommand extends FlutterCommand {
|
||||
defaultsTo: '10', abbr: 'd', help: 'Duration in seconds to trace.');
|
||||
}
|
||||
|
||||
bool get androidOnly => true;
|
||||
|
||||
bool get requiresDevice => true;
|
||||
|
||||
@override
|
||||
Future<int> runInProject() async {
|
||||
await downloadApplicationPackagesAndConnectToDevices();
|
||||
|
||||
@@ -39,7 +39,9 @@ class Doctor {
|
||||
|
||||
List<DoctorValidator> _validators = <DoctorValidator>[];
|
||||
|
||||
Iterable<Workflow> get workflows => _validators.where((DoctorValidator validator) => validator is Workflow);
|
||||
List<Workflow> get workflows {
|
||||
return new List<Workflow>.from(_validators.where((DoctorValidator validator) => validator is Workflow));
|
||||
}
|
||||
|
||||
/// Print a summary of the state of the tooling, as well as how to get more info.
|
||||
void summary() => printStatus(summaryText);
|
||||
|
||||
@@ -22,31 +22,68 @@ abstract class FlutterCommand extends Command {
|
||||
/// Whether this command needs to be run from the root of a project.
|
||||
bool get requiresProjectRoot => true;
|
||||
|
||||
/// Whether this command requires a (single) Flutter target device to be connected.
|
||||
bool get requiresDevice => false;
|
||||
|
||||
/// Whether this command only applies to Android devices.
|
||||
bool get androidOnly => false;
|
||||
|
||||
List<BuildConfiguration> get buildConfigurations => runner.buildConfigurations;
|
||||
|
||||
Future downloadApplicationPackages() async {
|
||||
if (applicationPackages == null)
|
||||
applicationPackages = await ApplicationPackageStore.forConfigs(buildConfigurations);
|
||||
}
|
||||
|
||||
Future downloadToolchain() async {
|
||||
if (toolchain == null)
|
||||
toolchain = await Toolchain.forConfigs(buildConfigurations);
|
||||
}
|
||||
|
||||
void connectToDevices() {
|
||||
if (devices == null)
|
||||
devices = new DeviceStore.forConfigs(buildConfigurations);
|
||||
toolchain ??= await Toolchain.forConfigs(buildConfigurations);
|
||||
}
|
||||
|
||||
Future downloadApplicationPackagesAndConnectToDevices() async {
|
||||
await downloadApplicationPackages();
|
||||
connectToDevices();
|
||||
await _downloadApplicationPackages();
|
||||
_connectToDevices();
|
||||
}
|
||||
|
||||
Future _downloadApplicationPackages() async {
|
||||
applicationPackages ??= await ApplicationPackageStore.forConfigs(buildConfigurations);
|
||||
}
|
||||
|
||||
void _connectToDevices() {
|
||||
devices ??= new DeviceStore.forConfigs(buildConfigurations);
|
||||
}
|
||||
|
||||
Future<int> run() async {
|
||||
if (requiresProjectRoot && !projectRootValidator())
|
||||
return 1;
|
||||
|
||||
// Ensure at least one toolchain is installed.
|
||||
if (requiresDevice && !doctor.canLaunchAnything) {
|
||||
printError("Unable to locate a development device; please run 'flutter doctor' "
|
||||
"for information about installing additional components.");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Validate devices.
|
||||
if (requiresDevice) {
|
||||
List<Device> devices = await deviceManager.getDevices();
|
||||
|
||||
if (devices.isEmpty && deviceManager.hasSpecifiedDeviceId) {
|
||||
printError("No device found with id '${deviceManager.specifiedDeviceId}'.");
|
||||
return 1;
|
||||
} else if (devices.isEmpty) {
|
||||
printStatus('No connected devices.');
|
||||
return 1;
|
||||
}
|
||||
|
||||
devices = devices.where((Device device) => device.isSupported()).toList();
|
||||
|
||||
if (androidOnly)
|
||||
devices = devices.where((Device device) => device.platform == TargetPlatform.android).toList();
|
||||
|
||||
// TODO(devoncarew): Switch this to just supporting one connected device?
|
||||
if (devices.isEmpty) {
|
||||
printStatus('No supported devices connected.');
|
||||
return 1;
|
||||
}
|
||||
|
||||
_devicesForCommand = await _getDevicesForCommand();
|
||||
}
|
||||
|
||||
return await runInProject();
|
||||
}
|
||||
|
||||
@@ -63,7 +100,36 @@ abstract class FlutterCommand extends Command {
|
||||
|
||||
Future<int> runInProject();
|
||||
|
||||
List<Device> get devicesForCommand => _devicesForCommand;
|
||||
|
||||
Device get deviceForCommand {
|
||||
// TODO(devoncarew): Switch this to just supporting one connected device?
|
||||
return devicesForCommand.isNotEmpty ? devicesForCommand.first : null;
|
||||
}
|
||||
|
||||
// This is caculated in run() if the command has [requiresDevice] specified.
|
||||
List<Device> _devicesForCommand;
|
||||
|
||||
ApplicationPackageStore applicationPackages;
|
||||
Toolchain toolchain;
|
||||
DeviceStore devices;
|
||||
|
||||
Future<List<Device>> _getDevicesForCommand() async {
|
||||
List<Device> devices = await deviceManager.getDevices();
|
||||
|
||||
if (devices.isEmpty)
|
||||
return null;
|
||||
|
||||
if (deviceManager.hasSpecifiedDeviceId) {
|
||||
Device device = await deviceManager.getDeviceById(deviceManager.specifiedDeviceId);
|
||||
return device == null ? <Device>[] : <Device>[device];
|
||||
}
|
||||
|
||||
devices = devices.where((Device device) => device.isSupported()).toList();
|
||||
|
||||
if (androidOnly)
|
||||
devices = devices.where((Device device) => device.platform == TargetPlatform.android).toList();
|
||||
|
||||
return devices;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user