| 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 | 6 |
| 7 import 'dart:io'; | 7 import 'dart:io'; |
| 8 | 8 |
| 9 import 'package:args/args.dart'; | 9 import 'package:args/args.dart'; |
| 10 import 'package:cli_util/cli_util.dart' show getSdkDir; | 10 import 'package:cli_util/cli_util.dart' show getSdkDir; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 | 39 |
| 40 /// Custom URI mappings, such as "dart:foo" -> "path/to/foo.dart" | 40 /// Custom URI mappings, such as "dart:foo" -> "path/to/foo.dart" |
| 41 final Map<String, String> customUrlMappings; | 41 final Map<String, String> customUrlMappings; |
| 42 | 42 |
| 43 /// Package root when resolving 'package:' urls the standard way. | 43 /// Package root when resolving 'package:' urls the standard way. |
| 44 final String packageRoot; | 44 final String packageRoot; |
| 45 | 45 |
| 46 /// List of paths used for the multi-package resolver. | 46 /// List of paths used for the multi-package resolver. |
| 47 final List<String> packagePaths; | 47 final List<String> packagePaths; |
| 48 | 48 |
| 49 /// List of additional non-Dart resources to resolve and serve. | |
| 50 final List<String> resources; | |
| 51 | |
| 52 // True if the resolver should implicitly provide an html entry point. | |
| 53 final bool useImplicitHtml; | |
| 54 static const String implicitHtmlFile = 'index.html'; | |
| 55 | |
| 56 /// Whether to use a mock-sdk during compilation. | 49 /// Whether to use a mock-sdk during compilation. |
| 57 final bool useMockSdk; | 50 final bool useMockSdk; |
| 58 | 51 |
| 59 /// Path to the dart-sdk. Null if `useMockSdk` is true or if the path couldn't | 52 /// Path to the dart-sdk. Null if `useMockSdk` is true or if the path couldn't |
| 60 /// be determined | 53 /// be determined |
| 61 final String dartSdkPath; | 54 final String dartSdkPath; |
| 62 | 55 |
| 63 const SourceResolverOptions( | 56 const SourceResolverOptions( |
| 64 {this.useMockSdk: false, | 57 {this.useMockSdk: false, |
| 65 this.dartSdkPath, | 58 this.dartSdkPath, |
| 66 this.useMultiPackage: false, | 59 this.useMultiPackage: false, |
| 67 this.customUrlMappings: const {}, | 60 this.customUrlMappings: const {}, |
| 68 this.packageRoot: 'packages/', | 61 this.packageRoot: 'packages/', |
| 69 this.packagePaths: const <String>[], | 62 this.packagePaths: const <String>[]}); |
| 70 this.resources: const <String>[], | |
| 71 this.useImplicitHtml: false}); | |
| 72 } | 63 } |
| 73 | 64 |
| 74 enum ModuleFormat { es6, legacy, node } | 65 enum ModuleFormat { es6, legacy, node } |
| 75 ModuleFormat parseModuleFormat(String s) => parseEnum(s, ModuleFormat.values); | 66 ModuleFormat parseModuleFormat(String s) => parseEnum(s, ModuleFormat.values); |
| 76 | 67 |
| 77 // TODO(jmesserly): refactor all codegen options here. | 68 // TODO(jmesserly): refactor all codegen options here. |
| 78 class CodegenOptions { | 69 class CodegenOptions { |
| 79 /// Whether to emit the source map files. | 70 /// Whether to emit the source map files. |
| 80 final bool emitSourceMaps; | 71 final bool emitSourceMaps; |
| 81 | 72 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 | 107 |
| 117 /// General options used by the dev compiler and server. | 108 /// General options used by the dev compiler and server. |
| 118 class CompilerOptions { | 109 class CompilerOptions { |
| 119 final SourceResolverOptions sourceOptions; | 110 final SourceResolverOptions sourceOptions; |
| 120 final CodegenOptions codegenOptions; | 111 final CodegenOptions codegenOptions; |
| 121 final RunnerOptions runnerOptions; | 112 final RunnerOptions runnerOptions; |
| 122 | 113 |
| 123 /// Whether to check the sdk libraries. | 114 /// Whether to check the sdk libraries. |
| 124 final bool checkSdk; | 115 final bool checkSdk; |
| 125 | 116 |
| 126 final bool htmlReport; | |
| 127 | |
| 128 /// Whether to use colors when interacting on the console. | 117 /// Whether to use colors when interacting on the console. |
| 129 final bool useColors; | 118 final bool useColors; |
| 130 | 119 |
| 131 /// Whether the user asked for help. | 120 /// Whether the user asked for help. |
| 132 final bool help; | 121 final bool help; |
| 133 | 122 |
| 134 /// Whether the user asked for the app version. | 123 /// Whether the user asked for the app version. |
| 135 final bool version; | 124 final bool version; |
| 136 | 125 |
| 137 /// Minimum log-level reported on the command-line. | 126 /// Minimum log-level reported on the command-line. |
| 138 final Level logLevel; | 127 final Level logLevel; |
| 139 | 128 |
| 140 /// Whether to run as a development server. | |
| 141 final bool serverMode; | |
| 142 | |
| 143 /// Whether to enable hash-based caching of files. | |
| 144 final bool enableHashing; | |
| 145 | |
| 146 /// Whether to serve the error / warning widget. | |
| 147 final bool widget; | |
| 148 | |
| 149 /// Port used for the HTTP server when [serverMode] is on. | |
| 150 final int port; | |
| 151 | |
| 152 /// Host name or address for HTTP server when [serverMode] is on. | |
| 153 final String host; | |
| 154 | |
| 155 /// Location for runtime files, such as `dart_runtime.js`. By default this is | 129 /// Location for runtime files, such as `dart_runtime.js`. By default this is |
| 156 /// inferred to be under `lib/runtime/` in the location of the `dev_compiler` | 130 /// inferred to be under `lib/runtime/` in the location of the `dev_compiler` |
| 157 /// package (if we can infer where that is located). | 131 /// package (if we can infer where that is located). |
| 158 final String runtimeDir; | 132 final String runtimeDir; |
| 159 | 133 |
| 160 /// The files to compile. | 134 /// The files to compile. |
| 161 final List<String> inputs; | 135 final List<String> inputs; |
| 162 | 136 |
| 163 /// The base directory for [inputs]. Module imports will be generated relative | 137 /// The base directory for [inputs]. Module imports will be generated relative |
| 164 /// to this directory. | 138 /// to this directory. |
| 165 final String inputBaseDir; | 139 final String inputBaseDir; |
| 166 | 140 |
| 167 const CompilerOptions( | 141 const CompilerOptions( |
| 168 {this.sourceOptions: const SourceResolverOptions(), | 142 {this.sourceOptions: const SourceResolverOptions(), |
| 169 this.codegenOptions: const CodegenOptions(), | 143 this.codegenOptions: const CodegenOptions(), |
| 170 this.runnerOptions: const RunnerOptions(), | 144 this.runnerOptions: const RunnerOptions(), |
| 171 this.checkSdk: false, | 145 this.checkSdk: false, |
| 172 this.htmlReport: false, | |
| 173 this.useColors: true, | 146 this.useColors: true, |
| 174 this.help: false, | 147 this.help: false, |
| 175 this.version: false, | 148 this.version: false, |
| 176 this.logLevel: Level.WARNING, | 149 this.logLevel: Level.WARNING, |
| 177 this.serverMode: false, | |
| 178 this.enableHashing: false, | |
| 179 this.widget: true, | |
| 180 this.host: 'localhost', | |
| 181 this.port: 8080, | |
| 182 this.runtimeDir, | 150 this.runtimeDir, |
| 183 this.inputs, | 151 this.inputs, |
| 184 this.inputBaseDir}); | 152 this.inputBaseDir}); |
| 185 } | 153 } |
| 186 | 154 |
| 187 /// Parses options from the command-line | 155 /// Parses options from the command-line |
| 188 CompilerOptions parseOptions(List<String> argv, {bool forceOutDir: false}) { | 156 CompilerOptions parseOptions(List<String> argv, {bool forceOutDir: false}) { |
| 189 ArgResults args = argParser.parse(argv); | 157 ArgResults args = argParser.parse(argv); |
| 190 bool showUsage = args['help']; | 158 bool showUsage = args['help']; |
| 191 bool showVersion = args['version']; | 159 bool showVersion = args['version']; |
| 192 | 160 |
| 193 var serverMode = args['server']; | |
| 194 var enableHashing = args['hashing']; | |
| 195 if (enableHashing == null) { | |
| 196 enableHashing = serverMode; | |
| 197 } | |
| 198 var logLevel = Level.WARNING; | 161 var logLevel = Level.WARNING; |
| 199 var levelName = args['log']; | 162 var levelName = args['log']; |
| 200 if (levelName != null) { | 163 if (levelName != null) { |
| 201 levelName = levelName.toUpperCase(); | 164 levelName = levelName.toUpperCase(); |
| 202 logLevel = Level.LEVELS | 165 logLevel = Level.LEVELS |
| 203 .firstWhere((l) => l.name == levelName, orElse: () => logLevel); | 166 .firstWhere((l) => l.name == levelName, orElse: () => logLevel); |
| 204 } | 167 } |
| 205 var useColors = stdioType(stdout) == StdioType.TERMINAL; | 168 var useColors = stdioType(stdout) == StdioType.TERMINAL; |
| 206 var sdkPath = args['dart-sdk']; | 169 var sdkPath = args['dart-sdk']; |
| 207 if (sdkPath == null && !args['mock-sdk']) { | 170 if (sdkPath == null && !args['mock-sdk']) { |
| 208 sdkPath = getSdkDir(argv).path; | 171 sdkPath = getSdkDir(argv).path; |
| 209 } | 172 } |
| 210 var runtimeDir = args['runtime-dir']; | 173 var runtimeDir = args['runtime-dir']; |
| 211 if (runtimeDir == null) { | 174 if (runtimeDir == null) { |
| 212 runtimeDir = _computeRuntimeDir(); | 175 runtimeDir = _computeRuntimeDir(); |
| 213 } | 176 } |
| 214 var outputDir = args['out']; | 177 var outputDir = args['out']; |
| 215 if (outputDir == null && (serverMode || forceOutDir)) { | 178 if (outputDir == null && forceOutDir) { |
| 216 outputDir = Directory.systemTemp.createTempSync("dev_compiler_out_").path; | 179 outputDir = Directory.systemTemp.createTempSync("dev_compiler_out_").path; |
| 217 } | 180 } |
| 218 var htmlReport = args['html-report']; | |
| 219 | 181 |
| 220 var v8Binary = args['v8-binary']; | 182 var v8Binary = args['v8-binary']; |
| 221 if (v8Binary == null) v8Binary = _V8_BINARY_DEFAULT; | 183 if (v8Binary == null) v8Binary = _V8_BINARY_DEFAULT; |
| 222 | 184 |
| 223 var customUrlMappings = <String, String>{}; | 185 var customUrlMappings = <String, String>{}; |
| 224 for (var mapping in args['url-mapping']) { | 186 for (var mapping in args['url-mapping']) { |
| 225 var splitMapping = mapping.split(','); | 187 var splitMapping = mapping.split(','); |
| 226 if (splitMapping.length != 2) { | 188 if (splitMapping.length != 2) { |
| 227 showUsage = true; | 189 showUsage = true; |
| 228 continue; | 190 continue; |
| 229 } | 191 } |
| 230 customUrlMappings[splitMapping[0]] = splitMapping[1]; | 192 customUrlMappings[splitMapping[0]] = splitMapping[1]; |
| 231 } | 193 } |
| 232 | 194 |
| 233 return new CompilerOptions( | 195 return new CompilerOptions( |
| 234 codegenOptions: new CodegenOptions( | 196 codegenOptions: new CodegenOptions( |
| 235 emitSourceMaps: args['source-maps'], | 197 emitSourceMaps: args['source-maps'], |
| 236 forceCompile: args['force-compile'] || serverMode, | 198 forceCompile: args['force-compile'], |
| 237 closure: args['closure'], | 199 closure: args['closure'], |
| 238 destructureNamedParams: args['destructure-named-params'], | 200 destructureNamedParams: args['destructure-named-params'], |
| 239 outputDir: outputDir, | 201 outputDir: outputDir, |
| 240 moduleFormat: parseModuleFormat(args['modules'])), | 202 moduleFormat: parseModuleFormat(args['modules'])), |
| 241 sourceOptions: new SourceResolverOptions( | 203 sourceOptions: new SourceResolverOptions( |
| 242 useMockSdk: args['mock-sdk'], | 204 useMockSdk: args['mock-sdk'], |
| 243 dartSdkPath: sdkPath, | 205 dartSdkPath: sdkPath, |
| 244 useImplicitHtml: serverMode && | |
| 245 args.rest.length == 1 && | |
| 246 args.rest[0].endsWith('.dart'), | |
| 247 customUrlMappings: customUrlMappings, | 206 customUrlMappings: customUrlMappings, |
| 248 useMultiPackage: args['use-multi-package'], | 207 useMultiPackage: args['use-multi-package'], |
| 249 packageRoot: args['package-root'], | 208 packageRoot: args['package-root'], |
| 250 packagePaths: args['package-paths'].split(','), | 209 packagePaths: args['package-paths'].split(',')), |
| 251 resources: | |
| 252 args['resources'].split(',').where((s) => s.isNotEmpty).toList()), | |
| 253 runnerOptions: new RunnerOptions(v8Binary: v8Binary), | 210 runnerOptions: new RunnerOptions(v8Binary: v8Binary), |
| 254 checkSdk: args['sdk-check'], | 211 checkSdk: args['sdk-check'], |
| 255 htmlReport: htmlReport, | |
| 256 useColors: useColors, | 212 useColors: useColors, |
| 257 help: showUsage, | 213 help: showUsage, |
| 258 version: showVersion, | 214 version: showVersion, |
| 259 logLevel: logLevel, | 215 logLevel: logLevel, |
| 260 serverMode: serverMode, | |
| 261 enableHashing: enableHashing, | |
| 262 widget: args['widget'], | |
| 263 host: args['host'], | |
| 264 port: int.parse(args['port']), | |
| 265 runtimeDir: runtimeDir, | 216 runtimeDir: runtimeDir, |
| 266 inputs: args.rest); | 217 inputs: args.rest); |
| 267 } | 218 } |
| 268 | 219 |
| 269 final ArgParser argParser = new ArgParser() | 220 final ArgParser argParser = new ArgParser() |
| 270 ..addFlag('sdk-check', | 221 ..addFlag('sdk-check', |
| 271 abbr: 's', help: 'Typecheck sdk libs', defaultsTo: false) | 222 abbr: 's', help: 'Typecheck sdk libs', defaultsTo: false) |
| 272 ..addFlag('mock-sdk', | 223 ..addFlag('mock-sdk', |
| 273 abbr: 'm', help: 'Use a mock Dart SDK', defaultsTo: false) | 224 abbr: 'm', help: 'Use a mock Dart SDK', defaultsTo: false) |
| 274 | 225 |
| 275 // input/output options | 226 // input/output options |
| 276 ..addOption('out', abbr: 'o', help: 'Output directory', defaultsTo: null) | 227 ..addOption('out', abbr: 'o', help: 'Output directory', defaultsTo: null) |
| 277 ..addOption('dart-sdk', help: 'Dart SDK Path', defaultsTo: null) | 228 ..addOption('dart-sdk', help: 'Dart SDK Path', defaultsTo: null) |
| 278 ..addOption('dump-src-to', help: 'Dump dart src code', defaultsTo: null) | |
| 279 ..addOption('package-root', | 229 ..addOption('package-root', |
| 280 abbr: 'p', | 230 abbr: 'p', |
| 281 help: 'Package root to resolve "package:" imports', | 231 help: 'Package root to resolve "package:" imports', |
| 282 defaultsTo: 'packages/') | 232 defaultsTo: 'packages/') |
| 283 ..addOption('url-mapping', | 233 ..addOption('url-mapping', |
| 284 help: '--url-mapping=libraryUri,/path/to/library.dart uses library.dart\n' | 234 help: '--url-mapping=libraryUri,/path/to/library.dart uses library.dart\n' |
| 285 'as the source for an import of of "libraryUri".', | 235 'as the source for an import of of "libraryUri".', |
| 286 allowMultiple: true, | 236 allowMultiple: true, |
| 287 splitCommas: false) | 237 splitCommas: false) |
| 288 ..addFlag('use-multi-package', | 238 ..addFlag('use-multi-package', |
| 289 help: 'Whether to use the multi-package resolver for "package:" imports', | 239 help: 'Whether to use the multi-package resolver for "package:" imports', |
| 290 defaultsTo: false) | 240 defaultsTo: false) |
| 291 ..addOption('package-paths', | 241 ..addOption('package-paths', |
| 292 help: 'if using the multi-package resolver, the list of directories to\n' | 242 help: 'if using the multi-package resolver, the list of directories to\n' |
| 293 'look for packages in.', | 243 'look for packages in.', |
| 294 defaultsTo: '') | 244 defaultsTo: '') |
| 295 ..addOption('resources', | |
| 296 help: 'Additional resources to serve', defaultsTo: '') | |
| 297 ..addFlag('source-maps', | 245 ..addFlag('source-maps', |
| 298 help: 'Whether to emit source map files', defaultsTo: true) | 246 help: 'Whether to emit source map files', defaultsTo: true) |
| 299 ..addOption('runtime-dir', | 247 ..addOption('runtime-dir', |
| 300 help: 'Where to find dev_compiler\'s runtime files', defaultsTo: null) | 248 help: 'Where to find dev_compiler\'s runtime files', defaultsTo: null) |
| 301 ..addOption('modules', | 249 ..addOption('modules', |
| 302 help: 'Which module pattern to emit', | 250 help: 'Which module pattern to emit', |
| 303 allowed: ModuleFormat.values.map(getEnumName).toList(), | 251 allowed: ModuleFormat.values.map(getEnumName).toList(), |
| 304 allowedHelp: { | 252 allowedHelp: { |
| 305 getEnumName(ModuleFormat.es6): 'es6 modules', | 253 getEnumName(ModuleFormat.es6): 'es6 modules', |
| 306 getEnumName(ModuleFormat.legacy): | 254 getEnumName(ModuleFormat.legacy): |
| 307 'a custom format used by dartdevc, similar to AMD', | 255 'a custom format used by dartdevc, similar to AMD', |
| 308 getEnumName(ModuleFormat.node): | 256 getEnumName(ModuleFormat.node): |
| 309 'node.js modules (https://nodejs.org/api/modules.html)' | 257 'node.js modules (https://nodejs.org/api/modules.html)' |
| 310 }, | 258 }, |
| 311 defaultsTo: getEnumName(ModuleFormat.legacy)) | 259 defaultsTo: getEnumName(ModuleFormat.legacy)) |
| 312 | 260 |
| 313 // general options | 261 // general options |
| 314 ..addFlag('help', abbr: 'h', help: 'Display this message') | 262 ..addFlag('help', abbr: 'h', help: 'Display this message') |
| 315 ..addFlag('version', | 263 ..addFlag('version', |
| 316 negatable: false, help: 'Display the Dev Compiler verion') | 264 negatable: false, help: 'Display the Dev Compiler verion') |
| 317 ..addFlag('server', help: 'Run as a development server.', defaultsTo: false) | |
| 318 ..addFlag('hashing', | |
| 319 help: 'Enable hash-based file caching.', defaultsTo: null) | |
| 320 ..addFlag('widget', | |
| 321 help: 'Serve embedded widget with static errors and warnings.', | |
| 322 defaultsTo: true) | |
| 323 ..addOption('host', | |
| 324 help: 'Host name or address to serve files from, e.g. --host=0.0.0.0\n' | |
| 325 'to listen on all interfaces (used only when --serve is on)', | |
| 326 defaultsTo: 'localhost') | |
| 327 ..addOption('port', | |
| 328 help: 'Port to serve files from (used only when --serve is on)', | |
| 329 defaultsTo: '8080') | |
| 330 ..addFlag('closure', | 265 ..addFlag('closure', |
| 331 help: 'Emit Closure Compiler-friendly code (experimental)', | 266 help: 'Emit Closure Compiler-friendly code (experimental)', |
| 332 defaultsTo: _CLOSURE_DEFAULT) | 267 defaultsTo: _CLOSURE_DEFAULT) |
| 333 ..addFlag('destructure-named-params', | 268 ..addFlag('destructure-named-params', |
| 334 help: 'Destructure named parameters (requires ES6-enabled runtime)', | 269 help: 'Destructure named parameters (requires ES6-enabled runtime)', |
| 335 defaultsTo: _DESTRUCTURE_NAMED_PARAMS_DEFAULT) | 270 defaultsTo: _DESTRUCTURE_NAMED_PARAMS_DEFAULT) |
| 336 ..addFlag('force-compile', | 271 ..addFlag('force-compile', |
| 337 abbr: 'f', help: 'Compile code with static errors', defaultsTo: false) | 272 abbr: 'f', help: 'Compile code with static errors', defaultsTo: false) |
| 338 ..addOption('log', abbr: 'l', help: 'Logging level (defaults to warning)') | 273 ..addOption('log', abbr: 'l', help: 'Logging level (defaults to warning)') |
| 339 ..addFlag('dump-info', | |
| 340 abbr: 'i', help: 'Dump summary information', defaultsTo: null) | |
| 341 ..addFlag('html-report', | |
| 342 help: 'Output compilation results to html', defaultsTo: false) | |
| 343 ..addOption('v8-binary', | 274 ..addOption('v8-binary', |
| 344 help: 'V8-based binary to run JavaScript output with (iojs, node, d8)', | 275 help: 'V8-based binary to run JavaScript output with (iojs, node, d8)', |
| 345 defaultsTo: _V8_BINARY_DEFAULT) | 276 defaultsTo: _V8_BINARY_DEFAULT); |
| 346 ..addOption('dump-info-file', | |
| 347 help: 'Dump info json file (requires dump-info)', defaultsTo: null); | |
| 348 | 277 |
| 349 // TODO: Switch over to the `pub_cache` package (or the Resource API)? | 278 // TODO: Switch over to the `pub_cache` package (or the Resource API)? |
| 350 | 279 |
| 351 const _ENTRY_POINTS = const [ | 280 const _ENTRY_POINTS = const [ |
| 352 'dartdevc.dart', | 281 'dartdevc.dart', |
| 353 'dev_compiler.dart', | 282 'dev_compiler.dart', |
| 354 'devrun.dart' | 283 'devrun.dart' |
| 355 ]; | 284 ]; |
| 356 | 285 |
| 357 final _ENTRY_POINT_SNAPSHOTS = _ENTRY_POINTS.map((f) => "$f.snapshot"); | 286 final _ENTRY_POINT_SNAPSHOTS = _ENTRY_POINTS.map((f) => "$f.snapshot"); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 // The pub-cache directory is two levels up, but we verify that the layout | 329 // The pub-cache directory is two levels up, but we verify that the layout |
| 401 // looks correct. | 330 // looks correct. |
| 402 if (path.basename(dir) != 'dev_compiler') return null; | 331 if (path.basename(dir) != 'dev_compiler') return null; |
| 403 dir = path.dirname(dir); | 332 dir = path.dirname(dir); |
| 404 if (path.basename(dir) != 'global_packages') return null; | 333 if (path.basename(dir) != 'global_packages') return null; |
| 405 dir = path.dirname(dir); | 334 dir = path.dirname(dir); |
| 406 return path.join(dir, cacheDir, 'lib', 'runtime'); | 335 return path.join(dir, cacheDir, 'lib', 'runtime'); |
| 407 } | 336 } |
| 408 return null; | 337 return null; |
| 409 } | 338 } |
| OLD | NEW |