OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:io'; | 5 import 'dart:io'; |
6 import 'package:analyzer/src/command_line/arguments.dart' | 6 import 'package:analyzer/src/command_line/arguments.dart' |
7 show | 7 show |
8 defineAnalysisArguments, | 8 defineAnalysisArguments, |
9 filterUnknownArguments, | 9 filterUnknownArguments, |
10 ignoreUnrecognizedFlagsFlag; | 10 ignoreUnrecognizedFlagsFlag; |
11 import 'package:analyzer/src/generated/source.dart' show Source; | 11 import 'package:analyzer/src/generated/source.dart' show Source; |
12 import 'package:analyzer/src/summary/package_bundle_reader.dart' | 12 import 'package:analyzer/src/summary/package_bundle_reader.dart' |
13 show InSummarySource; | 13 show InSummarySource; |
14 import 'package:args/args.dart' show ArgParser, ArgResults; | 14 import 'package:args/args.dart' show ArgParser, ArgResults; |
15 import 'package:args/command_runner.dart' show UsageException; | 15 import 'package:args/command_runner.dart' show UsageException; |
16 import 'package:path/path.dart' as path; | 16 import 'package:path/path.dart' as path; |
17 | 17 |
18 import '../analyzer/context.dart' show AnalyzerOptions; | 18 import '../analyzer/context.dart' show AnalyzerOptions; |
19 import 'compiler.dart' show BuildUnit, CompilerOptions, ModuleCompiler; | 19 import 'compiler.dart' show BuildUnit, CompilerOptions, ModuleCompiler; |
20 import 'module_builder.dart'; | 20 import 'module_builder.dart'; |
21 | 21 |
| 22 const _binaryName = 'dartdevc'; |
| 23 |
22 bool _verbose = false; | 24 bool _verbose = false; |
23 | 25 |
24 /// Runs a single compile for dartdevc. | 26 /// Runs a single compile for dartdevc. |
25 /// | 27 /// |
26 /// This handles argument parsing, usage, error handling. | 28 /// This handles argument parsing, usage, error handling. |
27 /// See bin/dartdevc.dart for the actual entry point, which includes Bazel | 29 /// See bin/dartdevc.dart for the actual entry point, which includes Bazel |
28 /// worker support. | 30 /// worker support. |
29 int compile(List<String> args, {void printFn(Object obj)}) { | 31 int compile(List<String> args, {void printFn(Object obj)}) { |
30 printFn ??= print; | 32 printFn ??= print; |
31 | 33 |
32 ArgResults argResults; | 34 ArgResults argResults; |
33 AnalyzerOptions analyzerOptions; | 35 AnalyzerOptions analyzerOptions; |
34 try { | 36 try { |
35 var parser = ddcArgParser(); | 37 var parser = ddcArgParser(); |
36 if (args.contains('--$ignoreUnrecognizedFlagsFlag')) { | 38 if (args.contains('--$ignoreUnrecognizedFlagsFlag')) { |
37 args = filterUnknownArguments(args, parser); | 39 args = filterUnknownArguments(args, parser); |
38 } | 40 } |
39 argResults = parser.parse(args); | 41 argResults = parser.parse(args); |
40 analyzerOptions = new AnalyzerOptions.fromArguments(argResults); | 42 analyzerOptions = new AnalyzerOptions.fromArguments(argResults); |
41 } on FormatException catch (error) { | 43 } on FormatException catch (error) { |
42 printFn('$error\n\n$_usageMessage'); | 44 printFn('$error\n\n$_usageMessage'); |
43 return 64; | 45 return 64; |
44 } | 46 } |
45 | 47 |
46 _verbose = argResults['verbose']; | 48 _verbose = argResults['verbose']; |
47 if (argResults['help']) { | 49 if (argResults['help'] || args.isEmpty) { |
48 printFn(_usageMessage); | 50 printFn(_usageMessage); |
49 return 0; | 51 return 0; |
50 } | 52 } |
51 | 53 |
| 54 if (argResults['version']) { |
| 55 printFn('$_binaryName version ${_getVersion()}'); |
| 56 return 0; |
| 57 } |
| 58 |
52 try { | 59 try { |
53 _compile(argResults, analyzerOptions, printFn); | 60 _compile(argResults, analyzerOptions, printFn); |
54 return 0; | 61 return 0; |
55 } on UsageException catch (error) { | 62 } on UsageException catch (error) { |
56 // Incorrect usage, input file not found, etc. | 63 // Incorrect usage, input file not found, etc. |
57 printFn(error); | 64 printFn(error); |
58 return 64; | 65 return 64; |
59 } on CompileErrorException catch (error) { | 66 } on CompileErrorException catch (error) { |
60 // Code has error(s) and failed to compile. | 67 // Code has error(s) and failed to compile. |
61 printFn(error); | 68 printFn(error); |
62 return 1; | 69 return 1; |
63 } catch (error, stackTrace) { | 70 } catch (error, stackTrace) { |
64 // Anything else is likely a compiler bug. | 71 // Anything else is likely a compiler bug. |
65 // | 72 // |
66 // --unsafe-force-compile is a bit of a grey area, but it's nice not to | 73 // --unsafe-force-compile is a bit of a grey area, but it's nice not to |
67 // crash while compiling | 74 // crash while compiling |
68 // (of course, output code may crash, if it had errors). | 75 // (of course, output code may crash, if it had errors). |
69 // | 76 // |
70 printFn(''' | 77 printFn(''' |
71 We're sorry, you've found a bug in our compiler. | 78 We're sorry, you've found a bug in our compiler. |
72 You can report this bug at: | 79 You can report this bug at: |
73 https://github.com/dart-lang/sdk/issues/labels/area-dev-compiler | 80 https://github.com/dart-lang/sdk/issues/labels/area-dev-compiler |
74 Please include the information below in your report, along with | 81 Please include the information below in your report, along with |
75 any other information that may help us track it down. Thanks! | 82 any other information that may help us track it down. Thanks! |
76 dartdevc arguments: ${args.join(' ')} | 83 $_binaryName arguments: ${args.join(' ')} |
77 dart --version: ${Platform.version} | 84 dart --version: ${Platform.version} |
78 ``` | 85 ``` |
79 $error | 86 $error |
80 $stackTrace | 87 $stackTrace |
81 ```'''); | 88 ```'''); |
82 return 70; | 89 return 70; |
83 } | 90 } |
84 } | 91 } |
85 | 92 |
86 ArgParser ddcArgParser({bool hide: true}) { | 93 ArgParser ddcArgParser({bool hide: true}) { |
87 var argParser = new ArgParser(allowTrailingOptions: true) | 94 var argParser = new ArgParser(allowTrailingOptions: true) |
88 ..addFlag('help', | 95 ..addFlag('help', |
89 abbr: 'h', | 96 abbr: 'h', |
90 help: 'Display this message.\n' | 97 help: 'Display this message. Add --verbose to show hidden options.', |
91 'Add --verbose to show hidden options.', | |
92 negatable: false) | 98 negatable: false) |
93 ..addFlag('verbose', abbr: 'v', help: 'Verbose output.') | 99 ..addFlag('verbose', abbr: 'v', help: 'Verbose output.') |
| 100 ..addFlag('version', |
| 101 negatable: false, help: 'Print the $_binaryName version.') |
94 ..addFlag(ignoreUnrecognizedFlagsFlag, | 102 ..addFlag(ignoreUnrecognizedFlagsFlag, |
95 help: 'Ignore unrecognized command line flags.', | 103 help: 'Ignore unrecognized command line flags.', |
96 defaultsTo: false, | 104 defaultsTo: false, |
97 negatable: false) | 105 negatable: false) |
98 ..addOption('out', | 106 ..addOption('out', |
99 abbr: 'o', allowMultiple: true, help: 'Output file (required).') | 107 abbr: 'o', allowMultiple: true, help: 'Output file (required).') |
100 ..addOption('module-root', | 108 ..addOption('module-root', |
101 help: 'Root module directory.\n' | 109 help: 'Root module directory. ' |
102 'Generated module paths are relative to this root.') | 110 'Generated module paths are relative to this root.') |
103 ..addOption('library-root', | 111 ..addOption('library-root', |
104 help: 'Root of source files.\n' | 112 help: 'Root of source files. ' |
105 'Generated library names are relative to this root.'); | 113 'Generated library names are relative to this root.'); |
106 defineAnalysisArguments(argParser, hide: hide, ddc: true); | 114 defineAnalysisArguments(argParser, hide: hide, ddc: true); |
107 addModuleFormatOptions(argParser, allowMultiple: true, hide: hide); | 115 addModuleFormatOptions(argParser, allowMultiple: true, hide: hide); |
108 AnalyzerOptions.addArguments(argParser, hide: hide); | 116 AnalyzerOptions.addArguments(argParser, hide: hide); |
109 CompilerOptions.addArguments(argParser, hide: hide); | 117 CompilerOptions.addArguments(argParser, hide: hide); |
110 return argParser; | 118 return argParser; |
111 } | 119 } |
112 | 120 |
113 bool _changed(List<int> list1, List<int> list2) { | 121 bool _changed(List<int> list1, List<int> list2) { |
114 var length = list1.length; | 122 var length = list1.length; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 } | 225 } |
218 | 226 |
219 _usageException( | 227 _usageException( |
220 'Imported file "${source.uri}" was not found as a summary or source ' | 228 'Imported file "${source.uri}" was not found as a summary or source ' |
221 'file. Please pass in either the summary or the source file ' | 229 'file. Please pass in either the summary or the source file ' |
222 'for this import.'); | 230 'for this import.'); |
223 return null; // unreachable | 231 return null; // unreachable |
224 } | 232 } |
225 | 233 |
226 String get _usageMessage => | 234 String get _usageMessage => |
227 'Dart Development Compiler compiles Dart into a JavaScript module.' | 235 'The Dart Development Compiler compiles Dart sources into a JavaScript ' |
228 '\n\n${ddcArgParser(hide: !_verbose).usage}'; | 236 'module.\n\n' |
| 237 'Usage: $_binaryName [options...] <sources...>\n\n' |
| 238 '${ddcArgParser(hide: !_verbose).usage}'; |
| 239 |
| 240 String _getVersion() { |
| 241 try { |
| 242 // This is relative to bin/snapshot, so ../.. |
| 243 String versionPath = Platform.script.resolve('../../version').toFilePath(); |
| 244 File versionFile = new File(versionPath); |
| 245 return versionFile.readAsStringSync().trim(); |
| 246 } catch (_) { |
| 247 // This happens when the script is not running in the context of an SDK. |
| 248 return "<unknown>"; |
| 249 } |
| 250 } |
229 | 251 |
230 void _usageException(String message) { | 252 void _usageException(String message) { |
231 throw new UsageException(message, _usageMessage); | 253 throw new UsageException(message, _usageMessage); |
232 } | 254 } |
233 | 255 |
234 /// Thrown when the input source code has errors. | 256 /// Thrown when the input source code has errors. |
235 class CompileErrorException implements Exception { | 257 class CompileErrorException implements Exception { |
236 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; | 258 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; |
237 } | 259 } |
OLD | NEW |