Chromium Code Reviews| 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/args.dart'; | |
| 7 import 'package:args/command_runner.dart'; | 8 import 'package:args/command_runner.dart'; |
| 8 import 'package:analyzer/src/generated/source.dart' show Source; | 9 import 'package:analyzer/src/generated/source.dart' show Source; |
| 9 import 'package:analyzer/src/summary/package_bundle_reader.dart' | 10 import 'package:analyzer/src/summary/package_bundle_reader.dart' |
| 10 show InSummarySource; | 11 show InSummarySource; |
| 12 import 'package:bazel_worker/bazel_worker.dart'; | |
| 11 import 'compiler.dart' | 13 import 'compiler.dart' |
| 12 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; | 14 show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler; |
| 13 import '../analyzer/context.dart' show AnalyzerOptions; | 15 import '../analyzer/context.dart' show AnalyzerOptions; |
| 14 import 'package:path/path.dart' as path; | 16 import 'package:path/path.dart' as path; |
| 15 | 17 |
| 16 /// The command for invoking the modular compiler. | 18 /// The command for invoking the modular compiler. |
| 17 class CompileCommand extends Command { | 19 class CompileCommand extends Command { |
| 18 get name => 'compile'; | 20 get name => 'compile'; |
| 19 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.'; |
| 20 | 22 |
| 21 CompileCommand() { | 23 CompileCommand() { |
| 22 argParser.addOption('out', abbr: 'o', help: 'Output file (required)'); | 24 argParser.addOption('out', abbr: 'o', help: 'Output file (required)'); |
| 25 argParser.addFlag('persistent_worker', | |
|
Jennifer Messerly
2016/04/14 21:02:14
Hmmm, we use dash "-" instead of underscore "_" fo
jakemac
2016/04/14 21:48:25
Ya, unfortunately we don't have any control over t
| |
| 26 help: 'Whether or not we are running as a persistent bazel worker. ' | |
|
Jennifer Messerly
2016/04/14 21:02:14
few UI comments:
* should "Bazel" be capitalized a
jakemac
2016/04/14 21:48:25
Done.
| |
| 27 'This is automatically added by bazel when running in worker mode.', | |
| 28 defaultsTo: false); | |
|
Jennifer Messerly
2016/04/14 21:02:15
"defaultsTo: false" is not usually needed, but may
jakemac
2016/04/14 21:48:25
Nice, I didn't realize there was an option to hide
| |
| 23 CompilerOptions.addArguments(argParser); | 29 CompilerOptions.addArguments(argParser); |
| 24 AnalyzerOptions.addArguments(argParser); | 30 AnalyzerOptions.addArguments(argParser); |
| 25 } | 31 } |
| 26 | 32 |
| 27 @override | 33 @override |
| 28 void run() { | 34 void run() { |
| 29 var compilerOptions = new CompilerOptions.fromArguments(argResults); | |
| 30 var compiler = | 35 var compiler = |
| 31 new ModuleCompiler(new AnalyzerOptions.fromArguments(argResults)); | 36 new ModuleCompiler(new AnalyzerOptions.fromArguments(argResults)); |
| 37 if (argResults['persistent_worker'] == true) { | |
|
Jennifer Messerly
2016/04/14 21:02:15
if you have defaultsTo: false, do you also need "=
jakemac
2016/04/14 21:48:25
Done, I was definitely being a bit overly defensiv
| |
| 38 new CompilerLoop(compiler, argParser, runCompiler, argResults.rest).run(); | |
| 39 } else { | |
| 40 runCompiler(compiler, new CompilerOptions.fromArguments(argResults), | |
| 41 argResults['out'], argResults.rest); | |
| 42 } | |
| 43 } | |
| 32 | 44 |
| 33 var outPath = argResults['out']; | 45 void runCompiler(ModuleCompiler compiler, CompilerOptions compilerOptions, |
|
Jennifer Messerly
2016/04/14 21:02:15
suggestion: rename to "compile"
jakemac
2016/04/14 21:48:25
Done.
| |
| 46 String outPath, List<String> extraArgs, | |
| 47 {void forEachError(String error): print}) { | |
| 34 if (outPath == null) { | 48 if (outPath == null) { |
| 35 usageException('Please include the output file location. For example:\n' | 49 usageException('Please include the output file location. For example:\n' |
| 36 ' -o PATH/TO/OUTPUT_FILE.js'); | 50 ' -o PATH/TO/OUTPUT_FILE.js'); |
| 37 } | 51 } |
| 38 var unit = new BuildUnit(path.basenameWithoutExtension(outPath), | 52 var unit = new BuildUnit( |
| 39 argResults.rest, _moduleForLibrary); | 53 path.basenameWithoutExtension(outPath), extraArgs, _moduleForLibrary); |
| 40 | 54 |
| 41 JSModuleFile module = compiler.compile(unit, compilerOptions); | 55 JSModuleFile module = compiler.compile(unit, compilerOptions); |
| 42 module.errors.forEach(print); | 56 module.errors.forEach(forEachError); |
| 43 | 57 |
| 44 if (!module.isValid) throw new CompileErrorException(); | 58 if (!module.isValid) throw new CompileErrorException(); |
| 45 | 59 |
| 46 // Write JS file, as well as source map and summary (if requested). | 60 // Write JS file, as well as source map and summary (if requested). |
| 47 new File(outPath).writeAsStringSync(module.code); | 61 new File(outPath).writeAsStringSync(module.code); |
| 48 if (module.sourceMap != null) { | 62 if (module.sourceMap != null) { |
| 49 var mapPath = outPath + '.map'; | 63 var mapPath = outPath + '.map'; |
| 50 new File(mapPath) | 64 new File(mapPath) |
| 51 .writeAsStringSync(JSON.encode(module.placeSourceMap(mapPath))); | 65 .writeAsStringSync(JSON.encode(module.placeSourceMap(mapPath))); |
| 52 } | 66 } |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 65 'Imported file "${source.uri}" was not found as a summary or source ' | 79 'Imported file "${source.uri}" was not found as a summary or source ' |
| 66 'file. Please pass in either the summary or the source file ' | 80 'file. Please pass in either the summary or the source file ' |
| 67 'for this import.'); | 81 'for this import.'); |
| 68 } | 82 } |
| 69 } | 83 } |
| 70 | 84 |
| 71 /// Thrown when the input source code has errors. | 85 /// Thrown when the input source code has errors. |
| 72 class CompileErrorException implements Exception { | 86 class CompileErrorException implements Exception { |
| 73 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; | 87 toString() => '\nPlease fix all errors before compiling (warnings are okay).'; |
| 74 } | 88 } |
| 89 | |
| 90 typedef void RunCompilerFn(ModuleCompiler compiler, CompilerOptions options, | |
|
Jennifer Messerly
2016/04/14 21:02:14
this can be removed if following suggestion below
jakemac
2016/04/14 21:48:25
Done.
| |
| 91 String outPath, List<String> extraArgs, | |
| 92 {void forEachError(String error)}); | |
| 93 | |
| 94 /// Runs the compiler worker loop. | |
| 95 class CompilerLoop extends SyncWorkerLoop { | |
|
Jennifer Messerly
2016/04/14 21:02:15
"_CompilerWorker" would be a better name, IMO.
(al
jakemac
2016/04/14 21:48:25
Done.
| |
| 96 final ModuleCompiler compiler; | |
| 97 final ArgParser argParser; | |
| 98 final RunCompilerFn runCompilerFn; | |
|
Jennifer Messerly
2016/04/14 21:02:15
This could just be a CompileCommand. That way we d
jakemac
2016/04/14 21:48:25
Done.
| |
| 99 final List<String> extraArgs; | |
| 100 | |
| 101 CompilerLoop( | |
| 102 this.compiler, this.argParser, this.runCompilerFn, this.extraArgs) | |
| 103 : super(); | |
| 104 | |
| 105 WorkResponse performRequest(WorkRequest request) { | |
| 106 var argResults = argParser.parse(request.arguments); | |
| 107 var output = new StringBuffer(); | |
| 108 try { | |
| 109 runCompilerFn(compiler, new CompilerOptions.fromArguments(argResults), | |
|
Jennifer Messerly
2016/04/14 21:02:14
after suggestion above, this will become just "com
jakemac
2016/04/14 21:48:25
Done.
| |
| 110 argResults['out'], new List.from(argResults.rest)..addAll(extraArgs), | |
| 111 forEachError: output.writeln); | |
| 112 return new WorkResponse() | |
| 113 ..exitCode = EXIT_CODE_OK | |
| 114 ..output = output.toString(); | |
| 115 } catch (e, s) { | |
|
Jennifer Messerly
2016/04/14 21:02:15
Rather than catch all exceptions, it may be worth
jakemac
2016/04/14 21:48:25
Done, moved the error handling logic into a shared
| |
| 116 return new WorkResponse() | |
| 117 ..exitCode = EXIT_CODE_ERROR | |
|
Jennifer Messerly
2016/04/14 21:02:15
not sure if they have different exit codes for "to
jakemac
2016/04/14 21:48:25
Afaik bazel does not do anything with the error co
| |
| 118 ..output = '$output\n$e\n$s'; | |
| 119 } | |
| 120 } | |
| 121 } | |
| OLD | NEW |