| 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:convert' show JSON; | 5 import 'dart:convert' show JSON; |
| 6 import 'dart:io'; | 6 import 'dart:io'; |
| 7 import 'package:args/command_runner.dart'; | 7 import 'package:args/command_runner.dart'; |
| 8 import 'package:analyzer/src/generated/source.dart' show Source; | 8 import 'package:analyzer/src/generated/source.dart' show Source; |
| 9 import 'package:analyzer/src/summary/package_bundle_reader.dart' | 9 import 'package:analyzer/src/summary/package_bundle_reader.dart' |
| 10 show InSummarySource; | 10 show InSummarySource; |
| 11 import 'package:bazel_worker/bazel_worker.dart'; | |
| 12 import 'compiler.dart' | 11 import 'compiler.dart' |
| 13 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; | 12 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; |
| 14 import '../analyzer/context.dart' show AnalyzerOptions; | 13 import '../analyzer/context.dart' show AnalyzerOptions; |
| 15 import 'package:path/path.dart' as path; | 14 import 'package:path/path.dart' as path; |
| 16 | 15 |
| 16 typedef void MessageHandler(Object message); |
| 17 |
| 17 /// The command for invoking the modular compiler. | 18 /// The command for invoking the modular compiler. |
| 18 class CompileCommand extends Command { | 19 class CompileCommand extends Command { |
| 19 get name => 'compile'; | 20 get name => 'compile'; |
| 20 get description => 'Compile a set of Dart files into a JavaScript module.'; | 21 get description => 'Compile a set of Dart files into a JavaScript module.'; |
| 22 final MessageHandler messageHandler; |
| 21 | 23 |
| 22 CompileCommand() { | 24 CompileCommand({MessageHandler messageHandler}) |
| 25 : this.messageHandler = messageHandler ?? print { |
| 23 argParser.addOption('out', abbr: 'o', help: 'Output file (required)'); | 26 argParser.addOption('out', abbr: 'o', help: 'Output file (required)'); |
| 24 argParser.addFlag('persistent_worker', | |
| 25 help: 'Run in a persistent Bazel worker (http://bazel.io/)\n', | |
| 26 defaultsTo: false, | |
| 27 hide: true); | |
| 28 CompilerOptions.addArguments(argParser); | 27 CompilerOptions.addArguments(argParser); |
| 29 AnalyzerOptions.addArguments(argParser); | 28 AnalyzerOptions.addArguments(argParser); |
| 30 } | 29 } |
| 31 | 30 |
| 32 @override | 31 @override |
| 33 void run() { | 32 void run() { |
| 34 var analyzerOptions = new AnalyzerOptions.fromArguments(argResults); | 33 var compiler = |
| 35 if (argResults['persistent_worker']) { | 34 new ModuleCompiler(new AnalyzerOptions.fromArguments(argResults)); |
| 36 new _CompilerWorker(analyzerOptions, this).run(); | 35 var compilerOptions = new CompilerOptions.fromArguments(argResults); |
| 37 } else { | 36 var outPath = argResults['out']; |
| 38 compile( | |
| 39 new ModuleCompiler(analyzerOptions), | |
| 40 new CompilerOptions.fromArguments(argResults), | |
| 41 argResults['out'], | |
| 42 argResults.rest); | |
| 43 } | |
| 44 } | |
| 45 | 37 |
| 46 void compile(ModuleCompiler compiler, CompilerOptions compilerOptions, | |
| 47 String outPath, List<String> extraArgs, | |
| 48 {void forEachError(String error): print}) { | |
| 49 if (outPath == null) { | 38 if (outPath == null) { |
| 50 usageException('Please include the output file location. For example:\n' | 39 usageException('Please include the output file location. For example:\n' |
| 51 ' -o PATH/TO/OUTPUT_FILE.js'); | 40 ' -o PATH/TO/OUTPUT_FILE.js'); |
| 52 } | 41 } |
| 53 var unit = new BuildUnit( | 42 var unit = new BuildUnit(path.basenameWithoutExtension(outPath), |
| 54 path.basenameWithoutExtension(outPath), extraArgs, _moduleForLibrary); | 43 argResults.rest, _moduleForLibrary); |
| 55 | 44 |
| 56 JSModuleFile module = compiler.compile(unit, compilerOptions); | 45 JSModuleFile module = compiler.compile(unit, compilerOptions); |
| 57 module.errors.forEach(forEachError); | 46 module.errors.forEach(messageHandler); |
| 58 | 47 |
| 59 if (!module.isValid) throw new CompileErrorException(); | 48 if (!module.isValid) throw new CompileErrorException(); |
| 60 | 49 |
| 61 // Write JS file, as well as source map and summary (if requested). | 50 // Write JS file, as well as source map and summary (if requested). |
| 62 new File(outPath).writeAsStringSync(module.code); | 51 new File(outPath).writeAsStringSync(module.code); |
| 63 if (module.sourceMap != null) { | 52 if (module.sourceMap != null) { |
| 64 var mapPath = outPath + '.map'; | 53 var mapPath = outPath + '.map'; |
| 65 new File(mapPath) | 54 new File(mapPath) |
| 66 .writeAsStringSync(JSON.encode(module.placeSourceMap(mapPath))); | 55 .writeAsStringSync(JSON.encode(module.placeSourceMap(mapPath))); |
| 67 } | 56 } |
| 68 if (module.summaryBytes != null) { | 57 if (module.summaryBytes != null) { |
| 69 var summaryPath = path.withoutExtension(outPath) + '.sum'; | 58 var summaryPath = path.withoutExtension(outPath) + '.sum'; |
| 70 new File(summaryPath).writeAsBytesSync(module.summaryBytes); | 59 new File(summaryPath).writeAsBytesSync(module.summaryBytes); |
| 71 } | 60 } |
| 72 } | 61 } |
| 73 | 62 |
| 74 String _moduleForLibrary(Source source) { | 63 String _moduleForLibrary(Source source) { |
| 75 if (source is InSummarySource) { | 64 if (source is InSummarySource) { |
| 76 return path.basenameWithoutExtension(source.summaryPath); | 65 return path.basenameWithoutExtension(source.summaryPath); |
| 77 } | 66 } |
| 78 | 67 |
| 79 throw usageException( | 68 throw usageException( |
| 80 'Imported file "${source.uri}" was not found as a summary or source ' | 69 'Imported file "${source.uri}" was not found as a summary or source ' |
| 81 'file. Please pass in either the summary or the source file ' | 70 'file. Please pass in either the summary or the source file ' |
| 82 'for this import.'); | 71 'for this import.'); |
| 83 } | 72 } |
| 84 } | 73 } |
| 85 | 74 |
| 86 /// Handles [error] in a uniform fashion. Returns the proper exit code and calls | |
| 87 /// [messageHandler] with messages. | |
| 88 int handleError(dynamic error, dynamic stackTrace, List<String> args, | |
| 89 {void messageHandler(Object message): print}) { | |
| 90 if (error is UsageException) { | |
| 91 // Incorrect usage, input file not found, etc. | |
| 92 messageHandler(error); | |
| 93 return 64; | |
| 94 } else if (error is CompileErrorException) { | |
| 95 // Code has error(s) and failed to compile. | |
| 96 messageHandler(error); | |
| 97 return 1; | |
| 98 } else { | |
| 99 // Anything else is likely a compiler bug. | |
| 100 // | |
| 101 // --unsafe-force-compile is a bit of a grey area, but it's nice not to | |
| 102 // crash while compiling | |
| 103 // (of course, output code may crash, if it had errors). | |
| 104 // | |
| 105 messageHandler(""); | |
| 106 messageHandler("We're sorry, you've found a bug in our compiler."); | |
| 107 messageHandler("You can report this bug at:"); | |
| 108 messageHandler(" https://github.com/dart-lang/dev_compiler/issues"); | |
| 109 messageHandler(""); | |
| 110 messageHandler( | |
| 111 "Please include the information below in your report, along with"); | |
| 112 messageHandler( | |
| 113 "any other information that may help us track it down. Thanks!"); | |
| 114 messageHandler(""); | |
| 115 messageHandler(" dartdevc arguments: " + args.join(' ')); | |
| 116 messageHandler(" dart --version: ${Platform.version}"); | |
| 117 messageHandler(""); | |
| 118 messageHandler("```"); | |
| 119 messageHandler(error); | |
| 120 messageHandler(stackTrace); | |
| 121 messageHandler("```"); | |
| 122 return 1; | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 /// Thrown when the input source code has errors. | 75 /// Thrown when the input source code has errors. |
| 127 class CompileErrorException implements Exception { | 76 class CompileErrorException implements Exception { |
| 128 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; | 77 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; |
| 129 } | 78 } |
| 130 | |
| 131 /// Runs the compiler worker loop. | |
| 132 class _CompilerWorker extends SyncWorkerLoop { | |
| 133 final AnalyzerOptions analyzerOptions; | |
| 134 final CompileCommand compileCommand; | |
| 135 | |
| 136 _CompilerWorker(this.analyzerOptions, this.compileCommand) : super(); | |
| 137 | |
| 138 WorkResponse performRequest(WorkRequest request) { | |
| 139 var arguments = new List.from(request.arguments) | |
| 140 ..addAll(compileCommand.argResults.rest); | |
| 141 var argResults = compileCommand.argParser.parse(arguments); | |
| 142 | |
| 143 var output = new StringBuffer(); | |
| 144 try { | |
| 145 compileCommand.compile( | |
| 146 new ModuleCompiler(analyzerOptions), | |
| 147 new CompilerOptions.fromArguments(argResults), | |
| 148 argResults['out'], | |
| 149 argResults.rest, | |
| 150 forEachError: output.writeln); | |
| 151 return new WorkResponse() | |
| 152 ..exitCode = EXIT_CODE_OK | |
| 153 ..output = output.toString(); | |
| 154 } catch (e, s) { | |
| 155 var response = new WorkResponse(); | |
| 156 var output = new StringBuffer(); | |
| 157 response.exitCode = | |
| 158 handleError(e, s, request.arguments, messageHandler: output.writeln); | |
| 159 response.output = output.toString(); | |
| 160 return response; | |
| 161 } | |
| 162 } | |
| 163 } | |
| OLD | NEW |