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

import 'dart:async';

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

15
class InstallCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts {
16 17 18 19
  InstallCommand() {
    requiresPubspecYaml();
  }

20
  @override
21
  final String name = 'install';
22 23

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

26 27
  Device device;

28
  @override
29
  Future<void> validateCommand() async {
30
    await super.validateCommand();
31
    device = await findTargetDevice();
32
    if (device == null) {
33
      throwToolExit('No target device found');
34
    }
35
  }
36 37

  @override
38
  Future<FlutterCommandResult> runCommand() async {
39
    final ApplicationPackage package = await applicationPackages.getPackageForPlatform(await device.targetPlatform);
40

41 42
    Cache.releaseLockEarly();

43
    globals.printStatus('Installing $package to $device...');
44

45
    if (!await installApp(device, package)) {
46
      throwToolExit('Install failed');
47
    }
48

49
    return FlutterCommandResult.success();
50
  }
51
}
52

53
Future<bool> installApp(Device device, ApplicationPackage package, { bool uninstall = true }) async {
54
  if (package == null) {
55
    return false;
56
  }
57

58 59 60 61 62 63
  try {
    if (uninstall && await device.isAppInstalled(package)) {
      globals.printStatus('Uninstalling old version...');
      if (!await device.uninstallApp(package)) {
        globals.printError('Warning: uninstalling old version failed');
      }
64
    }
65 66
  } on ProcessException catch (e) {
    globals.printError('Error accessing device ${device.id}:\n${e.message}');
67
  }
68

69
  return device.installApp(package);
70
}