install.dart 1.69 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 '../cache.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() {
    requiresPubspecYaml();
  }

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

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

25 26
  Device device;

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

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

40 41
    Cache.releaseLockEarly();

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

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

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

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

57
  if (uninstall && await device.isAppInstalled(package)) {
58
    globals.printStatus('Uninstalling old version...');
59
    if (!await device.uninstallApp(package)) {
60
      globals.printError('Warning: uninstalling old version failed');
61
    }
62
  }
63

64
  return device.installApp(package);
65
}