Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(964)

Unified Diff: lib/src/compiler/command.dart

Issue 1884073003: Add bazel worker support to the dev compiler. (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: remove hacks since they are no longer necessary Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/src/compiler/command.dart
diff --git a/lib/src/compiler/command.dart b/lib/src/compiler/command.dart
index 2b0eca1c297132f40c324d4c4c14dccb066bb00d..50f526c98427d4eabbb02a8fc87205c71c0b9219 100644
--- a/lib/src/compiler/command.dart
+++ b/lib/src/compiler/command.dart
@@ -4,10 +4,12 @@
import 'dart:convert' show JSON;
import 'dart:io';
+import 'package:args/args.dart';
import 'package:args/command_runner.dart';
import 'package:analyzer/src/generated/source.dart' show Source;
import 'package:analyzer/src/summary/package_bundle_reader.dart'
show InSummarySource;
+import 'package:bazel_worker/bazel_worker.dart';
import 'compiler.dart'
show BuildUnit, CompilerOptions, JSModuleFile, ModuleCompiler;
import '../analyzer/context.dart' show AnalyzerOptions;
@@ -20,26 +22,38 @@ class CompileCommand extends Command {
CompileCommand() {
argParser.addOption('out', abbr: 'o', help: 'Output file (required)');
+ 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
+ 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.
+ 'This is automatically added by bazel when running in worker mode.',
+ 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
CompilerOptions.addArguments(argParser);
AnalyzerOptions.addArguments(argParser);
}
@override
void run() {
- var compilerOptions = new CompilerOptions.fromArguments(argResults);
var compiler =
new ModuleCompiler(new AnalyzerOptions.fromArguments(argResults));
+ 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
+ new CompilerLoop(compiler, argParser, runCompiler, argResults.rest).run();
+ } else {
+ runCompiler(compiler, new CompilerOptions.fromArguments(argResults),
+ argResults['out'], argResults.rest);
+ }
+ }
- var outPath = argResults['out'];
+ 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.
+ String outPath, List<String> extraArgs,
+ {void forEachError(String error): print}) {
if (outPath == null) {
usageException('Please include the output file location. For example:\n'
' -o PATH/TO/OUTPUT_FILE.js');
}
- var unit = new BuildUnit(path.basenameWithoutExtension(outPath),
- argResults.rest, _moduleForLibrary);
+ var unit = new BuildUnit(
+ path.basenameWithoutExtension(outPath), extraArgs, _moduleForLibrary);
JSModuleFile module = compiler.compile(unit, compilerOptions);
- module.errors.forEach(print);
+ module.errors.forEach(forEachError);
if (!module.isValid) throw new CompileErrorException();
@@ -72,3 +86,36 @@ class CompileCommand extends Command {
class CompileErrorException implements Exception {
toString() => '\nPlease fix all errors before compiling (warnings are okay).';
}
+
+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.
+ String outPath, List<String> extraArgs,
+ {void forEachError(String error)});
+
+/// Runs the compiler worker loop.
+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.
+ final ModuleCompiler compiler;
+ final ArgParser argParser;
+ 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.
+ final List<String> extraArgs;
+
+ CompilerLoop(
+ this.compiler, this.argParser, this.runCompilerFn, this.extraArgs)
+ : super();
+
+ WorkResponse performRequest(WorkRequest request) {
+ var argResults = argParser.parse(request.arguments);
+ var output = new StringBuffer();
+ try {
+ 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.
+ argResults['out'], new List.from(argResults.rest)..addAll(extraArgs),
+ forEachError: output.writeln);
+ return new WorkResponse()
+ ..exitCode = EXIT_CODE_OK
+ ..output = output.toString();
+ } 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
+ return new WorkResponse()
+ ..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
+ ..output = '$output\n$e\n$s';
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698