| @@ -0,0 +1,25 | |||
|
|
1 | package org.implab.gradle.common.exec; | |
|
|
2 | ||
|
|
3 | import java.util.concurrent.CompletableFuture; | |
|
|
4 | ||
|
|
5 | import java.io.File; | |
|
|
6 | import java.io.IOException; | |
|
|
7 | ||
|
|
8 | /** Interface for a command line execution. */ | |
|
|
9 | public interface Executor { | |
|
|
10 | void argument(String arg); | |
|
|
11 | ||
|
|
12 | void directory(File directory); | |
|
|
13 | ||
|
|
14 | void stdin(RedirectFrom from); | |
|
|
15 | ||
|
|
16 | void stdout(RedirectTo out); | |
|
|
17 | ||
|
|
18 | void stderr(RedirectTo err); | |
|
|
19 | ||
|
|
20 | CompletableFuture<Integer> start() throws IOException; | |
|
|
21 | ||
|
|
22 | public static Executor system() { | |
|
|
23 | return new SystemExec(); | |
|
|
24 | } | |
|
|
25 | } | |
| @@ -0,0 +1,76 | |||
|
|
1 | package org.implab.gradle.common.exec; | |
|
|
2 | ||
|
|
3 | import java.io.File; | |
|
|
4 | import java.io.IOException; | |
|
|
5 | import java.lang.ProcessBuilder.Redirect; | |
|
|
6 | import java.util.ArrayList; | |
|
|
7 | import java.util.concurrent.CompletableFuture; | |
|
|
8 | ||
|
|
9 | class SystemExec implements Executor { | |
|
|
10 | ||
|
|
11 | private final ProcessBuilder builder = new ProcessBuilder(); | |
|
|
12 | ||
|
|
13 | private RedirectFrom inputRedirect; | |
|
|
14 | ||
|
|
15 | private RedirectTo outputRedirect; | |
|
|
16 | ||
|
|
17 | private RedirectTo errorRedirect; | |
|
|
18 | ||
|
|
19 | @Override | |
|
|
20 | public CompletableFuture<Integer> start() throws IOException { | |
|
|
21 | // TODO Auto-generated method stub | |
|
|
22 | var tasks = new ArrayList<CompletableFuture<?>>(); | |
|
|
23 | ||
|
|
24 | // discard stdout if not redirected | |
|
|
25 | if (outputRedirect == null) | |
|
|
26 | builder.redirectOutput(Redirect.DISCARD); | |
|
|
27 | ||
|
|
28 | // discard stderr if not redirected | |
|
|
29 | if (errorRedirect == null) | |
|
|
30 | builder.redirectError(Redirect.DISCARD); | |
|
|
31 | ||
|
|
32 | // run process | |
|
|
33 | var proc = builder.start(); | |
|
|
34 | ||
|
|
35 | tasks.add(proc.onExit()); | |
|
|
36 | ||
|
|
37 | if (inputRedirect != null) | |
|
|
38 | tasks.add(inputRedirect.redirect(proc.getOutputStream())); | |
|
|
39 | ||
|
|
40 | if (outputRedirect != null) | |
|
|
41 | tasks.add(outputRedirect.redirect(proc.getInputStream())); | |
|
|
42 | ||
|
|
43 | if (errorRedirect != null) | |
|
|
44 | tasks.add(errorRedirect.redirect(proc.getErrorStream())); | |
|
|
45 | ||
|
|
46 | return CompletableFuture | |
|
|
47 | .allOf(tasks.toArray(new CompletableFuture<?>[0])) | |
|
|
48 | .thenApply(t -> proc.exitValue()); | |
|
|
49 | } | |
|
|
50 | ||
|
|
51 | public void command(String... args) { | |
|
|
52 | builder.command(args); | |
|
|
53 | } | |
|
|
54 | ||
|
|
55 | public void directory(File workingDir) { | |
|
|
56 | builder.directory(workingDir); | |
|
|
57 | } | |
|
|
58 | ||
|
|
59 | public void stderr(RedirectTo to) { | |
|
|
60 | errorRedirect = to; | |
|
|
61 | } | |
|
|
62 | ||
|
|
63 | public void stdin(RedirectFrom from) { | |
|
|
64 | inputRedirect = from; | |
|
|
65 | } | |
|
|
66 | ||
|
|
67 | public void stdout(RedirectTo to) { | |
|
|
68 | outputRedirect = to; | |
|
|
69 | } | |
|
|
70 | ||
|
|
71 | @Override | |
|
|
72 | public void argument(String arg) { | |
|
|
73 | builder.command().add(arg); | |
|
|
74 | } | |
|
|
75 | ||
|
|
76 | } | |
General Comments 0
You need to be logged in to leave comments.
Login now
