run_cold.dart 6.6 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7 8
import 'dart:async';

9
import 'package:meta/meta.dart';
10

11
import 'base/file_system.dart';
12
import 'base/logger.dart';
13
import 'build_info.dart';
14
import 'device.dart';
15
import 'globals.dart' as globals;
16
import 'resident_devtools_handler.dart';
17
import 'resident_runner.dart';
18
import 'tracing.dart';
19
import 'vmservice.dart';
20

21
const String kFlutterTestOutputsDirEnvName = 'FLUTTER_TEST_OUTPUTS_DIR';
22 23
class ColdRunner extends ResidentRunner {
  ColdRunner(
24
    List<FlutterDevice> devices, {
25
    @required String target,
26
    @required DebuggingOptions debuggingOptions,
27
    this.traceStartup = false,
28
    this.awaitFirstFrameWhenTracing = true,
29
    this.applicationBinary,
30
    this.multidexEnabled = false,
31
    bool ipv6 = false,
32
    bool stayResident = true,
33
    bool machine = false,
34
    ResidentDevtoolsHandlerFactory devtoolsHandler = createDefaultHandler,
35 36 37 38 39 40 41 42
  }) : super(
          devices,
          target: target,
          debuggingOptions: debuggingOptions,
          hotMode: false,
          stayResident: stayResident,
          ipv6: ipv6,
          machine: machine,
43
          devtoolsHandler: devtoolsHandler,
44
        );
45

46
  final bool traceStartup;
47
  final bool awaitFirstFrameWhenTracing;
48
  final File applicationBinary;
49
  final bool multidexEnabled;
50
  bool _didAttach = false;
51

52 53 54 55
  @override
  bool get canHotReload => false;

  @override
56 57 58 59
  Logger get logger => globals.logger;

  @override
  FileSystem get fileSystem => globals.fs;
60

61
  @override
62
  Future<int> run({
63
    Completer<DebugConnectionInfo> connectionInfoCompleter,
64
    Completer<void> appStartedCompleter,
65
    bool enableDevTools = false,
66
    String route,
67
  }) async {
68 69 70 71 72 73 74 75 76 77
    try {
      for (final FlutterDevice device in flutterDevices) {
        final int result = await device.runCold(
          coldRunner: this,
          route: route,
        );
        if (result != 0) {
          appFailedToStart();
          return result;
        }
78
      }
79 80
    } on Exception catch (err, stack) {
      globals.printError('$err\n$stack');
81 82
      appFailedToStart();
      return 1;
83 84
    }

85
    // Connect to observatory.
86
    if (debuggingEnabled) {
87
      try {
88
        await connectToServiceProtocol(allowExistingDdsInstance: false);
89 90
      } on Exception catch (exception) {
        globals.printError(exception.toString());
91
        appFailedToStart();
92 93 94
        return 2;
      }
    }
95

96 97 98 99 100 101 102 103
    if (enableDevTools && debuggingEnabled) {
      // The method below is guaranteed never to return a failing future.
      unawaited(residentDevtoolsHandler.serveAndAnnounceDevTools(
        devToolsServerAddress: debuggingOptions.devToolsServerAddress,
        flutterDevices: flutterDevices,
      ));
    }

104 105
    if (flutterDevices.first.observatoryUris != null) {
      // For now, only support one debugger connection.
106
      connectionInfoCompleter?.complete(DebugConnectionInfo(
107 108
        httpUri: flutterDevices.first.vmService.httpAddress,
        wsUri: flutterDevices.first.vmService.wsAddress,
109 110
      ));
    }
111

112
    globals.printTrace('Application running.');
113

114
    for (final FlutterDevice device in flutterDevices) {
115
      if (device.vmService == null) {
116
        continue;
117
      }
118
      await device.initLogReader();
119
      globals.printTrace('Connected to ${device.device.name}');
120
    }
121

122 123 124
    if (traceStartup) {
      // Only trace startup for the first device.
      final FlutterDevice device = flutterDevices.first;
125
      if (device.vmService != null) {
126
        globals.printStatus('Tracing startup on ${device.device.name}.');
127
        final String outputPath = globals.platform.environment[kFlutterTestOutputsDirEnvName] ?? getBuildDirectory();
128
        await downloadStartupTrace(
129
          device.vmService,
130
          awaitFirstFrame: awaitFirstFrameWhenTracing,
131
          logger: globals.logger,
132
          output: globals.fs.directory(outputPath),
133
        );
134
      }
135
      appFinished();
136 137
    }

138 139
    appStartedCompleter?.complete();

140
    writeVmServiceFile();
141

142
    if (stayResident && !traceStartup) {
143
      return waitForAppToFinish();
144
    }
145 146
    await cleanupAtFinish();
    return 0;
147 148
  }

149 150 151 152
  @override
  Future<int> attach({
    Completer<DebugConnectionInfo> connectionInfoCompleter,
    Completer<void> appStartedCompleter,
153
    bool allowExistingDdsInstance = false,
154
    bool enableDevTools = false,
155 156 157
  }) async {
    _didAttach = true;
    try {
158 159 160 161
      await connectToServiceProtocol(
        getSkSLMethod: writeSkSL,
        allowExistingDdsInstance: allowExistingDdsInstance,
      );
162
    } on Exception catch (error) {
163
      globals.printError('Error connecting to the service protocol: $error');
164 165
      return 2;
    }
166

167
    for (final FlutterDevice device in flutterDevices) {
168
      await device.initLogReader();
169
    }
170
    for (final FlutterDevice device in flutterDevices) {
171 172
      final List<FlutterView> views = await device.vmService.getFlutterViews();
      for (final FlutterView view in views) {
173
        globals.printTrace('Connected to $view.');
174 175
      }
    }
176

177 178 179 180 181 182 183
    if (enableDevTools && debuggingEnabled) {
      // The method below is guaranteed never to return a failing future.
      unawaited(residentDevtoolsHandler.serveAndAnnounceDevTools(
        devToolsServerAddress: debuggingOptions.devToolsServerAddress,
        flutterDevices: flutterDevices,
      ));
    }
184

185 186 187 188 189 190 191 192
    appStartedCompleter?.complete();
    if (stayResident) {
      return waitForAppToFinish();
    }
    await cleanupAtFinish();
    return 0;
  }

193
  @override
194
  Future<void> cleanupAfterSignal() async {
195
    await stopEchoingDeviceLog();
196 197 198
    if (_didAttach) {
      appFinished();
    }
199
    await exitApp();
200 201
  }

202
  @override
203
  Future<void> cleanupAtFinish() async {
204
    for (final FlutterDevice flutterDevice in flutterDevices) {
205
      await flutterDevice.device.dispose();
206 207
    }

208
    await residentDevtoolsHandler.shutdown();
209
    await stopEchoingDeviceLog();
210 211
  }

212
  @override
213
  void printHelp({ @required bool details }) {
214
    globals.printStatus('Flutter run key commands.');
215 216
    if (details) {
      printHelpDetails();
217 218 219
      commandHelp.hWithDetails.print();
    } else {
      commandHelp.hWithoutDetails.print();
220
    }
221
    if (_didAttach) {
222
      commandHelp.d.print();
223
    }
224
    commandHelp.c.print();
225
    commandHelp.q.print();
226
    printDebuggerList();
227
  }
228 229

  @override
230
  Future<void> preExit() async {
231
    for (final FlutterDevice device in flutterDevices) {
232
      // If we're running in release mode, stop the app using the device logic.
233
      if (device.vmService == null) {
234
        await device.device.stopApp(device.package, userIdentifier: device.userIdentifier);
235
      }
236
    }
237
    await super.preExit();
238
  }
239
}