| 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 /// Helper functionality to make working with IO easier. | 5 /// Helper functionality to make working with IO easier. |
| 6 import 'dart:async'; | 6 import 'dart:async'; |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 import 'dart:io'; | 9 import 'dart:io'; |
| 10 | 10 |
| (...skipping 663 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 674 | 674 |
| 675 /// Spawns and runs the process located at [executable], passing in [args]. | 675 /// Spawns and runs the process located at [executable], passing in [args]. |
| 676 /// | 676 /// |
| 677 /// Returns a [Future] that will complete with the results of the process after | 677 /// Returns a [Future] that will complete with the results of the process after |
| 678 /// it has ended. | 678 /// it has ended. |
| 679 /// | 679 /// |
| 680 /// The spawned process will inherit its parent's environment variables. If | 680 /// The spawned process will inherit its parent's environment variables. If |
| 681 /// [environment] is provided, that will be used to augment (not replace) the | 681 /// [environment] is provided, that will be used to augment (not replace) the |
| 682 /// the inherited variables. | 682 /// the inherited variables. |
| 683 Future<PubProcessResult> runProcess(String executable, List<String> args, | 683 Future<PubProcessResult> runProcess(String executable, List<String> args, |
| 684 {workingDir, Map<String, String> environment}) { | 684 {workingDir, Map<String, String> environment, bool runInShell: false}) { |
| 685 return _descriptorPool.withResource(() { | 685 return _descriptorPool.withResource(() async { |
| 686 return _doProcess(Process.run, executable, args, workingDir, environment) | 686 var result = await _doProcess(Process.run, executable, args, |
| 687 .then((result) { | 687 workingDir: workingDir, |
| 688 var pubResult = new PubProcessResult( | 688 environment: environment, |
| 689 result.stdout, result.stderr, result.exitCode); | 689 runInShell: runInShell); |
| 690 log.processResult(executable, pubResult); | 690 |
| 691 return pubResult; | 691 var pubResult = new PubProcessResult( |
| 692 }); | 692 result.stdout, result.stderr, result.exitCode); |
| 693 log.processResult(executable, pubResult); |
| 694 return pubResult; |
| 693 }); | 695 }); |
| 694 } | 696 } |
| 695 | 697 |
| 696 /// Spawns the process located at [executable], passing in [args]. | 698 /// Spawns the process located at [executable], passing in [args]. |
| 697 /// | 699 /// |
| 698 /// Returns a [Future] that will complete with the [Process] once it's been | 700 /// Returns a [Future] that will complete with the [Process] once it's been |
| 699 /// started. | 701 /// started. |
| 700 /// | 702 /// |
| 701 /// The spawned process will inherit its parent's environment variables. If | 703 /// The spawned process will inherit its parent's environment variables. If |
| 702 /// [environment] is provided, that will be used to augment (not replace) the | 704 /// [environment] is provided, that will be used to augment (not replace) the |
| 703 /// the inherited variables. | 705 /// the inherited variables. |
| 704 Future<PubProcess> startProcess(String executable, List<String> args, | 706 Future<PubProcess> startProcess(String executable, List<String> args, |
| 705 {workingDir, Map<String, String> environment}) { | 707 {workingDir, Map<String, String> environment, bool runInShell: false}) { |
| 706 return _descriptorPool.request().then((resource) { | 708 return _descriptorPool.request().then((resource) async { |
| 707 return _doProcess(Process.start, executable, args, workingDir, environment) | 709 var ioProcess = await _doProcess(Process.start, executable, args, |
| 708 .then((ioProcess) { | 710 workingDir: workingDir, |
| 709 var process = new PubProcess(ioProcess); | 711 environment: environment, |
| 710 process.exitCode.whenComplete(resource.release); | 712 runInShell: runInShell); |
| 711 return process; | 713 |
| 712 }); | 714 var process = new PubProcess(ioProcess); |
| 715 process.exitCode.whenComplete(resource.release); |
| 716 return process; |
| 713 }); | 717 }); |
| 714 } | 718 } |
| 715 | 719 |
| 716 /// Like [runProcess], but synchronous. | 720 /// Like [runProcess], but synchronous. |
| 717 PubProcessResult runProcessSync(String executable, List<String> args, | 721 PubProcessResult runProcessSync(String executable, List<String> args, |
| 718 {String workingDir, Map<String, String> environment}) { | 722 {String workingDir, Map<String, String> environment, |
| 723 bool runInShell: false}) { |
| 719 var result = _doProcess( | 724 var result = _doProcess( |
| 720 Process.runSync, executable, args, workingDir, environment); | 725 Process.runSync, executable, args, |
| 726 workingDir: workingDir, |
| 727 environment: environment, |
| 728 runInShell: runInShell); |
| 721 var pubResult = new PubProcessResult( | 729 var pubResult = new PubProcessResult( |
| 722 result.stdout, result.stderr, result.exitCode); | 730 result.stdout, result.stderr, result.exitCode); |
| 723 log.processResult(executable, pubResult); | 731 log.processResult(executable, pubResult); |
| 724 return pubResult; | 732 return pubResult; |
| 725 } | 733 } |
| 726 | 734 |
| 727 /// A wrapper around [Process] that exposes `dart:async`-style APIs. | 735 /// A wrapper around [Process] that exposes `dart:async`-style APIs. |
| 728 class PubProcess { | 736 class PubProcess { |
| 729 /// The underlying `dart:io` [Process]. | 737 /// The underlying `dart:io` [Process]. |
| 730 final Process _process; | 738 final Process _process; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 806 /// Sends [signal] to the underlying process. | 814 /// Sends [signal] to the underlying process. |
| 807 bool kill([ProcessSignal signal = ProcessSignal.SIGTERM]) => | 815 bool kill([ProcessSignal signal = ProcessSignal.SIGTERM]) => |
| 808 _process.kill(signal); | 816 _process.kill(signal); |
| 809 } | 817 } |
| 810 | 818 |
| 811 /// Calls [fn] with appropriately modified arguments. | 819 /// Calls [fn] with appropriately modified arguments. |
| 812 /// | 820 /// |
| 813 /// [fn] should have the same signature as [Process.start], except that the | 821 /// [fn] should have the same signature as [Process.start], except that the |
| 814 /// returned value may have any return type. | 822 /// returned value may have any return type. |
| 815 _doProcess(Function fn, String executable, List<String> args, | 823 _doProcess(Function fn, String executable, List<String> args, |
| 816 String workingDir, Map<String, String> environment) { | 824 {String workingDir, Map<String, String> environment, |
| 825 bool runInShell: false}) { |
| 817 // TODO(rnystrom): Should dart:io just handle this? | 826 // TODO(rnystrom): Should dart:io just handle this? |
| 818 // Spawning a process on Windows will not look for the executable in the | 827 // Spawning a process on Windows will not look for the executable in the |
| 819 // system path. So, if executable looks like it needs that (i.e. it doesn't | 828 // system path. So, if executable looks like it needs that (i.e. it doesn't |
| 820 // have any path separators in it), then spawn it through a shell. | 829 // have any path separators in it), then spawn it through a shell. |
| 821 if ((Platform.operatingSystem == "windows") && | 830 if ((Platform.operatingSystem == "windows") && |
| 822 (executable.indexOf('\\') == -1)) { | 831 (executable.indexOf('\\') == -1)) { |
| 823 args = flatten(["/c", executable, args]); | 832 args = flatten(["/c", executable, args]); |
| 824 executable = "cmd"; | 833 executable = "cmd"; |
| 825 } | 834 } |
| 826 | 835 |
| 827 log.process(executable, args, workingDir == null ? '.' : workingDir); | 836 log.process(executable, args, workingDir == null ? '.' : workingDir); |
| 828 | 837 |
| 829 return fn(executable, args, | 838 return fn(executable, args, |
| 830 workingDirectory: workingDir, | 839 workingDirectory: workingDir, |
| 831 environment: environment); | 840 environment: environment, |
| 841 runInShell: runInShell); |
| 832 } | 842 } |
| 833 | 843 |
| 834 /// Updates [path]'s modification time. | 844 /// Updates [path]'s modification time. |
| 835 void touch(String path) { | 845 void touch(String path) { |
| 836 var file = new File(path).openSync(mode: FileMode.APPEND); | 846 var file = new File(path).openSync(mode: FileMode.APPEND); |
| 837 var originalLength = file.lengthSync(); | 847 var originalLength = file.lengthSync(); |
| 838 file.writeByteSync(0); | 848 file.writeByteSync(0); |
| 839 file.truncateSync(originalLength); | 849 file.truncateSync(originalLength); |
| 840 file.closeSync(); | 850 file.closeSync(); |
| 841 } | 851 } |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1066 | 1076 |
| 1067 // TODO(rnystrom): Remove this and change to returning one string. | 1077 // TODO(rnystrom): Remove this and change to returning one string. |
| 1068 static List<String> _toLines(String output) { | 1078 static List<String> _toLines(String output) { |
| 1069 var lines = splitLines(output); | 1079 var lines = splitLines(output); |
| 1070 if (!lines.isEmpty && lines.last == "") lines.removeLast(); | 1080 if (!lines.isEmpty && lines.last == "") lines.removeLast(); |
| 1071 return lines; | 1081 return lines; |
| 1072 } | 1082 } |
| 1073 | 1083 |
| 1074 bool get success => exitCode == exit_codes.SUCCESS; | 1084 bool get success => exitCode == exit_codes.SUCCESS; |
| 1075 } | 1085 } |
| OLD | NEW |