| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of dart.io; | 5 part of dart.io; |
| 6 | 6 |
| 7 // TODO(ager): The only reason for this class is that we | 7 // TODO(ager): The only reason for this class is that we |
| 8 // cannot patch a top-level at this point. | 8 // cannot patch a top-level at this point. |
| 9 class _ProcessUtils { | 9 class _ProcessUtils { |
| 10 external static void _exit(int status); | 10 external static void _exit(int status); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 */ | 65 */ |
| 66 abstract class Process { | 66 abstract class Process { |
| 67 /** | 67 /** |
| 68 * Starts a process running the [executable] with the specified | 68 * Starts a process running the [executable] with the specified |
| 69 * [arguments]. Returns a [:Future<Process>:] that completes with a | 69 * [arguments]. Returns a [:Future<Process>:] that completes with a |
| 70 * Process instance when the process has been successfully | 70 * Process instance when the process has been successfully |
| 71 * started. That [Process] object can be used to interact with the | 71 * started. That [Process] object can be used to interact with the |
| 72 * process. If the process cannot be started the returned [Future] | 72 * process. If the process cannot be started the returned [Future] |
| 73 * completes with an exception. | 73 * completes with an exception. |
| 74 * | 74 * |
| 75 * An optional [ProcessOptions] object can be passed to specify | 75 * Use [workingDirectory] to set the working directory for the process. Note |
| 76 * options other than the executable and the arguments. | 76 * that the change of directory occurs before executing the process on some |
| 77 * platforms, which may have impact when using relative paths for the |
| 78 * executable and the arguments. |
| 79 * |
| 80 * Use [environment] to set the environment variables for the process. If not |
| 81 * set the environment of the parent process is inherited. Currently, only |
| 82 * US-ASCII environment variables are supported and errors are likely to occur |
| 83 * if an environment variable with code-points outside the US-ASCII range is |
| 84 * passed in. |
| 85 * |
| 86 * If [runInShell] is true, the process will be spawned through a system |
| 87 * shell. On Linux and Mac OS, [:/bin/sh:] is used, while |
| 88 * [:%WINDIR%\system32\cmd.exe:] is used on Windows. |
| 77 * | 89 * |
| 78 * Users must read all data coming on the [stdout] and [stderr] | 90 * Users must read all data coming on the [stdout] and [stderr] |
| 79 * streams of processes started with [:Process.start:]. If the user | 91 * streams of processes started with [:Process.start:]. If the user |
| 80 * does not read all data on the streams the underlying system | 92 * does not read all data on the streams the underlying system |
| 81 * resources will not be freed since there is still pending data. | 93 * resources will not be freed since there is still pending data. |
| 82 */ | 94 */ |
| 83 external static Future<Process> start(String executable, | 95 external static Future<Process> start( |
| 84 List<String> arguments, | 96 String executable, |
| 85 [ProcessOptions options]); | 97 List<String> arguments, |
| 98 {String workingDirectory, |
| 99 Map<String, String> environment, |
| 100 bool runInShell: false}); |
| 86 | 101 |
| 87 /** | 102 /** |
| 88 * Starts a process and runs it non-interactively to completion. The | 103 * Starts a process and runs it non-interactively to completion. The |
| 89 * process run is [executable] with the specified [arguments]. | 104 * process run is [executable] with the specified [arguments]. |
| 90 * | 105 * |
| 91 * An optional [ProcessOptions] object can be passed to specify | 106 * Use [workingDirectory] to set the working directory for the process. Note |
| 92 * options other than the executable and the arguments. | 107 * that the change of directory occurs before executing the process on some |
| 108 * platforms, which may have impact when using relative paths for the |
| 109 * executable and the arguments. |
| 110 * |
| 111 * Use [environment] to set the environment variables for the process. If not |
| 112 * set the environment of the parent process is inherited. Currently, only |
| 113 * US-ASCII environment variables are supported and errors are likely to occur |
| 114 * if an environment variable with code-points outside the US-ASCII range is |
| 115 * passed in. |
| 116 * |
| 117 * If [runInShell] is true, the process will be spawned through a system |
| 118 * shell. On Linux and Mac OS, [:/bin/sh:] is used, while |
| 119 * [:%WINDIR%\system32\cmd.exe:] is used on Windows. |
| 120 * |
| 121 * The encoding used for text on stdout and stderr can be set by changing |
| 122 * [stdoutEncoding] and [stderrEncoding]. The default encoding is SYSTEM. |
| 93 * | 123 * |
| 94 * Returns a [:Future<ProcessResult>:] that completes with the | 124 * Returns a [:Future<ProcessResult>:] that completes with the |
| 95 * result of running the process, i.e., exit code, standard out and | 125 * result of running the process, i.e., exit code, standard out and |
| 96 * standard in. | 126 * standard in. |
| 97 */ | 127 */ |
| 98 external static Future<ProcessResult> run(String executable, | 128 external static Future<ProcessResult> run( |
| 99 List<String> arguments, | 129 String executable, |
| 100 [ProcessOptions options]); | 130 List<String> arguments, |
| 101 | 131 {String workingDirectory, |
| 102 /** | 132 Map<String, String> environment, |
| 103 * Starts a process in the system shell and runs it non-interactively to | 133 bool runInShell: false, |
| 104 * completion. | 134 Encoding stdoutEncoding: Encoding.SYSTEM, |
| 105 * | 135 Encoding stderrEncoding: Encoding.SYSTEM}); |
| 106 * On Linux and Mac OS, [:/bin/sh:] is used to execute the [executable]. | |
| 107 * On Windows, [:%WINDIR%\system32\cmd.exe:] is used. | |
| 108 * | |
| 109 * An optional [ProcessOptions] object can be passed to specify | |
| 110 * options other than the executable and the arguments. | |
| 111 * | |
| 112 * Returns a [:Future<ProcessResult>:] that completes with the | |
| 113 * result of running the process, i.e., exit code, standard out and | |
| 114 * standard in. | |
| 115 */ | |
| 116 static Future<ProcessResult> runShell(String executable, | |
| 117 List<String> arguments, | |
| 118 [ProcessOptions options]) | |
| 119 => run(_getShellCommand(), | |
| 120 _getShellArguments(executable, arguments), | |
| 121 options); | |
| 122 | |
| 123 static String _getShellCommand() { | |
| 124 if (Platform.operatingSystem == 'windows') { | |
| 125 return 'cmd.exe'; | |
| 126 } | |
| 127 return '/bin/sh'; | |
| 128 } | |
| 129 | |
| 130 static List<String> _getShellArguments(String executable, | |
| 131 List<String> arguments) { | |
| 132 List<String> shellArguments = []; | |
| 133 if (Platform.operatingSystem == 'windows') { | |
| 134 shellArguments.add('/c'); | |
| 135 shellArguments.add(executable); | |
| 136 for (var arg in arguments) { | |
| 137 arg = arg.replaceAll('"', r'\"'); | |
| 138 shellArguments.add(arg); | |
| 139 } | |
| 140 } else { | |
| 141 var commandLine = new StringBuffer(); | |
| 142 commandLine.write(executable); | |
| 143 shellArguments.add("-c"); | |
| 144 for (var arg in arguments) { | |
| 145 arg = arg.replaceAll("'", "'\"'\"'"); | |
| 146 commandLine.write(" '$arg'"); | |
| 147 } | |
| 148 shellArguments.add(commandLine.toString()); | |
| 149 } | |
| 150 return shellArguments; | |
| 151 } | |
| 152 | 136 |
| 153 /** | 137 /** |
| 154 * Returns the standard output stream of the process as a [:Stream:]. | 138 * Returns the standard output stream of the process as a [:Stream:]. |
| 155 * | 139 * |
| 156 * Throws an [UnsupportedError] if the process is | 140 * Throws an [UnsupportedError] if the process is |
| 157 * non-interactive. | 141 * non-interactive. |
| 158 */ | 142 */ |
| 159 Stream<List<int>> get stdout; | 143 Stream<List<int>> get stdout; |
| 160 | 144 |
| 161 /** | 145 /** |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 String get stderr; | 210 String get stderr; |
| 227 | 211 |
| 228 /** | 212 /** |
| 229 * Process id from the process. | 213 * Process id from the process. |
| 230 */ | 214 */ |
| 231 int get pid; | 215 int get pid; |
| 232 } | 216 } |
| 233 | 217 |
| 234 | 218 |
| 235 /** | 219 /** |
| 236 * [ProcessOptions] represents the options that can be supplied when | |
| 237 * starting a process. | |
| 238 */ | |
| 239 class ProcessOptions { | |
| 240 /** | |
| 241 * The working directory from which the process is started. Note | |
| 242 * that the change of directory occurs before executing the process | |
| 243 * on some platforms, which may have impact when using relative | |
| 244 * paths for the executable and the arguments. | |
| 245 */ | |
| 246 String workingDirectory; | |
| 247 | |
| 248 /** | |
| 249 * The encoding used for text on stdout when starting a | |
| 250 * non-interactive process with [:Process.run:]. | |
| 251 * | |
| 252 * This option is ignored for interactive processes started with | |
| 253 * [:Process.start:]. | |
| 254 * | |
| 255 * The default stdoutEncoding is SYSTEM. | |
| 256 */ | |
| 257 Encoding stdoutEncoding; | |
| 258 | |
| 259 /** | |
| 260 * The encoding used for text on stderr when starting a | |
| 261 * non-interactive process with [:Process.run:]. | |
| 262 * | |
| 263 * This option is ignored for interactive processes started with | |
| 264 * [:Process.start:]. | |
| 265 * | |
| 266 * The default stderrEncoding is SYSTEM. | |
| 267 */ | |
| 268 Encoding stderrEncoding; | |
| 269 | |
| 270 /** | |
| 271 * Provides the environment variables for the process. If not set | |
| 272 * the environment of the parent process is inherited. | |
| 273 * | |
| 274 * Currently, only ASCII environment variables are supported and | |
| 275 * errors are likely to occur if an environment variables with | |
| 276 * code-points outside the ASCII range is passed in. | |
| 277 */ | |
| 278 Map<String, String> environment; | |
| 279 } | |
| 280 | |
| 281 /** | |
| 282 * On Posix systems, [ProcessSignal] is used to send a specific signal | 220 * On Posix systems, [ProcessSignal] is used to send a specific signal |
| 283 * to a child process, see [:Process.kill:]. | 221 * to a child process, see [:Process.kill:]. |
| 284 */ | 222 */ |
| 285 class ProcessSignal { | 223 class ProcessSignal { |
| 286 static const ProcessSignal SIGHUP = const ProcessSignal._signal(1); | 224 static const ProcessSignal SIGHUP = const ProcessSignal._signal(1); |
| 287 static const ProcessSignal SIGINT = const ProcessSignal._signal(2); | 225 static const ProcessSignal SIGINT = const ProcessSignal._signal(2); |
| 288 static const ProcessSignal SIGQUIT = const ProcessSignal._signal(3); | 226 static const ProcessSignal SIGQUIT = const ProcessSignal._signal(3); |
| 289 static const ProcessSignal SIGILL = const ProcessSignal._signal(4); | 227 static const ProcessSignal SIGILL = const ProcessSignal._signal(4); |
| 290 static const ProcessSignal SIGTRAP = const ProcessSignal._signal(5); | 228 static const ProcessSignal SIGTRAP = const ProcessSignal._signal(5); |
| 291 static const ProcessSignal SIGABRT = const ProcessSignal._signal(6); | 229 static const ProcessSignal SIGABRT = const ProcessSignal._signal(6); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 /** | 279 /** |
| 342 * Contains the system message for the process exception if any. | 280 * Contains the system message for the process exception if any. |
| 343 */ | 281 */ |
| 344 final String message; | 282 final String message; |
| 345 | 283 |
| 346 /** | 284 /** |
| 347 * Contains the OS error code for the process exception if any. | 285 * Contains the OS error code for the process exception if any. |
| 348 */ | 286 */ |
| 349 final int errorCode; | 287 final int errorCode; |
| 350 } | 288 } |
| OLD | NEW |