Chromium Code Reviews| Index: bin/dartdevc.dart |
| diff --git a/bin/dartdevc.dart b/bin/dartdevc.dart |
| index 0ee480ac83faca90afbbc6f5ab5cc0e827c6ac7f..e9677a996964935aa8d5b2ea73a012e06951f706 100755 |
| --- a/bin/dartdevc.dart |
| +++ b/bin/dartdevc.dart |
| @@ -37,30 +37,104 @@ |
| import 'dart:io'; |
| import 'package:args/command_runner.dart'; |
| +import 'package:bazel_worker/bazel_worker.dart'; |
| import 'package:dev_compiler/src/compiler/command.dart'; |
| main(List<String> args) async { |
| + // Always returns a new modifiable list. |
| args = _preprocessArgs(args); |
| - var runner = new CommandRunner('dartdevc', 'Dart Development Compiler'); |
| - runner.addCommand(new CompileCommand()); |
| + if (args.contains('--persistent_worker')) { |
| + new _CompilerWorker(args..remove('--persistent_worker')).run(); |
| + } else { |
| + exitCode = _runCommand(args); |
| + } |
| +} |
| + |
| +/// Runs a single compile command, and returns an exit code. |
| +int _runCommand(List<String> args, {MessageHandler messageHandler}) { |
| try { |
| - await runner.run(args); |
| + var runner = new CommandRunner('dartdevc', 'Dart Development Compiler'); |
| + runner.addCommand(new CompileCommand(messageHandler: messageHandler)); |
| + runner.run(args); |
| } catch (e, s) { |
| - exit(handleError(e, s, args)); |
| + return _handleError(e, s, args, messageHandler: messageHandler); |
| } |
| + return EXIT_CODE_OK; |
| } |
| +/// Runs the compiler worker loop. |
| +class _CompilerWorker extends SyncWorkerLoop { |
| + /// The original args supplied to the executable. |
| + final List<String> _startupArgs; |
| + |
| + _CompilerWorker(this._startupArgs) : super(); |
| + |
| + /// Performs each individual work request. |
| + WorkResponse performRequest(WorkRequest request) { |
| + var args = new List.from(_startupArgs)..addAll(request.arguments); |
| + |
| + var output = new StringBuffer(); |
| + return new WorkResponse() |
| + ..exitCode = _runCommand(args, messageHandler: output.writeln) |
| + ..output = output.toString(); |
| + } |
| +} |
| + |
| +/// Handles [error] in a uniform fashion. Returns the proper exit code and calls |
| +/// [messageHandler] with messages. |
| +int _handleError(dynamic error, dynamic stackTrace, List<String> args, |
| + {MessageHandler messageHandler}) { |
| + messageHandler ??= print; |
| + |
| + if (error is UsageException) { |
| + // Incorrect usage, input file not found, etc. |
| + messageHandler(error); |
| + return 64; |
| + } else if (error is CompileErrorException) { |
| + // Code has error(s) and failed to compile. |
| + messageHandler(error); |
| + return 1; |
| + } else { |
| + // Anything else is likely a compiler bug. |
| + // |
| + // --unsafe-force-compile is a bit of a grey area, but it's nice not to |
| + // crash while compiling |
| + // (of course, output code may crash, if it had errors). |
| + // |
| + messageHandler(""); |
| + messageHandler("We're sorry, you've found a bug in our compiler."); |
| + messageHandler("You can report this bug at:"); |
| + messageHandler(" https://github.com/dart-lang/dev_compiler/issues"); |
| + messageHandler(""); |
| + messageHandler( |
| + "Please include the information below in your report, along with"); |
| + messageHandler( |
| + "any other information that may help us track it down. Thanks!"); |
| + messageHandler(""); |
| + messageHandler(" dartdevc arguments: " + args.join(' ')); |
| + messageHandler(" dart --version: ${Platform.version}"); |
| + messageHandler(""); |
| + messageHandler("```"); |
| + messageHandler(error); |
| + messageHandler(stackTrace); |
| + messageHandler("```"); |
| + return 1; |
|
Jennifer Messerly
2016/04/18 18:08:41
BTW, this was probably a bug in my original code,
jakemac
2016/04/18 18:14:26
Done.
|
| + } |
| +} |
| + |
| +/// Always returns a new modifiable list. |
| +/// |
| /// If the final arg is `@file_path` then read in all the lines of that file |
| /// and add those as args. |
| /// |
| /// Bazel actions that support workers must provide all their per-WorkRequest |
| /// arguments in a file like this instead of as normal args. |
| List<String> _preprocessArgs(List<String> args) { |
| + args = new List.from(args); |
| if (args.isNotEmpty && args.last.startsWith('@')) { |
| - return new List.from(args) |
| - ..addAll(new File(args.last.substring(1)).readAsLinesSync()); |
| - } else { |
| - return args; |
| + var fileArg = args.removeLast(); |
| + args.addAll(new File(fileArg.substring(1)).readAsLinesSync()); |
| } |
| + return args; |
| } |