runner.dart 8.53 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7
// 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 9
import 'package:intl/intl.dart' as intl;
import 'package:intl/intl_standalone.dart' as intl_standalone;
10
import 'package:http/http.dart' as http;
11 12 13 14 15 16 17

import 'src/base/common.dart';
import 'src/base/context.dart';
import 'src/base/file_system.dart';
import 'src/base/io.dart';
import 'src/base/logger.dart';
import 'src/base/process.dart';
18
import 'src/context_runner.dart';
19
import 'src/doctor.dart';
20
import 'src/globals.dart' as globals;
21
import 'src/reporting/reporting.dart';
22 23 24 25 26 27 28
import 'src/runner/flutter_command.dart';
import 'src/runner/flutter_command_runner.dart';

/// Runs the Flutter tool with support for the specified list of [commands].
Future<int> run(
  List<String> args,
  List<FlutterCommand> commands, {
29 30 31 32 33 34 35
    bool muteCommandLogging = false,
    bool verbose = false,
    bool verboseHelp = false,
    bool reportCrashes,
    String flutterVersion,
    Map<Type, Generator> overrides,
  }) async {
36 37 38
  if (muteCommandLogging) {
    // Remove the verbose option; for help and doctor, users don't need to see
    // verbose logs.
39
    args = List<String>.of(args);
40 41 42
    args.removeWhere((String option) => option == '-v' || option == '--verbose');
  }

43
  final FlutterCommandRunner runner = FlutterCommandRunner(verboseHelp: verboseHelp);
44 45
  commands.forEach(runner.addCommand);

46
  return runInContext<int>(() async {
47 48
    reportCrashes ??= !await globals.isRunningOnBot;

49
    // Initialize the system locale.
50 51 52
    final String systemLocale = await intl_standalone.findSystemLocale();
    intl.Intl.defaultLocale = intl.Intl.verifiedLocale(
      systemLocale, intl.NumberFormat.localeExists,
53
      onFailure: (String _) => 'en_US',
54
    );
55

56
    String getVersion() => flutterVersion ?? globals.flutterVersion.getVersionString(redactUnknownBranches: true);
57 58
    Object firstError;
    StackTrace firstStackTrace;
59 60 61 62
    return await runZoned<Future<int>>(() async {
      try {
        await runner.run(args);
        return await _exit(0);
63 64
      // This catches all exceptions to send to crash logging, etc.
      } catch (error, stackTrace) {  // ignore: avoid_catches_without_on_clauses
65 66
        firstError = error;
        firstStackTrace = stackTrace;
67 68 69
        return await _handleToolError(
            error, stackTrace, verbose, args, reportCrashes, getVersion);
      }
70
    }, onError: (Object error, StackTrace stackTrace) async { // ignore: deprecated_member_use
71 72 73 74 75 76
      // If sending a crash report throws an error into the zone, we don't want
      // to re-try sending the crash report with *that* error. Rather, we want
      // to send the original error that triggered the crash report.
      final Object e = firstError ?? error;
      final StackTrace s = firstStackTrace ?? stackTrace;
      await _handleToolError(e, s, verbose, args, reportCrashes, getVersion);
77
    });
78
  }, overrides: overrides);
79 80 81
}

Future<int> _handleToolError(
82 83 84 85 86 87 88
  dynamic error,
  StackTrace stackTrace,
  bool verbose,
  List<String> args,
  bool reportCrashes,
  String getFlutterVersion(),
) async {
89
  if (error is UsageException) {
90 91
    globals.printError('${error.message}\n');
    globals.printError("Run 'flutter -h' (or 'flutter <command> -h') for available flutter commands and options.");
92 93 94
    // Argument error exit code.
    return _exit(64);
  } else if (error is ToolExit) {
95
    if (error.message != null) {
96
      globals.printError(error.message);
97 98
    }
    if (verbose) {
99
      globals.printError('\n$stackTrace\n');
100
    }
101 102 103 104 105 106 107 108 109 110 111
    return _exit(error.exitCode ?? 1);
  } else if (error is ProcessExit) {
    // We've caught an exit code.
    if (error.immediate) {
      exit(error.exitCode);
      return error.exitCode;
    } else {
      return _exit(error.exitCode);
    }
  } else {
    // We've crashed; emit a log report.
112
    globals.stdio.stderrWrite('\n');
113 114 115

    if (!reportCrashes) {
      // Print the stack trace on the bots - don't write a crash report.
116 117
      globals.stdio.stderrWrite('$error\n');
      globals.stdio.stderrWrite('$stackTrace\n');
118
      return _exit(1);
119
    }
120

121
    // Report to both [Usage] and [CrashReportSender].
122
    globals.flutterUsage.sendException(error);
123 124 125 126 127 128 129 130
    final CrashReportSender crashReportSender = CrashReportSender(
      client: http.Client(),
      usage: globals.flutterUsage,
      platform: globals.platform,
      logger: globals.logger,
      operatingSystemUtils: globals.os,
    );
    await crashReportSender.sendReport(
131 132 133 134 135
      error: error,
      stackTrace: stackTrace,
      getFlutterVersion: getFlutterVersion,
      command: args.join(' '),
    );
136

137
    globals.printError('Oops; flutter has exited unexpectedly: "$error".');
138 139

    try {
140 141 142 143 144 145 146 147
      final CrashDetails details = CrashDetails(
        command: _crashCommand(args),
        error: error,
        stackTrace: stackTrace,
        doctorText: await _doctorText(),
      );
      final File file = await _createLocalCrashReport(details);
      await globals.crashReporter.informUser(details, file);
148

149
      return _exit(1);
150 151
    // This catch catches all exceptions to ensure the message below is printed.
    } catch (error) { // ignore: avoid_catches_without_on_clauses
152
      globals.stdio.stderrWrite(
153
        'Unable to generate crash report due to secondary error: $error\n'
154 155
        '${globals.userMessages.flutterToolBugInstructions}\n');
      // Any exception thrown here (including one thrown by `_exit()`) will
156 157 158 159
      // get caught by our zone's `onError` handler. In order to avoid an
      // infinite error loop, we throw an error that is recognized above
      // and will trigger an immediate exit.
      throw ProcessExit(1, immediate: true);
160 161 162 163
    }
  }
}

164 165 166 167
String _crashCommand(List<String> args) => 'flutter ${args.join(' ')}';

String _crashException(dynamic error) => '${error.runtimeType}: $error';

168
/// Saves the crash report to a local file.
169
Future<File> _createLocalCrashReport(CrashDetails details) async {
170
  File crashFile = globals.fsUtils.getUniqueFile(
171
    globals.fs.currentDirectory,
172 173 174
    'flutter',
    'log',
  );
175

176
  final StringBuffer buffer = StringBuffer();
177

178 179
  buffer.writeln('Flutter crash report.');
  buffer.writeln('${globals.userMessages.flutterToolBugInstructions}\n');
180 181

  buffer.writeln('## command\n');
182
  buffer.writeln('${details.command}\n');
183 184

  buffer.writeln('## exception\n');
185 186
  buffer.writeln('${_crashException(details.error)}\n');
  buffer.writeln('```\n${details.stackTrace}```\n');
187 188

  buffer.writeln('## flutter doctor\n');
189
  buffer.writeln('```\n${details.doctorText}```');
190 191

  try {
192
    crashFile.writeAsStringSync(buffer.toString());
193 194
  } on FileSystemException catch (_) {
    // Fallback to the system temporary directory.
195
    crashFile = globals.fsUtils.getUniqueFile(
196
      globals.fs.systemTempDirectory,
197 198 199
      'flutter',
      'log',
    );
200
    try {
201
      crashFile.writeAsStringSync(buffer.toString());
202
    } on FileSystemException catch (e) {
203 204
      globals.printError('Could not write crash report to disk: $e');
      globals.printError(buffer.toString());
205 206 207 208 209 210 211 212
    }
  }

  return crashFile;
}

Future<String> _doctorText() async {
  try {
213
    final BufferLogger logger = BufferLogger(
214
      terminal: globals.terminal,
215
      outputPreferences: globals.outputPreferences,
216
    );
217

218 219
    final Doctor doctor = Doctor(logger: logger);
    await doctor.diagnose(verbose: true, showColor: false);
220 221

    return logger.statusText;
222
  } on Exception catch (error, trace) {
223 224 225 226 227
    return 'encountered exception: $error\n\n${trace.toString().trim()}\n';
  }
}

Future<int> _exit(int code) async {
228
  // Prints the welcome message if needed.
229
  globals.flutterUsage.printWelcome();
230 231 232

  // Send any last analytics calls that are in progress without overly delaying
  // the tool's exit (we wait a maximum of 250ms).
233
  if (globals.flutterUsage.enabled) {
234
    final Stopwatch stopwatch = Stopwatch()..start();
235
    await globals.flutterUsage.ensureAnalyticsSent();
236
    globals.printTrace('ensureAnalyticsSent: ${stopwatch.elapsedMilliseconds}ms');
237 238 239
  }

  // Run shutdown hooks before flushing logs
240
  await shutdownHooks.runShutdownHooks();
241

242
  final Completer<void> completer = Completer<void>();
243 244 245 246

  // Give the task / timer queue one cycle through before we hard exit.
  Timer.run(() {
    try {
247
      globals.printTrace('exiting with code $code');
248 249
      exit(code);
      completer.complete();
250 251 252
    // This catches all exceptions becauce the error is propagated on the
    // completer.
    } catch (error, stackTrace) { // ignore: avoid_catches_without_on_clauses
253 254 255 256 257 258 259
      completer.completeError(error, stackTrace);
    }
  });

  await completer.future;
  return code;
}