| OLD | NEW |
| 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 [String workingDirectory]) { |
| 20 return new _InteractiveProcess.start(path, arguments, workingDirectory); |
| 21 } |
| 22 |
| 23 factory _Process.startNonInteractive(String path, |
| 24 List<String> arguments, |
| 25 String workingDirectory, |
| 26 void callback(int exitCode, |
| 27 String stdout, |
| 28 String stderr)) { |
| 29 return new _NonInteractiveProcess.start(path, |
| 30 arguments, |
| 31 workingDirectory, |
| 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 String workingDirectory) { |
| 16 if (path is !String) { | 44 if (path is !String) { |
| 17 throw new ProcessException("Path is not a String: $path"); | 45 throw new ProcessException("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 ProcessException("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); |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 _Socket _err; | 280 _Socket _err; |
| 253 Socket _exitHandler; | 281 Socket _exitHandler; |
| 254 int _pid; | 282 int _pid; |
| 255 bool _closed; | 283 bool _closed; |
| 256 bool _killed; | 284 bool _killed; |
| 257 bool _started; | 285 bool _started; |
| 258 Function _onExit; | 286 Function _onExit; |
| 259 Function _onError; | 287 Function _onError; |
| 260 Function _onStart; | 288 Function _onStart; |
| 261 } | 289 } |
| 290 |
| 291 |
| 292 // _NonInteractiveProcess is a wrapper around an interactive process |
| 293 // that restricts the interface to disallow access to the streams and |
| 294 // buffers output so it can be delivered to the callback when the |
| 295 // process exits. |
| 296 class _NonInteractiveProcess extends _Process { |
| 297 |
| 298 _NonInteractiveProcess.start(String path, |
| 299 List<String> arguments, |
| 300 String workingDirectory, |
| 301 Function this._callback) { |
| 302 _process = new _InteractiveProcess.start(path, arguments, workingDirectory); |
| 303 |
| 304 // Setup process exit handling. |
| 305 _process.onExit = (exitCode) { |
| 306 _exitCode = exitCode; |
| 307 _checkDone(); |
| 308 }; |
| 309 |
| 310 // Setup stdout handling. |
| 311 _stdoutBuffer = new StringBuffer(); |
| 312 var stdoutStream = new StringInputStream(_process.stdout); |
| 313 stdoutStream.onData = () { |
| 314 var data = stdoutStream.read(); |
| 315 if (data != null) _stdoutBuffer.add(data); |
| 316 }; |
| 317 stdoutStream.onClosed = () { |
| 318 _stdoutClosed = true; |
| 319 _checkDone(); |
| 320 }; |
| 321 |
| 322 // Setup stderr handling. |
| 323 _stderrBuffer = new StringBuffer(); |
| 324 var stderrStream = new StringInputStream(_process.stderr); |
| 325 stderrStream.onData = () { |
| 326 var data = stderrStream.read(); |
| 327 if (data != null) _stderrBuffer.add(data); |
| 328 }; |
| 329 stderrStream.onClosed = () { |
| 330 _stderrClosed = true; |
| 331 _checkDone(); |
| 332 }; |
| 333 } |
| 334 |
| 335 void _checkDone() { |
| 336 if (_exitCode != null && _stderrClosed && _stdoutClosed) { |
| 337 _callback(_exitCode, _stdoutBuffer.toString(), _stderrBuffer.toString()); |
| 338 } |
| 339 } |
| 340 |
| 341 InputStream get stdout() { |
| 342 throw new ProcessException( |
| 343 'Cannot get stdout stream for process started with ' |
| 344 'the startNonInteractive constructor. The entire stdout ' |
| 345 'will be supplied in the callback on completion.'); |
| 346 } |
| 347 |
| 348 InputStream get stderr() { |
| 349 throw new ProcessException( |
| 350 'Cannot get stderr stream for process started with ' |
| 351 'the startNonInteractive constructor. The entire stderr ' |
| 352 'will be supplied in the callback on completion.'); |
| 353 } |
| 354 |
| 355 OutputStream get stdin() { |
| 356 throw new ProcessException( |
| 357 'Cannot communicate via stdin with process started with ' |
| 358 'the startNonInteractive constructor'); |
| 359 } |
| 360 |
| 361 void set onStart(void callback()) => _process.onStart = callback; |
| 362 |
| 363 void set onExit(void callback(int exitCode)) { |
| 364 throw new ProcessException( |
| 365 'Cannot set exit handler on process started with ' |
| 366 'the startNonInteractive constructor. The exit code will ' |
| 367 'be supplied in the callback on completion.'); |
| 368 } |
| 369 |
| 370 void set onError(void callback(ProcessException error)) { |
| 371 _process.onError = callback; |
| 372 } |
| 373 |
| 374 void kill() => _process.kill(); |
| 375 |
| 376 void close() => _process.close(); |
| 377 |
| 378 Process _process; |
| 379 Function _callback; |
| 380 StringBuffer _stdoutBuffer; |
| 381 StringBuffer _stderrBuffer; |
| 382 int _exitCode; |
| 383 bool _stdoutClosed = false; |
| 384 bool _stderrClosed = false; |
| 385 } |
| OLD | NEW |