OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 /// Set of flags and options passed to the compiler | 5 /// Set of flags and options passed to the compiler |
6 library dev_compiler.src.options; | 6 library dev_compiler.src.options; |
7 | 7 |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import 'package:args/args.dart'; | 10 import 'package:args/args.dart'; |
11 import 'package:cli_util/cli_util.dart' show getSdkDir; | 11 import 'package:cli_util/cli_util.dart' show getSdkDir; |
12 import 'package:logging/logging.dart' show Level; | 12 import 'package:logging/logging.dart' show Level; |
13 import 'package:path/path.dart' as path; | 13 import 'package:path/path.dart' as path; |
14 import 'package:yaml/yaml.dart'; | 14 import 'package:yaml/yaml.dart'; |
15 | 15 |
| 16 import 'utils.dart' show parseEnum, getEnumName; |
| 17 |
16 const String _V8_BINARY_DEFAULT = 'node'; | 18 const String _V8_BINARY_DEFAULT = 'node'; |
17 const bool _CLOSURE_DEFAULT = false; | 19 const bool _CLOSURE_DEFAULT = false; |
18 const bool _DESTRUCTURE_NAMED_PARAMS_DEFAULT = false; | 20 const bool _DESTRUCTURE_NAMED_PARAMS_DEFAULT = false; |
19 | 21 |
20 /// Options used to set up Source URI resolution in the analysis context. | 22 /// Options used to set up Source URI resolution in the analysis context. |
21 class SourceResolverOptions { | 23 class SourceResolverOptions { |
22 /// Whether to resolve 'package:' uris using the multi-package resolver. | 24 /// Whether to resolve 'package:' uris using the multi-package resolver. |
23 final bool useMultiPackage; | 25 final bool useMultiPackage; |
24 | 26 |
25 /// Custom URI mappings, such as "dart:foo" -> "path/to/foo.dart" | 27 /// Custom URI mappings, such as "dart:foo" -> "path/to/foo.dart" |
(...skipping 23 matching lines...) Expand all Loading... |
49 {this.useMockSdk: false, | 51 {this.useMockSdk: false, |
50 this.dartSdkPath, | 52 this.dartSdkPath, |
51 this.useMultiPackage: false, | 53 this.useMultiPackage: false, |
52 this.customUrlMappings: const {}, | 54 this.customUrlMappings: const {}, |
53 this.packageRoot: 'packages/', | 55 this.packageRoot: 'packages/', |
54 this.packagePaths: const <String>[], | 56 this.packagePaths: const <String>[], |
55 this.resources: const <String>[], | 57 this.resources: const <String>[], |
56 this.useImplicitHtml: false}); | 58 this.useImplicitHtml: false}); |
57 } | 59 } |
58 | 60 |
| 61 enum ModuleFormat { es6, legacy } |
| 62 ModuleFormat parseModuleFormat(String s) => parseEnum(s, ModuleFormat.values); |
| 63 |
59 // TODO(jmesserly): refactor all codegen options here. | 64 // TODO(jmesserly): refactor all codegen options here. |
60 class CodegenOptions { | 65 class CodegenOptions { |
61 /// Whether to emit the source map files. | 66 /// Whether to emit the source map files. |
62 final bool emitSourceMaps; | 67 final bool emitSourceMaps; |
63 | 68 |
64 /// Whether to force compilation of code with static errors. | 69 /// Whether to force compilation of code with static errors. |
65 final bool forceCompile; | 70 final bool forceCompile; |
66 | 71 |
67 /// Output directory for generated code. | 72 /// Output directory for generated code. |
68 final String outputDir; | 73 final String outputDir; |
69 | 74 |
70 /// Emit Closure Compiler-friendly code. | 75 /// Emit Closure Compiler-friendly code. |
71 final bool closure; | 76 final bool closure; |
72 | 77 |
73 /// Enable ES6 destructuring of named parameters. | 78 /// Enable ES6 destructuring of named parameters. |
74 final bool destructureNamedParams; | 79 final bool destructureNamedParams; |
75 | 80 |
76 /// Whether to emit a workaround for missing arrow function bind-this in | 81 /// Whether to emit a workaround for missing arrow function bind-this in |
77 /// other V8 builds | 82 /// other V8 builds |
78 final bool arrowFnBindThisWorkaround; | 83 final bool arrowFnBindThisWorkaround; |
79 | 84 |
80 /// Which module format to support. | 85 /// Which module format to support. |
81 /// Currently 'es6' and 'dart' are supported. | 86 /// Currently 'es6' and 'legacy' are supported. |
82 final String moduleFormat; | 87 final ModuleFormat moduleFormat; |
83 | 88 |
84 const CodegenOptions( | 89 const CodegenOptions( |
85 {this.emitSourceMaps: true, | 90 {this.emitSourceMaps: true, |
86 this.forceCompile: false, | 91 this.forceCompile: false, |
87 this.closure: _CLOSURE_DEFAULT, | 92 this.closure: _CLOSURE_DEFAULT, |
88 this.destructureNamedParams: _DESTRUCTURE_NAMED_PARAMS_DEFAULT, | 93 this.destructureNamedParams: _DESTRUCTURE_NAMED_PARAMS_DEFAULT, |
89 this.outputDir, | 94 this.outputDir, |
90 this.arrowFnBindThisWorkaround: false, | 95 this.arrowFnBindThisWorkaround: false, |
91 this.moduleFormat: 'dart'}); | 96 this.moduleFormat: ModuleFormat.legacy}); |
92 } | 97 } |
93 | 98 |
94 /// Options for devrun. | 99 /// Options for devrun. |
95 class RunnerOptions { | 100 class RunnerOptions { |
96 /// V8-based binary to be used to run the .js output (d8, iojs, node). | 101 /// V8-based binary to be used to run the .js output (d8, iojs, node). |
97 /// Can be just the executable name if it's in the path, or a path to the | 102 /// Can be just the executable name if it's in the path, or a path to the |
98 /// executable. | 103 /// executable. |
99 final String v8Binary; | 104 final String v8Binary; |
100 | 105 |
101 const RunnerOptions({this.v8Binary: _V8_BINARY_DEFAULT}); | 106 const RunnerOptions({this.v8Binary: _V8_BINARY_DEFAULT}); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 /// package (if we can infer where that is located). | 156 /// package (if we can infer where that is located). |
152 final String runtimeDir; | 157 final String runtimeDir; |
153 | 158 |
154 /// The files to compile. | 159 /// The files to compile. |
155 final List<String> inputs; | 160 final List<String> inputs; |
156 | 161 |
157 /// The base directory for [inputs]. Module imports will be generated relative | 162 /// The base directory for [inputs]. Module imports will be generated relative |
158 /// to this directory. | 163 /// to this directory. |
159 final String inputBaseDir; | 164 final String inputBaseDir; |
160 | 165 |
161 CompilerOptions( | 166 const CompilerOptions( |
162 {this.sourceOptions: const SourceResolverOptions(), | 167 {this.sourceOptions: const SourceResolverOptions(), |
163 this.codegenOptions: const CodegenOptions(), | 168 this.codegenOptions: const CodegenOptions(), |
164 this.runnerOptions: const RunnerOptions(), | 169 this.runnerOptions: const RunnerOptions(), |
165 this.checkSdk: false, | 170 this.checkSdk: false, |
166 this.dumpInfo: false, | 171 this.dumpInfo: false, |
167 this.htmlReport: false, | 172 this.htmlReport: false, |
168 this.dumpInfoFile, | 173 this.dumpInfoFile, |
169 this.useColors: true, | 174 this.useColors: true, |
170 this.help: false, | 175 this.help: false, |
171 this.version: false, | 176 this.version: false, |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 if (serverMode && args.rest.length != 1) showUsage = true; | 237 if (serverMode && args.rest.length != 1) showUsage = true; |
233 | 238 |
234 return new CompilerOptions( | 239 return new CompilerOptions( |
235 codegenOptions: new CodegenOptions( | 240 codegenOptions: new CodegenOptions( |
236 emitSourceMaps: args['source-maps'], | 241 emitSourceMaps: args['source-maps'], |
237 forceCompile: args['force-compile'] || serverMode, | 242 forceCompile: args['force-compile'] || serverMode, |
238 closure: args['closure'], | 243 closure: args['closure'], |
239 destructureNamedParams: args['destructure-named-params'], | 244 destructureNamedParams: args['destructure-named-params'], |
240 outputDir: outputDir, | 245 outputDir: outputDir, |
241 arrowFnBindThisWorkaround: args['arrow-fn-bind-this'], | 246 arrowFnBindThisWorkaround: args['arrow-fn-bind-this'], |
242 moduleFormat: args['modules']), | 247 moduleFormat: parseModuleFormat(args['modules'])), |
243 sourceOptions: new SourceResolverOptions( | 248 sourceOptions: new SourceResolverOptions( |
244 useMockSdk: args['mock-sdk'], | 249 useMockSdk: args['mock-sdk'], |
245 dartSdkPath: sdkPath, | 250 dartSdkPath: sdkPath, |
246 useImplicitHtml: serverMode && | 251 useImplicitHtml: serverMode && |
247 args.rest.length == 1 && | 252 args.rest.length == 1 && |
248 args.rest[0].endsWith('.dart'), | 253 args.rest[0].endsWith('.dart'), |
249 customUrlMappings: customUrlMappings, | 254 customUrlMappings: customUrlMappings, |
250 useMultiPackage: args['use-multi-package'], | 255 useMultiPackage: args['use-multi-package'], |
251 packageRoot: args['package-root'], | 256 packageRoot: args['package-root'], |
252 packagePaths: args['package-paths'].split(','), | 257 packagePaths: args['package-paths'].split(','), |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 ..addOption('resources', | 304 ..addOption('resources', |
300 help: 'Additional resources to serve', defaultsTo: '') | 305 help: 'Additional resources to serve', defaultsTo: '') |
301 ..addFlag('source-maps', | 306 ..addFlag('source-maps', |
302 help: 'Whether to emit source map files', defaultsTo: true) | 307 help: 'Whether to emit source map files', defaultsTo: true) |
303 ..addOption('runtime-dir', | 308 ..addOption('runtime-dir', |
304 help: 'Where to find dev_compiler\'s runtime files', defaultsTo: null) | 309 help: 'Where to find dev_compiler\'s runtime files', defaultsTo: null) |
305 ..addFlag('arrow-fn-bind-this', | 310 ..addFlag('arrow-fn-bind-this', |
306 help: 'Work around `this` binding in => functions') | 311 help: 'Work around `this` binding in => functions') |
307 ..addOption('modules', | 312 ..addOption('modules', |
308 help: 'Which module pattern to emit', | 313 help: 'Which module pattern to emit', |
309 allowed: ['es6', 'dart'], | 314 allowed: ModuleFormat.values.map(getEnumName).toList(), |
310 allowedHelp: { | 315 allowedHelp: { |
311 'es6': 'es6 modules', | 316 getEnumName(ModuleFormat.es6): 'es6 modules', |
312 'custom-dart': 'a custom format used by dartdevc, similar to AMD' | 317 getEnumName(ModuleFormat.legacy): |
| 318 'a custom format used by dartdevc, similar to AMD' |
313 }, | 319 }, |
314 defaultsTo: 'dart') | 320 defaultsTo: getEnumName(ModuleFormat.legacy)) |
315 | 321 |
316 // general options | 322 // general options |
317 ..addFlag('help', abbr: 'h', help: 'Display this message') | 323 ..addFlag('help', abbr: 'h', help: 'Display this message') |
318 ..addFlag('version', | 324 ..addFlag('version', |
319 negatable: false, help: 'Display the Dev Compiler verion') | 325 negatable: false, help: 'Display the Dev Compiler verion') |
320 ..addFlag('server', help: 'Run as a development server.', defaultsTo: false) | 326 ..addFlag('server', help: 'Run as a development server.', defaultsTo: false) |
321 ..addFlag('hashing', | 327 ..addFlag('hashing', |
322 help: 'Enable hash-based file caching.', defaultsTo: null) | 328 help: 'Enable hash-based file caching.', defaultsTo: null) |
323 ..addFlag('widget', | 329 ..addFlag('widget', |
324 help: 'Serve embedded widget with static errors and warnings.', | 330 help: 'Serve embedded widget with static errors and warnings.', |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 // The pub-cache directory is two levels up, but we verify that the layout | 409 // The pub-cache directory is two levels up, but we verify that the layout |
404 // looks correct. | 410 // looks correct. |
405 if (path.basename(dir) != 'dev_compiler') return null; | 411 if (path.basename(dir) != 'dev_compiler') return null; |
406 dir = path.dirname(dir); | 412 dir = path.dirname(dir); |
407 if (path.basename(dir) != 'global_packages') return null; | 413 if (path.basename(dir) != 'global_packages') return null; |
408 dir = path.dirname(dir); | 414 dir = path.dirname(dir); |
409 return path.join(dir, cacheDir, 'lib', 'runtime'); | 415 return path.join(dir, cacheDir, 'lib', 'runtime'); |
410 } | 416 } |
411 return null; | 417 return null; |
412 } | 418 } |
OLD | NEW |