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; |
(...skipping 14 matching lines...) Expand all Loading... |
25 /// | 25 /// |
26 /// This handles argument parsing, usage, error handling. | 26 /// This handles argument parsing, usage, error handling. |
27 /// See bin/dartdevc.dart for the actual entry point, which includes Bazel | 27 /// See bin/dartdevc.dart for the actual entry point, which includes Bazel |
28 /// worker support. | 28 /// worker support. |
29 int compile(List<String> args, {void printFn(Object obj)}) { | 29 int compile(List<String> args, {void printFn(Object obj)}) { |
30 printFn ??= print; | 30 printFn ??= print; |
31 | 31 |
32 ArgResults argResults; | 32 ArgResults argResults; |
33 AnalyzerOptions analyzerOptions; | 33 AnalyzerOptions analyzerOptions; |
34 try { | 34 try { |
35 var parser = _argParser(); | 35 var parser = ddcArgParser(); |
36 if (args.contains('--$ignoreUnrecognizedFlagsFlag')) { | 36 if (args.contains('--$ignoreUnrecognizedFlagsFlag')) { |
37 args = filterUnknownArguments(args, parser); | 37 args = filterUnknownArguments(args, parser); |
38 } | 38 } |
39 argResults = parser.parse(args); | 39 argResults = parser.parse(args); |
40 analyzerOptions = new AnalyzerOptions.fromArguments(argResults); | 40 analyzerOptions = new AnalyzerOptions.fromArguments(argResults); |
41 } on FormatException catch (error) { | 41 } on FormatException catch (error) { |
42 printFn('$error\n\n$_usageMessage'); | 42 printFn('$error\n\n$_usageMessage'); |
43 return 64; | 43 return 64; |
44 } | 44 } |
45 | 45 |
(...skipping 30 matching lines...) Expand all Loading... |
76 dartdevc arguments: ${args.join(' ')} | 76 dartdevc arguments: ${args.join(' ')} |
77 dart --version: ${Platform.version} | 77 dart --version: ${Platform.version} |
78 ``` | 78 ``` |
79 $error | 79 $error |
80 $stackTrace | 80 $stackTrace |
81 ```'''); | 81 ```'''); |
82 return 70; | 82 return 70; |
83 } | 83 } |
84 } | 84 } |
85 | 85 |
86 ArgParser _argParser({bool hide: true}) { | 86 ArgParser ddcArgParser({bool hide: true}) { |
87 var argParser = new ArgParser(allowTrailingOptions: true) | 87 var argParser = new ArgParser(allowTrailingOptions: true) |
88 ..addFlag('help', | 88 ..addFlag('help', |
89 abbr: 'h', | 89 abbr: 'h', |
90 help: 'Display this message.\n' | 90 help: 'Display this message.\n' |
91 'Add --verbose to show hidden options.', | 91 'Add --verbose to show hidden options.', |
92 negatable: false) | 92 negatable: false) |
93 ..addFlag('verbose', abbr: 'v', help: 'Verbose output.') | 93 ..addFlag('verbose', abbr: 'v', help: 'Verbose output.') |
94 ..addFlag(ignoreUnrecognizedFlagsFlag, | 94 ..addFlag(ignoreUnrecognizedFlagsFlag, |
95 help: 'Ignore unrecognized command line flags.', | 95 help: 'Ignore unrecognized command line flags.', |
96 defaultsTo: false, | 96 defaultsTo: false, |
(...skipping 17 matching lines...) Expand all Loading... |
114 var length = list1.length; | 114 var length = list1.length; |
115 if (length != list2.length) return true; | 115 if (length != list2.length) return true; |
116 for (var i = 0; i < length; ++i) { | 116 for (var i = 0; i < length; ++i) { |
117 if (list1[i] != list2[i]) return true; | 117 if (list1[i] != list2[i]) return true; |
118 } | 118 } |
119 return false; | 119 return false; |
120 } | 120 } |
121 | 121 |
122 void _compile(ArgResults argResults, AnalyzerOptions analyzerOptions, | 122 void _compile(ArgResults argResults, AnalyzerOptions analyzerOptions, |
123 void printFn(Object obj)) { | 123 void printFn(Object obj)) { |
124 var compiler = new ModuleCompiler(analyzerOptions); | 124 var libraryRoot = argResults['library-root'] as String; |
| 125 if (libraryRoot != null) { |
| 126 libraryRoot = path.absolute(libraryRoot); |
| 127 } else { |
| 128 libraryRoot = Directory.current.path; |
| 129 } |
| 130 |
| 131 var compiler = new ModuleCompiler(libraryRoot, analyzerOptions); |
125 var compilerOpts = new CompilerOptions.fromArguments(argResults); | 132 var compilerOpts = new CompilerOptions.fromArguments(argResults); |
126 var outPaths = argResults['out'] as List<String>; | 133 var outPaths = argResults['out'] as List<String>; |
127 var moduleFormats = parseModuleFormatOption(argResults); | 134 var moduleFormats = parseModuleFormatOption(argResults); |
128 bool singleOutFile = argResults['single-out-file']; | 135 bool singleOutFile = argResults['single-out-file']; |
129 if (singleOutFile) { | 136 if (singleOutFile) { |
130 for (var format in moduleFormats) { | 137 for (var format in moduleFormats) { |
131 if (format != ModuleFormat.amd && format != ModuleFormat.legacy) { | 138 if (format != ModuleFormat.amd && format != ModuleFormat.legacy) { |
132 _usageException('Format $format cannot be combined with ' | 139 _usageException('Format $format cannot be combined with ' |
133 'single-out-file. Only amd and legacy modes are supported.'); | 140 'single-out-file. Only amd and legacy modes are supported.'); |
134 } | 141 } |
135 } | 142 } |
136 } | 143 } |
137 | 144 |
138 if (outPaths.isEmpty) { | 145 if (outPaths.isEmpty) { |
139 _usageException('Please include the output file location. For example:\n' | 146 _usageException('Please include the output file location. For example:\n' |
140 ' -o PATH/TO/OUTPUT_FILE.js'); | 147 ' -o PATH/TO/OUTPUT_FILE.js'); |
141 } else if (outPaths.length != moduleFormats.length) { | 148 } else if (outPaths.length != moduleFormats.length) { |
142 _usageException('Number of output files (${outPaths.length}) must match ' | 149 _usageException('Number of output files (${outPaths.length}) must match ' |
143 'number of module formats (${moduleFormats.length}).'); | 150 'number of module formats (${moduleFormats.length}).'); |
144 } | 151 } |
145 | 152 |
146 // TODO(jmesserly): for now the first one is special. This will go away once | 153 // TODO(jmesserly): for now the first one is special. This will go away once |
147 // we've removed the "root" and "module name" variables. | 154 // we've removed the "root" and "module name" variables. |
148 var firstOutPath = outPaths[0]; | 155 var firstOutPath = outPaths[0]; |
149 | 156 |
150 var libraryRoot = argResults['library-root'] as String; | |
151 if (libraryRoot != null) { | |
152 libraryRoot = path.absolute(libraryRoot); | |
153 } else { | |
154 libraryRoot = Directory.current.path; | |
155 } | |
156 var moduleRoot = argResults['module-root'] as String; | 157 var moduleRoot = argResults['module-root'] as String; |
157 String modulePath; | 158 String modulePath; |
158 if (moduleRoot != null) { | 159 if (moduleRoot != null) { |
159 moduleRoot = path.absolute(moduleRoot); | 160 moduleRoot = path.absolute(moduleRoot); |
160 if (!path.isWithin(moduleRoot, firstOutPath)) { | 161 if (!path.isWithin(moduleRoot, firstOutPath)) { |
161 _usageException('Output file $firstOutPath must be within the module ' | 162 _usageException('Output file $firstOutPath must be within the module ' |
162 'root directory $moduleRoot'); | 163 'root directory $moduleRoot'); |
163 } | 164 } |
164 modulePath = | 165 modulePath = |
165 path.withoutExtension(path.relative(firstOutPath, from: moduleRoot)); | 166 path.withoutExtension(path.relative(firstOutPath, from: moduleRoot)); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 | 219 |
219 _usageException( | 220 _usageException( |
220 'Imported file "${source.uri}" was not found as a summary or source ' | 221 'Imported file "${source.uri}" was not found as a summary or source ' |
221 'file. Please pass in either the summary or the source file ' | 222 'file. Please pass in either the summary or the source file ' |
222 'for this import.'); | 223 'for this import.'); |
223 return null; // unreachable | 224 return null; // unreachable |
224 } | 225 } |
225 | 226 |
226 String get _usageMessage => | 227 String get _usageMessage => |
227 'Dart Development Compiler compiles Dart into a JavaScript module.' | 228 'Dart Development Compiler compiles Dart into a JavaScript module.' |
228 '\n\n${_argParser(hide: !_verbose).usage}'; | 229 '\n\n${ddcArgParser(hide: !_verbose).usage}'; |
229 | 230 |
230 void _usageException(String message) { | 231 void _usageException(String message) { |
231 throw new UsageException(message, _usageMessage); | 232 throw new UsageException(message, _usageMessage); |
232 } | 233 } |
233 | 234 |
234 /// Thrown when the input source code has errors. | 235 /// Thrown when the input source code has errors. |
235 class CompileErrorException implements Exception { | 236 class CompileErrorException implements Exception { |
236 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; | 237 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; |
237 } | 238 } |
OLD | NEW |