Chromium Code Reviews| 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 _exit(int status) native "Exit"; | 5 _exit(int status) native "Exit"; |
| 6 | 6 |
| 7 class _ProcessStartStatus { | 7 class _ProcessStartStatus { |
| 8 int _errorCode; // Set to OS error code if process start failed. | 8 int _errorCode; // Set to OS error code if process start failed. |
| 9 String _errorMessage; // Set to OS error message if process start failed. | 9 String _errorMessage; // Set to OS error message if process start failed. |
| 10 } | 10 } |
| 11 | 11 |
| 12 | 12 |
| 13 class _Process extends NativeFieldWrapperClass1 implements Process { | 13 class _Process extends NativeFieldWrapperClass1 implements Process { |
| 14 static Future<ProcessResult> run(String path, | 14 static Future<ProcessResult> run(String path, |
| 15 List<String> arguments, | 15 List<String> arguments, |
| 16 [ProcessOptions options]) { | 16 [ProcessOptions options]) { |
| 17 return new _NonInteractiveProcess._start(path, arguments, options)._result; | 17 return new _NonInteractiveProcess(path, arguments, options)._result; |
| 18 } | 18 } |
| 19 | 19 |
| 20 _Process.start(String path, | 20 static Future<Process> start(String path, |
| 21 List<String> arguments, | 21 List<String> arguments, |
| 22 ProcessOptions options) { | 22 ProcessOptions options) { |
| 23 Completer completer = new Completer(); | |
| 24 _Process process = new _Process(path, arguments, options); | |
| 25 process._onStart = () { | |
| 26 process.onError = null; | |
| 27 completer.complete(process); | |
| 28 }; | |
| 29 process.onError = (e) { | |
| 30 process.onError = null; | |
| 31 completer.completeException(e); | |
| 32 }; | |
| 33 return completer.future; | |
| 34 } | |
| 35 | |
| 36 _Process(String path, List<String> arguments, ProcessOptions options) { | |
| 23 if (path is !String) { | 37 if (path is !String) { |
| 24 throw new ArgumentError("Path is not a String: $path"); | 38 throw new ArgumentError("Path is not a String: $path"); |
| 25 } | 39 } |
| 26 _path = path; | 40 _path = path; |
| 27 | 41 |
| 28 if (arguments is !List) { | 42 if (arguments is !List) { |
| 29 throw new ArgumentError("Arguments is not a List: $arguments"); | 43 throw new ArgumentError("Arguments is not a List: $arguments"); |
| 30 } | 44 } |
| 31 int len = arguments.length; | 45 int len = arguments.length; |
| 32 _arguments = new List<String>(len); | 46 _arguments = new List<String>(len); |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 258 void set onExit(void callback(int exitCode)) { | 272 void set onExit(void callback(int exitCode)) { |
| 259 if (_closed) { | 273 if (_closed) { |
| 260 throw new ProcessException("Process closed"); | 274 throw new ProcessException("Process closed"); |
| 261 } | 275 } |
| 262 if (_ended) { | 276 if (_ended) { |
| 263 throw new ProcessException("Process killed"); | 277 throw new ProcessException("Process killed"); |
| 264 } | 278 } |
| 265 _onExit = callback; | 279 _onExit = callback; |
| 266 } | 280 } |
| 267 | 281 |
| 268 void set onError(void callback(e)) { | 282 void set onError(void callback(e)) { |
|
Søren Gjesse
2012/10/12 07:47:22
Shouldn't we try to get rid of onError as well? As
Mads Ager (google)
2012/10/12 08:44:46
I would love to get rid of onError, that was one o
| |
| 269 _onError = callback; | 283 _onError = callback; |
| 270 } | 284 } |
| 271 | 285 |
| 272 void set onStart(void callback()) { | |
| 273 _onStart = callback; | |
| 274 } | |
| 275 | |
| 276 void _reportError(e) { | 286 void _reportError(e) { |
| 277 if (_onError != null) { | 287 if (_onError != null) { |
| 278 _onError(e); | 288 _onError(e); |
| 279 } else { | 289 } else { |
| 280 throw e; | 290 throw e; |
| 281 } | 291 } |
| 282 } | 292 } |
| 283 | 293 |
| 284 String _path; | 294 String _path; |
| 285 List<String> _arguments; | 295 List<String> _arguments; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 297 Function _onError; | 307 Function _onError; |
| 298 Function _onStart; | 308 Function _onStart; |
| 299 } | 309 } |
| 300 | 310 |
| 301 | 311 |
| 302 // _NonInteractiveProcess is a wrapper around an interactive process | 312 // _NonInteractiveProcess is a wrapper around an interactive process |
| 303 // that buffers output so it can be delivered when the process exits. | 313 // that buffers output so it can be delivered when the process exits. |
| 304 // _NonInteractiveProcess is used to implement the Process.run | 314 // _NonInteractiveProcess is used to implement the Process.run |
| 305 // method. | 315 // method. |
| 306 class _NonInteractiveProcess { | 316 class _NonInteractiveProcess { |
| 307 _NonInteractiveProcess._start(String path, | 317 _NonInteractiveProcess(String path, |
| 308 List<String> arguments, | 318 List<String> arguments, |
| 309 ProcessOptions options) { | 319 ProcessOptions options) { |
| 310 _completer = new Completer<ProcessResult>(); | 320 _completer = new Completer<ProcessResult>(); |
| 311 // Extract output encoding options and verify arguments. | 321 // Extract output encoding options and verify arguments. |
| 312 var stdoutEncoding = Encoding.UTF_8; | 322 var stdoutEncoding = Encoding.UTF_8; |
| 313 var stderrEncoding = Encoding.UTF_8; | 323 var stderrEncoding = Encoding.UTF_8; |
| 314 if (options !== null) { | 324 if (options !== null) { |
| 315 if (options.stdoutEncoding !== null) { | 325 if (options.stdoutEncoding !== null) { |
| 316 stdoutEncoding = options.stdoutEncoding; | 326 stdoutEncoding = options.stdoutEncoding; |
| 317 if (stdoutEncoding is !Encoding) { | 327 if (stdoutEncoding is !Encoding) { |
| 318 throw new ArgumentError( | 328 throw new ArgumentError( |
| 319 'stdoutEncoding option is not an encoding: $stdoutEncoding'); | 329 'stdoutEncoding option is not an encoding: $stdoutEncoding'); |
| 320 } | 330 } |
| 321 } | 331 } |
| 322 if (options.stderrEncoding !== null) { | 332 if (options.stderrEncoding !== null) { |
| 323 stderrEncoding = options.stderrEncoding; | 333 stderrEncoding = options.stderrEncoding; |
| 324 if (stderrEncoding is !Encoding) { | 334 if (stderrEncoding is !Encoding) { |
| 325 throw new ArgumentError( | 335 throw new ArgumentError( |
| 326 'stderrEncoding option is not an encoding: $stderrEncoding'); | 336 'stderrEncoding option is not an encoding: $stderrEncoding'); |
| 327 } | 337 } |
| 328 } | 338 } |
| 329 } | 339 } |
| 330 | 340 |
| 331 // Start the underlying process. | 341 // Start the underlying process. |
| 332 _process = new _Process.start(path, arguments, options); | 342 _process = new _Process(path, arguments, options); |
| 333 | 343 |
| 334 // Make sure stdin is closed. | 344 // Make sure stdin is closed. |
| 335 _process.onStart = _process.stdin.close; | 345 _process._onStart = _process.stdin.close; |
| 336 | 346 |
| 337 // Setup process error handling. | 347 // Setup process error handling. |
| 338 _process.onError = (e) => _completer.completeException(e); | 348 _process.onError = (e) => _completer.completeException(e); |
| 339 | 349 |
| 340 // Setup process exit handling. | 350 // Setup process exit handling. |
| 341 _process.onExit = (exitCode) { | 351 _process.onExit = (exitCode) { |
| 342 _exitCode = exitCode; | 352 _exitCode = exitCode; |
| 343 _checkDone(); | 353 _checkDone(); |
| 344 }; | 354 }; |
| 345 | 355 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 390 | 400 |
| 391 class _ProcessResult implements ProcessResult { | 401 class _ProcessResult implements ProcessResult { |
| 392 const _ProcessResult(int this.exitCode, | 402 const _ProcessResult(int this.exitCode, |
| 393 String this.stdout, | 403 String this.stdout, |
| 394 String this.stderr); | 404 String this.stderr); |
| 395 | 405 |
| 396 final int exitCode; | 406 final int exitCode; |
| 397 final String stdout; | 407 final String stdout; |
| 398 final String stderr; | 408 final String stderr; |
| 399 } | 409 } |
| OLD | NEW |