start.dart 2.42 KB
Newer Older
1 2 3 4 5 6 7 8
// 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';

import 'package:logging/logging.dart';
import 'package:path/path.dart' as path;
9 10 11

import '../application_package.dart';
import '../device.dart';
12
import 'flutter_command.dart';
13 14
import 'install.dart';
import 'stop.dart';
15 16 17

final Logger _logging = new Logger('sky_tools.start');

18 19 20
class StartCommand extends FlutterCommand {
  final String name = 'start';
  final String description = 'Start your Flutter app on attached devices.';
21

22
  StartCommand() {
23 24 25 26 27 28 29 30 31 32 33
    argParser.addFlag('poke',
        negatable: false,
        help: 'Restart the connection to the server (Android only).');
    argParser.addFlag('checked',
        negatable: true,
        defaultsTo: true,
        help: 'Toggle Dart\'s checked mode.');
    argParser.addOption('target',
        defaultsTo: '.',
        abbr: 't',
        help: 'Target app path or filename to start.');
34 35
    argParser.addFlag('boot',
        help: 'Boot the iOS Simulator if it isn\'t already running.');
36 37 38
  }

  @override
39
  Future<int> runInProject() async {
40
    await downloadApplicationPackagesAndConnectToDevices();
41

42 43
    bool poke = argResults['poke'];
    if (!poke) {
44 45
      StopCommand stopper = new StopCommand();
      stopper.inheritFromParent(this);
46 47 48
      stopper.stop();

      // Only install if the user did not specify a poke
49 50 51
      InstallCommand installer = new InstallCommand();
      installer.inheritFromParent(this);
      installer.install(boot: argResults['boot']);
52 53
    }

54
    bool startedSomething = false;
55

56 57 58 59 60 61 62 63 64 65 66 67
    for (Device device in devices.all) {
      ApplicationPackage package = applicationPackages.getPackageForPlatform(device.platform);
      if (package == null || !device.isConnected())
        continue;
      if (device is AndroidDevice) {
        String target = path.absolute(argResults['target']);
        if (await device.startServer(target, poke, argResults['checked'], package))
          startedSomething = true;
      } else {
        if (await device.startApp(package))
          startedSomething = true;
      }
68 69
    }

70 71 72 73 74 75 76 77
    if (!startedSomething) {
      if (!devices.all.any((device) => device.isConnected())) {
        _logging.severe('Unable to run application - no connected devices.');
      } else {
        _logging.severe('Unable to run application.');
      }
    }

78
    return startedSomething ? 0 : 2;
79 80
  }
}