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 io; | 8 library io; |
| 9 | 9 |
| 10 import 'dart:io'; | 10 import 'dart:io'; |
| (...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 464 */ | 464 */ |
| 465 Future<List<int>> consumeInputStream(InputStream stream) { | 465 Future<List<int>> consumeInputStream(InputStream stream) { |
| 466 var completer = new Completer<List<int>>(); | 466 var completer = new Completer<List<int>>(); |
| 467 var buffer = <int>[]; | 467 var buffer = <int>[]; |
| 468 stream.onClosed = () => completer.complete(buffer); | 468 stream.onClosed = () => completer.complete(buffer); |
| 469 stream.onData = () => buffer.addAll(stream.read()); | 469 stream.onData = () => buffer.addAll(stream.read()); |
| 470 stream.onError = (e) => completer.completeException(e); | 470 stream.onError = (e) => completer.completeException(e); |
| 471 return completer.future; | 471 return completer.future; |
| 472 } | 472 } |
| 473 | 473 |
| 474 /** | 474 /// Spawns and runs the process located at [executable], passing in [args]. |
| 475 * Spawns and runs the process located at [executable], passing in [args]. | 475 /// Returns a [Future] that will complete the results of the process after it |
| 476 * Returns a [Future] that will complete the results of the process after it | 476 /// has ended. |
| 477 * has ended. | 477 /// |
| 478 * | 478 /// The spawned process will inherit its parents environment variables. If |
|
nweiz
2012/10/30 23:29:59
"parents" -> "parent's"
Bob Nystrom
2012/10/30 23:31:42
Done.
| |
| 479 * If [pipeStdout] and/or [pipeStderr] are set, all output from the subprocess's | 479 /// [environment] is provided, that will be used to augment (not replace) the |
| 480 * output streams are sent to the parent process's output streams. Output from | 480 /// the inherited variables. |
| 481 * piped streams won't be available in the result object. | 481 /// |
| 482 */ | 482 /// If [pipeStdout] and/or [pipeStderr] are set, all output from the |
| 483 /// subprocess's output streams are sent to the parent process's output streams. | |
| 484 /// Output from piped streams won't be available in the result object. | |
| 483 Future<PubProcessResult> runProcess(String executable, List<String> args, | 485 Future<PubProcessResult> runProcess(String executable, List<String> args, |
| 484 {workingDir, Map<String, String> environment, bool pipeStdout: false, | 486 {workingDir, Map<String, String> environment, bool pipeStdout: false, |
| 485 bool pipeStderr: false}) { | 487 bool pipeStderr: false}) { |
| 486 int exitCode; | 488 int exitCode; |
| 487 | 489 |
| 488 // TODO(rnystrom): Should dart:io just handle this? | 490 // TODO(rnystrom): Should dart:io just handle this? |
| 489 // Spawning a process on Windows will not look for the executable in the | 491 // Spawning a process on Windows will not look for the executable in the |
| 490 // system path. So, if executable looks like it needs that (i.e. it doesn't | 492 // system path. So, if executable looks like it needs that (i.e. it doesn't |
| 491 // have any path separators in it), then spawn it through a shell. | 493 // have any path separators in it), then spawn it through a shell. |
| 492 if ((Platform.operatingSystem == "windows") && | 494 if ((Platform.operatingSystem == "windows") && |
| 493 (executable.indexOf('\\') == -1)) { | 495 (executable.indexOf('\\') == -1)) { |
| 494 args = flatten(["/c", executable, args]); | 496 args = flatten(["/c", executable, args]); |
| 495 executable = "cmd"; | 497 executable = "cmd"; |
| 496 } | 498 } |
| 497 | 499 |
| 498 final options = new ProcessOptions(); | 500 final options = new ProcessOptions(); |
| 499 if (workingDir != null) { | 501 if (workingDir != null) { |
| 500 options.workingDirectory = _getDirectory(workingDir).path; | 502 options.workingDirectory = _getDirectory(workingDir).path; |
| 501 } | 503 } |
| 502 options.environment = environment; | 504 |
| 505 if (environment != null) { | |
| 506 options.environment = new Map.from(Platform.environment); | |
| 507 environment.forEach((key, value) => options.environment[key] = value); | |
| 508 } | |
| 503 | 509 |
| 504 var future = Process.run(executable, args, options); | 510 var future = Process.run(executable, args, options); |
| 505 return future.transform((result) { | 511 return future.transform((result) { |
| 506 // TODO(rnystrom): Remove this and change to returning one string. | 512 // TODO(rnystrom): Remove this and change to returning one string. |
| 507 List<String> toLines(String output) { | 513 List<String> toLines(String output) { |
| 508 var lines = output.split(NEWLINE_PATTERN); | 514 var lines = output.split(NEWLINE_PATTERN); |
| 509 if (!lines.isEmpty && lines.last == "") lines.removeLast(); | 515 if (!lines.isEmpty && lines.last == "") lines.removeLast(); |
| 510 return lines; | 516 return lines; |
| 511 } | 517 } |
| 512 return new PubProcessResult(toLines(result.stdout), | 518 return new PubProcessResult(toLines(result.stdout), |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 549 if (_isGitInstalledCache != null) { | 555 if (_isGitInstalledCache != null) { |
| 550 // TODO(rnystrom): The sleep is to pump the message queue. Can use | 556 // TODO(rnystrom): The sleep is to pump the message queue. Can use |
| 551 // Future.immediate() when #3356 is fixed. | 557 // Future.immediate() when #3356 is fixed. |
| 552 return sleep(0).transform((_) => _isGitInstalledCache); | 558 return sleep(0).transform((_) => _isGitInstalledCache); |
| 553 } | 559 } |
| 554 | 560 |
| 555 return _gitCommand.transform((git) => git != null); | 561 return _gitCommand.transform((git) => git != null); |
| 556 } | 562 } |
| 557 | 563 |
| 558 /// Run a git process with [args] from [workingDir]. | 564 /// Run a git process with [args] from [workingDir]. |
| 559 Future<PubProcessResult> runGit(List<String> args, {String workingDir}) => | 565 Future<PubProcessResult> runGit(List<String> args, |
| 560 _gitCommand.chain((git) => runProcess(git, args, workingDir: workingDir)); | 566 {String workingDir, Map<String, String> environment}) { |
| 567 return _gitCommand.chain((git) => runProcess(git, args, | |
| 568 workingDir: workingDir, environment: environment)); | |
| 569 } | |
| 561 | 570 |
| 562 /// Returns the name of the git command-line app, or null if Git could not be | 571 /// Returns the name of the git command-line app, or null if Git could not be |
| 563 /// found on the user's PATH. | 572 /// found on the user's PATH. |
| 564 Future<String> get _gitCommand { | 573 Future<String> get _gitCommand { |
| 565 // TODO(nweiz): Just use Future.immediate once issue 3356 is fixed. | 574 // TODO(nweiz): Just use Future.immediate once issue 3356 is fixed. |
| 566 if (_gitCommandCache != null) { | 575 if (_gitCommandCache != null) { |
| 567 return sleep(0).transform((_) => _gitCommandCache); | 576 return sleep(0).transform((_) => _gitCommandCache); |
| 568 } | 577 } |
| 569 | 578 |
| 570 return _tryGitCommand("git").chain((success) { | 579 return _tryGitCommand("git").chain((success) { |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 748 return new Directory(entry); | 757 return new Directory(entry); |
| 749 } | 758 } |
| 750 | 759 |
| 751 /** | 760 /** |
| 752 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. | 761 * Gets a [Uri] for [uri], which can either already be one, or be a [String]. |
| 753 */ | 762 */ |
| 754 Uri _getUri(uri) { | 763 Uri _getUri(uri) { |
| 755 if (uri is Uri) return uri; | 764 if (uri is Uri) return uri; |
| 756 return new Uri.fromString(uri); | 765 return new Uri.fromString(uri); |
| 757 } | 766 } |
| OLD | NEW |