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:dev_compiler/config.dart'; | 12 import 'package:dev_compiler/config.dart'; |
13 import 'package:logging/logging.dart' show Level; | 13 import 'package:logging/logging.dart' show Level; |
14 import 'package:path/path.dart' as path; | 14 import 'package:path/path.dart' as path; |
15 import 'package:yaml/yaml.dart'; | 15 import 'package:yaml/yaml.dart'; |
16 | 16 |
17 /// Options used by our checker. | 17 /// Options used by our checker. |
18 // TODO(jmesserly): move useMultiPackage/packageRoot to CompilerOptions. | 18 abstract class ResolverOptions { |
19 class ResolverOptions { | |
20 /// Whether to resolve 'package:' uris using the multi-package resolver. | |
21 final bool useMultiPackage; | |
vsm
2015/06/03 15:37:47
Seems a little weird to move these out of Resolver
Jennifer Messerly
2015/06/03 16:24:11
Hmmm. What is "resolution" in this context? Analyz
| |
22 | |
23 /// Package root when resolving 'package:' urls the standard way. | |
24 final String packageRoot; | |
25 | |
26 /// List of paths used for the multi-package resolver. | |
27 final List<String> packagePaths; | |
28 | |
29 /// List of additional non-Dart resources to resolve and serve. | |
30 final List<String> resources; | |
31 | |
32 /// Whether to infer return types and field types from overriden members. | 19 /// Whether to infer return types and field types from overriden members. |
33 final bool inferFromOverrides; | 20 bool get inferFromOverrides; |
34 static const inferFromOverridesDefault = true; | 21 static const inferFromOverridesDefault = true; |
35 | 22 |
36 /// Whether to infer types for consts and fields by looking at initializers on | 23 /// Whether to infer types for consts and fields by looking at initializers on |
37 /// the RHS. For example, in a constant declaration like: | 24 /// the RHS. For example, in a constant declaration like: |
38 /// | 25 /// |
39 /// const A = B; | 26 /// const A = B; |
40 /// | 27 /// |
41 /// We can infer the type of `A` based on the type of `B`. | 28 /// We can infer the type of `A` based on the type of `B`. |
42 /// | 29 /// |
43 /// The inference algorithm determines what variables depend on others, and | 30 /// The inference algorithm determines what variables depend on others, and |
44 /// computes types by visiting the variable dependency graph in topological | 31 /// computes types by visiting the variable dependency graph in topological |
45 /// order. This ensures that the inferred type is deterministic when applying | 32 /// order. This ensures that the inferred type is deterministic when applying |
46 /// inference on library cycles. | 33 /// inference on library cycles. |
47 /// | 34 /// |
48 /// When this feature is turned off, we don't use the type of `B` to infer the | 35 /// When this feature is turned off, we don't use the type of `B` to infer the |
49 /// type of `A`, even if `B` has a declared type. | 36 /// type of `A`, even if `B` has a declared type. |
50 final bool inferTransitively; | 37 bool get inferTransitively; |
51 static const inferTransitivelyDefault = true; | 38 static const inferTransitivelyDefault = true; |
52 | 39 |
53 /// Restrict inference of fields and top-levels to those that are final and | 40 /// Restrict inference of fields and top-levels to those that are final and |
54 /// const. | 41 /// const. |
55 final bool onlyInferConstsAndFinalFields; | 42 bool get onlyInferConstsAndFinalFields; |
56 static const onlyInferConstAndFinalFieldsDefault = false; | 43 static const onlyInferConstAndFinalFieldsDefault = false; |
57 | 44 |
58 /// File where to start compilation from. | 45 /// File where to start compilation from. |
59 final String entryPointFile; | 46 String get entryPointFile; |
60 | 47 |
61 // True if the resolver should implicitly provide an html entry point. | 48 // True if the resolver should implicitly provide an html entry point. |
62 final bool useImplicitHtml; | 49 bool get useImplicitHtml; |
63 static const String implicitHtmlFile = 'index.html'; | 50 static const String implicitHtmlFile = 'index.html'; |
64 | |
65 ResolverOptions({this.useMultiPackage: false, this.packageRoot: 'packages/', | |
66 this.packagePaths: const <String>[], this.resources: const <String>[], | |
67 this.inferFromOverrides: inferFromOverridesDefault, | |
68 this.inferTransitively: inferTransitivelyDefault, | |
69 this.onlyInferConstsAndFinalFields: onlyInferConstAndFinalFieldsDefault, | |
70 this.entryPointFile: null, this.useImplicitHtml: false}); | |
71 } | 51 } |
72 | 52 |
73 // TODO(vsm): Merge RulesOptions and TypeOptions | 53 // TODO(vsm): Merge RulesOptions and TypeOptions |
74 /// Options used by our RestrictedRules. | 54 /// Options used by our RestrictedRules. |
75 class RulesOptions extends TypeOptions { | 55 class RulesOptions extends TypeOptions { |
76 /// Whether to allow casts in constant contexts. | 56 /// Whether to allow casts in constant contexts. |
77 final bool allowConstCasts; | 57 final bool allowConstCasts; |
78 | 58 |
79 /// Whether to use covariant generics | 59 /// Whether to use covariant generics |
80 final bool covariantGenerics; | 60 final bool covariantGenerics; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 final String host; | 154 final String host; |
175 | 155 |
176 /// Whether to use covariant generics | 156 /// Whether to use covariant generics |
177 @override | 157 @override |
178 final bool covariantGenerics; | 158 final bool covariantGenerics; |
179 | 159 |
180 /// Whether to inject casts between Dart assignable types. | 160 /// Whether to inject casts between Dart assignable types. |
181 @override | 161 @override |
182 final bool relaxedCasts; | 162 final bool relaxedCasts; |
183 | 163 |
164 /// Custom URI mappings, such as "dart:foo" -> "path/to/foo.dart" | |
165 final Map<String, String> customUrlMappings; | |
166 | |
184 /// Whether to resolve 'package:' uris using the multi-package resolver. | 167 /// Whether to resolve 'package:' uris using the multi-package resolver. |
185 @override | |
186 final bool useMultiPackage; | 168 final bool useMultiPackage; |
187 | 169 |
188 /// Package root when resolving 'package:' urls the standard way. | 170 /// Package root when resolving 'package:' urls the standard way. |
189 @override | |
190 final String packageRoot; | 171 final String packageRoot; |
191 | 172 |
192 /// List of paths used for the multi-package resolver. | 173 /// List of paths used for the multi-package resolver. |
193 @override | |
194 final List<String> packagePaths; | 174 final List<String> packagePaths; |
195 | 175 |
196 /// List of additional non-Dart resources to resolve and serve. | 176 /// List of additional non-Dart resources to resolve and serve. |
197 @override | |
198 final List<String> resources; | 177 final List<String> resources; |
199 | 178 |
200 /// Whether to infer types downwards from local context | 179 /// Whether to infer types downwards from local context |
201 @override | 180 @override |
202 final bool inferDownwards; | 181 final bool inferDownwards; |
203 | 182 |
204 /// Whether to infer return types and field types from overriden members. | 183 /// Whether to infer return types and field types from overriden members. |
205 @override | 184 @override |
206 final bool inferFromOverrides; | 185 final bool inferFromOverrides; |
207 | 186 |
(...skipping 28 matching lines...) Expand all Loading... | |
236 /// package (if we can infer where that is located). | 215 /// package (if we can infer where that is located). |
237 final String runtimeDir; | 216 final String runtimeDir; |
238 | 217 |
239 CompilerOptions({this.allowConstCasts: true, this.checkSdk: false, | 218 CompilerOptions({this.allowConstCasts: true, this.checkSdk: false, |
240 this.dumpInfo: false, this.dumpInfoFile, this.dumpSrcDir, | 219 this.dumpInfo: false, this.dumpInfoFile, this.dumpSrcDir, |
241 this.forceCompile: false, this.formatOutput: false, | 220 this.forceCompile: false, this.formatOutput: false, |
242 this.ignoreTypes: false, | 221 this.ignoreTypes: false, |
243 this.wrapClosures: RulesOptions.wrapClosuresDefault, this.outputDir, | 222 this.wrapClosures: RulesOptions.wrapClosuresDefault, this.outputDir, |
244 this.outputDart: false, this.useColors: true, | 223 this.outputDart: false, this.useColors: true, |
245 this.covariantGenerics: true, this.relaxedCasts: true, | 224 this.covariantGenerics: true, this.relaxedCasts: true, |
246 this.useMultiPackage: false, this.packageRoot: 'packages/', | 225 this.customUrlMappings: const {}, this.useMultiPackage: false, |
247 this.packagePaths: const <String>[], this.resources: const <String>[], | 226 this.packageRoot: 'packages/', this.packagePaths: const <String>[], |
227 this.resources: const <String>[], | |
248 this.inferDownwards: RulesOptions.inferDownwardsDefault, | 228 this.inferDownwards: RulesOptions.inferDownwardsDefault, |
249 this.inferFromOverrides: ResolverOptions.inferFromOverridesDefault, | 229 this.inferFromOverrides: ResolverOptions.inferFromOverridesDefault, |
250 this.inferTransitively: ResolverOptions.inferTransitivelyDefault, | 230 this.inferTransitively: ResolverOptions.inferTransitivelyDefault, |
251 this.onlyInferConstsAndFinalFields: ResolverOptions.onlyInferConstAndFinal FieldsDefault, | 231 this.onlyInferConstsAndFinalFields: ResolverOptions.onlyInferConstAndFinal FieldsDefault, |
252 this.nonnullableTypes: TypeOptions.NONNULLABLE_TYPES, this.help: false, | 232 this.nonnullableTypes: TypeOptions.NONNULLABLE_TYPES, this.help: false, |
253 this.useMockSdk: false, this.dartSdkPath, this.logLevel: Level.SEVERE, | 233 this.useMockSdk: false, this.dartSdkPath, this.logLevel: Level.SEVERE, |
254 this.emitSourceMaps: true, this.entryPointFile: null, | 234 this.emitSourceMaps: true, this.entryPointFile: null, |
255 this.serverMode: false, this.useImplicitHtml: false, | 235 this.serverMode: false, this.useImplicitHtml: false, |
256 this.enableHashing: false, this.host: 'localhost', this.port: 8080, | 236 this.enableHashing: false, this.host: 'localhost', this.port: 8080, |
257 this.runtimeDir}); | 237 this.runtimeDir}); |
258 } | 238 } |
259 | 239 |
260 /// Parses options from the command-line | 240 /// Parses options from the command-line |
261 CompilerOptions parseOptions(List<String> argv) { | 241 CompilerOptions parseOptions(List<String> argv) { |
262 ArgResults args = argParser.parse(argv); | 242 ArgResults args = argParser.parse(argv); |
243 bool showUsage = args['help']; | |
244 | |
263 var serverMode = args['server']; | 245 var serverMode = args['server']; |
264 var enableHashing = args['hashing']; | 246 var enableHashing = args['hashing']; |
265 if (enableHashing == null) { | 247 if (enableHashing == null) { |
266 enableHashing = serverMode; | 248 enableHashing = serverMode; |
267 } | 249 } |
268 // TODO(jmesserly): shouldn't level always default to warning? | 250 // TODO(jmesserly): shouldn't level always default to warning? |
269 var logLevel = serverMode ? Level.WARNING : Level.SEVERE; | 251 var logLevel = serverMode ? Level.WARNING : Level.SEVERE; |
270 var levelName = args['log']; | 252 var levelName = args['log']; |
271 if (levelName != null) { | 253 if (levelName != null) { |
272 levelName = levelName.toUpperCase(); | 254 levelName = levelName.toUpperCase(); |
273 logLevel = Level.LEVELS.firstWhere((l) => l.name == levelName, | 255 logLevel = Level.LEVELS.firstWhere((l) => l.name == levelName, |
274 orElse: () => logLevel); | 256 orElse: () => logLevel); |
275 } | 257 } |
276 var useColors = stdioType(stdout) == StdioType.TERMINAL; | 258 var useColors = stdioType(stdout) == StdioType.TERMINAL; |
277 var sdkPath = args['dart-sdk']; | 259 var sdkPath = args['dart-sdk']; |
278 if (sdkPath == null && !args['mock-sdk']) { | 260 if (sdkPath == null && !args['mock-sdk']) { |
279 sdkPath = getSdkDir(argv).path; | 261 sdkPath = getSdkDir(argv).path; |
280 } | 262 } |
281 var runtimeDir = args['runtime-dir']; | 263 var runtimeDir = args['runtime-dir']; |
282 if (runtimeDir == null) { | 264 if (runtimeDir == null) { |
283 runtimeDir = _computeRuntimeDir(); | 265 runtimeDir = _computeRuntimeDir(); |
284 } | 266 } |
285 var outputDir = args['out']; | 267 var outputDir = args['out']; |
286 if (outputDir == null && serverMode) { | 268 if (outputDir == null && serverMode) { |
287 outputDir = Directory.systemTemp.createTempSync("dev_compiler_out_").path; | 269 outputDir = Directory.systemTemp.createTempSync("dev_compiler_out_").path; |
288 } | 270 } |
289 var dumpInfo = args['dump-info']; | 271 var dumpInfo = args['dump-info']; |
290 if (dumpInfo == null) dumpInfo = serverMode; | 272 if (dumpInfo == null) dumpInfo = serverMode; |
291 | 273 |
274 var customUrlMappings = <String, String>{}; | |
275 for (var mapping in args['url-mapping']) { | |
276 var splitMapping = mapping.split(','); | |
277 if (splitMapping.length != 2) { | |
278 showUsage = true; | |
279 continue; | |
280 } | |
281 customUrlMappings[splitMapping[0]] = splitMapping[1]; | |
282 } | |
283 | |
292 var entryPointFile = args.rest.length == 0 ? null : args.rest.first; | 284 var entryPointFile = args.rest.length == 0 ? null : args.rest.first; |
293 | 285 |
294 return new CompilerOptions( | 286 return new CompilerOptions( |
295 allowConstCasts: args['allow-const-casts'], | 287 allowConstCasts: args['allow-const-casts'], |
296 checkSdk: args['sdk-check'], | 288 checkSdk: args['sdk-check'], |
297 dumpInfo: dumpInfo, | 289 dumpInfo: dumpInfo, |
298 dumpInfoFile: args['dump-info-file'], | 290 dumpInfoFile: args['dump-info-file'], |
299 dumpSrcDir: args['dump-src-to'], | 291 dumpSrcDir: args['dump-src-to'], |
300 forceCompile: args['force-compile'] || serverMode, | 292 forceCompile: args['force-compile'] || serverMode, |
301 formatOutput: args['dart-gen-fmt'], | 293 formatOutput: args['dart-gen-fmt'], |
302 ignoreTypes: args['ignore-types'], | 294 ignoreTypes: args['ignore-types'], |
303 wrapClosures: args['wrap-closures'], | 295 wrapClosures: args['wrap-closures'], |
304 outputDart: args['dart-gen'], | 296 outputDart: args['dart-gen'], |
305 outputDir: outputDir, | 297 outputDir: outputDir, |
306 covariantGenerics: args['covariant-generics'], | 298 covariantGenerics: args['covariant-generics'], |
307 relaxedCasts: args['relaxed-casts'], | 299 relaxedCasts: args['relaxed-casts'], |
308 useColors: useColors, | 300 useColors: useColors, |
301 customUrlMappings: customUrlMappings, | |
309 useMultiPackage: args['use-multi-package'], | 302 useMultiPackage: args['use-multi-package'], |
310 packageRoot: args['package-root'], | 303 packageRoot: args['package-root'], |
311 packagePaths: args['package-paths'].split(','), | 304 packagePaths: args['package-paths'].split(','), |
312 resources: args['resources'] | 305 resources: args['resources'] |
313 .split(',') | 306 .split(',') |
314 .where((s) => s.isNotEmpty) | 307 .where((s) => s.isNotEmpty) |
315 .toList(), | 308 .toList(), |
316 inferDownwards: args['infer-downwards'], | 309 inferDownwards: args['infer-downwards'], |
317 inferFromOverrides: args['infer-from-overrides'], | 310 inferFromOverrides: args['infer-from-overrides'], |
318 inferTransitively: args['infer-transitively'], | 311 inferTransitively: args['infer-transitively'], |
319 onlyInferConstsAndFinalFields: args['infer-only-finals'], | 312 onlyInferConstsAndFinalFields: args['infer-only-finals'], |
320 nonnullableTypes: optionsToList(args['nonnullable'], | 313 nonnullableTypes: optionsToList(args['nonnullable'], |
321 defaultValue: TypeOptions.NONNULLABLE_TYPES), | 314 defaultValue: TypeOptions.NONNULLABLE_TYPES), |
322 help: args['help'], | 315 help: showUsage, |
323 useMockSdk: args['mock-sdk'], | 316 useMockSdk: args['mock-sdk'], |
324 dartSdkPath: sdkPath, | 317 dartSdkPath: sdkPath, |
325 logLevel: logLevel, | 318 logLevel: logLevel, |
326 emitSourceMaps: args['source-maps'], | 319 emitSourceMaps: args['source-maps'], |
327 entryPointFile: entryPointFile, | 320 entryPointFile: entryPointFile, |
328 serverMode: serverMode, | 321 serverMode: serverMode, |
329 useImplicitHtml: serverMode && entryPointFile.endsWith('.dart'), | 322 useImplicitHtml: serverMode && entryPointFile.endsWith('.dart'), |
330 enableHashing: enableHashing, | 323 enableHashing: enableHashing, |
331 host: args['host'], | 324 host: args['host'], |
332 port: int.parse(args['port']), | 325 port: int.parse(args['port']), |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
373 ..addOption('dart-sdk', help: 'Dart SDK Path', defaultsTo: null) | 366 ..addOption('dart-sdk', help: 'Dart SDK Path', defaultsTo: null) |
374 ..addFlag('dart-gen', | 367 ..addFlag('dart-gen', |
375 abbr: 'd', help: 'Generate dart output', defaultsTo: false) | 368 abbr: 'd', help: 'Generate dart output', defaultsTo: false) |
376 ..addFlag('dart-gen-fmt', | 369 ..addFlag('dart-gen-fmt', |
377 help: 'Generate readable dart output', defaultsTo: true) | 370 help: 'Generate readable dart output', defaultsTo: true) |
378 ..addOption('dump-src-to', help: 'Dump dart src code', defaultsTo: null) | 371 ..addOption('dump-src-to', help: 'Dump dart src code', defaultsTo: null) |
379 ..addOption('package-root', | 372 ..addOption('package-root', |
380 abbr: 'p', | 373 abbr: 'p', |
381 help: 'Package root to resolve "package:" imports', | 374 help: 'Package root to resolve "package:" imports', |
382 defaultsTo: 'packages/') | 375 defaultsTo: 'packages/') |
376 ..addOption('url-mapping', | |
377 help: '--url-mapping=libraryUri,/path/to/library.dart uses library.dart\n' | |
378 'as the source for an import of of "libraryUri".', | |
379 allowMultiple: true, | |
380 splitCommas: false) | |
383 ..addFlag('use-multi-package', | 381 ..addFlag('use-multi-package', |
384 help: 'Whether to use the multi-package resolver for "package:" imports', | 382 help: 'Whether to use the multi-package resolver for "package:" imports', |
385 defaultsTo: false) | 383 defaultsTo: false) |
386 ..addOption('package-paths', | 384 ..addOption('package-paths', |
387 help: 'if using the multi-package resolver, the list of directories to\n' | 385 help: 'if using the multi-package resolver, the list of directories to\n' |
388 'look for packages in.', defaultsTo: '') | 386 'look for packages in.', defaultsTo: '') |
389 ..addOption('resources', | 387 ..addOption('resources', |
390 help: 'Additional resources to serve', defaultsTo: '') | 388 help: 'Additional resources to serve', defaultsTo: '') |
391 ..addFlag('source-maps', | 389 ..addFlag('source-maps', |
392 help: 'Whether to emit source map files', defaultsTo: true) | 390 help: 'Whether to emit source map files', defaultsTo: true) |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
458 // The pub-cache directory is two levels up, but we verify that the layout | 456 // The pub-cache directory is two levels up, but we verify that the layout |
459 // looks correct. | 457 // looks correct. |
460 if (path.basename(dir) != 'dev_compiler') return null; | 458 if (path.basename(dir) != 'dev_compiler') return null; |
461 dir = path.dirname(dir); | 459 dir = path.dirname(dir); |
462 if (path.basename(dir) != 'global_packages') return null; | 460 if (path.basename(dir) != 'global_packages') return null; |
463 dir = path.dirname(dir); | 461 dir = path.dirname(dir); |
464 return path.join(dir, cacheDir, 'lib', 'runtime'); | 462 return path.join(dir, cacheDir, 'lib', 'runtime'); |
465 } | 463 } |
466 return null; | 464 return null; |
467 } | 465 } |
OLD | NEW |