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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 } | 78 } |
79 _ProcessUtils._sleep(milliseconds); | 79 _ProcessUtils._sleep(milliseconds); |
80 } | 80 } |
81 | 81 |
82 /** | 82 /** |
83 * Returns the PID of the current process. | 83 * Returns the PID of the current process. |
84 */ | 84 */ |
85 int get pid => _ProcessUtils._pid(null); | 85 int get pid => _ProcessUtils._pid(null); |
86 | 86 |
87 /** | 87 /** |
88 * [Process] is used to start new processes using the static | 88 * The means to execute a program. |
89 * [start] and [run] methods. | 89 * |
90 * Use the static [start] and [run] methods to start a new process. | |
91 * The run method executes the process non-interactively to completion. | |
92 * In contrast, the start method allows your code to interact with the process. | |
Søren Gjesse
2014/02/12 13:16:35
the process -> the running process
mem
2014/02/12 19:17:07
Done.
| |
93 * | |
94 * ## Start a process with the run method | |
95 * | |
96 * The following code sample uses the run method to create a process | |
97 * that runs the UNIX command `ls`, which lists the contents of a directory. | |
98 * The code gets the output from the standard output stream by way of a | |
Søren Gjesse
2014/02/12 13:16:35
Instead of the sentence "The code gest ..." someth
mem
2014/02/12 19:17:07
Done.
| |
99 * [ProcessResult] object. | |
100 * The run method does not return a Process object; this prevents your | |
101 * code from interacting with the running process. | |
102 * | |
103 * import 'dart:io'; | |
104 * | |
105 * main() { | |
106 * // List all files in the current directory in UNIX-like systems. | |
107 * Process.run('ls', ['-l']).then((ProcessResult results) { | |
108 * print(results.stdout); | |
109 * }); | |
110 * } | |
111 * | |
112 * ## Start a process with the start method | |
113 * | |
114 * The following example uses start to create the process. | |
115 * The start method returns a [Future], which returns a Process object. | |
Søren Gjesse
2014/02/12 13:16:35
which returns -> for
mem
2014/02/12 19:17:07
Done.
| |
116 * Within the [Future]'s callback function, your code can interact with the | |
Søren Gjesse
2014/02/12 13:16:35
Within the [Future]'s callback function -> When th
mem
2014/02/12 19:17:07
Done.
| |
117 * Process: writing to stdin, listening to stdout, and so on. | |
118 * | |
119 * The following sample starts the UNIX `cat` utility, which when given no | |
120 * command-line arguments, echos its input. | |
121 * The program writes to the process's standard input stream | |
122 * and prints data from its standard output stream. | |
123 * | |
124 * import 'dart:io'; | |
125 * import 'dart:convert'; | |
126 * | |
127 * main() { | |
128 * Process.start('cat', []).then((Process proc) { | |
Søren Gjesse
2014/02/12 13:16:35
Why not spell it out: proc -> process
mem
2014/02/12 19:17:07
Done.
| |
129 * proc.stdout | |
130 * .transform(UTF8.decoder) | |
131 * .listen((data) { print(data); }); | |
132 * proc.stdin.writeln('Hello, world!'); | |
133 * proc.stdin.writeln('Hello, galaxy!'); | |
134 * proc.stdin.writeln('Hello, universe!'); | |
135 * }); | |
136 * } | |
137 * | |
138 * ## Exit codes | |
139 * | |
140 * Call the [exitCode] method to get the exit code of the process. | |
141 * The exit code indicates whether the program terminated successfully | |
142 * (usually indicated with an exit code of 0) or with an error. | |
143 * `exitCode` returns the code via a [Future] object. | |
Søren Gjesse
2014/02/12 13:16:35
`exitCode` returns the code via a [Future] object.
mem
2014/02/12 19:17:07
Done.
| |
144 * | |
145 * import 'dart:io'; | |
146 * | |
147 * main() { | |
148 * Process.start('ls', ['-l']).then((process) { | |
149 * // Get the exit code from the new process. | |
150 * process.exitCode.then((exitCode) { | |
151 * print('exit code: $exitCode'); | |
152 * }); | |
153 * }); | |
154 * } | |
155 * | |
156 * ## Standard streams | |
Søren Gjesse
2014/02/12 13:16:35
Standard streams -> Standard IO streams
mem
2014/02/12 19:17:07
Done.
| |
157 * | |
158 * As seen in a previous code sample, you can use the getters [stderr], | |
Søren Gjesse
2014/02/12 13:16:35
the getters -> the top-level getters
mem
2014/02/12 19:17:07
Done.
| |
159 * [stdin], and [stdout] to get the process's standard error, standard input, | |
160 * and standard output streams, respectively. | |
Søren Gjesse
2014/02/12 13:16:35
I don't see this in a previous example, maybe add
mem
2014/02/12 19:17:07
On 2014/02/12 13:16:35, Søren Gjesse wrote:
I was
Søren Gjesse
2014/02/13 16:03:55
It was just that there was the exit code example i
| |
161 * | |
162 * ## Other resources | |
163 * | |
164 * [Dart by Example](https://www.dartlang.org/dart-by-example/#dart-io-and-comma nd-line-apps) | |
165 * provides additional task-oriented code samples that show how to use | |
166 * various API from the [dart:io] library. | |
90 */ | 167 */ |
91 abstract class Process { | 168 abstract class Process { |
92 /** | 169 /** |
93 * Returns a [:Future:] which completes with the exit code of the process | 170 * Returns a [:Future:] which completes with the exit code of the process |
94 * when the process completes. | 171 * when the process completes. |
95 * | 172 * |
96 * The handling of exit codes is platform specific. | 173 * The handling of exit codes is platform specific. |
97 * | 174 * |
98 * On Linux and Mac a normal exit code will be a positive value in | 175 * On Linux and Mac a normal exit code will be a positive value in |
99 * the range [0..255]. If the process was terminated due to a signal | 176 * the range [0..255]. If the process was terminated due to a signal |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
402 final int errorCode; | 479 final int errorCode; |
403 | 480 |
404 const ProcessException(this.executable, this.arguments, [this.message = "", | 481 const ProcessException(this.executable, this.arguments, [this.message = "", |
405 this.errorCode = 0]); | 482 this.errorCode = 0]); |
406 String toString() { | 483 String toString() { |
407 var msg = (message == null) ? 'OS error code: $errorCode' : message; | 484 var msg = (message == null) ? 'OS error code: $errorCode' : message; |
408 var args = arguments.join(' '); | 485 var args = arguments.join(' '); |
409 return "ProcessException: $msg\n Command: $executable $args"; | 486 return "ProcessException: $msg\n Command: $executable $args"; |
410 } | 487 } |
411 } | 488 } |
OLD | NEW |