24
24
import java .nio .charset .StandardCharsets ;
25
25
import java .util .Arrays ;
26
26
import java .util .List ;
27
+ import java .util .Map ;
27
28
import java .util .concurrent .ExecutionException ;
28
29
import java .util .concurrent .ExecutorService ;
29
30
import java .util .concurrent .Executors ;
@@ -57,18 +58,18 @@ public Result shell(String cmd) throws IOException, InterruptedException {
57
58
58
59
/** Executes the given shell command (using {@code cmd} on windows and {@code sh} on unix). */
59
60
public Result shellWinUnix (String cmdWin , String cmdUnix ) throws IOException , InterruptedException {
60
- return shellWinUnix (null , cmdWin , cmdUnix );
61
+ return shellWinUnix (null , null , cmdWin , cmdUnix );
61
62
}
62
63
63
64
/** Executes the given shell command (using {@code cmd} on windows and {@code sh} on unix). */
64
- public Result shellWinUnix (File cwd , String cmdWin , String cmdUnix ) throws IOException , InterruptedException {
65
+ public Result shellWinUnix (File cwd , Map < String , String > environment , String cmdWin , String cmdUnix ) throws IOException , InterruptedException {
65
66
List <String > args ;
66
67
if (FileSignature .machineIsWin ()) {
67
68
args = Arrays .asList ("cmd" , "/c" , cmdWin );
68
69
} else {
69
70
args = Arrays .asList ("sh" , "-c" , cmdUnix );
70
71
}
71
- return exec (cwd , args );
72
+ return exec (cwd , environment , new byte [ 0 ], args );
72
73
}
73
74
74
75
/** Creates a process with the given arguments. */
@@ -83,25 +84,23 @@ public Result exec(byte[] stdin, String... args) throws IOException, Interrupted
83
84
84
85
/** Creates a process with the given arguments. */
85
86
public Result exec (List <String > args ) throws IOException , InterruptedException {
86
- return exec ((File ) null , args );
87
- }
88
-
89
- /** Creates a process with the given arguments. */
90
- public Result exec (File cwd , List <String > args ) throws IOException , InterruptedException {
91
- return exec (cwd , new byte [0 ], args );
87
+ return exec (null , args );
92
88
}
93
89
94
90
/** Creates a process with the given arguments, the given byte array is written to stdin immediately. */
95
91
public Result exec (byte [] stdin , List <String > args ) throws IOException , InterruptedException {
96
- return exec (null , stdin , args );
92
+ return exec (null , null , stdin , args );
97
93
}
98
94
99
95
/** Creates a process with the given arguments, the given byte array is written to stdin immediately. */
100
- public Result exec (File cwd , byte [] stdin , List <String > args ) throws IOException , InterruptedException {
96
+ public Result exec (File cwd , Map < String , String > environment , byte [] stdin , List <String > args ) throws IOException , InterruptedException {
101
97
ProcessBuilder builder = new ProcessBuilder (args );
102
98
if (cwd != null ) {
103
99
builder .directory (cwd );
104
100
}
101
+ if (environment != null ) {
102
+ builder .environment ().putAll (environment );
103
+ }
105
104
Process process = builder .start ();
106
105
Future <byte []> outputFut = threadStdOut .submit (() -> drainToBytes (process .getInputStream (), bufStdOut ));
107
106
Future <byte []> errorFut = threadStdErr .submit (() -> drainToBytes (process .getErrorStream (), bufStdErr ));
0 commit comments