run.dart 8.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2016 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 'dart:io';

import 'application_package.dart';
import 'base/logger.dart';
import 'base/utils.dart';
11
import 'build_info.dart';
12 13 14 15 16
import 'commands/build_apk.dart';
import 'commands/install.dart';
import 'commands/trace.dart';
import 'device.dart';
import 'globals.dart';
17
import 'vmservice.dart';
18
import 'resident_runner.dart';
19

20
class RunAndStayResident extends ResidentRunner {
21
  RunAndStayResident(
22 23 24
    Device device, {
    String target,
    DebuggingOptions debuggingOptions,
25 26
    bool usesTerminalUI: true,
    this.traceStartup: false,
27 28
    this.benchmark: false,
    this.applicationBinary
29 30 31 32
  }) : super(device,
             target: target,
             debuggingOptions: debuggingOptions,
             usesTerminalUI: usesTerminalUI);
33 34 35 36

  ApplicationPackage _package;
  String _mainPath;
  LaunchResult _result;
37 38 39
  final bool traceStartup;
  final bool benchmark;
  final String applicationBinary;
40

41 42
  bool get prebuiltMode => applicationBinary != null;

43
  @override
44
  Future<int> run({
45
    Completer<DebugConnectionInfo> connectionInfoCompleter,
46 47 48
    String route,
    bool shouldBuild: true
  }) {
49 50
    // Don't let uncaught errors kill the process.
    return runZoned(() {
51
      assert(shouldBuild == !prebuiltMode);
52 53 54
      return _run(
        traceStartup: traceStartup,
        benchmark: benchmark,
55
        connectionInfoCompleter: connectionInfoCompleter,
56 57
        route: route,
        shouldBuild: shouldBuild
58 59 60 61 62 63
      );
    }, onError: (dynamic error, StackTrace stackTrace) {
      printError('Exception from flutter run: $error', stackTrace);
    });
  }

64
  @override
65
  Future<bool> restart({ bool fullRestart: false }) async {
66
    if (vmService == null) {
67 68 69 70 71
      printError('Debugging is not enabled.');
      return false;
    } else {
      Status status = logger.startProgress('Re-starting application...');

72
      Future<ServiceEvent> extensionAddedEvent;
73 74

      if (device.restartSendsFrameworkInitEvent) {
75 76
        extensionAddedEvent = vmService.onExtensionEvent
          .where((ServiceEvent event) => event.extensionKind == 'Flutter.FrameworkInitialization')
77 78
          .first;
      }
79 80 81 82 83

      bool restartResult = await device.restartApp(
        _package,
        _result,
        mainPath: _mainPath,
84 85
        observatory: vmService,
        prebuiltApplication: prebuiltMode
86 87 88 89
      );

      status.stop(showElapsedTime: true);

90
      if (restartResult && extensionAddedEvent != null) {
91 92 93 94 95 96 97 98 99 100 101
        // TODO(devoncarew): We should restore the route here.
        await extensionAddedEvent;
      }

      return restartResult;
    }
  }

  Future<int> _run({
    bool traceStartup: false,
    bool benchmark: false,
102
    Completer<DebugConnectionInfo> connectionInfoCompleter,
103 104
    String route,
    bool shouldBuild: true
105
  }) async {
106 107 108 109 110 111 112 113 114
    if (!prebuiltMode) {
      _mainPath = findMainDartFile(target);
      if (!FileSystemEntity.isFileSync(_mainPath)) {
        String message = 'Tried to run $_mainPath, but that file does not exist.';
        if (target == null)
          message += '\nConsider using the -t option to specify the Dart file to start.';
        printError(message);
        return 1;
      }
115 116
    }

117
    _package = getApplicationPackageForPlatform(device.platform, applicationBinary: applicationBinary);
118 119 120 121 122 123 124 125 126 127 128 129 130

    if (_package == null) {
      String message = 'No application found for ${device.platform}.';
      String hint = getMissingPackageHintForPlatform(device.platform);
      if (hint != null)
        message += '\n$hint';
      printError(message);
      return 1;
    }

    Stopwatch startTime = new Stopwatch()..start();

    // TODO(devoncarew): We shouldn't have to do type checks here.
131
    if (shouldBuild && device is AndroidDevice) {
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
      printTrace('Running build command.');

      int result = await buildApk(
        device.platform,
        target: target,
        buildMode: debuggingOptions.buildMode
      );

      if (result != 0)
        return result;
    }

    // TODO(devoncarew): Move this into the device.startApp() impls.
    if (_package != null) {
      printTrace("Stopping app '${_package.name}' on ${device.name}.");
147
      await device.stopApp(_package);
148 149 150
    }

    // TODO(devoncarew): This fails for ios devices - we haven't built yet.
151
    if (prebuiltMode || device is AndroidDevice) {
152
      printTrace('Running install command.');
153
      if (!(installApp(device, _package, uninstall: false)))
154 155 156 157 158 159 160
        return 1;
    }

    Map<String, dynamic> platformArgs;
    if (traceStartup != null)
      platformArgs = <String, dynamic>{ 'trace-startup': traceStartup };

161
    await startEchoingDeviceLog();
162 163 164 165 166 167
    if (_mainPath == null) {
      assert(prebuiltMode);
      printStatus('Running ${_package.displayName} on ${device.name}');
    } else {
      printStatus('Running ${getDisplayPath(_mainPath)} on ${device.name}...');
    }
168 169 170 171

    _result = await device.startApp(
      _package,
      debuggingOptions.buildMode,
172
      mainPath: _mainPath,
173
      debuggingOptions: debuggingOptions,
Devon Carew's avatar
Devon Carew committed
174
      platformArgs: platformArgs,
175 176
      route: route,
      prebuiltApplication: prebuiltMode
177 178 179 180
    );

    if (!_result.started) {
      printError('Error running application on ${device.name}.');
181
      await stopEchoingDeviceLog();
182 183 184 185 186
      return 2;
    }

    startTime.stop();

187 188
    if (connectionInfoCompleter != null && _result.hasObservatory)
      connectionInfoCompleter.complete(new DebugConnectionInfo(_result.observatoryPort));
189 190 191

    // Connect to observatory.
    if (debuggingOptions.debuggingEnabled) {
192
      await connectToServiceProtocol(_result.observatoryPort);
193

194 195 196
      if (benchmark) {
        await vmService.getVM();
      }
197 198 199
    }

    printStatus('Application running.');
200 201
    if (debuggingOptions.buildMode == BuildMode.release)
      return 0;
202

203 204
    if (vmService != null) {
      await vmService.vm.refreshViews();
205
      printStatus('Connected to ${vmService.vm.mainView}\.');
206
    }
207 208

    if (vmService != null && traceStartup) {
209
      printStatus('Downloading startup trace info...');
210
      try {
211
        await downloadStartupTrace(vmService);
212 213 214 215
      } catch(error) {
        printError(error);
        return 2;
      }
216
      appFinished();
217
    } else {
218 219
      setupTerminal();
      registerSignalHandlers();
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    }

    if (benchmark) {
      await new Future<Null>.delayed(new Duration(seconds: 4));

      // Touch the file.
      File mainFile = new File(_mainPath);
      mainFile.writeAsBytesSync(mainFile.readAsBytesSync());

      Stopwatch restartTime = new Stopwatch()..start();
      bool restarted = await restart();
      restartTime.stop();
      writeRunBenchmarkFile(startTime, restarted ? restartTime : null);
      await new Future<Null>.delayed(new Duration(seconds: 2));
      stop();
    }

237
    return waitForAppToFinish();
238 239
  }

240
  @override
241
  Future<Null> handleTerminalCommand(String code) async {
242 243 244 245
    String lower = code.toLowerCase();
    if (lower == 'r' || code == AnsiTerminal.KEY_F5) {
      if (device.supportsRestart) {
        // F5, restart
246
        await restart();
247
      }
248
    }
249 250
  }

251 252 253 254
  @override
  Future<Null> cleanupAfterSignal() async {
    await stopEchoingDeviceLog();
    await stopApp();
255 256
  }

257 258 259
  @override
  Future<Null> cleanupAtFinish() async {
    await stopEchoingDeviceLog();
260 261
  }

262 263
  @override
  void printHelp() {
264 265
    final bool showRestartText = !prebuiltMode && device.supportsRestart;
    String restartText = showRestartText ? ', "r" or F5 to restart the app,' : '';
266 267
    printStatus('Type "h" or F1 for help$restartText and "q", F10, or ctrl-c to quit.');
    printStatus('Type "w" to print the widget hierarchy of the app, and "t" for the render tree.');
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
  }
}

void writeRunBenchmarkFile(Stopwatch startTime, [Stopwatch restartTime]) {
  final String benchmarkOut = 'refresh_benchmark.json';
  Map<String, dynamic> data = <String, dynamic>{
    'start': startTime.elapsedMilliseconds,
    'time': (restartTime ?? startTime).elapsedMilliseconds // time and restart are the same
  };
  if (restartTime != null)
    data['restart'] = restartTime.elapsedMilliseconds;

  new File(benchmarkOut).writeAsStringSync(toPrettyJson(data));
  printStatus('Run benchmark written to $benchmarkOut ($data).');
}