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

import '../application_package.dart';
11
import '../base/common.dart';
12
import '../base/file_system.dart';
13
import '../build_info.dart';
14
import '../dart/package_map.dart';
15
import '../dart/pub.dart';
16
import '../device.dart';
17
import '../doctor.dart';
18
import '../flx.dart' as flx;
19
import '../globals.dart';
20
import '../usage.dart';
21 22
import 'flutter_command_runner.dart';

23
typedef void Validator();
24

25
abstract class FlutterCommand extends Command<Null> {
26
  FlutterCommand() {
27
    commandValidator = commonCommandValidator;
28 29
  }

30
  @override
31 32
  FlutterCommandRunner get runner => super.runner;

33 34 35 36
  /// Whether this command uses the 'target' option.
  bool _usesTargetOption = false;

  bool _usesPubOption = false;
37

38 39
  bool get shouldRunPub => _usesPubOption && argResults['pub'];

40 41
  BuildMode _defaultBuildMode;

42 43 44 45 46 47 48 49
  void usesTargetOption() {
    argParser.addOption('target',
      abbr: 't',
      defaultsTo: flx.defaultMainPath,
      help: 'Target app path / main entry-point file.');
    _usesTargetOption = true;
  }

50 51 52 53 54 55 56 57 58
  String get targetFile {
    if (argResults.wasParsed('target'))
      return argResults['target'];
    else if (argResults.rest.isNotEmpty)
      return argResults.rest.first;
    else
      return flx.defaultMainPath;
  }

59 60 61
  void usesPubOption() {
    argParser.addFlag('pub',
      defaultsTo: true,
62
      help: 'Whether to run "flutter packages get" before executing this command.');
63 64 65
    _usesPubOption = true;
  }

66
  void addBuildModeFlags({ bool defaultToRelease: true }) {
67
    defaultBuildMode = defaultToRelease ? BuildMode.release : BuildMode.debug;
68

69 70
    argParser.addFlag('debug',
      negatable: false,
71
      help: 'Build a debug version of your app${defaultToRelease ? '' : ' (default mode)'}.');
72 73
    argParser.addFlag('profile',
      negatable: false,
74
      help: 'Build a version of your app specialized for performance profiling.');
75
    argParser.addFlag('release',
76
      negatable: false,
77
      help: 'Build a release version of your app${defaultToRelease ? ' (default mode)' : ''}.');
78 79
  }

80 81 82 83
  set defaultBuildMode(BuildMode buildMode) {
    _defaultBuildMode = buildMode;
  }

84
  BuildMode getBuildMode() {
85
    final List<bool> modeFlags = <bool>[argResults['debug'], argResults['profile'], argResults['release']];
86
    if (modeFlags.where((bool flag) => flag).length > 1)
87 88 89
      throw new UsageException('Only one of --debug, --profile, or --release can be specified.', null);
    if (argResults['debug'])
      return BuildMode.debug;
90
    if (argResults['profile'])
91
      return BuildMode.profile;
92
    if (argResults['release'])
93 94
      return BuildMode.release;
    return _defaultBuildMode;
95 96
  }

97
  void setupApplicationPackages() {
98
    applicationPackages ??= new ApplicationPackageStore();
99 100
  }

101 102 103 104
  /// The path to send to Google Analytics. Return `null` here to disable
  /// tracking of the command.
  String get usagePath => name;

105 106 107 108 109 110
  /// Runs this command.
  ///
  /// Rather than overriding this method, subclasses should override
  /// [verifyThenRunCommand] to perform any verification
  /// and [runCommand] to execute the command
  /// so that this method can record and report the overall time to analytics.
111
  @override
112
  Future<Null> run() {
113 114
    final Stopwatch stopwatch = new Stopwatch()..start();
    final UsageTimer analyticsTimer = usagePath == null ? null : flutterUsage.startTimer(name);
Devon Carew's avatar
Devon Carew committed
115

116 117 118
    if (flutterUsage.isFirstRun)
      flutterUsage.printUsage();

119
    return verifyThenRunCommand().whenComplete(() {
120
      final int ms = stopwatch.elapsedMilliseconds;
121
      printTrace("'flutter $name' took ${ms}ms.");
122
      analyticsTimer?.finish();
Devon Carew's avatar
Devon Carew committed
123 124 125
    });
  }

126 127 128 129 130 131 132 133
  /// Perform validation then call [runCommand] to execute the command.
  /// Return a [Future] that completes with an exit code
  /// indicating whether execution was successful.
  ///
  /// Subclasses should override this method to perform verification
  /// then call this method to execute the command
  /// rather than calling [runCommand] directly.
  @mustCallSuper
134
  Future<Null> verifyThenRunCommand() async {
135 136 137 138
    // Populate the cache. We call this before pub get below so that the sky_engine
    // package is available in the flutter cache for pub to find.
    await cache.updateAll();

139 140
    if (shouldRunPub)
      await pubGet();
141

142
    setupApplicationPackages();
Devon Carew's avatar
Devon Carew committed
143

144
    final String commandPath = usagePath;
145 146 147
    if (commandPath != null)
      flutterUsage.sendCommand(usagePath);

148
    await runCommand();
149 150 151
  }

  /// Subclasses must implement this to execute the command.
152
  Future<Null> runCommand();
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

  /// Find and return the target [Device] based upon currently connected
  /// devices and criteria entered by the user on the command line.
  /// If a device cannot be found that meets specified criteria,
  /// then print an error message and return `null`.
  Future<Device> findTargetDevice({bool androidOnly: false}) async {
    if (!doctor.canLaunchAnything) {
      printError("Unable to locate a development device; please run 'flutter doctor' "
          "for information about installing additional components.");
      return null;
    }

    List<Device> devices = await deviceManager.getDevices();

    if (devices.isEmpty && deviceManager.hasSpecifiedDeviceId) {
      printStatus("No devices found with name or id "
          "matching '${deviceManager.specifiedDeviceId}'");
      return null;
    } else if (devices.isEmpty) {
      printNoConnectedDevices();
      return null;
    }

    devices = devices.where((Device device) => device.isSupported()).toList();

    if (androidOnly)
      devices = devices.where((Device device) => device.platform == TargetPlatform.android_arm).toList();

    if (devices.isEmpty) {
      printStatus('No supported devices connected.');
      return null;
    } else if (devices.length > 1) {
      if (deviceManager.hasSpecifiedDeviceId) {
        printStatus("Found ${devices.length} devices with name or id matching "
            "'${deviceManager.specifiedDeviceId}':");
      } else {
        printStatus("More than one device connected; please specify a device with "
            "the '-d <deviceId>' flag.");
        devices = await deviceManager.getAllConnectedDevices();
      }
      printStatus('');
      Device.printDevices(devices);
      return null;
    }
    return devices.single;
198 199
  }

200 201 202 203
  void printNoConnectedDevices() {
    printStatus('No connected devices.');
  }

204
  // This is a field so that you can modify the value for testing.
205 206
  Validator commandValidator;

207
  void commonCommandValidator() {
208 209
    if (!PackageMap.isUsingCustomPackagesPath) {
      // Don't expect a pubspec.yaml file if the user passed in an explicit .packages file path.
210
      if (!fs.isFileSync('pubspec.yaml')) {
211 212
        throw new ToolExit(
          'Error: No pubspec.yaml file found.\n'
213
          'This command should be run from the root of your Flutter project.\n'
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
          'Do not run this command from the root of your git clone of Flutter.'
        );
      }
      if (fs.isFileSync('flutter.yaml')) {
        throw new ToolExit(
          'Please merge your flutter.yaml into your pubspec.yaml.\n\n'
          'We have changed from having separate flutter.yaml and pubspec.yaml\n'
          'files to having just one pubspec.yaml file. Transitioning is simple:\n'
          'add a line that just says "flutter:" to your pubspec.yaml file, and\n'
          'move everything from your current flutter.yaml file into the\n'
          'pubspec.yaml file, below that line, with everything indented by two\n'
          'extra spaces compared to how it was in the flutter.yaml file. Then, if\n'
          'you had a "name:" line, move that to the top of your "pubspec.yaml"\n'
          'file (you may already have one there), so that there is only one\n'
          '"name:" line. Finally, delete the flutter.yaml file.\n\n'
          'For an example of what a new-style pubspec.yaml file might look like,\n'
          'check out the Flutter Gallery pubspec.yaml:\n'
          'https://github.com/flutter/flutter/blob/master/examples/flutter_gallery/pubspec.yaml\n'
        );
233
      }
234
    }
235 236

    if (_usesTargetOption) {
237
      final String targetPath = targetFile;
238
      if (!fs.isFileSync(targetPath))
239
        throw new ToolExit('Target file "$targetPath" not found.');
240 241
    }

242 243
    // Validate the current package map only if we will not be running "pub get" later.
    if (!(_usesPubOption && argResults['pub'])) {
244
      final String error = new PackageMap(PackageMap.globalPackagesPath).checkValid();
245 246
      if (error != null)
        throw new ToolExit(error);
247
    }
248
  }
249

250 251
  ApplicationPackageStore applicationPackages;
}