install.dart 3.15 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 6
// @dart = 2.8

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

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

27
  @override
28
  final String name = 'install';
29 30

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

33 34
  Device device;

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

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

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

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

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

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

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

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

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

105
  return device.installApp(package, userIdentifier: userIdentifier);
106
}