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 * 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 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 313 * Given [entry] which may be a [String], [File], or [Directory] relative to | 313 * Given [entry] which may be a [String], [File], or [Directory] relative to |
| 314 * the current working directory, returns its full canonicalized path. | 314 * the current working directory, returns its full canonicalized path. |
| 315 */ | 315 */ |
| 316 // TODO(rnystrom): Should this be async? | 316 // TODO(rnystrom): Should this be async? |
| 317 String getFullPath(entry) => new File(_getPath(entry)).fullPathSync(); | 317 String getFullPath(entry) => new File(_getPath(entry)).fullPathSync(); |
| 318 | 318 |
| 319 /** | 319 /** |
| 320 * Spawns and runs the process located at [executable], passing in [args]. | 320 * Spawns and runs the process located at [executable], passing in [args]. |
| 321 * Returns a [Future] that will complete the results of the process after it | 321 * Returns a [Future] that will complete the results of the process after it |
| 322 * has ended. | 322 * has ended. |
| 323 * | |
| 324 * If [pipeStdout] and/or [pipeStderr] are set, all output from the subprocess's | |
| 325 * output stream(s) to the parent process's output stream(s). Output from piped | |
|
Bob Nystrom
2012/05/08 17:01:49
"to the" -> "are sent to the".
Also "(s)" -> "s".
nweiz
2012/05/09 00:21:04
Done.
| |
| 326 * streams won't be available in the result object. | |
| 323 */ | 327 */ |
| 324 Future<ProcessResult> runProcess(String executable, List<String> args, | 328 Future<ProcessResult> runProcess(String executable, List<String> args, |
| 325 [String workingDir]) { | 329 [String workingDir, bool pipeStdout = false, bool pipeStderr = false]) { |
| 326 int exitCode; | 330 int exitCode; |
| 327 | 331 |
| 328 final options = new ProcessOptions(); | 332 final options = new ProcessOptions(); |
| 329 if (workingDir != null) options.workingDirectory = workingDir; | 333 if (workingDir != null) options.workingDirectory = workingDir; |
| 330 | 334 |
| 331 final process = new Process.start(executable, args, options); | 335 final process = new Process.start(executable, args, options); |
| 332 | 336 |
| 333 final outStream = new StringInputStream(process.stdout); | 337 final outStream = new StringInputStream(process.stdout); |
| 334 final processStdout = <String>[]; | 338 final processStdout = <String>[]; |
| 335 | 339 |
| 336 final errStream = new StringInputStream(process.stderr); | 340 final errStream = new StringInputStream(process.stderr); |
| 337 final processStderr = <String>[]; | 341 final processStderr = <String>[]; |
| 338 | 342 |
| 339 final completer = new Completer<ProcessResult>(); | 343 final completer = new Completer<ProcessResult>(); |
| 340 | 344 |
| 341 checkComplete() { | 345 checkComplete() { |
| 342 // Wait until the process is done and its output streams are closed. | 346 // Wait until the process is done and its output streams are closed. |
| 343 if (!outStream.closed) return; | 347 if (!outStream.closed) return; |
| 344 if (!errStream.closed) return; | 348 if (!errStream.closed) return; |
| 345 if (exitCode == null) return; | 349 if (exitCode == null) return; |
| 346 | 350 |
| 347 completer.complete(new ProcessResult( | 351 completer.complete(new ProcessResult( |
| 348 processStdout, processStderr, exitCode)); | 352 processStdout, processStderr, exitCode)); |
| 349 } | 353 } |
| 350 | 354 |
| 351 outStream.onLine = () => processStdout.add(outStream.readLine()); | 355 if (pipeStdout) { |
| 352 outStream.onClosed = checkComplete; | 356 process.stdout.pipe(stdout, close: false); |
| 353 outStream.onError = (error) => completer.completeException(error); | 357 } else { |
| 358 outStream.onLine = () => processStdout.add(outStream.readLine()); | |
| 359 outStream.onClosed = checkComplete; | |
| 360 outStream.onError = (error) => completer.completeException(error); | |
| 361 } | |
| 354 | 362 |
| 355 errStream.onLine = () => processStderr.add(errStream.readLine()); | 363 if (pipeStderr) { |
| 356 errStream.onClosed = checkComplete; | 364 process.stderr.pipe(stderr, close: false); |
| 357 errStream.onError = (error) => completer.completeException(error); | 365 } else { |
| 366 errStream.onLine = () => processStderr.add(errStream.readLine()); | |
| 367 errStream.onClosed = checkComplete; | |
| 368 errStream.onError = (error) => completer.completeException(error); | |
| 369 } | |
| 358 | 370 |
| 359 process.onExit = (actualExitCode) { | 371 process.onExit = (actualExitCode) { |
| 360 exitCode = actualExitCode; | 372 exitCode = actualExitCode; |
| 361 checkComplete(); | 373 checkComplete(); |
| 362 }; | 374 }; |
| 363 | 375 |
| 364 process.onError = (error) => completer.completeException(error); | 376 process.onError = (error) => completer.completeException(error); |
| 365 | 377 |
| 366 return completer.future; | 378 return completer.future; |
| 367 } | 379 } |
| 368 | 380 |
| 369 /** | 381 /** |
| 370 * Contains the results of invoking a [Process] and waiting for it to complete. | 382 * Contains the results of invoking a [Process] and waiting for it to complete. |
| 371 */ | 383 */ |
| 372 class ProcessResult { | 384 class ProcessResult { |
| 373 final List<String> stdout; | 385 final List<String> stdout; |
| 374 final List<String> stderr; | 386 final List<String> stderr; |
| 375 final int exitCode; | 387 final int exitCode; |
| 376 | 388 |
| 377 const ProcessResult(this.stdout, this.stderr, this.exitCode); | 389 const ProcessResult(this.stdout, this.stderr, this.exitCode); |
| 390 | |
| 391 bool get success() => exitCode == 0; | |
| 378 } | 392 } |
| 379 | 393 |
| 380 /** | 394 /** |
| 381 * Gets the path string for [entry], which can either already be a path string, | 395 * Gets the path string for [entry], which can either already be a path string, |
| 382 * or be a [File] or [Directory]. Allows working generically with "file-like" | 396 * or be a [File] or [Directory]. Allows working generically with "file-like" |
| 383 * objects. | 397 * objects. |
| 384 */ | 398 */ |
| 385 String _getPath(entry) { | 399 String _getPath(entry) { |
| 386 if (entry is String) return entry; | 400 if (entry is String) return entry; |
| 387 if (entry is File) return entry.name; | 401 if (entry is File) return entry.name; |
| 388 if (entry is Directory) return entry.path; | 402 if (entry is Directory) return entry.path; |
| 389 throw 'Entry $entry is not a supported type.'; | 403 throw 'Entry $entry is not a supported type.'; |
| 390 } | 404 } |
| 391 | 405 |
| 392 /** | 406 /** |
| 393 * Gets a [Directory] for [entry], which can either already be one, or be a | 407 * Gets a [Directory] for [entry], which can either already be one, or be a |
| 394 * [String]. | 408 * [String]. |
| 395 */ | 409 */ |
| 396 Directory _getDirectory(entry) { | 410 Directory _getDirectory(entry) { |
| 397 if (entry is Directory) return entry; | 411 if (entry is Directory) return entry; |
| 398 return new Directory(entry); | 412 return new Directory(entry); |
| 399 } | 413 } |
| OLD | NEW |