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

Side by Side Diff: bin/dartdevc.dart

Issue 1896613002: Move all args to per-request for the worker, and add a test (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: use exit code 70 for internal errors 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 unified diff | Download patch
« no previous file with comments | « no previous file | lib/src/compiler/command.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env dart 1 #!/usr/bin/env dart
2 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 2 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
3 // for details. All rights reserved. Use of this source code is governed by a 3 // for details. All rights reserved. Use of this source code is governed by a
4 // BSD-style license that can be found in the LICENSE file. 4 // BSD-style license that can be found in the LICENSE file.
5 5
6 /// Command line entry point for Dart Development Compiler (dartdevc). 6 /// Command line entry point for Dart Development Compiler (dartdevc).
7 /// 7 ///
8 /// Supported commands are 8 /// Supported commands are
9 /// * compile: builds a collection of dart libraries into a single JS module 9 /// * compile: builds a collection of dart libraries into a single JS module
10 /// 10 ///
(...skipping 19 matching lines...) Expand all
30 /// file server, 30 /// file server,
31 /// * We didn't have a file watcher API at first, 31 /// * We didn't have a file watcher API at first,
32 /// * We had no conventions about where compiled output should go (or even 32 /// * We had no conventions about where compiled output should go (or even
33 /// that we would be compiling at all, vs running on an in-browser Dart VM), 33 /// that we would be compiling at all, vs running on an in-browser Dart VM),
34 /// * We wanted a good first impression with our simple examples, so we used 34 /// * We wanted a good first impression with our simple examples, so we used
35 /// local file servers, and users have an expectation of it now, even though 35 /// local file servers, and users have an expectation of it now, even though
36 /// it doesn't scale to typical apps that need their own real servers. 36 /// it doesn't scale to typical apps that need their own real servers.
37 37
38 import 'dart:io'; 38 import 'dart:io';
39 import 'package:args/command_runner.dart'; 39 import 'package:args/command_runner.dart';
40 import 'package:bazel_worker/bazel_worker.dart';
40 import 'package:dev_compiler/src/compiler/command.dart'; 41 import 'package:dev_compiler/src/compiler/command.dart';
41 42
42 main(List<String> args) async { 43 main(List<String> args) async {
44 // Always returns a new modifiable list.
43 args = _preprocessArgs(args); 45 args = _preprocessArgs(args);
44 46
45 var runner = new CommandRunner('dartdevc', 'Dart Development Compiler'); 47 if (args.contains('--persistent_worker')) {
46 runner.addCommand(new CompileCommand()); 48 new _CompilerWorker(args..remove('--persistent_worker')).run();
47 try { 49 } else {
48 await runner.run(args); 50 exitCode = _runCommand(args);
49 } catch (e, s) {
50 exit(handleError(e, s, args));
51 } 51 }
52 } 52 }
53 53
54 /// Runs a single compile command, and returns an exit code.
55 int _runCommand(List<String> args, {MessageHandler messageHandler}) {
56 try {
57 var runner = new CommandRunner('dartdevc', 'Dart Development Compiler');
58 runner.addCommand(new CompileCommand(messageHandler: messageHandler));
59 runner.run(args);
60 } catch (e, s) {
61 return _handleError(e, s, args, messageHandler: messageHandler);
62 }
63 return EXIT_CODE_OK;
64 }
65
66 /// Runs the compiler worker loop.
67 class _CompilerWorker extends SyncWorkerLoop {
68 /// The original args supplied to the executable.
69 final List<String> _startupArgs;
70
71 _CompilerWorker(this._startupArgs) : super();
72
73 /// Performs each individual work request.
74 WorkResponse performRequest(WorkRequest request) {
75 var args = new List.from(_startupArgs)..addAll(request.arguments);
76
77 var output = new StringBuffer();
78 return new WorkResponse()
79 ..exitCode = _runCommand(args, messageHandler: output.writeln)
80 ..output = output.toString();
81 }
82 }
83
84 /// Handles [error] in a uniform fashion. Returns the proper exit code and calls
85 /// [messageHandler] with messages.
86 int _handleError(dynamic error, dynamic stackTrace, List<String> args,
87 {MessageHandler messageHandler}) {
88 messageHandler ??= print;
89
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 70;
123 }
124 }
125
126 /// Always returns a new modifiable list.
127 ///
54 /// If the final arg is `@file_path` then read in all the lines of that file 128 /// If the final arg is `@file_path` then read in all the lines of that file
55 /// and add those as args. 129 /// and add those as args.
56 /// 130 ///
57 /// Bazel actions that support workers must provide all their per-WorkRequest 131 /// Bazel actions that support workers must provide all their per-WorkRequest
58 /// arguments in a file like this instead of as normal args. 132 /// arguments in a file like this instead of as normal args.
59 List<String> _preprocessArgs(List<String> args) { 133 List<String> _preprocessArgs(List<String> args) {
134 args = new List.from(args);
60 if (args.isNotEmpty && args.last.startsWith('@')) { 135 if (args.isNotEmpty && args.last.startsWith('@')) {
61 return new List.from(args) 136 var fileArg = args.removeLast();
62 ..addAll(new File(args.last.substring(1)).readAsLinesSync()); 137 args.addAll(new File(fileArg.substring(1)).readAsLinesSync());
63 } else {
64 return args;
65 } 138 }
139 return args;
66 } 140 }
OLDNEW
« no previous file with comments | « no previous file | lib/src/compiler/command.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698