"Fossies" - the Fresh Open Source Software Archive

Member "flutter-1.22.4/packages/flutter_tools/lib/runner.dart" (13 Nov 2020, 9293 Bytes) of package /linux/misc/flutter-1.22.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2014 The Flutter Authors. All rights reserved.
    2 // Use of this source code is governed by a BSD-style license that can be
    3 // found in the LICENSE file.
    4 
    5 import 'dart:async';
    6 
    7 import 'package:args/command_runner.dart';
    8 import 'package:intl/intl.dart' as intl;
    9 import 'package:intl/intl_standalone.dart' as intl_standalone;
   10 import 'package:http/http.dart' as http;
   11 
   12 import 'src/base/common.dart';
   13 import 'src/base/context.dart';
   14 import 'src/base/file_system.dart';
   15 import 'src/base/io.dart';
   16 import 'src/base/logger.dart';
   17 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 import 'src/runner/flutter_command.dart';
   23 import 'src/runner/flutter_command_runner.dart';
   24 
   25 /// Runs the Flutter tool with support for the specified list of [commands].
   26 ///
   27 /// [commands] must be either `List<FlutterCommand>` or `List<FlutterCommand> Function()`.
   28 // TODO(jonahwilliams): update command type once g3 has rolled.
   29 Future<int> run(
   30   List<String> args,
   31   List<FlutterCommand> Function() commands, {
   32     bool muteCommandLogging = false,
   33     bool verbose = false,
   34     bool verboseHelp = false,
   35     bool reportCrashes,
   36     String flutterVersion,
   37     Map<Type, Generator> overrides,
   38   }) async {
   39   if (muteCommandLogging) {
   40     // Remove the verbose option; for help and doctor, users don't need to see
   41     // verbose logs.
   42     args = List<String>.of(args);
   43     args.removeWhere((String option) => option == '-v' || option == '--verbose');
   44   }
   45 
   46   return runInContext<int>(() async {
   47     reportCrashes ??= !await globals.isRunningOnBot;
   48     final FlutterCommandRunner runner = FlutterCommandRunner(verboseHelp: verboseHelp);
   49     commands().forEach(runner.addCommand);
   50 
   51     // Initialize the system locale.
   52     final String systemLocale = await intl_standalone.findSystemLocale();
   53     intl.Intl.defaultLocale = intl.Intl.verifiedLocale(
   54       systemLocale, intl.NumberFormat.localeExists,
   55       onFailure: (String _) => 'en_US',
   56     );
   57 
   58     String getVersion() => flutterVersion ?? globals.flutterVersion.getVersionString(redactUnknownBranches: true);
   59     Object firstError;
   60     StackTrace firstStackTrace;
   61     return await runZoned<Future<int>>(() async {
   62       try {
   63         await runner.run(args);
   64 
   65         // Triggering [runZoned]'s error callback does not necessarily mean that
   66         // we stopped executing the body.  See https://github.com/dart-lang/sdk/issues/42150.
   67         if (firstError == null) {
   68           return await _exit(0);
   69         }
   70 
   71         // We already hit some error, so don't return success.  The error path
   72         // (which should be in progress) is responsible for calling _exit().
   73         return 1;
   74       // This catches all exceptions to send to crash logging, etc.
   75       } catch (error, stackTrace) {  // ignore: avoid_catches_without_on_clauses
   76         firstError = error;
   77         firstStackTrace = stackTrace;
   78         return await _handleToolError(
   79             error, stackTrace, verbose, args, reportCrashes, getVersion);
   80       }
   81     }, onError: (Object error, StackTrace stackTrace) async { // ignore: deprecated_member_use
   82       // If sending a crash report throws an error into the zone, we don't want
   83       // to re-try sending the crash report with *that* error. Rather, we want
   84       // to send the original error that triggered the crash report.
   85       firstError ??= error;
   86       firstStackTrace ??= stackTrace;
   87       await _handleToolError(firstError, firstStackTrace, verbose, args, reportCrashes, getVersion);
   88     });
   89   }, overrides: overrides);
   90 }
   91 
   92 Future<int> _handleToolError(
   93   dynamic error,
   94   StackTrace stackTrace,
   95   bool verbose,
   96   List<String> args,
   97   bool reportCrashes,
   98   String getFlutterVersion(),
   99 ) async {
  100   if (error is UsageException) {
  101     globals.printError('${error.message}\n');
  102     globals.printError("Run 'flutter -h' (or 'flutter <command> -h') for available flutter commands and options.");
  103     // Argument error exit code.
  104     return _exit(64);
  105   } else if (error is ToolExit) {
  106     if (error.message != null) {
  107       globals.printError(error.message);
  108     }
  109     if (verbose) {
  110       globals.printError('\n$stackTrace\n');
  111     }
  112     return _exit(error.exitCode ?? 1);
  113   } else if (error is ProcessExit) {
  114     // We've caught an exit code.
  115     if (error.immediate) {
  116       exit(error.exitCode);
  117       return error.exitCode;
  118     } else {
  119       return _exit(error.exitCode);
  120     }
  121   } else {
  122     // We've crashed; emit a log report.
  123     globals.stdio.stderrWrite('\n');
  124 
  125     if (!reportCrashes) {
  126       // Print the stack trace on the bots - don't write a crash report.
  127       globals.stdio.stderrWrite('$error\n');
  128       globals.stdio.stderrWrite('$stackTrace\n');
  129       return _exit(1);
  130     }
  131 
  132     // Report to both [Usage] and [CrashReportSender].
  133     globals.flutterUsage.sendException(error);
  134     final CrashReportSender crashReportSender = CrashReportSender(
  135       client: http.Client(),
  136       usage: globals.flutterUsage,
  137       platform: globals.platform,
  138       logger: globals.logger,
  139       operatingSystemUtils: globals.os,
  140     );
  141     await crashReportSender.sendReport(
  142       error: error,
  143       stackTrace: stackTrace,
  144       getFlutterVersion: getFlutterVersion,
  145       command: args.join(' '),
  146     );
  147 
  148     globals.printError('Oops; flutter has exited unexpectedly: "$error".');
  149 
  150     try {
  151       final CrashDetails details = CrashDetails(
  152         command: _crashCommand(args),
  153         error: error,
  154         stackTrace: stackTrace,
  155         doctorText: await _doctorText(),
  156       );
  157       final File file = await _createLocalCrashReport(details);
  158       await globals.crashReporter.informUser(details, file);
  159 
  160       return _exit(1);
  161     // This catch catches all exceptions to ensure the message below is printed.
  162     } catch (error) { // ignore: avoid_catches_without_on_clauses
  163       globals.stdio.stderrWrite(
  164         'Unable to generate crash report due to secondary error: $error\n'
  165         '${globals.userMessages.flutterToolBugInstructions}\n');
  166       // Any exception thrown here (including one thrown by `_exit()`) will
  167       // get caught by our zone's `onError` handler. In order to avoid an
  168       // infinite error loop, we throw an error that is recognized above
  169       // and will trigger an immediate exit.
  170       throw ProcessExit(1, immediate: true);
  171     }
  172   }
  173 }
  174 
  175 String _crashCommand(List<String> args) => 'flutter ${args.join(' ')}';
  176 
  177 String _crashException(dynamic error) => '${error.runtimeType}: $error';
  178 
  179 /// Saves the crash report to a local file.
  180 Future<File> _createLocalCrashReport(CrashDetails details) async {
  181   File crashFile = globals.fsUtils.getUniqueFile(
  182     globals.fs.currentDirectory,
  183     'flutter',
  184     'log',
  185   );
  186 
  187   final StringBuffer buffer = StringBuffer();
  188 
  189   buffer.writeln('Flutter crash report.');
  190   buffer.writeln('${globals.userMessages.flutterToolBugInstructions}\n');
  191 
  192   buffer.writeln('## command\n');
  193   buffer.writeln('${details.command}\n');
  194 
  195   buffer.writeln('## exception\n');
  196   buffer.writeln('${_crashException(details.error)}\n');
  197   buffer.writeln('```\n${details.stackTrace}```\n');
  198 
  199   buffer.writeln('## flutter doctor\n');
  200   buffer.writeln('```\n${details.doctorText}```');
  201 
  202   try {
  203     crashFile.writeAsStringSync(buffer.toString());
  204   } on FileSystemException catch (_) {
  205     // Fallback to the system temporary directory.
  206     crashFile = globals.fsUtils.getUniqueFile(
  207       globals.fs.systemTempDirectory,
  208       'flutter',
  209       'log',
  210     );
  211     try {
  212       crashFile.writeAsStringSync(buffer.toString());
  213     } on FileSystemException catch (e) {
  214       globals.printError('Could not write crash report to disk: $e');
  215       globals.printError(buffer.toString());
  216     }
  217   }
  218 
  219   return crashFile;
  220 }
  221 
  222 Future<String> _doctorText() async {
  223   try {
  224     final BufferLogger logger = BufferLogger(
  225       terminal: globals.terminal,
  226       outputPreferences: globals.outputPreferences,
  227     );
  228 
  229     final Doctor doctor = Doctor(logger: logger);
  230     await doctor.diagnose(verbose: true, showColor: false);
  231 
  232     return logger.statusText;
  233   } on Exception catch (error, trace) {
  234     return 'encountered exception: $error\n\n${trace.toString().trim()}\n';
  235   }
  236 }
  237 
  238 Future<int> _exit(int code) async {
  239   // Prints the welcome message if needed.
  240   globals.flutterUsage.printWelcome();
  241 
  242   // Send any last analytics calls that are in progress without overly delaying
  243   // the tool's exit (we wait a maximum of 250ms).
  244   if (globals.flutterUsage.enabled) {
  245     final Stopwatch stopwatch = Stopwatch()..start();
  246     await globals.flutterUsage.ensureAnalyticsSent();
  247     globals.printTrace('ensureAnalyticsSent: ${stopwatch.elapsedMilliseconds}ms');
  248   }
  249 
  250   // Run shutdown hooks before flushing logs
  251   await shutdownHooks.runShutdownHooks();
  252 
  253   final Completer<void> completer = Completer<void>();
  254 
  255   // Give the task / timer queue one cycle through before we hard exit.
  256   Timer.run(() {
  257     try {
  258       globals.printTrace('exiting with code $code');
  259       exit(code);
  260       completer.complete();
  261     // This catches all exceptions becauce the error is propagated on the
  262     // completer.
  263     } catch (error, stackTrace) { // ignore: avoid_catches_without_on_clauses
  264       completer.completeError(error, stackTrace);
  265     }
  266   });
  267 
  268   await completer.future;
  269   return code;
  270 }