| OLD | NEW |
| 1 #!/usr/bin/env dart | 1 #!/usr/bin/env dart |
| 2 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 2 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 3 // for details. All rights reserved. Use of this source code is governed by a | 3 // for details. All rights reserved. Use of this source code is governed by a |
| 4 // BSD-style license that can be found in the LICENSE file. | 4 // BSD-style license that can be found in the LICENSE file. |
| 5 | 5 |
| 6 @JS() | 6 @JS() |
| 7 library dev_compiler.web.main; | 7 library dev_compiler.web.main; |
| 8 | 8 |
| 9 import 'dart:async'; | 9 import 'dart:async'; |
| 10 | 10 |
| 11 import 'package:args/command_runner.dart'; | 11 import 'package:args/command_runner.dart'; |
| 12 import 'package:js/js.dart'; | 12 import 'package:js/js.dart'; |
| 13 | 13 |
| 14 import 'web_command.dart'; | 14 import 'web_command.dart'; |
| 15 | 15 |
| 16 @JS(r'$setUpDartDevCompilerInBrowser') | 16 @JS(r'$setUpDartDevCompilerInBrowser') |
| 17 external set setUpCompilerInBrowser(Function function); | 17 external set setUpCompilerInBrowser(Function function); |
| 18 | 18 |
| 19 Future main() async { | 19 Future<Function> _setUpCompilerInBrowser; |
| 20 main() { |
| 20 var args = ['compile', '--repl-compile']; | 21 var args = ['compile', '--repl-compile']; |
| 22 |
| 23 // Avoid race condition when users try to call $setUpDartDevCompilerInBrowser |
| 24 // before it is ready by installing the method immediately and making the body |
| 25 // of the method async. |
| 26 setUpCompilerInBrowser = allowInterop((String summaryRoot, String sdkUrl, |
| 27 List<String> summaryUrls, |
| 28 Function onCompileReady, |
| 29 Function onError) async { |
| 30 (await _setUpCompilerInBrowser)( |
| 31 summaryRoot, sdkUrl, summaryUrls, onCompileReady, onError); |
| 32 }); |
| 21 _runCommand(args); | 33 _runCommand(args); |
| 22 } | 34 } |
| 23 | 35 |
| 24 /// Runs a single compile command, and returns an exit code. | 36 /// Runs a single compile command, and returns an exit code. |
| 25 Future<int> _runCommand(List<String> args, | 37 _runCommand(List<String> args, {MessageHandler messageHandler}) { |
| 26 {MessageHandler messageHandler}) async { | |
| 27 try { | 38 try { |
| 28 // TODO: Remove CommandRunner and args if possible. May run into issues | 39 // TODO: Remove CommandRunner and args if possible. May run into issues |
| 29 // with ArgResults or ArgParsers. | 40 // with ArgResults or ArgParsers. |
| 30 var runner = new CommandRunner('dartdevc', 'Dart Development Compiler'); | 41 var runner = new CommandRunner('dartdevc', 'Dart Development Compiler'); |
| 31 runner.addCommand(new WebCompileCommand(messageHandler: messageHandler)); | 42 runner.addCommand(new WebCompileCommand(messageHandler: messageHandler)); |
| 32 setUpCompilerInBrowser = allowInterop((await runner.run(args)) as Function); | 43 _setUpCompilerInBrowser = runner.run(args) as Future<Function>; |
| 33 } catch (e, s) { | 44 } catch (e, s) { |
| 34 return _handleError(e, s, args, messageHandler: messageHandler); | 45 _handleError(e, s, args, messageHandler: messageHandler); |
| 35 } | 46 } |
| 36 return 1; | |
| 37 } | 47 } |
| 38 | 48 |
| 39 /// Handles [error] in a uniform fashion. Returns the proper exit code and calls | 49 /// Handles [error] in a uniform fashion. Calls [messageHandler] with messages. |
| 40 /// [messageHandler] with messages. | 50 _handleError(dynamic error, dynamic stackTrace, List<String> args, |
| 41 int _handleError(dynamic error, dynamic stackTrace, List<String> args, | |
| 42 {MessageHandler messageHandler}) { | 51 {MessageHandler messageHandler}) { |
| 43 messageHandler ??= print; | 52 messageHandler ??= print; |
| 44 | 53 |
| 45 if (error is UsageException) { | 54 if (error is UsageException) { |
| 46 // Incorrect usage, input file not found, etc. | 55 // Incorrect usage, input file not found, etc. |
| 47 messageHandler(error); | 56 messageHandler(error); |
| 48 return 64; | |
| 49 } else if (error is CompileErrorException) { | 57 } else if (error is CompileErrorException) { |
| 50 // Code has error(s) and failed to compile. | 58 // Code has error(s) and failed to compile. |
| 51 messageHandler(error); | 59 messageHandler(error); |
| 52 return 1; | |
| 53 } else { | 60 } else { |
| 54 // Anything else is likely a compiler bug. | 61 // Anything else is likely a compiler bug. |
| 55 // | 62 // |
| 56 // --unsafe-force-compile is a bit of a grey area, but it's nice not to | 63 // --unsafe-force-compile is a bit of a grey area, but it's nice not to |
| 57 // crash while compiling | 64 // crash while compiling |
| 58 // (of course, output code may crash, if it had errors). | 65 // (of course, output code may crash, if it had errors). |
| 59 // | 66 // |
| 60 messageHandler(""); | 67 messageHandler(""); |
| 61 messageHandler("We're sorry, you've found a bug in our compiler."); | 68 messageHandler("We're sorry, you've found a bug in our compiler."); |
| 62 messageHandler("You can report this bug at:"); | 69 messageHandler("You can report this bug at:"); |
| 63 messageHandler( | 70 messageHandler( |
| 64 " https://github.com/dart-lang/sdk/issues/labels/area-dev-compiler"); | 71 " https://github.com/dart-lang/sdk/issues/labels/area-dev-compiler"); |
| 65 messageHandler(""); | 72 messageHandler(""); |
| 66 messageHandler( | 73 messageHandler( |
| 67 "Please include the information below in your report, along with"); | 74 "Please include the information below in your report, along with"); |
| 68 messageHandler( | 75 messageHandler( |
| 69 "any other information that may help us track it down. Thanks!"); | 76 "any other information that may help us track it down. Thanks!"); |
| 70 messageHandler(""); | 77 messageHandler(""); |
| 71 messageHandler(" dartdevc arguments: " + args.join(' ')); | 78 messageHandler(" dartdevc arguments: " + args.join(' ')); |
| 72 messageHandler(""); | 79 messageHandler(""); |
| 73 messageHandler("```"); | 80 messageHandler("```"); |
| 74 messageHandler(error); | 81 messageHandler(error); |
| 75 messageHandler(stackTrace); | 82 messageHandler(stackTrace); |
| 76 messageHandler("```"); | 83 messageHandler("```"); |
| 77 return 70; | |
| 78 } | 84 } |
| 79 } | 85 } |
| OLD | NEW |