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:async'; |
5 import 'dart:io'; | 6 import 'dart:io'; |
6 import 'package:analyzer/src/command_line/arguments.dart' | 7 import 'package:analyzer/src/command_line/arguments.dart' |
7 show | 8 show |
8 defineAnalysisArguments, | 9 defineAnalysisArguments, |
9 filterUnknownArguments, | 10 filterUnknownArguments, |
10 ignoreUnrecognizedFlagsFlag; | 11 ignoreUnrecognizedFlagsFlag; |
11 import 'package:analyzer/src/generated/source.dart' show Source; | 12 import 'package:analyzer/src/generated/source.dart' show Source; |
12 import 'package:analyzer/src/summary/package_bundle_reader.dart' | 13 import 'package:analyzer/src/summary/package_bundle_reader.dart' |
13 show InSummarySource; | 14 show InSummarySource; |
14 import 'package:args/args.dart' show ArgParser, ArgResults; | 15 import 'package:args/args.dart' show ArgParser, ArgResults; |
15 import 'package:args/command_runner.dart' show UsageException; | 16 import 'package:args/command_runner.dart' show UsageException; |
| 17 import 'package:dev_compiler/src/compiler/compiler.dart'; |
16 import 'package:path/path.dart' as path; | 18 import 'package:path/path.dart' as path; |
17 | 19 |
18 import '../analyzer/context.dart' show AnalyzerOptions; | 20 import '../analyzer/context.dart' show AnalyzerOptions; |
19 import 'compiler.dart' show BuildUnit, CompilerOptions, ModuleCompiler; | 21 import 'compiler.dart' show BuildUnit, CompilerOptions, ModuleCompiler; |
20 import 'module_builder.dart'; | 22 import 'module_builder.dart'; |
21 | 23 |
22 const _binaryName = 'dartdevc'; | 24 const _binaryName = 'dartdevc'; |
23 | 25 |
24 bool _verbose = false; | 26 bool _verbose = false; |
25 | 27 |
26 /// Runs a single compile for dartdevc. | 28 /// Runs a single compile for dartdevc. |
27 /// | 29 /// |
28 /// This handles argument parsing, usage, error handling. | 30 /// This handles argument parsing, usage, error handling. |
29 /// See bin/dartdevc.dart for the actual entry point, which includes Bazel | 31 /// See bin/dartdevc.dart for the actual entry point, which includes Bazel |
30 /// worker support. | 32 /// worker support. |
31 int compile(List<String> args, {void printFn(Object obj)}) { | 33 Future<int> compile(List<String> args, {void printFn(Object obj)}) async { |
32 printFn ??= print; | 34 printFn ??= print; |
33 | 35 |
34 ArgResults argResults; | 36 ArgResults argResults; |
35 AnalyzerOptions analyzerOptions; | 37 AnalyzerOptions analyzerOptions; |
36 try { | 38 try { |
37 var parser = ddcArgParser(); | 39 var parser = ddcArgParser(); |
38 if (args.contains('--$ignoreUnrecognizedFlagsFlag')) { | 40 if (args.contains('--$ignoreUnrecognizedFlagsFlag')) { |
39 args = filterUnknownArguments(args, parser); | 41 args = filterUnknownArguments(args, parser); |
40 } | 42 } |
41 argResults = parser.parse(args); | 43 argResults = parser.parse(args); |
42 analyzerOptions = new AnalyzerOptions.fromArguments(argResults); | 44 analyzerOptions = new AnalyzerOptions.fromArguments(argResults); |
43 } on FormatException catch (error) { | 45 } on FormatException catch (error) { |
44 printFn('$error\n\n$_usageMessage'); | 46 printFn('$error\n\n$_usageMessage'); |
45 return 64; | 47 return 64; |
46 } | 48 } |
47 | 49 |
48 _verbose = argResults['verbose']; | 50 _verbose = argResults['verbose']; |
49 if (argResults['help'] || args.isEmpty) { | 51 if (argResults['help'] || args.isEmpty) { |
50 printFn(_usageMessage); | 52 printFn(_usageMessage); |
51 return 0; | 53 return 0; |
52 } | 54 } |
53 | 55 |
54 if (argResults['version']) { | 56 if (argResults['version']) { |
55 printFn('$_binaryName version ${_getVersion()}'); | 57 printFn('$_binaryName version ${_getVersion()}'); |
56 return 0; | 58 return 0; |
57 } | 59 } |
58 | 60 |
59 try { | 61 try { |
60 _compile(argResults, analyzerOptions, printFn); | 62 await _compile(argResults, analyzerOptions, printFn); |
61 return 0; | 63 return 0; |
62 } on UsageException catch (error) { | 64 } on UsageException catch (error) { |
63 // Incorrect usage, input file not found, etc. | 65 // Incorrect usage, input file not found, etc. |
64 printFn(error); | 66 printFn(error); |
65 return 64; | 67 return 64; |
66 } on CompileErrorException catch (error) { | 68 } on CompileErrorException catch (error) { |
67 // Code has error(s) and failed to compile. | 69 // Code has error(s) and failed to compile. |
68 printFn(error); | 70 printFn(error); |
69 return 1; | 71 return 1; |
70 } catch (error, stackTrace) { | 72 } catch (error, stackTrace) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 | 122 |
121 bool _changed(List<int> list1, List<int> list2) { | 123 bool _changed(List<int> list1, List<int> list2) { |
122 var length = list1.length; | 124 var length = list1.length; |
123 if (length != list2.length) return true; | 125 if (length != list2.length) return true; |
124 for (var i = 0; i < length; ++i) { | 126 for (var i = 0; i < length; ++i) { |
125 if (list1[i] != list2[i]) return true; | 127 if (list1[i] != list2[i]) return true; |
126 } | 128 } |
127 return false; | 129 return false; |
128 } | 130 } |
129 | 131 |
130 void _compile(ArgResults argResults, AnalyzerOptions analyzerOptions, | 132 Future<Null> _compile(ArgResults argResults, AnalyzerOptions analyzerOptions, |
131 void printFn(Object obj)) { | 133 void printFn(Object obj)) async { |
132 var compiler = new ModuleCompiler(analyzerOptions); | 134 var compiler = new ModuleCompiler(analyzerOptions); |
133 var compilerOpts = new CompilerOptions.fromArguments(argResults); | 135 var compilerOpts = new CompilerOptions.fromArguments(argResults); |
134 var outPaths = argResults['out'] as List<String>; | 136 var outPaths = argResults['out'] as List<String>; |
135 var moduleFormats = parseModuleFormatOption(argResults); | 137 var moduleFormats = parseModuleFormatOption(argResults); |
136 bool singleOutFile = argResults['single-out-file']; | 138 bool singleOutFile = argResults['single-out-file']; |
137 if (singleOutFile) { | 139 if (singleOutFile) { |
138 for (var format in moduleFormats) { | 140 for (var format in moduleFormats) { |
139 if (format != ModuleFormat.amd && format != ModuleFormat.legacy) { | 141 if (format != ModuleFormat.amd && format != ModuleFormat.legacy) { |
140 _usageException('Format $format cannot be combined with ' | 142 _usageException('Format $format cannot be combined with ' |
141 'single-out-file. Only amd and legacy modes are supported.'); | 143 'single-out-file. Only amd and legacy modes are supported.'); |
(...skipping 30 matching lines...) Expand all Loading... |
172 modulePath = | 174 modulePath = |
173 path.withoutExtension(path.relative(firstOutPath, from: moduleRoot)); | 175 path.withoutExtension(path.relative(firstOutPath, from: moduleRoot)); |
174 } else { | 176 } else { |
175 moduleRoot = path.dirname(firstOutPath); | 177 moduleRoot = path.dirname(firstOutPath); |
176 modulePath = path.basenameWithoutExtension(firstOutPath); | 178 modulePath = path.basenameWithoutExtension(firstOutPath); |
177 } | 179 } |
178 | 180 |
179 var unit = new BuildUnit(modulePath, libraryRoot, argResults.rest, | 181 var unit = new BuildUnit(modulePath, libraryRoot, argResults.rest, |
180 (source) => _moduleForLibrary(moduleRoot, source, compilerOpts)); | 182 (source) => _moduleForLibrary(moduleRoot, source, compilerOpts)); |
181 | 183 |
182 var module = compiler.compile(unit, compilerOpts); | 184 JSModuleFile module = await compiler.compile(unit, compilerOpts); |
183 module.errors.forEach(printFn); | 185 module.errors.forEach(printFn); |
184 | 186 |
185 if (!module.isValid) throw new CompileErrorException(); | 187 if (!module.isValid) throw new CompileErrorException(); |
186 | 188 |
187 // Write JS file, as well as source map and summary (if requested). | 189 // Write JS file, as well as source map and summary (if requested). |
188 for (var i = 0; i < outPaths.length; i++) { | 190 for (var i = 0; i < outPaths.length; i++) { |
189 module.writeCodeSync(moduleFormats[i], outPaths[i], | 191 module.writeCodeSync(moduleFormats[i], outPaths[i], |
190 singleOutFile: singleOutFile); | 192 singleOutFile: singleOutFile); |
191 } | 193 } |
192 if (module.summaryBytes != null) { | 194 if (module.summaryBytes != null) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 } | 252 } |
251 | 253 |
252 void _usageException(String message) { | 254 void _usageException(String message) { |
253 throw new UsageException(message, _usageMessage); | 255 throw new UsageException(message, _usageMessage); |
254 } | 256 } |
255 | 257 |
256 /// Thrown when the input source code has errors. | 258 /// Thrown when the input source code has errors. |
257 class CompileErrorException implements Exception { | 259 class CompileErrorException implements Exception { |
258 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; | 260 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; |
259 } | 261 } |
OLD | NEW |