##// END OF EJS Templates
woring on exec spec
cin -
r3:56bc6876834e default
parent child
Show More
@@ -9,6 +9,8 import org.gradle.api.Project;
9 9 import org.gradle.api.Task;
10 10 import org.gradle.api.artifacts.Configuration;
11 11 import org.gradle.api.file.Directory;
12 import org.gradle.api.tasks.TaskContainer;
13 import org.implab.gradle.common.dsl.TaskGroup;
12 14 import org.implab.gradle.common.dsl.TaskSpec;
13 15 import org.implab.gradle.common.dsl.TasksMixin;
14 16 import org.implab.gradle.common.utils.ExtraProps;
@@ -24,6 +26,16 public interface ProjectMixin extends Ta
24 26 return new TaskSpec<>(provider);
25 27 }
26 28
29 /** Creates a new task group */
30 default TaskGroup taskGroup(String name) {
31 return new TaskGroup(name) {
32 @Override
33 protected TaskContainer tasks() {
34 return getProject().getTasks();
35 }
36 };
37 }
38
27 39 /** Registers the new configuration */
28 40 default NamedDomainObjectProvider<Configuration> configuration(String name, Action<? super Configuration> configure) {
29 41 return getProject().getConfigurations().register(name, configure);
@@ -1,17 +1,15
1 package org.implab.gradle.common.exec;
1 package org.implab.gradle.common.dsl;
2 2
3 3 import java.io.File;
4 import java.io.IOException;
5 import java.lang.ProcessBuilder.Redirect;
6 4 import java.util.ArrayList;
7 5 import java.util.Collection;
8 6 import java.util.List;
9 import java.util.concurrent.CompletableFuture;
10 import java.util.concurrent.ExecutionException;
7
8 import org.implab.gradle.common.exec.Executor;
9 import org.implab.gradle.common.exec.RedirectFrom;
10 import org.implab.gradle.common.exec.RedirectTo;
11 11
12 12 public class ProcessSpec {
13 private final ProcessBuilder builder;
14
15 13 private RedirectFrom inputRedirect;
16 14
17 15 private RedirectTo outputRedirect;
@@ -22,51 +20,6 public class ProcessSpec {
22 20
23 21 private File directory;
24 22
25 public ProcessSpec() {
26 builder = new ProcessBuilder();
27 }
28
29 public CompletableFuture<Integer> start() throws IOException {
30 var tasks = new ArrayList<CompletableFuture<?>>();
31
32 builder.command(command);
33 builder.directory(directory);
34
35 // discard stdout if not redirected
36 if (outputRedirect == null)
37 builder.redirectOutput(Redirect.DISCARD);
38
39 // discard stderr if not redirected
40 if (errorRedirect == null)
41 builder.redirectError(Redirect.DISCARD);
42
43 // run process
44 var proc = builder.start();
45
46 tasks.add(proc.onExit());
47
48 if (inputRedirect != null)
49 tasks.add(inputRedirect.redirect(proc.getOutputStream()));
50
51 if (outputRedirect != null)
52 tasks.add(outputRedirect.redirect(proc.getInputStream()));
53
54 if (errorRedirect != null)
55 tasks.add(errorRedirect.redirect(proc.getErrorStream()));
56
57 return CompletableFuture
58 .allOf(tasks.toArray(new CompletableFuture<?>[0]))
59 .thenApply(t -> proc.exitValue());
60 }
61
62 public Integer exec() throws InterruptedException, ExecutionException, IOException {
63 return start().get();
64 }
65
66 public List<String> command() {
67 return command;
68 }
69
70 23 public ProcessSpec command(String... args) {
71 24 command = new ArrayList<>();
72 25 args(args);
@@ -109,4 +62,17 public class ProcessSpec {
109 62 outputRedirect = to;
110 63 return this;
111 64 }
65
66 public void accept(Executor executor) {
67 command.forEach(executor::argument);
68 executor.directory(directory);
69 if (inputRedirect != null)
70 executor.stdin(inputRedirect);
71 if (outputRedirect != null)
72 executor.stdout(outputRedirect);
73 if (errorRedirect != null)
74 executor.stderr(errorRedirect);
75 }
76
77
112 78 }
@@ -3,19 +3,19 package org.implab.gradle.common.dsl;
3 3 import org.gradle.api.Buildable;
4 4 import org.gradle.api.tasks.TaskProvider;
5 5
6 public interface TaskReference {
6 public interface TaskDependency {
7 7
8 8 Object reference();
9 9
10 public static TaskReference named(String name) {
10 public static TaskDependency named(String name) {
11 11 return () -> name;
12 12 }
13 13
14 public static TaskReference provider(TaskProvider<?> provider) {
14 public static TaskDependency provider(TaskProvider<?> provider) {
15 15 return () -> provider;
16 16 }
17 17
18 public static TaskReference buildable(Buildable dependency) {
18 public static TaskDependency buildable(Buildable dependency) {
19 19 return () -> dependency;
20 20 }
21 21
@@ -6,7 +6,10 import org.gradle.api.Action;
6 6 import org.gradle.api.Task;
7 7 import org.gradle.api.tasks.TaskProvider;
8 8
9 public class TaskSpec<T extends Task> implements TaskReference {
9 /**
10 * Wrapper around TaskProvider for simplified lazy task configuration
11 */
12 public class TaskSpec<T extends Task> implements TaskDependency {
10 13 private final TaskProvider<T> taskProvider;
11 14
12 15 public TaskSpec(TaskProvider<T> taskProvider) {
@@ -18,17 +21,17 public class TaskSpec<T extends Task> im
18 21 return this;
19 22 }
20 23
21 public TaskSpec<T> dependsOn(TaskReference... other) {
24 public TaskSpec<T> dependsOn(TaskDependency... other) {
22 25 taskProvider.configure(t -> t.dependsOn(references(other)));
23 26 return this;
24 27 }
25 28
26 public TaskSpec<T> finalizedBy(TaskReference... other) {
29 public TaskSpec<T> finalizedBy(TaskDependency... other) {
27 30 taskProvider.configure(t -> t.finalizedBy(references(other)));
28 31 return this;
29 32 }
30 33
31 public TaskSpec<T> mustRunAfter(TaskReference... other) {
34 public TaskSpec<T> mustRunAfter(TaskDependency... other) {
32 35 taskProvider.configure(t -> t.mustRunAfter(references(other)));
33 36 return this;
34 37 }
@@ -43,12 +46,13 public class TaskSpec<T extends Task> im
43 46 return this;
44 47 }
45 48
49 /** Returns a task provider for this task, can be used as dependency object */
46 50 @Override
47 51 public Object reference() {
48 52 return taskProvider;
49 53 }
50 54
51 private static Object[] references(TaskReference[] other) {
52 return Stream.of(other).map(TaskReference::reference).toArray(Object[]::new);
55 private static Object[] references(TaskDependency[] other) {
56 return Stream.of(other).map(TaskDependency::reference).toArray(Object[]::new);
53 57 }
54 58 }
General Comments 0
You need to be logged in to leave comments. Login now