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

Side by Side Diff: runtime/bin/process_impl.dart

Issue 9863015: This is a request for comments. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Minor indentation fixes Created 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 class _ProcessStartStatus { 5 class _ProcessStartStatus {
6 int _errorCode; // Set to OS error code if process start failed. 6 int _errorCode; // Set to OS error code if process start failed.
7 String _errorMessage; // Set to OS error message if process start failed. 7 String _errorMessage; // Set to OS error message if process start failed.
8 } 8 }
9 9
10 10
11 // Abstract factory class capable of producing interactive and
12 // non-interactive processes.
11 class _Process implements Process { 13 class _Process implements Process {
12 14
13 _Process.start(String path, 15 _Process();
14 List<String> arguments, 16
15 [String workingDirectory]) { 17 factory _Process.start(String path,
18 List<String> arguments,
19 [ProcessOptions options]) {
20 return new _InteractiveProcess.start(path, arguments, options);
21 }
22
23 factory _Process.run(String path,
24 List<String> arguments,
25 ProcessOptions options,
26 void callback(int exitCode,
27 String stdout,
28 String stderr)) {
29 return new _NonInteractiveProcess.start(path,
30 arguments,
31 options,
32 callback);
33 }
34 }
35
36
37 // _InteractiveProcess is the actual implementation of all processes
38 // started from Dart code.
39 class _InteractiveProcess extends _Process {
40
41 _InteractiveProcess.start(String path,
42 List<String> arguments,
43 ProcessOptions options) {
16 if (path is !String) { 44 if (path is !String) {
17 throw new ProcessException("Path is not a String: $path"); 45 throw new IllegalArgumentException("Path is not a String: $path");
18 } 46 }
19 _path = path; 47 _path = path;
20 48
21 if (arguments is !List) { 49 if (arguments is !List) {
22 throw new ProcessException("Arguments is not a List: $arguments"); 50 throw new IllegalArgumentException("Arguments is not a List: $arguments");
23 } 51 }
24 int len = arguments.length; 52 int len = arguments.length;
25 _arguments = new ObjectArray<String>(len); 53 _arguments = new ObjectArray<String>(len);
26 for (int i = 0; i < len; i++) { 54 for (int i = 0; i < len; i++) {
27 var arg = arguments[i]; 55 var arg = arguments[i];
28 if (arg is !String) { 56 if (arg is !String) {
29 throw new ProcessException("Non-string argument: $arg"); 57 throw new IllegalArgumentException("Non-string argument: $arg");
30 } 58 }
31 _arguments[i] = arguments[i]; 59 _arguments[i] = arguments[i];
32 if (new Platform().operatingSystem() == 'windows') { 60 if (new Platform().operatingSystem() == 'windows') {
33 _arguments[i] = _windowsArgumentEscape(_arguments[i]); 61 _arguments[i] = _windowsArgumentEscape(_arguments[i]);
34 } 62 }
35 } 63 }
36 64
37 if (workingDirectory is !String && workingDirectory !== null) { 65 if (options !== null && options.workingDirectory !== null) {
38 throw new ProcessException( 66 _workingDirectory = options.workingDirectory;
39 "WorkingDirectory is not a String: $workingDirectory"); 67 if (_workingDirectory is !String) {
68 throw new IllegalArgumentException(
69 "WorkingDirectory is not a String: $_workingDirectory");
70 }
40 } 71 }
41 _workingDirectory = workingDirectory;
42 72
43 _in = new _Socket._internalReadOnly(); // stdout coming from process. 73 _in = new _Socket._internalReadOnly(); // stdout coming from process.
44 _out = new _Socket._internalWriteOnly(); // stdin going to process. 74 _out = new _Socket._internalWriteOnly(); // stdin going to process.
45 _err = new _Socket._internalReadOnly(); // stderr coming from process. 75 _err = new _Socket._internalReadOnly(); // stderr coming from process.
46 _exitHandler = new _Socket._internalReadOnly(); 76 _exitHandler = new _Socket._internalReadOnly();
47 _closed = false; 77 _closed = false;
48 _killed = false; 78 _killed = false;
49 _started = false; 79 _started = false;
50 _onExit = null; 80 _onExit = null;
51 // TODO(ager): Make the actual process starting really async instead of 81 // TODO(ager): Make the actual process starting really async instead of
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 _Socket _err; 282 _Socket _err;
253 Socket _exitHandler; 283 Socket _exitHandler;
254 int _pid; 284 int _pid;
255 bool _closed; 285 bool _closed;
256 bool _killed; 286 bool _killed;
257 bool _started; 287 bool _started;
258 Function _onExit; 288 Function _onExit;
259 Function _onError; 289 Function _onError;
260 Function _onStart; 290 Function _onStart;
261 } 291 }
292
293
294 // _NonInteractiveProcess is a wrapper around an interactive process
295 // that restricts the interface to disallow access to the streams and
296 // buffers output so it can be delivered to the callback when the
297 // process exits.
298 class _NonInteractiveProcess extends _Process {
299
300 _NonInteractiveProcess.start(String path,
301 List<String> arguments,
302 ProcessOptions options,
303 Function this._callback) {
304 _process = new _InteractiveProcess.start(path, arguments, options);
305
306 // Setup process exit handling.
307 _process.onExit = (exitCode) {
308 _exitCode = exitCode;
309 _checkDone();
310 };
311
312 // Extract output encoding options.
313 var stdoutEncoding = Encoding.UTF_8;
314 var stderrEncoding = Encoding.UTF_8;
315 if (options !== null) {
316 if (options.stdoutEncoding !== null) {
317 stdoutEncoding = options.stdoutEncoding;
318 if (stdoutEncoding is !Encoding) {
Søren Gjesse 2012/04/12 15:02:21 Maybe we should get rid of this type check and mov
Mads Ager (google) 2012/04/13 08:49:39 It will throw an exception through the StringInput
319 throw new IllegalArgumentException(
320 'stdoutEncoding option is not an encoding: $stdoutEncoding');
321 }
322 }
323 if (options.stderrEncoding !== null) {
324 stderrEncoding = options.stderrEncoding;
325 if (stderrEncoding is !Encoding) {
Søren Gjesse 2012/04/12 15:02:21 Ditto.
326 throw new IllegalArgumentException(
327 'stderrEncoding option is not an encoding: $stderrEncoding');
328 }
329 }
330 }
331
332 // Setup stdout handling.
333 _stdoutBuffer = new StringBuffer();
334 var stdoutStream = new StringInputStream(_process.stdout, stdoutEncoding);
335 stdoutStream.onData = () {
336 var data = stdoutStream.read();
337 if (data != null) _stdoutBuffer.add(data);
338 };
339 stdoutStream.onClosed = () {
340 _stdoutClosed = true;
341 _checkDone();
342 };
343
344 // Setup stderr handling.
345 _stderrBuffer = new StringBuffer();
346 var stderrStream = new StringInputStream(_process.stderr, stderrEncoding);
347 stderrStream.onData = () {
348 var data = stderrStream.read();
349 if (data != null) _stderrBuffer.add(data);
350 };
351 stderrStream.onClosed = () {
352 _stderrClosed = true;
353 _checkDone();
354 };
355 }
356
357 void _checkDone() {
358 if (_exitCode != null && _stderrClosed && _stdoutClosed) {
359 _callback(_exitCode, _stdoutBuffer.toString(), _stderrBuffer.toString());
360 }
361 }
362
363 InputStream get stdout() {
364 throw new UnsupportedOperationException(
365 'Cannot get stdout stream for process started with '
366 'the run constructor. The entire stdout '
367 'will be supplied in the callback on completion.');
368 }
369
370 InputStream get stderr() {
371 throw new UnsupportedOperationException(
372 'Cannot get stderr stream for process started with '
373 'the run constructor. The entire stderr '
374 'will be supplied in the callback on completion.');
375 }
376
377 OutputStream get stdin() {
378 throw new UnsupportedOperationException(
379 'Cannot communicate via stdin with process started with '
380 'the run constructor');
381 }
382
383 void set onStart(void callback()) => _process.onStart = callback;
384
385 void set onExit(void callback(int exitCode)) {
386 throw new UnsupportedOperationException(
387 'Cannot set exit handler on process started with '
388 'the run constructor. The exit code will '
389 'be supplied in the callback on completion.');
390 }
391
392 void set onError(void callback(ProcessException error)) {
393 _process.onError = callback;
394 }
395
396 void kill() => _process.kill();
397
398 void close() => _process.close();
399
400 Process _process;
401 Function _callback;
402 StringBuffer _stdoutBuffer;
403 StringBuffer _stderrBuffer;
404 int _exitCode;
405 bool _stdoutClosed = false;
406 bool _stderrClosed = false;
407 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698