install.dart 4 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 17 18
  InstallCommand({
    required bool verboseHelp,
  }) {
    addBuildModeFlags(verboseHelp: verboseHelp);
19
    requiresPubspecYaml();
20
    usesApplicationBinaryOption();
21
    usesDeviceTimeoutOption();
22
    usesDeviceConnectionOption();
23 24
    usesDeviceUserOption();
    usesFlavorOption();
25 26 27
    argParser.addFlag('uninstall-only',
      help: 'Uninstall the app if already on the device. Skip install.',
    );
28 29
  }

30
  @override
31
  final String name = 'install';
32 33

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

36 37 38
  @override
  final String category = FlutterCommandCategory.tools;

39 40 41
  @override
  bool get refreshWirelessDevices => true;

42
  Device? device;
43

44 45
  bool get uninstallOnly => boolArg('uninstall-only');
  String? get userIdentifier => stringArg(FlutterOptions.kDeviceUser);
46

47
  String? get _applicationBinaryPath => stringArg(FlutterOptions.kUseApplicationBinary);
48 49
  File? get _applicationBinary => _applicationBinaryPath == null ? null : globals.fs.file(_applicationBinaryPath);

50
  @override
51
  Future<void> validateCommand() async {
52
    await super.validateCommand();
53
    device = await findTargetDevice();
54
    if (device == null) {
55
      throwToolExit('No target device found');
56
    }
57 58 59
    if (userIdentifier != null && device is! AndroidDevice) {
      throwToolExit('--${FlutterOptions.kDeviceUser} is only supported for Android');
    }
60 61 62
    if (_applicationBinaryPath != null && !(_applicationBinary?.existsSync() ?? true)) {
      throwToolExit('Prebuilt binary $_applicationBinaryPath does not exist');
    }
63
  }
64 65

  @override
66
  Future<FlutterCommandResult> runCommand() async {
67 68 69
    final Device targetDevice = device!;
    final ApplicationPackage? package = await applicationPackages?.getPackageForPlatform(
      await targetDevice.targetPlatform,
70
      applicationBinary: _applicationBinary,
71
      buildInfo: await getBuildInfo(),
72
    );
73 74 75
    if (package == null) {
      throwToolExit('Could not find or build package');
    }
76

77
    if (uninstallOnly) {
78
      await _uninstallApp(package, targetDevice);
79
    } else {
80
      await _installApp(package, targetDevice);
81 82 83 84
    }
    return FlutterCommandResult.success();
  }

85
  Future<void> _uninstallApp(ApplicationPackage package, Device device) async {
86
    if (await device.isAppInstalled(package, userIdentifier: userIdentifier)) {
87
      globals.printStatus('Uninstalling $package from $device...');
88
      if (!await device.uninstallApp(package, userIdentifier: userIdentifier)) {
89 90 91 92 93 94 95
        globals.printError('Uninstalling old version failed');
      }
    } else {
      globals.printStatus('$package not found on $device, skipping uninstall');
    }
  }

96
  Future<void> _installApp(ApplicationPackage package, Device device) async {
97
    globals.printStatus('Installing $package to $device...');
98

99
    if (!await installApp(device, package, userIdentifier: userIdentifier)) {
100
      throwToolExit('Install failed');
101
    }
102
  }
103
}
104

105 106 107
Future<bool> installApp(
  Device device,
  ApplicationPackage package, {
108
  String? userIdentifier,
109 110
  bool uninstall = true
}) async {
111
  try {
112
    if (uninstall && await device.isAppInstalled(package, userIdentifier: userIdentifier)) {
113
      globals.printStatus('Uninstalling old version...');
114
      if (!await device.uninstallApp(package, userIdentifier: userIdentifier)) {
115
        globals.printWarning('Warning: uninstalling old version failed');
116
      }
117
    }
118 119
  } on ProcessException catch (e) {
    globals.printError('Error accessing device ${device.id}:\n${e.message}');
120
  }
121

122
  return device.installApp(package, userIdentifier: userIdentifier);
123
}