install.dart 3.33 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
    argParser.addFlag('uninstall-only',
      help: 'Uninstall the app if already on the device. Skip install.',
    );
21 22
  }

23
  @override
24
  final String name = 'install';
25 26

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

29 30 31
  @override
  final String category = FlutterCommandCategory.tools;

32
  Device? device;
33

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

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

  @override
50
  Future<FlutterCommandResult> runCommand() async {
51 52 53
    final Device targetDevice = device!;
    final ApplicationPackage? package = await applicationPackages?.getPackageForPlatform(
      await targetDevice.targetPlatform,
54
    );
55 56 57
    if (package == null) {
      throwToolExit('Could not find or build package');
    }
58

59
    if (uninstallOnly) {
60
      await _uninstallApp(package, targetDevice);
61
    } else {
62
      await _installApp(package, targetDevice);
63 64 65 66
    }
    return FlutterCommandResult.success();
  }

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

78
  Future<void> _installApp(ApplicationPackage package, Device device) async {
79
    globals.printStatus('Installing $package to $device...');
80

81
    if (!await installApp(device, package, userIdentifier: userIdentifier)) {
82
      throwToolExit('Install failed');
83
    }
84
  }
85
}
86

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

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

108
  return device.installApp(package, userIdentifier: userIdentifier);
109
}