install.dart 3.12 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import '../android/android_device.dart';
6
import '../application_package.dart';
7
import '../base/common.dart';
8
import '../base/io.dart';
9
import '../device.dart';
10
import '../globals.dart' as globals;
11
import '../runner/flutter_command.dart';
12

13
class InstallCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
14 15
  InstallCommand() {
    requiresPubspecYaml();
16
    usesDeviceUserOption();
17
    usesDeviceTimeoutOption();
18 19 20 21 22
    argParser.addFlag('uninstall-only',
      negatable: true,
      defaultsTo: false,
      help: 'Uninstall the app if already on the device. Skip install.',
    );
23 24
  }

25
  @override
26
  final String name = 'install';
27 28

  @override
29
  final String description = 'Install a Flutter app on an attached device.';
30

31 32
  Device device;

33
  bool get uninstallOnly => boolArg('uninstall-only');
34
  String get userIdentifier => stringArg(FlutterOptions.kDeviceUser);
35

36
  @override
37
  Future<void> validateCommand() async {
38
    await super.validateCommand();
39
    device = await findTargetDevice();
40
    if (device == null) {
41
      throwToolExit('No target device found');
42
    }
43 44 45
    if (userIdentifier != null && device is! AndroidDevice) {
      throwToolExit('--${FlutterOptions.kDeviceUser} is only supported for Android');
    }
46
  }
47 48

  @override
49
  Future<FlutterCommandResult> runCommand() async {
50 51 52
    final ApplicationPackage package = await applicationPackages.getPackageForPlatform(
      await device.targetPlatform,
    );
53

54 55 56 57 58 59 60 61 62
    if (uninstallOnly) {
      await _uninstallApp(package);
    } else {
      await _installApp(package);
    }
    return FlutterCommandResult.success();
  }

  Future<void> _uninstallApp(ApplicationPackage package) async {
63
    if (await device.isAppInstalled(package, userIdentifier: userIdentifier)) {
64
      globals.printStatus('Uninstalling $package from $device...');
65
      if (!await device.uninstallApp(package, userIdentifier: userIdentifier)) {
66 67 68 69 70 71 72 73
        globals.printError('Uninstalling old version failed');
      }
    } else {
      globals.printStatus('$package not found on $device, skipping uninstall');
    }
  }

  Future<void> _installApp(ApplicationPackage package) async {
74
    globals.printStatus('Installing $package to $device...');
75

76
    if (!await installApp(device, package, userIdentifier: userIdentifier)) {
77
      throwToolExit('Install failed');
78
    }
79
  }
80
}
81

82 83 84 85 86 87
Future<bool> installApp(
  Device device,
  ApplicationPackage package, {
  String userIdentifier,
  bool uninstall = true
}) async {
88
  if (package == null) {
89
    return false;
90
  }
91

92
  try {
93
    if (uninstall && await device.isAppInstalled(package, userIdentifier: userIdentifier)) {
94
      globals.printStatus('Uninstalling old version...');
95
      if (!await device.uninstallApp(package, userIdentifier: userIdentifier)) {
96 97
        globals.printError('Warning: uninstalling old version failed');
      }
98
    }
99 100
  } on ProcessException catch (e) {
    globals.printError('Error accessing device ${device.id}:\n${e.message}');
101
  }
102

103
  return device.installApp(package, userIdentifier: userIdentifier);
104
}