| @@ -0,0 +1,16 | |||
|
|
1 | package org.implab.gradle.common.exec; | |
|
|
2 | ||
|
|
3 | /** Shell used to start processes */ | |
|
|
4 | public interface ExecShell { | |
|
|
5 | ||
|
|
6 | /** Creates new process builder {@link ExecBuilder} */ | |
|
|
7 | ExecBuilder builder(); | |
|
|
8 | ||
|
|
9 | /** | |
|
|
10 | * Creates default process execution "shell" which will execute processes | |
|
|
11 | * directly. | |
|
|
12 | */ | |
|
|
13 | static ExecShell system() { | |
|
|
14 | return () -> new SystemExecBuilder(); | |
|
|
15 | } | |
|
|
16 | } | |
| @@ -5,7 +5,7 import java.util.ArrayList; | |||
|
|
5 | 5 | import java.util.Collection; |
|
|
6 | 6 | import java.util.List; |
|
|
7 | 7 | |
|
|
8 |
import org.implab.gradle.common.exec.Exec |
|
|
|
8 | import org.implab.gradle.common.exec.ExecBuilder; | |
|
|
9 | 9 | import org.implab.gradle.common.exec.RedirectFrom; |
|
|
10 | 10 | import org.implab.gradle.common.exec.RedirectTo; |
|
|
11 | 11 | |
| @@ -63,7 +63,7 public class ProcessSpec { | |||
|
|
63 | 63 | return this; |
|
|
64 | 64 | } |
|
|
65 | 65 | |
|
|
66 |
public void accept(Exec |
|
|
|
66 | public void accept(ExecBuilder executor) { | |
|
|
67 | 67 | command.forEach(executor::argument); |
|
|
68 | 68 | executor.directory(directory); |
|
|
69 | 69 | if (inputRedirect != null) |
| @@ -2,24 +2,60 package org.implab.gradle.common.exec; | |||
|
|
2 | 2 | |
|
|
3 | 3 | import java.util.concurrent.CompletableFuture; |
|
|
4 | 4 | |
|
|
5 | import org.eclipse.jdt.annotation.NonNullByDefault; | |
|
|
6 | ||
|
|
5 | 7 | import java.io.File; |
|
|
6 | 8 | import java.io.IOException; |
|
|
7 | 9 | |
|
|
8 | /** Interface for a command line execution. */ | |
|
|
9 | public interface Executor { | |
|
|
10 | /** Command line builder */ | |
|
|
11 | @NonNullByDefault | |
|
|
12 | public interface ExecBuilder { | |
|
|
13 | ||
|
|
14 | /** Sets command line, clears previous one if any */ | |
|
|
15 | void command(String cmd, String... args); | |
|
|
16 | ||
|
|
17 | /** Adds an argument to the command line */ | |
|
|
10 | 18 | void argument(String arg); |
|
|
11 | 19 | |
|
|
20 | /** Sets the working directory */ | |
|
|
12 | 21 | void directory(File directory); |
|
|
13 | 22 | |
|
|
23 | /** | |
|
|
24 | * Sets the environment value. The value cannot be null. | |
|
|
25 | * | |
|
|
26 | * @param envVar The name of the environment variable | |
|
|
27 | * @param value The value to set, | |
|
|
28 | */ | |
|
|
29 | void setEnvironment(String envVar, String value); | |
|
|
30 | ||
|
|
31 | void unsetEnvironment(String envVar); | |
|
|
32 | ||
|
|
33 | /** | |
|
|
34 | * Sets redirection for the stdin, {@link RedirectFrom} will be applied | |
|
|
35 | * every time the process is started. | |
|
|
36 | * | |
|
|
37 | * <p>If redirection | |
|
|
38 | */ | |
|
|
14 | 39 | void stdin(RedirectFrom from); |
|
|
15 | 40 | |
|
|
41 | /** | |
|
|
42 | * Sets redirection for the stdout, {@link RedirectTo} will be applied | |
|
|
43 | * every time the process is started. | |
|
|
44 | */ | |
|
|
16 | 45 | void stdout(RedirectTo out); |
|
|
17 | 46 | |
|
|
47 | /** | |
|
|
48 | * Sets redirection for the stderr, {@link RedirectTo} will be applied | |
|
|
49 | * every time the process is started. | |
|
|
50 | */ | |
|
|
18 | 51 | void stderr(RedirectTo err); |
|
|
19 | 52 | |
|
|
53 | /** | |
|
|
54 | * Creates and starts new process and returns {@link CompletableFuture}. The | |
|
|
55 | * process may be a remote process, or a shell process or etc. | |
|
|
56 | * @return | |
|
|
57 | * @throws IOException | |
|
|
58 | */ | |
|
|
20 | 59 | CompletableFuture<Integer> start() throws IOException; |
|
|
21 | 60 | |
|
|
22 | public static Executor system() { | |
|
|
23 | return new SystemExec(); | |
|
|
24 | 61 | } |
|
|
25 | } | |
| @@ -6,6 +6,10 import java.io.InputStream; | |||
|
|
6 | 6 | import java.io.OutputStream; |
|
|
7 | 7 | import java.util.concurrent.CompletableFuture; |
|
|
8 | 8 | |
|
|
9 | /** Describes how to redirect input streams. This interface is used to configure | |
|
|
10 | * lazy redirection. {@link #redirect(OutputStream)} is called when the process | |
|
|
11 | * is started. | |
|
|
12 | */ | |
|
|
9 | 13 | public interface RedirectFrom { |
|
|
10 | 14 | CompletableFuture<Void> redirect(OutputStream to); |
|
|
11 | 15 | |
| @@ -6,7 +6,7 import java.lang.ProcessBuilder.Redirect | |||
|
|
6 | 6 | import java.util.ArrayList; |
|
|
7 | 7 | import java.util.concurrent.CompletableFuture; |
|
|
8 | 8 | |
|
|
9 |
class SystemExec implements Exec |
|
|
|
9 | class SystemExecBuilder implements ExecBuilder { | |
|
|
10 | 10 | |
|
|
11 | 11 | private final ProcessBuilder builder = new ProcessBuilder(); |
|
|
12 | 12 | |
| @@ -48,22 +48,22 class SystemExec implements Executor { | |||
|
|
48 | 48 | .thenApply(t -> proc.exitValue()); |
|
|
49 | 49 | } |
|
|
50 | 50 | |
|
|
51 | public void command(String... args) { | |
|
|
52 | builder.command(args); | |
|
|
53 | } | |
|
|
54 | ||
|
|
51 | @Override | |
|
|
55 | 52 | public void directory(File workingDir) { |
|
|
56 | 53 | builder.directory(workingDir); |
|
|
57 | 54 | } |
|
|
58 | 55 | |
|
|
56 | @Override | |
|
|
59 | 57 | public void stderr(RedirectTo to) { |
|
|
60 | 58 | errorRedirect = to; |
|
|
61 | 59 | } |
|
|
62 | 60 | |
|
|
61 | @Override | |
|
|
63 | 62 | public void stdin(RedirectFrom from) { |
|
|
64 | 63 | inputRedirect = from; |
|
|
65 | 64 | } |
|
|
66 | 65 | |
|
|
66 | @Override | |
|
|
67 | 67 | public void stdout(RedirectTo to) { |
|
|
68 | 68 | outputRedirect = to; |
|
|
69 | 69 | } |
| @@ -73,4 +73,26 class SystemExec implements Executor { | |||
|
|
73 | 73 | builder.command().add(arg); |
|
|
74 | 74 | } |
|
|
75 | 75 | |
|
|
76 | @Override | |
|
|
77 | public void setEnvironment(String envVar, String value) { | |
|
|
78 | builder.environment().put(envVar, value); | |
|
|
79 | ||
|
|
76 | 80 | } |
|
|
81 | ||
|
|
82 | @Override | |
|
|
83 | public void unsetEnvironment(String envVar) { | |
|
|
84 | builder.environment().remove(envVar); | |
|
|
85 | } | |
|
|
86 | ||
|
|
87 | @Override | |
|
|
88 | public void command(String cmd, String... args) { | |
|
|
89 | var commandLine = new ArrayList<String>(); | |
|
|
90 | ||
|
|
91 | commandLine.add(cmd); | |
|
|
92 | for (var arg : args) | |
|
|
93 | commandLine.add(arg); | |
|
|
94 | ||
|
|
95 | builder.command(commandLine); | |
|
|
96 | } | |
|
|
97 | ||
|
|
98 | } | |
| @@ -6,7 +6,7 import java.util.concurrent.ExecutionExc | |||
|
|
6 | 6 | |
|
|
7 | 7 | import org.gradle.api.DefaultTask; |
|
|
8 | 8 | import org.implab.gradle.common.dsl.ProcessSpec; |
|
|
9 |
import org.implab.gradle.common.exec.Exec |
|
|
|
9 | import org.implab.gradle.common.exec.ExecBuilder; | |
|
|
10 | 10 | import org.implab.gradle.common.exec.RedirectFrom; |
|
|
11 | 11 | import org.implab.gradle.common.exec.RedirectTo; |
|
|
12 | 12 | |
| @@ -100,5 +100,5 public abstract class ExternalTask exten | |||
|
|
100 | 100 | return executor.start().get() == code; |
|
|
101 | 101 | } |
|
|
102 | 102 | |
|
|
103 |
protected abstract Exec |
|
|
|
103 | protected abstract ExecBuilder exec(); | |
|
|
104 | 104 | } |
General Comments 0
You need to be logged in to leave comments.
Login now
