install.dart 1.54 KB
Newer Older
1 2 3 4 5 6
// Copyright 2015 The Chromium Authors. All rights reserved.
// 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';
12
import '../runner/flutter_command.dart';
13

14
class InstallCommand extends FlutterCommand {
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 29
  Future<Null> validateCommand() async {
    await super.validateCommand();
30 31
    device = await findTargetDevice();
    if (device == null)
32
      throwToolExit('No target device found');
33
  }
34 35

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

39 40
    Cache.releaseLockEarly();

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

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

48
Future<bool> installApp(Device device, ApplicationPackage package, { bool uninstall: true }) async {
49 50
  if (package == null)
    return false;
51

52
  if (uninstall && await device.isAppInstalled(package)) {
53
    printStatus('Uninstalling old version...');
54
    if (!await device.uninstallApp(package))
55 56
      printError('Warning: uninstalling old version failed');
  }
57

58
  return device.installApp(package);
59
}