flutter_command.dart 2.12 KB
Newer Older
1 2 3 4 5
// 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';
6
import 'dart:io';
7 8 9 10

import 'package:args/command_runner.dart';

import '../application_package.dart';
Ian Hickson's avatar
Ian Hickson committed
11
import '../build_configuration.dart';
12
import '../device.dart';
13
import '../toolchain.dart';
14 15 16 17 18
import 'flutter_command_runner.dart';

abstract class FlutterCommand extends Command {
  FlutterCommandRunner get runner => super.runner;

19 20 21
  /// Whether this command needs to be run from the root of a project.
  bool get requiresProjectRoot => true;

22 23 24 25 26 27
  String get projectRootValidationErrorMessage {
    return 'Error: No pubspec.yaml file found.\n'
      'This command should be run from the root of your Flutter project. Do not run\n'
      'this command from the root of your git clone of Flutter.';
  }

Ian Hickson's avatar
Ian Hickson committed
28 29
  List<BuildConfiguration> get buildConfigurations => runner.buildConfigurations;

30 31
  Future downloadApplicationPackages() async {
    if (applicationPackages == null)
Ian Hickson's avatar
Ian Hickson committed
32
      applicationPackages = await ApplicationPackageStore.forConfigs(buildConfigurations);
33 34
  }

35 36
  Future downloadToolchain() async {
    if (toolchain == null)
Ian Hickson's avatar
Ian Hickson committed
37
      toolchain = await Toolchain.forConfigs(buildConfigurations);
38 39
  }

40 41
  void connectToDevices() {
    if (devices == null)
Ian Hickson's avatar
Ian Hickson committed
42
      devices = new DeviceStore.forConfigs(buildConfigurations);
43 44 45 46 47 48 49 50 51
  }

  Future downloadApplicationPackagesAndConnectToDevices() async {
    await downloadApplicationPackages();
    connectToDevices();
  }

  void inheritFromParent(FlutterCommand other) {
    applicationPackages = other.applicationPackages;
52
    toolchain = other.toolchain;
53 54 55
    devices = other.devices;
  }

56
  Future<int> run() async {
57 58
    if (requiresProjectRoot && !validateProjectRoot())
      return 1;
Hixie's avatar
Hixie committed
59
    return await runInProject();
60 61
  }

62 63 64 65 66 67 68 69
  bool validateProjectRoot() {
    if (!FileSystemEntity.isFileSync('pubspec.yaml')) {
      stderr.writeln(projectRootValidationErrorMessage);
      return false;
    }
    return true;
  }

70 71
  Future<int> runInProject();

72
  ApplicationPackageStore applicationPackages;
73
  Toolchain toolchain;
74 75
  DeviceStore devices;
}