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 /** | 5 /** |
| 6 * [Process] objects are used to start new processes and interact with | 6 * [Process] objects are used to start new processes and interact with |
| 7 * them. | 7 * them. |
| 8 */ | 8 */ |
| 9 interface Process default _Process { | 9 interface Process default _Process { |
|
Søren Gjesse
2012/04/12 15:02:21
As the implementation of start and run use two alm
| |
| 10 /** | 10 /** |
| 11 * Creates a new process object and starts a process running the | 11 * Creates a new process object and starts a process running the |
| 12 * [executable] with the specified [arguments]. When the process has | 12 * [executable] with the specified [arguments]. When the process has |
| 13 * been successfully started [onStart] is called. If the process | 13 * been successfully started [onStart] is called. If the process |
| 14 * fails to start [onError] is called. | 14 * fails to start [onError] is called. |
| 15 * | 15 * |
| 16 * An optional [workingDirectory] can be passed to specify where the process | 16 * An optional [ProcessOptions] object can be passed to specify |
| 17 * is run from. Note that the change of directory occurs before executing | 17 * options other than the executable and the arguments. |
| 18 * the process on some platforms, which may have impact when using relative | |
| 19 * paths for [executable] and [arguments]. | |
| 20 * | 18 * |
| 21 * No data can be written to the process stdin and the process | 19 * No data can be written to the process stdin and the process |
| 22 * cannot be closed nor killed before [onStart] has been invoked. | 20 * cannot be closed nor killed before [onStart] has been invoked. |
| 23 */ | 21 */ |
| 24 Process.start(String executable, | 22 Process.start(String executable, |
| 25 List<String> arguments, | 23 List<String> arguments, |
| 26 [String workingDirectory]); | 24 [ProcessOptions options]); |
| 25 | |
| 26 /** | |
| 27 * Creates a new process object, starts a process and runs it | |
| 28 * non-interactively to completion. The process run is [executable] | |
| 29 * with the specified [arguments]. When the process has been | |
| 30 * successfully started [onStart] is called. If the process fails to | |
| 31 * start [onError] is called. | |
| 32 * | |
| 33 * Options other than the executable and the arguments are specified | |
| 34 * using a [ProcessOptions] object. If no options are required, | |
| 35 * [null] can be passed as the options. | |
| 36 * | |
| 37 * No communication via [stdin], [stdout] or [stderr] can take place | |
| 38 * with a non-interactive process. Instead, the process is run to | |
| 39 * completion at which point the exit code and stdout and stderr are | |
| 40 * supplied to the [callback] parameter. | |
| 41 */ | |
| 42 Process.run(String executable, | |
| 43 List<String> arguments, | |
| 44 ProcessOptions options, | |
| 45 void callback(int exitCode, String stdout, String stderr)); | |
| 27 | 46 |
| 28 /** | 47 /** |
| 29 * Returns an input stream of the process stdout. | 48 * Returns an input stream of the process stdout. |
| 49 * | |
| 50 * Throws an [UnsupportedOperationException] if the process is | |
| 51 * non-interactive. | |
| 30 */ | 52 */ |
| 31 InputStream get stdout(); | 53 InputStream get stdout(); |
| 32 | 54 |
| 33 /** | 55 /** |
| 34 * Returns an input stream of the process stderr. | 56 * Returns an input stream of the process stderr. |
| 57 * | |
| 58 * Throws an [UnsupportedOperationException] if the process is | |
| 59 * non-interactive. | |
| 35 */ | 60 */ |
| 36 InputStream get stderr(); | 61 InputStream get stderr(); |
| 37 | 62 |
| 38 /** | 63 /** |
| 39 * Returns an output stream to the process stdin. | 64 * Returns an output stream to the process stdin. |
| 65 * | |
| 66 * Throws an [UnsupportedOperationException] if the process is | |
| 67 * non-interactive. | |
| 40 */ | 68 */ |
| 41 OutputStream get stdin(); | 69 OutputStream get stdin(); |
| 42 | 70 |
| 43 /** | 71 /** |
| 44 * Set the start handler which gets invoked when the process is | 72 * Set the start handler which gets invoked when the process is |
| 45 * successfully started. | 73 * successfully started. |
| 46 */ | 74 */ |
| 47 void set onStart(void callback()); | 75 void set onStart(void callback()); |
| 48 | 76 |
| 49 /** | 77 /** |
| 50 * Sets an exit handler which gets invoked when the process terminates. | 78 * Sets an exit handler which gets invoked when the process |
| 79 * terminates. | |
| 80 * | |
| 81 * Throws an [UnsupportedOperationException] if the process is | |
| 82 * non-interactive. | |
| 51 */ | 83 */ |
| 52 void set onExit(void callback(int exitCode)); | 84 void set onExit(void callback(int exitCode)); |
| 53 | 85 |
| 54 /** | 86 /** |
| 55 * Set an error handler which gets invoked if an operation on the process | 87 * Set an error handler which gets invoked if an operation on the process |
| 56 * fails. | 88 * fails. |
| 57 */ | 89 */ |
| 58 void set onError(void callback(ProcessException error)); | 90 void set onError(void callback(ProcessException error)); |
| 59 | 91 |
| 60 /** | 92 /** |
| 61 * Kills the process. When the process terminates as a result of | 93 * Kills the process. When the process terminates as a result of |
| 62 * calling [kill] [onExit] is called. If the kill operation fails, | 94 * calling [kill] [onExit] is called. If the kill operation fails, |
| 63 * [onError] is called. | 95 * [onError] is called. |
| 64 */ | 96 */ |
| 65 void kill(); | 97 void kill(); |
| 66 | 98 |
| 67 /** | 99 /** |
| 68 * Terminates the streams of a process. [close] most be called on a | 100 * Terminates the streams of a process. [close] most be called on a |
| 69 * process to free the system resources associated with it. Usually, | 101 * process to free the system resources associated with it. Usually, |
| 70 * close should be called in [onExit]. Once a process has been | 102 * close should be called in [onExit]. Once a process has been |
| 71 * closed it can no longer be killed and [onExit] is detached so the | 103 * closed it can no longer be killed and [onExit] is detached so the |
| 72 * application is not notified of process termination. | 104 * application is not notified of process termination. |
| 73 */ | 105 */ |
| 74 void close(); | 106 void close(); |
| 75 } | 107 } |
| 76 | 108 |
| 77 | 109 |
| 110 /** | |
| 111 * [ProcessOptions]q represents the options that can be supplied when | |
|
Søren Gjesse
2012/04/12 15:02:21
q?
Mads Ager (google)
2012/04/13 08:49:39
Done.
| |
| 112 * starting a process. | |
| 113 */ | |
| 114 class ProcessOptions { | |
| 115 /** | |
| 116 * The working directory from which the process is started. Note | |
| 117 * that the change of directory occurs before executing the process | |
| 118 * on some platforms, which may have impact when using relative | |
| 119 * paths for the executable and the arguments. | |
| 120 */ | |
| 121 String workingDirectory; | |
| 122 | |
| 123 /** | |
| 124 * The encoding used for text on stdout when starting a | |
| 125 * non-interactive process with [:Process.run:]. | |
| 126 * | |
| 127 * This option is ignored for interactive processes started with | |
| 128 * [:Process.start:]. | |
| 129 * | |
| 130 * The default stdoutEncoding is UTF_8. | |
| 131 */ | |
| 132 Encoding stdoutEncoding; | |
|
Søren Gjesse
2012/04/12 15:02:21
The encodings are gnored when passing this to Proc
Mads Ager (google)
2012/04/13 08:49:39
I went for ignoring. I don't think it matters much
| |
| 133 | |
| 134 /** | |
| 135 * The encoding used for text on stderr when starting a | |
| 136 * non-interactive process with [:Process.run:]. | |
| 137 * | |
| 138 * This option is ignored for interactive processes started with | |
| 139 * [:Process.start:]. | |
| 140 * | |
| 141 * The default stderrEncoding is UTF_8. | |
| 142 */ | |
| 143 Encoding stderrEncoding; | |
|
Søren Gjesse
2012/04/12 15:02:21
Should there be a string or bytearray that will be
Mads Ager (google)
2012/04/13 08:49:39
Yes, there probably should. I'll add that in a sep
| |
| 144 } | |
| 145 | |
| 146 | |
| 78 class ProcessException implements Exception { | 147 class ProcessException implements Exception { |
| 79 const ProcessException([String this.message, int this.errorCode = 0]); | 148 const ProcessException([String this.message, int this.errorCode = 0]); |
| 80 String toString() => "ProcessException: $message"; | 149 String toString() => "ProcessException: $message"; |
| 81 | 150 |
| 82 /** | 151 /** |
| 83 * Contains the system message for the process exception if any. | 152 * Contains the system message for the process exception if any. |
| 84 */ | 153 */ |
| 85 final String message; | 154 final String message; |
| 86 | 155 |
| 87 /** | 156 /** |
| 88 * Contains the OS error code for the process exception if any. | 157 * Contains the OS error code for the process exception if any. |
| 89 */ | 158 */ |
| 90 final int errorCode; | 159 final int errorCode; |
| 91 } | 160 } |
| OLD | NEW |