1// Copyright 2015 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import 'dart:async'; 6 7import '../application_package.dart'; 8import '../base/common.dart'; 9import '../cache.dart'; 10import '../device.dart'; 11import '../globals.dart'; 12import '../runner/flutter_command.dart'; 13 14class InstallCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts { 15 InstallCommand() { 16 requiresPubspecYaml(); 17 } 18 19 @override 20 final String name = 'install'; 21 22 @override 23 final String description = 'Install a Flutter app on an attached device.'; 24 25 Device device; 26 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 @override 36 Future<FlutterCommandResult> runCommand() async { 37 final ApplicationPackage package = await applicationPackages.getPackageForPlatform(await device.targetPlatform); 38 39 Cache.releaseLockEarly(); 40 41 printStatus('Installing $package to $device...'); 42 43 if (!await installApp(device, package)) 44 throwToolExit('Install failed'); 45 46 return null; 47 } 48} 49 50Future<bool> installApp(Device device, ApplicationPackage package, { bool uninstall = true }) async { 51 if (package == null) 52 return false; 53 54 if (uninstall && await device.isAppInstalled(package)) { 55 printStatus('Uninstalling old version...'); 56 if (!await device.uninstallApp(package)) 57 printError('Warning: uninstalling old version failed'); 58 } 59 60 return device.installApp(package); 61} 62