install.dart 3.85 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/file_system.dart';
9
import '../base/io.dart';
10
import '../device.dart';
11
import '../globals.dart' as globals;
12
import '../runner/flutter_command.dart';
13

14
class InstallCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
15 16
  InstallCommand() {
    requiresPubspecYaml();
17
    usesDeviceUserOption();
18
    usesDeviceTimeoutOption();
19
    usesApplicationBinaryOption();
20 21 22
    argParser.addFlag('uninstall-only',
      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 33
  @override
  final String category = FlutterCommandCategory.tools;

34
  Device? device;
35

36
  bool get uninstallOnly => boolArgDeprecated('uninstall-only');
37
  String? get userIdentifier => stringArgDeprecated(FlutterOptions.kDeviceUser);
38

39
  String? get _applicationBinaryPath => stringArgDeprecated(FlutterOptions.kUseApplicationBinary);
40 41
  File? get _applicationBinary => _applicationBinaryPath == null ? null : globals.fs.file(_applicationBinaryPath);

42
  @override
43
  Future<void> validateCommand() async {
44
    await super.validateCommand();
45
    device = await findTargetDevice();
46
    if (device == null) {
47
      throwToolExit('No target device found');
48
    }
49 50 51
    if (userIdentifier != null && device is! AndroidDevice) {
      throwToolExit('--${FlutterOptions.kDeviceUser} is only supported for Android');
    }
52 53 54
    if (_applicationBinaryPath != null && !(_applicationBinary?.existsSync() ?? true)) {
      throwToolExit('Prebuilt binary $_applicationBinaryPath does not exist');
    }
55
  }
56 57

  @override
58
  Future<FlutterCommandResult> runCommand() async {
59 60 61
    final Device targetDevice = device!;
    final ApplicationPackage? package = await applicationPackages?.getPackageForPlatform(
      await targetDevice.targetPlatform,
62
      applicationBinary: _applicationBinary,
63
    );
64 65 66
    if (package == null) {
      throwToolExit('Could not find or build package');
    }
67

68
    if (uninstallOnly) {
69
      await _uninstallApp(package, targetDevice);
70
    } else {
71
      await _installApp(package, targetDevice);
72 73 74 75
    }
    return FlutterCommandResult.success();
  }

76
  Future<void> _uninstallApp(ApplicationPackage package, Device device) async {
77
    if (await device.isAppInstalled(package, userIdentifier: userIdentifier)) {
78
      globals.printStatus('Uninstalling $package from $device...');
79
      if (!await device.uninstallApp(package, userIdentifier: userIdentifier)) {
80 81 82 83 84 85 86
        globals.printError('Uninstalling old version failed');
      }
    } else {
      globals.printStatus('$package not found on $device, skipping uninstall');
    }
  }

87
  Future<void> _installApp(ApplicationPackage package, Device device) async {
88
    globals.printStatus('Installing $package to $device...');
89

90
    if (!await installApp(device, package, userIdentifier: userIdentifier)) {
91
      throwToolExit('Install failed');
92
    }
93
  }
94
}
95

96 97 98
Future<bool> installApp(
  Device device,
  ApplicationPackage package, {
99
  String? userIdentifier,
100 101
  bool uninstall = true
}) async {
102
  if (package == null) {
103
    return false;
104
  }
105

106
  try {
107
    if (uninstall && await device.isAppInstalled(package, userIdentifier: userIdentifier)) {
108
      globals.printStatus('Uninstalling old version...');
109
      if (!await device.uninstallApp(package, userIdentifier: userIdentifier)) {
110
        globals.printWarning('Warning: uninstalling old version failed');
111
      }
112
    }
113 114
  } on ProcessException catch (e) {
    globals.printError('Error accessing device ${device.id}:\n${e.message}');
115
  }
116

117
  return device.installApp(package, userIdentifier: userIdentifier);
118
}