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 20 matching lines...) Expand all Loading... |
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 import 'dart:async'; | 38 import 'dart:async'; |
39 import 'dart:io'; | 39 import 'dart:io'; |
40 import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine; | 40 import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine; |
41 import 'package:args/command_runner.dart'; | |
42 import 'package:bazel_worker/bazel_worker.dart'; | 41 import 'package:bazel_worker/bazel_worker.dart'; |
43 import 'package:dev_compiler/src/compiler/command.dart'; | 42 import 'package:dev_compiler/src/compiler/command.dart'; |
44 | 43 |
45 Future main(List<String> args) async { | 44 Future main(List<String> args) async { |
46 // Always returns a new modifiable list. | 45 // Always returns a new modifiable list. |
47 args = _preprocessArgs(args); | 46 args = _preprocessArgs(args); |
48 | 47 |
49 if (args.contains('--persistent_worker')) { | 48 if (args.contains('--persistent_worker')) { |
50 new _CompilerWorker(args..remove('--persistent_worker')).run(); | 49 new _CompilerWorker(args..remove('--persistent_worker')).run(); |
51 } else { | 50 } else { |
52 exitCode = await _runCommand(args); | 51 exitCode = compile(args); |
53 } | 52 } |
54 } | 53 } |
55 | 54 |
56 /// Runs a single compile command, and returns an exit code. | |
57 Future<int> _runCommand(List<String> args, | |
58 {MessageHandler messageHandler}) async { | |
59 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'); | |
66 runner.addCommand(new CompileCommand(messageHandler: messageHandler)); | |
67 await runner.run(args); | |
68 } catch (e, s) { | |
69 return _handleError(e, s, args, messageHandler: messageHandler); | |
70 } | |
71 return EXIT_CODE_OK; | |
72 } | |
73 | |
74 /// Runs the compiler worker loop. | 55 /// Runs the compiler worker loop. |
75 class _CompilerWorker extends AsyncWorkerLoop { | 56 class _CompilerWorker extends AsyncWorkerLoop { |
76 /// The original args supplied to the executable. | 57 /// The original args supplied to the executable. |
77 final List<String> _startupArgs; | 58 final List<String> _startupArgs; |
78 | 59 |
79 _CompilerWorker(this._startupArgs) : super(); | 60 _CompilerWorker(this._startupArgs) : super(); |
80 | 61 |
81 /// Performs each individual work request. | 62 /// Performs each individual work request. |
82 Future<WorkResponse> performRequest(WorkRequest request) async { | 63 Future<WorkResponse> performRequest(WorkRequest request) async { |
83 var args = _startupArgs.toList()..addAll(request.arguments); | 64 var args = _startupArgs.toList()..addAll(request.arguments); |
84 | 65 |
85 var output = new StringBuffer(); | 66 var output = new StringBuffer(); |
86 var exitCode = await _runCommand(args, messageHandler: output.writeln); | 67 var exitCode = compile(args, printFn: output.writeln); |
87 AnalysisEngine.instance.clearCaches(); | 68 AnalysisEngine.instance.clearCaches(); |
88 return new WorkResponse() | 69 return new WorkResponse() |
89 ..exitCode = exitCode | 70 ..exitCode = exitCode |
90 ..output = output.toString(); | 71 ..output = output.toString(); |
91 } | 72 } |
92 } | 73 } |
93 | 74 |
94 /// Handles [error] in a uniform fashion. Returns the proper exit code and calls | |
95 /// [messageHandler] with messages. | |
96 int _handleError(dynamic error, dynamic stackTrace, List<String> args, | |
97 {MessageHandler messageHandler}) { | |
98 messageHandler ??= print; | |
99 | |
100 if (error is UsageException) { | |
101 // Incorrect usage, input file not found, etc. | |
102 messageHandler(error); | |
103 return 64; | |
104 } else if (error is CompileErrorException) { | |
105 // Code has error(s) and failed to compile. | |
106 messageHandler(error); | |
107 return 1; | |
108 } else { | |
109 // Anything else is likely a compiler bug. | |
110 // | |
111 // --unsafe-force-compile is a bit of a grey area, but it's nice not to | |
112 // crash while compiling | |
113 // (of course, output code may crash, if it had errors). | |
114 // | |
115 messageHandler(""); | |
116 messageHandler("We're sorry, you've found a bug in our compiler."); | |
117 messageHandler("You can report this bug at:"); | |
118 messageHandler(" https://github.com/dart-lang/dev_compiler/issues"); | |
119 messageHandler(""); | |
120 messageHandler( | |
121 "Please include the information below in your report, along with"); | |
122 messageHandler( | |
123 "any other information that may help us track it down. Thanks!"); | |
124 messageHandler(""); | |
125 messageHandler(" dartdevc arguments: " + args.join(' ')); | |
126 messageHandler(" dart --version: ${Platform.version}"); | |
127 messageHandler(""); | |
128 messageHandler("```"); | |
129 messageHandler(error); | |
130 messageHandler(stackTrace); | |
131 messageHandler("```"); | |
132 return 70; | |
133 } | |
134 } | |
135 | |
136 /// Always returns a new modifiable list. | 75 /// Always returns a new modifiable list. |
137 /// | 76 /// |
138 /// If the final arg is `@file_path` then read in all the lines of that file | 77 /// If the final arg is `@file_path` then read in all the lines of that file |
139 /// and add those as args. | 78 /// and add those as args. |
140 /// | 79 /// |
141 /// Bazel actions that support workers must provide all their per-WorkRequest | 80 /// Bazel actions that support workers must provide all their per-WorkRequest |
142 /// arguments in a file like this instead of as normal args. | 81 /// arguments in a file like this instead of as normal args. |
143 List<String> _preprocessArgs(List<String> args) { | 82 List<String> _preprocessArgs(List<String> args) { |
144 args = new List.from(args); | 83 args = new List.from(args); |
145 if (args.isNotEmpty && args.last.startsWith('@')) { | 84 if (args.isNotEmpty && args.last.startsWith('@')) { |
146 var fileArg = args.removeLast(); | 85 var fileArg = args.removeLast(); |
147 args.addAll(new File(fileArg.substring(1)).readAsLinesSync()); | 86 args.addAll(new File(fileArg.substring(1)).readAsLinesSync()); |
148 } | 87 } |
149 return args; | 88 return args; |
150 } | 89 } |
OLD | NEW |