| 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 * Helper functionality to make working with IO easier. | 6 * Helper functionality to make working with IO easier. |
| 7 */ | 7 */ |
| 8 #library('pub_io'); | 8 #library('pub_io'); |
| 9 | 9 |
| 10 #import('dart:io'); | 10 #import('dart:io'); |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 * Given [entry] which may be a [String], [File], or [Directory] relative to | 251 * Given [entry] which may be a [String], [File], or [Directory] relative to |
| 252 * the current working directory, returns its full canonicalized path. | 252 * the current working directory, returns its full canonicalized path. |
| 253 */ | 253 */ |
| 254 // TODO(rnystrom): Should this be async? | 254 // TODO(rnystrom): Should this be async? |
| 255 String getFullPath(entry) => new File(_getPath(entry)).fullPathSync(); | 255 String getFullPath(entry) => new File(_getPath(entry)).fullPathSync(); |
| 256 | 256 |
| 257 /** | 257 /** |
| 258 * Spawns and runs the process located at [executable], passing in [args]. | 258 * Spawns and runs the process located at [executable], passing in [args]. |
| 259 * Returns a [Future] that will complete the results of the process after it | 259 * Returns a [Future] that will complete the results of the process after it |
| 260 * has ended. | 260 * has ended. |
| 261 * |
| 262 * If [pipeStdout] and/or [pipeStderr] are set, all output from the subprocess's |
| 263 * output streams are sent to the parent process's output streams. Output from |
| 264 * piped streams won't be available in the result object. |
| 261 */ | 265 */ |
| 262 Future<PubProcessResult> runProcess(String executable, List<String> args, | 266 Future<PubProcessResult> runProcess(String executable, List<String> args, |
| 263 [String workingDir]) { | 267 [String workingDir, bool pipeStdout = false, bool pipeStderr = false]) { |
| 264 int exitCode; | 268 int exitCode; |
| 265 | 269 |
| 266 final options = new ProcessOptions(); | 270 final options = new ProcessOptions(); |
| 267 if (workingDir != null) options.workingDirectory = workingDir; | 271 if (workingDir != null) options.workingDirectory = workingDir; |
| 268 | 272 |
| 269 final process = Process.start(executable, args, options); | 273 final process = Process.start(executable, args, options); |
| 270 | 274 |
| 271 final outStream = new StringInputStream(process.stdout); | 275 final outStream = new StringInputStream(process.stdout); |
| 272 final processStdout = <String>[]; | 276 final processStdout = <String>[]; |
| 273 | 277 |
| 274 final errStream = new StringInputStream(process.stderr); | 278 final errStream = new StringInputStream(process.stderr); |
| 275 final processStderr = <String>[]; | 279 final processStderr = <String>[]; |
| 276 | 280 |
| 277 final completer = new Completer<PubProcessResult>(); | 281 final completer = new Completer<PubProcessResult>(); |
| 278 | 282 |
| 279 checkComplete() { | 283 checkComplete() { |
| 280 // Wait until the process is done and its output streams are closed. | 284 // Wait until the process is done and its output streams are closed. |
| 281 if (!outStream.closed) return; | 285 if (!pipeStdout && !outStream.closed) return; |
| 282 if (!errStream.closed) return; | 286 if (!pipeStderr && !errStream.closed) return; |
| 283 if (exitCode == null) return; | 287 if (exitCode == null) return; |
| 284 | 288 |
| 285 completer.complete(new PubProcessResult( | 289 completer.complete(new PubProcessResult( |
| 286 processStdout, processStderr, exitCode)); | 290 processStdout, processStderr, exitCode)); |
| 287 } | 291 } |
| 288 | 292 |
| 289 outStream.onLine = () => processStdout.add(outStream.readLine()); | 293 if (pipeStdout) { |
| 290 outStream.onClosed = checkComplete; | 294 process.stdout.pipe(stdout, close: false); |
| 291 outStream.onError = (error) => completer.completeException(error); | 295 } else { |
| 296 outStream.onLine = () => processStdout.add(outStream.readLine()); |
| 297 outStream.onClosed = checkComplete; |
| 298 outStream.onError = (error) => completer.completeException(error); |
| 299 } |
| 292 | 300 |
| 293 errStream.onLine = () => processStderr.add(errStream.readLine()); | 301 if (pipeStderr) { |
| 294 errStream.onClosed = checkComplete; | 302 process.stderr.pipe(stderr, close: false); |
| 295 errStream.onError = (error) => completer.completeException(error); | 303 } else { |
| 304 errStream.onLine = () => processStderr.add(errStream.readLine()); |
| 305 errStream.onClosed = checkComplete; |
| 306 errStream.onError = (error) => completer.completeException(error); |
| 307 } |
| 296 | 308 |
| 297 process.onExit = (actualExitCode) { | 309 process.onExit = (actualExitCode) { |
| 298 exitCode = actualExitCode; | 310 exitCode = actualExitCode; |
| 299 checkComplete(); | 311 checkComplete(); |
| 300 }; | 312 }; |
| 301 | 313 |
| 302 process.onError = (error) => completer.completeException(error); | 314 process.onError = (error) => completer.completeException(error); |
| 303 | 315 |
| 304 return completer.future; | 316 return completer.future; |
| 305 } | 317 } |
| 306 | 318 |
| 307 /** | 319 /** |
| 308 * Contains the results of invoking a [Process] and waiting for it to complete. | 320 * Contains the results of invoking a [Process] and waiting for it to complete. |
| 309 */ | 321 */ |
| 310 class PubProcessResult { | 322 class PubProcessResult { |
| 311 final List<String> stdout; | 323 final List<String> stdout; |
| 312 final List<String> stderr; | 324 final List<String> stderr; |
| 313 final int exitCode; | 325 final int exitCode; |
| 314 | 326 |
| 315 const PubProcessResult(this.stdout, this.stderr, this.exitCode); | 327 const PubProcessResult(this.stdout, this.stderr, this.exitCode); |
| 328 |
| 329 bool get success() => exitCode == 0; |
| 316 } | 330 } |
| 317 | 331 |
| 318 /** | 332 /** |
| 319 * Gets the path string for [entry], which can either already be a path string, | 333 * Gets the path string for [entry], which can either already be a path string, |
| 320 * or be a [File] or [Directory]. Allows working generically with "file-like" | 334 * or be a [File] or [Directory]. Allows working generically with "file-like" |
| 321 * objects. | 335 * objects. |
| 322 */ | 336 */ |
| 323 String _getPath(entry) { | 337 String _getPath(entry) { |
| 324 if (entry is String) return entry; | 338 if (entry is String) return entry; |
| 325 if (entry is File) return entry.name; | 339 if (entry is File) return entry.name; |
| 326 if (entry is Directory) return entry.path; | 340 if (entry is Directory) return entry.path; |
| 327 throw 'Entry $entry is not a supported type.'; | 341 throw 'Entry $entry is not a supported type.'; |
| 328 } | 342 } |
| 329 | 343 |
| 330 /** | 344 /** |
| 331 * Gets a [Directory] for [entry], which can either already be one, or be a | 345 * Gets a [Directory] for [entry], which can either already be one, or be a |
| 332 * [String]. | 346 * [String]. |
| 333 */ | 347 */ |
| 334 Directory _getDirectory(entry) { | 348 Directory _getDirectory(entry) { |
| 335 if (entry is Directory) return entry; | 349 if (entry is Directory) return entry; |
| 336 return new Directory(entry); | 350 return new Directory(entry); |
| 337 } | 351 } |
| OLD | NEW |