| @@ -1,124 +1,147 | |||
|
|
1 | 1 | package org.implab.gradle.common.exec; |
|
|
2 | 2 | |
|
|
3 | 3 | import java.util.ArrayList; |
|
|
4 | 4 | import java.util.HashMap; |
|
|
5 | 5 | import java.util.List; |
|
|
6 | 6 | import java.util.Map; |
|
|
7 | 7 | import java.util.Objects; |
|
|
8 | 8 | import java.util.Optional; |
|
|
9 | 9 | import java.util.concurrent.CompletableFuture; |
|
|
10 | 10 | import java.util.stream.Stream; |
|
|
11 | import java.util.Collection; | |
|
|
11 | 12 | |
|
|
12 | 13 | import org.eclipse.jdt.annotation.NonNullByDefault; |
|
|
13 | 14 | |
|
|
14 | 15 | import java.io.File; |
|
|
15 | 16 | import java.io.IOException; |
|
|
16 | 17 | |
|
|
17 | 18 | /** Command line builder */ |
|
|
18 | 19 | @NonNullByDefault |
|
|
19 | 20 | public abstract class ExecBuilder { |
|
|
20 | 21 | |
|
|
21 | 22 | private final List<String> commandLine = new ArrayList<>(); |
|
|
22 | 23 | |
|
|
23 | 24 | private final Map<String, String> environment = new HashMap<>(); |
|
|
24 | 25 | |
|
|
25 | 26 | private File directory; |
|
|
26 | 27 | |
|
|
27 | 28 | private RedirectFrom inputRedirect; |
|
|
28 | 29 | |
|
|
29 | 30 | private RedirectTo outputRedirect; |
|
|
30 | 31 | |
|
|
31 | 32 | private RedirectTo errorRedirect; |
|
|
32 | 33 | |
|
|
33 | 34 | /** Sets command line, clears previous one if any */ |
|
|
34 |
public |
|
|
|
35 | public ExecBuilder command(String cmd, String... args) { | |
|
|
35 | 36 | commandLine.clear(); |
|
|
36 | 37 | commandLine.add(cmd); |
|
|
37 | 38 | Stream.of(args).forEach(commandLine::add); |
|
|
39 | return this; | |
|
|
40 | } | |
|
|
41 | ||
|
|
42 | public ExecBuilder command(Collection<String> cmd) { | |
|
|
43 | this.commandLine.clear(); | |
|
|
44 | this.commandLine.addAll(cmd); | |
|
|
45 | return this; | |
|
|
38 | 46 | } |
|
|
39 | 47 | |
|
|
40 | 48 | /** Adds an argument to the command line */ |
|
|
41 |
public |
|
|
|
49 | public ExecBuilder argument(String arg) { | |
|
|
42 | 50 | Objects.requireNonNull(arg, "arg parameter can't be null"); |
|
|
43 | 51 | commandLine.add(arg); |
|
|
52 | return this; | |
|
|
44 | 53 | } |
|
|
45 | 54 | |
|
|
46 | 55 | /** Sets the working directory */ |
|
|
47 |
public |
|
|
|
56 | public ExecBuilder directory(File directory) { | |
|
|
48 | 57 | Objects.requireNonNull(directory, "directory parameter can't be null"); |
|
|
49 | 58 | this.directory = directory; |
|
|
59 | return this; | |
|
|
50 | 60 | } |
|
|
51 | 61 | |
|
|
52 | 62 | /** |
|
|
53 | 63 | * Sets the environment value. The value cannot be null. |
|
|
54 | 64 | * |
|
|
55 | 65 | * @param envVar The name of the environment variable |
|
|
56 | 66 | * @param value The value to set, |
|
|
57 | 67 | */ |
|
|
58 |
public |
|
|
|
68 | public ExecBuilder setEnvironment(String envVar, String value) { | |
|
|
59 | 69 | Objects.requireNonNull(value, "Value can't be null"); |
|
|
60 | 70 | Objects.requireNonNull(envVar, "envVar parameter can't be null"); |
|
|
61 | 71 | environment.put(envVar, value); |
|
|
72 | return this; | |
|
|
62 | 73 | } |
|
|
63 | 74 | |
|
|
64 |
public |
|
|
|
75 | public ExecBuilder setEnvironment(Map<String, String> env) { | |
|
|
76 | Objects.requireNonNull(env, "env parameter can't be null"); | |
|
|
77 | ||
|
|
78 | environment.clear(); | |
|
|
79 | environment.putAll(env); | |
|
|
80 | return this; | |
|
|
81 | } | |
|
|
82 | ||
|
|
83 | public ExecBuilder unsetEnvironment(String envVar) { | |
|
|
65 | 84 | environment.remove(envVar); |
|
|
85 | return this; | |
|
|
66 | 86 | } |
|
|
67 | 87 | |
|
|
68 | 88 | /** |
|
|
69 | 89 | * Sets redirection for the stdin, {@link RedirectFrom} will be applied |
|
|
70 | 90 | * every time the process is started. |
|
|
71 | 91 | * |
|
|
72 | 92 | * <p> |
|
|
73 | 93 | * If redirection |
|
|
74 | 94 | */ |
|
|
75 |
public |
|
|
|
95 | public ExecBuilder stdin(RedirectFrom from) { | |
|
|
76 | 96 | Objects.requireNonNull(from, "from parameter can't be null"); |
|
|
77 | 97 | inputRedirect = from; |
|
|
98 | return this; | |
|
|
78 | 99 | } |
|
|
79 | 100 | |
|
|
80 | 101 | /** |
|
|
81 | 102 | * Sets redirection for the stdout, {@link RedirectTo} will be applied |
|
|
82 | 103 | * every time the process is started. |
|
|
83 | 104 | */ |
|
|
84 |
public |
|
|
|
105 | public ExecBuilder stdout(RedirectTo out) { | |
|
|
85 | 106 | Objects.requireNonNull(out, "out parameter can't be null"); |
|
|
86 | 107 | outputRedirect = out; |
|
|
108 | return this; | |
|
|
87 | 109 | } |
|
|
88 | 110 | |
|
|
89 | 111 | /** |
|
|
90 | 112 | * Sets redirection for the stderr, {@link RedirectTo} will be applied |
|
|
91 | 113 | * every time the process is started. |
|
|
92 | 114 | */ |
|
|
93 |
public |
|
|
|
115 | public ExecBuilder stderr(RedirectTo err) { | |
|
|
94 | 116 | Objects.requireNonNull(err, "err parameter can't be null"); |
|
|
95 | 117 | errorRedirect = err; |
|
|
118 | return this; | |
|
|
96 | 119 | } |
|
|
97 | 120 | |
|
|
98 | 121 | /** Implement this function to */ |
|
|
99 | 122 | protected abstract CompletableFuture<Integer> startInternal( |
|
|
100 | 123 | Map<String, String> environment, |
|
|
101 | 124 | File directory, |
|
|
102 | 125 | List<String> commandLine, |
|
|
103 | 126 | Optional<RedirectFrom> stdinRedirect, |
|
|
104 | 127 | Optional<RedirectTo> stdoutRedirect, |
|
|
105 | 128 | Optional<RedirectTo> stderrRedirect) throws IOException; |
|
|
106 | 129 | |
|
|
107 | 130 | /** |
|
|
108 | 131 | * Creates and starts new process and returns {@link CompletableFuture}. The |
|
|
109 | 132 | * process may be a remote process, or a shell process or etc. |
|
|
110 | 133 | * |
|
|
111 | 134 | * @return |
|
|
112 | 135 | * @throws IOException |
|
|
113 | 136 | */ |
|
|
114 | 137 | public CompletableFuture<Integer> start() throws IOException { |
|
|
115 | 138 | return startInternal( |
|
|
116 | 139 | environment, |
|
|
117 | 140 | directory, |
|
|
118 | 141 | commandLine, |
|
|
119 | 142 | Optional.ofNullable(inputRedirect), |
|
|
120 | 143 | Optional.ofNullable(outputRedirect), |
|
|
121 | 144 | Optional.ofNullable(errorRedirect)); |
|
|
122 | 145 | } |
|
|
123 | 146 | |
|
|
124 | 147 | } |
General Comments 0
You need to be logged in to leave comments.
Login now
