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 defineDDCAnalysisArguments, | 8 defineAnalysisArguments, |
9 extractDefinedVariables, | |
10 filterUnknownArguments, | 9 filterUnknownArguments, |
11 ignoreUnrecognizedFlagsFlag; | 10 ignoreUnrecognizedFlagsFlag; |
12 import 'package:analyzer/src/generated/source.dart' show Source; | 11 import 'package:analyzer/src/generated/source.dart' show Source; |
13 import 'package:analyzer/src/summary/package_bundle_reader.dart' | 12 import 'package:analyzer/src/summary/package_bundle_reader.dart' |
14 show InSummarySource; | 13 show InSummarySource; |
15 import 'package:args/args.dart' show ArgParser, ArgResults; | 14 import 'package:args/args.dart' show ArgParser, ArgResults; |
16 import 'package:args/command_runner.dart' show UsageException; | 15 import 'package:args/command_runner.dart' show UsageException; |
17 import 'package:path/path.dart' as path; | 16 import 'package:path/path.dart' as path; |
18 | 17 |
19 import '../analyzer/context.dart' show AnalyzerOptions; | 18 import '../analyzer/context.dart' show AnalyzerOptions; |
20 import 'compiler.dart' show BuildUnit, CompilerOptions, ModuleCompiler; | 19 import 'compiler.dart' show BuildUnit, CompilerOptions, ModuleCompiler; |
21 import 'module_builder.dart'; | 20 import 'module_builder.dart'; |
22 | 21 |
23 bool _verbose = false; | 22 bool _verbose = false; |
24 | 23 |
25 /// Runs a single compile for dartdevc. | 24 /// Runs a single compile for dartdevc. |
26 /// | 25 /// |
27 /// This handles argument parsing, usage, error handling. | 26 /// This handles argument parsing, usage, error handling. |
28 /// 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 |
29 /// worker support. | 28 /// worker support. |
30 int compile(List<String> args, {void printFn(Object obj)}) { | 29 int compile(List<String> args, {void printFn(Object obj)}) { |
31 printFn ??= print; | 30 printFn ??= print; |
| 31 |
32 ArgResults argResults; | 32 ArgResults argResults; |
33 var declaredVars = <String, String>{}; | 33 AnalyzerOptions analyzerOptions; |
34 try { | 34 try { |
35 args = extractDefinedVariables(args, declaredVars); | |
36 var parser = _argParser(); | 35 var parser = _argParser(); |
37 if (args.contains('--$ignoreUnrecognizedFlagsFlag')) { | 36 if (args.contains('--$ignoreUnrecognizedFlagsFlag')) { |
38 args = filterUnknownArguments(args, parser); | 37 args = filterUnknownArguments(args, parser); |
39 } | 38 } |
40 argResults = parser.parse(args); | 39 argResults = parser.parse(args); |
41 _verbose = argResults['verbose']; | 40 analyzerOptions = new AnalyzerOptions.fromArguments(argResults); |
42 } on FormatException catch (error) { | 41 } on FormatException catch (error) { |
43 printFn('$error\n\n$_usageMessage'); | 42 printFn('$error\n\n$_usageMessage'); |
44 return 64; | 43 return 64; |
45 } | 44 } |
| 45 |
| 46 _verbose = argResults['verbose']; |
| 47 if (argResults['help']) { |
| 48 printFn(_usageMessage); |
| 49 return 0; |
| 50 } |
| 51 |
46 try { | 52 try { |
47 _compile(argResults, declaredVars, printFn); | 53 _compile(argResults, analyzerOptions, printFn); |
48 return 0; | 54 return 0; |
49 } on UsageException catch (error) { | 55 } on UsageException catch (error) { |
50 // Incorrect usage, input file not found, etc. | 56 // Incorrect usage, input file not found, etc. |
51 printFn(error); | 57 printFn(error); |
52 return 64; | 58 return 64; |
53 } on CompileErrorException catch (error) { | 59 } on CompileErrorException catch (error) { |
54 // Code has error(s) and failed to compile. | 60 // Code has error(s) and failed to compile. |
55 printFn(error); | 61 printFn(error); |
56 return 1; | 62 return 1; |
57 } catch (error, stackTrace) { | 63 } catch (error, stackTrace) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 defaultsTo: false, | 96 defaultsTo: false, |
91 negatable: false) | 97 negatable: false) |
92 ..addOption('out', | 98 ..addOption('out', |
93 abbr: 'o', allowMultiple: true, help: 'Output file (required).') | 99 abbr: 'o', allowMultiple: true, help: 'Output file (required).') |
94 ..addOption('module-root', | 100 ..addOption('module-root', |
95 help: 'Root module directory.\n' | 101 help: 'Root module directory.\n' |
96 'Generated module paths are relative to this root.') | 102 'Generated module paths are relative to this root.') |
97 ..addOption('library-root', | 103 ..addOption('library-root', |
98 help: 'Root of source files.\n' | 104 help: 'Root of source files.\n' |
99 'Generated library names are relative to this root.'); | 105 'Generated library names are relative to this root.'); |
100 defineDDCAnalysisArguments(argParser, hide: hide); | 106 defineAnalysisArguments(argParser, hide: hide, ddc: true); |
101 addModuleFormatOptions(argParser, allowMultiple: true, hide: hide); | 107 addModuleFormatOptions(argParser, allowMultiple: true, hide: hide); |
102 AnalyzerOptions.addArguments(argParser, hide: hide); | 108 AnalyzerOptions.addArguments(argParser, hide: hide); |
103 CompilerOptions.addArguments(argParser, hide: hide); | 109 CompilerOptions.addArguments(argParser, hide: hide); |
104 return argParser; | 110 return argParser; |
105 } | 111 } |
106 | 112 |
107 bool _changed(List<int> list1, List<int> list2) { | 113 bool _changed(List<int> list1, List<int> list2) { |
108 var length = list1.length; | 114 var length = list1.length; |
109 if (length != list2.length) return true; | 115 if (length != list2.length) return true; |
110 for (var i = 0; i < length; ++i) { | 116 for (var i = 0; i < length; ++i) { |
111 if (list1[i] != list2[i]) return true; | 117 if (list1[i] != list2[i]) return true; |
112 } | 118 } |
113 return false; | 119 return false; |
114 } | 120 } |
115 | 121 |
116 void _compile(ArgResults argResults, Map<String, String> declaredVars, | 122 void _compile(ArgResults argResults, AnalyzerOptions analyzerOptions, |
117 void printFn(Object obj)) { | 123 void printFn(Object obj)) { |
118 if (argResults['help']) { | 124 var compiler = new ModuleCompiler(analyzerOptions); |
119 printFn(_usageMessage); | |
120 return; | |
121 } | |
122 var compiler = new ModuleCompiler( | |
123 new AnalyzerOptions.fromArguments(argResults, declaredVars)); | |
124 var compilerOpts = new CompilerOptions.fromArguments(argResults); | 125 var compilerOpts = new CompilerOptions.fromArguments(argResults); |
125 var outPaths = argResults['out'] as List<String>; | 126 var outPaths = argResults['out'] as List<String>; |
126 var moduleFormats = parseModuleFormatOption(argResults); | 127 var moduleFormats = parseModuleFormatOption(argResults); |
127 bool singleOutFile = argResults['single-out-file']; | 128 bool singleOutFile = argResults['single-out-file']; |
128 if (singleOutFile) { | 129 if (singleOutFile) { |
129 for (var format in moduleFormats) { | 130 for (var format in moduleFormats) { |
130 if (format != ModuleFormat.amd && format != ModuleFormat.legacy) { | 131 if (format != ModuleFormat.amd && format != ModuleFormat.legacy) { |
131 _usageException('Format $format cannot be combined with ' | 132 _usageException('Format $format cannot be combined with ' |
132 'single-out-file. Only amd and legacy modes are supported.'); | 133 'single-out-file. Only amd and legacy modes are supported.'); |
133 } | 134 } |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 '\n\n${_argParser(hide: !_verbose).usage}'; | 228 '\n\n${_argParser(hide: !_verbose).usage}'; |
228 | 229 |
229 void _usageException(String message) { | 230 void _usageException(String message) { |
230 throw new UsageException(message, _usageMessage); | 231 throw new UsageException(message, _usageMessage); |
231 } | 232 } |
232 | 233 |
233 /// Thrown when the input source code has errors. | 234 /// Thrown when the input source code has errors. |
234 class CompileErrorException implements Exception { | 235 class CompileErrorException implements Exception { |
235 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; | 236 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; |
236 } | 237 } |
OLD | NEW |