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