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; |
| 15 import 'package:yaml/yaml.dart'; |
14 | 16 |
15 /// Options used by our TypeResolver. | 17 /// Options used by our TypeResolver. |
16 class ResolverOptions { | 18 class ResolverOptions { |
17 /// Whether to resolve 'package:' uris using the multi-package resolver. | 19 /// Whether to resolve 'package:' uris using the multi-package resolver. |
18 final bool useMultiPackage; | 20 final bool useMultiPackage; |
19 | 21 |
20 /// Package root when resolving 'package:' urls the standard way. | 22 /// Package root when resolving 'package:' urls the standard way. |
21 final String packageRoot; | 23 final String packageRoot; |
22 | 24 |
23 /// List of paths used for the multi-package resolver. | 25 /// List of paths used for the multi-package resolver. |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 final List<String> nonnullableTypes; | 186 final List<String> nonnullableTypes; |
185 | 187 |
186 /// Whether to use static types for code generation. | 188 /// Whether to use static types for code generation. |
187 @override | 189 @override |
188 final bool ignoreTypes; | 190 final bool ignoreTypes; |
189 | 191 |
190 /// Whether to emit the source map files. | 192 /// Whether to emit the source map files. |
191 @override | 193 @override |
192 final bool emitSourceMaps; | 194 final bool emitSourceMaps; |
193 | 195 |
| 196 /// Location for runtime files, such as `dart_core.js`. By default this is |
| 197 /// inferred to be under `lib/runtime/` in the location of the `dev_compiler` |
| 198 /// package (if we can infer where that is located). |
| 199 final String runtimeDir; |
| 200 |
194 CompilerOptions({this.allowConstCasts: true, this.checkSdk: false, | 201 CompilerOptions({this.allowConstCasts: true, this.checkSdk: false, |
195 this.dumpInfo: false, this.dumpInfoFile, this.dumpSrcDir, | 202 this.dumpInfo: false, this.dumpInfoFile, this.dumpSrcDir, |
196 this.forceCompile: false, this.formatOutput: false, | 203 this.forceCompile: false, this.formatOutput: false, |
197 this.cheapTestFormat: false, this.ignoreTypes: false, this.outputDir, | 204 this.cheapTestFormat: false, this.ignoreTypes: false, this.outputDir, |
198 this.outputDart: false, this.useColors: true, | 205 this.outputDart: false, this.useColors: true, |
199 this.covariantGenerics: true, this.relaxedCasts: true, | 206 this.covariantGenerics: true, this.relaxedCasts: true, |
200 this.useMultiPackage: false, this.packageRoot: 'packages/', | 207 this.useMultiPackage: false, this.packageRoot: 'packages/', |
201 this.packagePaths: const <String>[], | 208 this.packagePaths: const <String>[], |
202 this.inferFromOverrides: ResolverOptions.inferFromOverridesDefault, | 209 this.inferFromOverrides: ResolverOptions.inferFromOverridesDefault, |
203 this.inferTransitively: ResolverOptions.inferTransitivelyDefault, | 210 this.inferTransitively: ResolverOptions.inferTransitivelyDefault, |
204 this.onlyInferConstsAndFinalFields: ResolverOptions.onlyInferConstAndFinal
FieldsDefault, | 211 this.onlyInferConstsAndFinalFields: ResolverOptions.onlyInferConstAndFinal
FieldsDefault, |
205 this.nonnullableTypes: TypeOptions.NONNULLABLE_TYPES, this.help: false, | 212 this.nonnullableTypes: TypeOptions.NONNULLABLE_TYPES, this.help: false, |
206 this.useMockSdk: false, this.dartSdkPath, this.logLevel: Level.SEVERE, | 213 this.useMockSdk: false, this.dartSdkPath, this.logLevel: Level.SEVERE, |
207 this.emitSourceMaps: true, this.entryPointFile: null, | 214 this.emitSourceMaps: true, this.entryPointFile: null, |
208 this.serverMode: false, this.port: 8080}); | 215 this.serverMode: false, this.port: 8080, this.runtimeDir}); |
209 } | 216 } |
210 | 217 |
211 /// Parses options from the command-line | 218 /// Parses options from the command-line |
212 CompilerOptions parseOptions(List<String> argv) { | 219 CompilerOptions parseOptions(List<String> argv) { |
213 ArgResults args = argParser.parse(argv); | 220 ArgResults args = argParser.parse(argv); |
214 var levelName = args['log'].toUpperCase(); | 221 var levelName = args['log'].toUpperCase(); |
215 var useColors = stdioType(stdout) == StdioType.TERMINAL; | 222 var useColors = stdioType(stdout) == StdioType.TERMINAL; |
216 var sdkPath = args['dart-sdk']; | 223 var sdkPath = args['dart-sdk']; |
217 if (sdkPath == null && !args['mock-sdk']) { | 224 if (sdkPath == null && !args['mock-sdk']) { |
218 sdkPath = getSdkDir(argv).path; | 225 sdkPath = getSdkDir(argv).path; |
219 } | 226 } |
| 227 var runtimeDir = args['runtime-dir']; |
| 228 if (runtimeDir == null) { |
| 229 runtimeDir = _computeRuntimeDir(); |
| 230 } |
220 return new CompilerOptions( | 231 return new CompilerOptions( |
221 allowConstCasts: args['allow-const-casts'], | 232 allowConstCasts: args['allow-const-casts'], |
222 checkSdk: args['sdk-check'], | 233 checkSdk: args['sdk-check'], |
223 dumpInfo: args['dump-info'], | 234 dumpInfo: args['dump-info'], |
224 dumpInfoFile: args['dump-info-file'], | 235 dumpInfoFile: args['dump-info-file'], |
225 dumpSrcDir: args['dump-src-to'], | 236 dumpSrcDir: args['dump-src-to'], |
226 forceCompile: args['force-compile'], | 237 forceCompile: args['force-compile'], |
227 formatOutput: args['dart-gen-fmt'], | 238 formatOutput: args['dart-gen-fmt'], |
228 ignoreTypes: args['ignore-types'], | 239 ignoreTypes: args['ignore-types'], |
229 outputDart: args['dart-gen'], | 240 outputDart: args['dart-gen'], |
(...skipping 10 matching lines...) Expand all Loading... |
240 nonnullableTypes: optionsToList(args['nonnullable'], | 251 nonnullableTypes: optionsToList(args['nonnullable'], |
241 defaultValue: TypeOptions.NONNULLABLE_TYPES), | 252 defaultValue: TypeOptions.NONNULLABLE_TYPES), |
242 help: args['help'], | 253 help: args['help'], |
243 useMockSdk: args['mock-sdk'], | 254 useMockSdk: args['mock-sdk'], |
244 dartSdkPath: sdkPath, | 255 dartSdkPath: sdkPath, |
245 logLevel: Level.LEVELS.firstWhere((Level l) => l.name == levelName, | 256 logLevel: Level.LEVELS.firstWhere((Level l) => l.name == levelName, |
246 orElse: () => Level.SEVERE), | 257 orElse: () => Level.SEVERE), |
247 emitSourceMaps: args['source-maps'], | 258 emitSourceMaps: args['source-maps'], |
248 entryPointFile: args.rest.length == 0 ? null : args.rest.first, | 259 entryPointFile: args.rest.length == 0 ? null : args.rest.first, |
249 serverMode: args['server'], | 260 serverMode: args['server'], |
250 port: int.parse(args['port'])); | 261 port: int.parse(args['port']), |
| 262 runtimeDir: runtimeDir); |
251 } | 263 } |
252 | 264 |
253 final ArgParser argParser = new ArgParser() | 265 final ArgParser argParser = new ArgParser() |
254 // resolver/checker options | 266 // resolver/checker options |
255 ..addFlag('allow-const-casts', | 267 ..addFlag('allow-const-casts', |
256 help: 'Allow casts in const contexts', defaultsTo: true) | 268 help: 'Allow casts in const contexts', defaultsTo: true) |
257 ..addFlag('sdk-check', | 269 ..addFlag('sdk-check', |
258 abbr: 's', help: 'Typecheck sdk libs', defaultsTo: false) | 270 abbr: 's', help: 'Typecheck sdk libs', defaultsTo: false) |
259 ..addFlag('mock-sdk', | 271 ..addFlag('mock-sdk', |
260 abbr: 'm', help: 'Use a mock Dart SDK', defaultsTo: false) | 272 abbr: 'm', help: 'Use a mock Dart SDK', defaultsTo: false) |
(...skipping 30 matching lines...) Expand all Loading... |
291 abbr: 'p', | 303 abbr: 'p', |
292 help: 'Package root to resolve "package:" imports', | 304 help: 'Package root to resolve "package:" imports', |
293 defaultsTo: 'packages/') | 305 defaultsTo: 'packages/') |
294 ..addFlag('use-multi-package', | 306 ..addFlag('use-multi-package', |
295 help: 'Whether to use the multi-package resolver for "package:" imports', | 307 help: 'Whether to use the multi-package resolver for "package:" imports', |
296 defaultsTo: false) | 308 defaultsTo: false) |
297 ..addOption('package-paths', help: 'if using the multi-package resolver, ' | 309 ..addOption('package-paths', help: 'if using the multi-package resolver, ' |
298 'the list of directories where to look for packages.', defaultsTo: '') | 310 'the list of directories where to look for packages.', defaultsTo: '') |
299 ..addFlag('source-maps', | 311 ..addFlag('source-maps', |
300 help: 'Whether to emit source map files', defaultsTo: true) | 312 help: 'Whether to emit source map files', defaultsTo: true) |
| 313 ..addOption('runtime-dir', |
| 314 help: 'Where to find dev_compiler\'s runtime files', defaultsTo: null) |
301 | 315 |
302 // general options | 316 // general options |
303 ..addFlag('help', abbr: 'h', help: 'Display this message') | 317 ..addFlag('help', abbr: 'h', help: 'Display this message') |
304 ..addFlag('server', help: 'Run as a development server.', defaultsTo: false) | 318 ..addFlag('server', help: 'Run as a development server.', defaultsTo: false) |
305 ..addOption('port', | 319 ..addOption('port', |
306 help: 'Port where to serve files from (used only when --serve is on)', | 320 help: 'Port where to serve files from (used only when --serve is on)', |
307 defaultsTo: '8080') | 321 defaultsTo: '8080') |
308 ..addFlag('force-compile', | 322 ..addFlag('force-compile', |
309 help: 'Compile code with static errors', defaultsTo: false) | 323 help: 'Compile code with static errors', defaultsTo: false) |
310 ..addOption('log', abbr: 'l', help: 'Logging level', defaultsTo: 'severe') | 324 ..addOption('log', abbr: 'l', help: 'Logging level', defaultsTo: 'severe') |
311 ..addFlag('dump-info', | 325 ..addFlag('dump-info', |
312 abbr: 'i', help: 'Dump summary information', defaultsTo: false) | 326 abbr: 'i', help: 'Dump summary information', defaultsTo: false) |
313 ..addOption('dump-info-file', | 327 ..addOption('dump-info-file', |
314 abbr: 'f', | 328 abbr: 'f', |
315 help: 'Dump info json file (requires dump-info)', | 329 help: 'Dump info json file (requires dump-info)', |
316 defaultsTo: null); | 330 defaultsTo: null); |
| 331 |
| 332 /// Tries to find the `lib/runtime/` directory of the dev_compiler package. This |
| 333 /// works when running devc from it's sources or from a snapshot that is |
| 334 /// activated via `pub global activate`. |
| 335 String _computeRuntimeDir() { |
| 336 var scriptUri = Platform.script; |
| 337 var scriptPath = scriptUri.path; |
| 338 var file = path.basename(scriptPath); |
| 339 var dir = path.dirname(scriptPath); |
| 340 var lastdir = path.basename(dir); |
| 341 dir = path.dirname(dir); |
| 342 |
| 343 // Both the source devc.dart and the snapshot generated by pub global activate |
| 344 // are under a bin folder. |
| 345 if (lastdir != 'bin') return null; |
| 346 |
| 347 // And both under a project directory containing a pubspec.lock file. |
| 348 var lockfile = path.join(dir, 'pubspec.lock'); |
| 349 if (!new File(lockfile).existsSync()) return null; |
| 350 |
| 351 // If running from sources we found it! |
| 352 if (file == 'devc.dart') return path.join(dir, 'lib', 'runtime'); |
| 353 |
| 354 // If running from a pub global snapshot, we need to read the lock file to |
| 355 // find where the actual sources are located in the pub cache. |
| 356 if (file == 'devc.dart.snapshot') { |
| 357 // Note: this depends on implementation details of pub. |
| 358 var yaml = loadYaml(new File(lockfile).readAsStringSync()); |
| 359 var info = yaml['packages']['dev_compiler']; |
| 360 if (info == null) return null; |
| 361 |
| 362 var cacheDir; |
| 363 if (info['source'] == 'hosted') { |
| 364 cacheDir = path.join( |
| 365 'hosted', 'pub.dartlang.org', 'dev_compiler-${info["version"]}'); |
| 366 } else if (info['source'] == 'git') { |
| 367 var ref = info['description']['resolved-ref']; |
| 368 cacheDir = path.join('git', 'dev_compiler-${ref}'); |
| 369 } |
| 370 |
| 371 // We should be under "/path/to/pub-cache/global_packages/dev_compiler". |
| 372 // The pub-cache directory is two levels up, but we verify that the layout |
| 373 // looks correct. |
| 374 if (path.basename(dir) != 'dev_compiler') return null; |
| 375 dir = path.dirname(dir); |
| 376 if (path.basename(dir) != 'global_packages') return null; |
| 377 dir = path.dirname(dir); |
| 378 return path.join(dir, cacheDir, 'lib', 'runtime'); |
| 379 } |
| 380 return null; |
| 381 } |
OLD | NEW |