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] is used to start new processes using the static |
7 * them. | 7 * [start] and [run] methods. |
8 */ | 8 */ |
9 interface Process default _Process { | 9 class Process { |
10 /** | 10 /** |
11 * Creates a new process object and starts a process running the | 11 * Starts a process running the [executable] with the specified |
12 * [executable] with the specified [arguments]. When the process has | 12 * [arguments]. Returns a [Process] instance that can be used to |
13 * been successfully started [onStart] is called. If the process | 13 * interact with the process. |
14 * fails to start [onError] is called. | |
15 * | 14 * |
16 * An optional [ProcessOptions] object can be passed to specify | 15 * An optional [ProcessOptions] object can be passed to specify |
17 * options other than the executable and the arguments. | 16 * options other than the executable and the arguments. |
18 * | 17 * |
| 18 * When the process has been successfully started [onStart] is |
| 19 * called on the returned Process object. If the process fails to |
| 20 * start [onError] is called on the returned Process object. |
| 21 * |
19 * No data can be written to the process stdin and the process | 22 * No data can be written to the process stdin and the process |
20 * cannot be closed nor killed before [onStart] has been invoked. | 23 * cannot be closed nor killed before [onStart] has been invoked. |
21 */ | 24 */ |
22 Process.start(String executable, | 25 static Process start(String executable, |
23 List<String> arguments, | 26 List<String> arguments, |
24 [ProcessOptions options]); | 27 [ProcessOptions options]) { |
| 28 return _Process.start(executable, arguments, options); |
| 29 } |
25 | 30 |
26 /** | 31 /** |
27 * Creates a new process object, starts a process and runs it | 32 * Starts a process and runs it non-interactively to completion. The |
28 * non-interactively to completion. The process run is [executable] | 33 * process run is [executable] with the specified [arguments]. |
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 * | 34 * |
33 * Options other than the executable and the arguments are specified | 35 * An optional [ProcessOptions] object can be passed to specify |
34 * using a [ProcessOptions] object. If no options are required, | 36 * options other than the executable and the arguments. |
35 * [null] can be passed as the options. | |
36 * | 37 * |
37 * No communication via [stdin], [stdout] or [stderr] can take place | 38 * Returns a [:Future<ProcessResult>:] that completes with the |
38 * with a non-interactive process. Instead, the process is run to | 39 * result of running the process, i.e., exit code, standard out and |
39 * completion at which point the exit code and stdout and stderr are | 40 * standard in. |
40 * supplied to the [callback] parameter. | |
41 */ | 41 */ |
42 Process.run(String executable, | 42 static Future<ProcessResult> run(String executable, |
43 List<String> arguments, | 43 List<String> arguments, |
44 ProcessOptions options, | 44 [ProcessOptions options]) { |
45 void callback(int exitCode, String stdout, String stderr)); | 45 return _Process.run(executable, arguments, options); |
| 46 } |
46 | 47 |
47 /** | 48 /** |
48 * Returns an input stream of the process stdout. | 49 * Returns an input stream of the process stdout. |
49 * | 50 * |
50 * Throws an [UnsupportedOperationException] if the process is | 51 * Throws an [UnsupportedOperationException] if the process is |
51 * non-interactive. | 52 * non-interactive. |
52 */ | 53 */ |
53 InputStream get stdout(); | 54 abstract InputStream get stdout(); |
54 | 55 |
55 /** | 56 /** |
56 * Returns an input stream of the process stderr. | 57 * Returns an input stream of the process stderr. |
57 * | 58 * |
58 * Throws an [UnsupportedOperationException] if the process is | 59 * Throws an [UnsupportedOperationException] if the process is |
59 * non-interactive. | 60 * non-interactive. |
60 */ | 61 */ |
61 InputStream get stderr(); | 62 abstract InputStream get stderr(); |
62 | 63 |
63 /** | 64 /** |
64 * Returns an output stream to the process stdin. | 65 * Returns an output stream to the process stdin. |
65 * | 66 * |
66 * Throws an [UnsupportedOperationException] if the process is | 67 * Throws an [UnsupportedOperationException] if the process is |
67 * non-interactive. | 68 * non-interactive. |
68 */ | 69 */ |
69 OutputStream get stdin(); | 70 abstract OutputStream get stdin(); |
70 | 71 |
71 /** | 72 /** |
72 * Set the start handler which gets invoked when the process is | 73 * Set the start handler which gets invoked when the process is |
73 * successfully started. | 74 * successfully started. |
74 */ | 75 */ |
75 void set onStart(void callback()); | 76 abstract void set onStart(void callback()); |
76 | 77 |
77 /** | 78 /** |
78 * Sets an exit handler which gets invoked when the process | 79 * Sets an exit handler which gets invoked when the process |
79 * terminates. | 80 * terminates. |
80 * | 81 * |
81 * Throws an [UnsupportedOperationException] if the process is | 82 * Throws an [UnsupportedOperationException] if the process is |
82 * non-interactive. | 83 * non-interactive. |
83 */ | 84 */ |
84 void set onExit(void callback(int exitCode)); | 85 abstract void set onExit(void callback(int exitCode)); |
85 | 86 |
86 /** | 87 /** |
87 * Set an error handler which gets invoked if an operation on the process | 88 * Set an error handler which gets invoked if an operation on the process |
88 * fails. | 89 * fails. |
89 */ | 90 */ |
90 void set onError(void callback(e)); | 91 abstract void set onError(void callback(e)); |
91 | 92 |
92 /** | 93 /** |
93 * Kills the process. When the process terminates as a result of | 94 * Kills the process. When the process terminates as a result of |
94 * calling [kill] [onExit] is called. If the kill operation fails, | 95 * calling [kill] [onExit] is called. If the kill operation fails, |
95 * [onError] is called. | 96 * [onError] is called. |
96 */ | 97 */ |
97 void kill(); | 98 abstract void kill(); |
98 | 99 |
99 /** | 100 /** |
100 * Terminates the streams of a process. [close] most be called on a | 101 * Terminates the streams of a process. [close] most be called on a |
101 * process to free the system resources associated with it. Usually, | 102 * process to free the system resources associated with it. Usually, |
102 * close should be called in [onExit]. Once a process has been | 103 * close should be called in [onExit]. Once a process has been |
103 * closed it can no longer be killed and [onExit] is detached so the | 104 * closed it can no longer be killed and [onExit] is detached so the |
104 * application is not notified of process termination. | 105 * application is not notified of process termination. |
105 */ | 106 */ |
106 void close(); | 107 abstract void close(); |
107 } | 108 } |
108 | 109 |
109 | 110 |
| 111 /** |
| 112 * [ProcessResult] represents the result of running a non-interactive |
| 113 * process started with [:Process.run:]. |
| 114 */ |
| 115 interface ProcessResult { |
| 116 /** |
| 117 * Exit code for the process. |
| 118 */ |
| 119 int get exitCode(); |
| 120 |
| 121 /** |
| 122 * Standard output from the process as a string. |
| 123 */ |
| 124 String get stdout(); |
| 125 |
| 126 /** |
| 127 * Standard error from the process as a string. |
| 128 */ |
| 129 String get stderr(); |
| 130 } |
| 131 |
| 132 |
110 /** | 133 /** |
111 * [ProcessOptions] represents the options that can be supplied when | 134 * [ProcessOptions] represents the options that can be supplied when |
112 * starting a process. | 135 * starting a process. |
113 */ | 136 */ |
114 class ProcessOptions { | 137 class ProcessOptions { |
115 /** | 138 /** |
116 * The working directory from which the process is started. Note | 139 * The working directory from which the process is started. Note |
117 * that the change of directory occurs before executing the process | 140 * that the change of directory occurs before executing the process |
118 * on some platforms, which may have impact when using relative | 141 * on some platforms, which may have impact when using relative |
119 * paths for the executable and the arguments. | 142 * paths for the executable and the arguments. |
(...skipping 29 matching lines...) Expand all Loading... |
149 * Currently, only ASCII environment variables are supported and | 172 * Currently, only ASCII environment variables are supported and |
150 * errors are likely to occur if an environment variables with | 173 * errors are likely to occur if an environment variables with |
151 * code-points outside the ASCII range is passed in. | 174 * code-points outside the ASCII range is passed in. |
152 */ | 175 */ |
153 Map<String, String> environment; | 176 Map<String, String> environment; |
154 } | 177 } |
155 | 178 |
156 | 179 |
157 class ProcessException implements Exception { | 180 class ProcessException implements Exception { |
158 const ProcessException([String this.message, int this.errorCode = 0]); | 181 const ProcessException([String this.message, int this.errorCode = 0]); |
159 String toString() => "ProcessException: $message"; | 182 String toString() => "ProcessException: $message ($errorCode)"; |
160 | 183 |
161 /** | 184 /** |
162 * Contains the system message for the process exception if any. | 185 * Contains the system message for the process exception if any. |
163 */ | 186 */ |
164 final String message; | 187 final String message; |
165 | 188 |
166 /** | 189 /** |
167 * Contains the OS error code for the process exception if any. | 190 * Contains the OS error code for the process exception if any. |
168 */ | 191 */ |
169 final int errorCode; | 192 final int errorCode; |
170 } | 193 } |
OLD | NEW |