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 /// Command line entry point for Dart Development Compiler (dartdevc). | 6 /// Command line entry point for Dart Development Compiler (dartdevc). |
7 /// | 7 /// |
8 /// Supported commands are | 8 /// Supported commands are |
9 /// * compile: builds a collection of dart libraries into a single JS module | 9 /// * compile: builds a collection of dart libraries into a single JS module |
10 /// | 10 /// |
(...skipping 17 matching lines...) Expand all Loading... |
28 /// * We do not assume a `node` JS installation, so we cannot easily reuse | 28 /// * We do not assume a `node` JS installation, so we cannot easily reuse |
29 /// existing tools for the "link" step, or assume users have a local | 29 /// existing tools for the "link" step, or assume users have a local |
30 /// file server, | 30 /// file server, |
31 /// * We didn't have a file watcher API at first, | 31 /// * We didn't have a file watcher API at first, |
32 /// * We had no conventions about where compiled output should go (or even | 32 /// * We had no conventions about where compiled output should go (or even |
33 /// that we would be compiling at all, vs running on an in-browser Dart VM), | 33 /// that we would be compiling at all, vs running on an in-browser Dart VM), |
34 /// * We wanted a good first impression with our simple examples, so we used | 34 /// * We wanted a good first impression with our simple examples, so we used |
35 /// local file servers, and users have an expectation of it now, even though | 35 /// local file servers, and users have an expectation of it now, even though |
36 /// it doesn't scale to typical apps that need their own real servers. | 36 /// it doesn't scale to typical apps that need their own real servers. |
37 | 37 |
| 38 @JS() |
| 39 library dev_compiler.web.main; |
| 40 |
38 import 'dart:async'; | 41 import 'dart:async'; |
39 import 'dart:io'; | 42 |
40 import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine; | |
41 import 'package:args/command_runner.dart'; | 43 import 'package:args/command_runner.dart'; |
42 import 'package:bazel_worker/bazel_worker.dart'; | 44 import 'package:js/js.dart'; |
43 import 'package:dev_compiler/src/compiler/command.dart'; | |
44 | 45 |
45 Future main(List<String> args) async { | 46 import 'web_command.dart'; |
46 // Always returns a new modifiable list. | |
47 args = _preprocessArgs(args); | |
48 | 47 |
49 if (args.contains('--persistent_worker')) { | 48 @JS() |
50 new _CompilerWorker(args..remove('--persistent_worker')).run(); | 49 external set compileDartExpression(Function function); |
51 } else { | 50 |
52 exitCode = await _runCommand(args); | 51 typedef String CompileFn(String dart); |
53 } | 52 typedef void OnLoadFn(CompileFn compile); |
| 53 |
| 54 Future main() async { |
| 55 var args = ['compile']; |
| 56 _runCommand((result) { |
| 57 compileDartExpression = allowInterop(result); |
| 58 }, args); |
54 } | 59 } |
55 | 60 |
56 /// Runs a single compile command, and returns an exit code. | 61 /// Runs a single compile command, and returns an exit code. |
57 Future<int> _runCommand(List<String> args, | 62 Future<int> _runCommand(OnLoadFn onload, List<String> args, |
58 {MessageHandler messageHandler}) async { | 63 {MessageHandler messageHandler}) async { |
59 try { | 64 try { |
60 if (args.isEmpty || args.first != 'compile' && args.first != 'help') { | |
61 // TODO(jmesserly): we should deprecate the commands. For now they are | |
62 // still supported for backwards compatibility. | |
63 args.insert(0, 'compile'); | |
64 } | |
65 var runner = new CommandRunner('dartdevc', 'Dart Development Compiler'); | 65 var runner = new CommandRunner('dartdevc', 'Dart Development Compiler'); |
66 runner.addCommand(new CompileCommand(messageHandler: messageHandler)); | 66 runner |
| 67 .addCommand(new WebCompileCommand(onload, messageHandler: messageHandler
)); |
67 await runner.run(args); | 68 await runner.run(args); |
68 } catch (e, s) { | 69 } catch (e, s) { |
69 return _handleError(e, s, args, messageHandler: messageHandler); | 70 return _handleError(e, s, args, messageHandler: messageHandler); |
70 } | 71 } |
71 return EXIT_CODE_OK; | 72 return 1; |
72 } | |
73 | |
74 /// Runs the compiler worker loop. | |
75 class _CompilerWorker extends AsyncWorkerLoop { | |
76 /// The original args supplied to the executable. | |
77 final List<String> _startupArgs; | |
78 | |
79 _CompilerWorker(this._startupArgs) : super(); | |
80 | |
81 /// Performs each individual work request. | |
82 Future<WorkResponse> performRequest(WorkRequest request) async { | |
83 var args = _startupArgs.toList()..addAll(request.arguments); | |
84 | |
85 var output = new StringBuffer(); | |
86 var exitCode = await _runCommand(args, messageHandler: output.writeln); | |
87 AnalysisEngine.instance.clearCaches(); | |
88 return new WorkResponse() | |
89 ..exitCode = exitCode | |
90 ..output = output.toString(); | |
91 } | |
92 } | 73 } |
93 | 74 |
94 /// Handles [error] in a uniform fashion. Returns the proper exit code and calls | 75 /// Handles [error] in a uniform fashion. Returns the proper exit code and calls |
95 /// [messageHandler] with messages. | 76 /// [messageHandler] with messages. |
96 int _handleError(dynamic error, dynamic stackTrace, List<String> args, | 77 int _handleError(dynamic error, dynamic stackTrace, List<String> args, |
97 {MessageHandler messageHandler}) { | 78 {MessageHandler messageHandler}) { |
98 messageHandler ??= print; | 79 messageHandler ??= print; |
99 | 80 |
100 if (error is UsageException) { | 81 if (error is UsageException) { |
101 // Incorrect usage, input file not found, etc. | 82 // Incorrect usage, input file not found, etc. |
(...skipping 14 matching lines...) Expand all Loading... |
116 messageHandler("We're sorry, you've found a bug in our compiler."); | 97 messageHandler("We're sorry, you've found a bug in our compiler."); |
117 messageHandler("You can report this bug at:"); | 98 messageHandler("You can report this bug at:"); |
118 messageHandler(" https://github.com/dart-lang/dev_compiler/issues"); | 99 messageHandler(" https://github.com/dart-lang/dev_compiler/issues"); |
119 messageHandler(""); | 100 messageHandler(""); |
120 messageHandler( | 101 messageHandler( |
121 "Please include the information below in your report, along with"); | 102 "Please include the information below in your report, along with"); |
122 messageHandler( | 103 messageHandler( |
123 "any other information that may help us track it down. Thanks!"); | 104 "any other information that may help us track it down. Thanks!"); |
124 messageHandler(""); | 105 messageHandler(""); |
125 messageHandler(" dartdevc arguments: " + args.join(' ')); | 106 messageHandler(" dartdevc arguments: " + args.join(' ')); |
126 messageHandler(" dart --version: ${Platform.version}"); | |
127 messageHandler(""); | 107 messageHandler(""); |
128 messageHandler("```"); | 108 messageHandler("```"); |
129 messageHandler(error); | 109 messageHandler(error); |
130 messageHandler(stackTrace); | 110 messageHandler(stackTrace); |
131 messageHandler("```"); | 111 messageHandler("```"); |
132 return 70; | 112 return 70; |
133 } | 113 } |
134 } | 114 } |
135 | |
136 /// Always returns a new modifiable list. | |
137 /// | |
138 /// If the final arg is `@file_path` then read in all the lines of that file | |
139 /// and add those as args. | |
140 /// | |
141 /// Bazel actions that support workers must provide all their per-WorkRequest | |
142 /// arguments in a file like this instead of as normal args. | |
143 List<String> _preprocessArgs(List<String> args) { | |
144 args = new List.from(args); | |
145 if (args.isNotEmpty && args.last.startsWith('@')) { | |
146 var fileArg = args.removeLast(); | |
147 args.addAll(new File(fileArg.substring(1)).readAsLinesSync()); | |
148 } | |
149 return args; | |
150 } | |
OLD | NEW |