| @@ -9,6 +9,8 import org.gradle.api.Project; | |||||
| 9 | import org.gradle.api.Task; |
|
9 | import org.gradle.api.Task; | |
| 10 | import org.gradle.api.artifacts.Configuration; |
|
10 | import org.gradle.api.artifacts.Configuration; | |
| 11 | import org.gradle.api.file.Directory; |
|
11 | import org.gradle.api.file.Directory; | |
|
|
12 | import org.gradle.api.tasks.TaskContainer; | |||
|
|
13 | import org.implab.gradle.common.dsl.TaskGroup; | |||
| 12 | import org.implab.gradle.common.dsl.TaskSpec; |
|
14 | import org.implab.gradle.common.dsl.TaskSpec; | |
| 13 | import org.implab.gradle.common.dsl.TasksMixin; |
|
15 | import org.implab.gradle.common.dsl.TasksMixin; | |
| 14 | import org.implab.gradle.common.utils.ExtraProps; |
|
16 | import org.implab.gradle.common.utils.ExtraProps; | |
| @@ -24,6 +26,16 public interface ProjectMixin extends Ta | |||||
| 24 | return new TaskSpec<>(provider); |
|
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 | /** Registers the new configuration */ |
|
39 | /** Registers the new configuration */ | |
| 28 | default NamedDomainObjectProvider<Configuration> configuration(String name, Action<? super Configuration> configure) { |
|
40 | default NamedDomainObjectProvider<Configuration> configuration(String name, Action<? super Configuration> configure) { | |
| 29 | return getProject().getConfigurations().register(name, configure); |
|
41 | return getProject().getConfigurations().register(name, configure); | |
| @@ -1,17 +1,15 | |||||
| 1 |
package org.implab.gradle.common. |
|
1 | package org.implab.gradle.common.dsl; | |
| 2 |
|
2 | |||
| 3 | import java.io.File; |
|
3 | import java.io.File; | |
| 4 | import java.io.IOException; |
|
|||
| 5 | import java.lang.ProcessBuilder.Redirect; |
|
|||
| 6 | import java.util.ArrayList; |
|
4 | import java.util.ArrayList; | |
| 7 | import java.util.Collection; |
|
5 | import java.util.Collection; | |
| 8 | import java.util.List; |
|
6 | import java.util.List; | |
| 9 | import java.util.concurrent.CompletableFuture; |
|
7 | ||
| 10 | import java.util.concurrent.ExecutionException; |
|
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 | public class ProcessSpec { |
|
12 | public class ProcessSpec { | |
| 13 | private final ProcessBuilder builder; |
|
|||
| 14 |
|
||||
| 15 | private RedirectFrom inputRedirect; |
|
13 | private RedirectFrom inputRedirect; | |
| 16 |
|
14 | |||
| 17 | private RedirectTo outputRedirect; |
|
15 | private RedirectTo outputRedirect; | |
| @@ -22,51 +20,6 public class ProcessSpec { | |||||
| 22 |
|
20 | |||
| 23 | private File directory; |
|
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 | public ProcessSpec command(String... args) { |
|
23 | public ProcessSpec command(String... args) { | |
| 71 | command = new ArrayList<>(); |
|
24 | command = new ArrayList<>(); | |
| 72 | args(args); |
|
25 | args(args); | |
| @@ -109,4 +62,17 public class ProcessSpec { | |||||
| 109 | outputRedirect = to; |
|
62 | outputRedirect = to; | |
| 110 | return this; |
|
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); | |||
| 112 | } |
|
75 | } | |
|
|
76 | ||||
|
|
77 | ||||
|
|
78 | } | |||
| @@ -3,19 +3,19 package org.implab.gradle.common.dsl; | |||||
| 3 | import org.gradle.api.Buildable; |
|
3 | import org.gradle.api.Buildable; | |
| 4 | import org.gradle.api.tasks.TaskProvider; |
|
4 | import org.gradle.api.tasks.TaskProvider; | |
| 5 |
|
5 | |||
| 6 |
public interface Task |
|
6 | public interface TaskDependency { | |
| 7 |
|
7 | |||
| 8 | Object reference(); |
|
8 | Object reference(); | |
| 9 |
|
9 | |||
| 10 |
public static Task |
|
10 | public static TaskDependency named(String name) { | |
| 11 | return () -> name; |
|
11 | return () -> name; | |
| 12 | } |
|
12 | } | |
| 13 |
|
13 | |||
| 14 |
public static Task |
|
14 | public static TaskDependency provider(TaskProvider<?> provider) { | |
| 15 | return () -> provider; |
|
15 | return () -> provider; | |
| 16 | } |
|
16 | } | |
| 17 |
|
17 | |||
| 18 |
public static Task |
|
18 | public static TaskDependency buildable(Buildable dependency) { | |
| 19 | return () -> dependency; |
|
19 | return () -> dependency; | |
| 20 | } |
|
20 | } | |
| 21 |
|
21 | |||
| @@ -6,7 +6,10 import org.gradle.api.Action; | |||||
| 6 | import org.gradle.api.Task; |
|
6 | import org.gradle.api.Task; | |
| 7 | import org.gradle.api.tasks.TaskProvider; |
|
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 | private final TaskProvider<T> taskProvider; |
|
13 | private final TaskProvider<T> taskProvider; | |
| 11 |
|
14 | |||
| 12 | public TaskSpec(TaskProvider<T> taskProvider) { |
|
15 | public TaskSpec(TaskProvider<T> taskProvider) { | |
| @@ -18,17 +21,17 public class TaskSpec<T extends Task> im | |||||
| 18 | return this; |
|
21 | return this; | |
| 19 | } |
|
22 | } | |
| 20 |
|
23 | |||
| 21 |
public TaskSpec<T> dependsOn(Task |
|
24 | public TaskSpec<T> dependsOn(TaskDependency... other) { | |
| 22 | taskProvider.configure(t -> t.dependsOn(references(other))); |
|
25 | taskProvider.configure(t -> t.dependsOn(references(other))); | |
| 23 | return this; |
|
26 | return this; | |
| 24 | } |
|
27 | } | |
| 25 |
|
28 | |||
| 26 |
public TaskSpec<T> finalizedBy(Task |
|
29 | public TaskSpec<T> finalizedBy(TaskDependency... other) { | |
| 27 | taskProvider.configure(t -> t.finalizedBy(references(other))); |
|
30 | taskProvider.configure(t -> t.finalizedBy(references(other))); | |
| 28 | return this; |
|
31 | return this; | |
| 29 | } |
|
32 | } | |
| 30 |
|
33 | |||
| 31 |
public TaskSpec<T> mustRunAfter(Task |
|
34 | public TaskSpec<T> mustRunAfter(TaskDependency... other) { | |
| 32 | taskProvider.configure(t -> t.mustRunAfter(references(other))); |
|
35 | taskProvider.configure(t -> t.mustRunAfter(references(other))); | |
| 33 | return this; |
|
36 | return this; | |
| 34 | } |
|
37 | } | |
| @@ -43,12 +46,13 public class TaskSpec<T extends Task> im | |||||
| 43 | return this; |
|
46 | return this; | |
| 44 | } |
|
47 | } | |
| 45 |
|
48 | |||
|
|
49 | /** Returns a task provider for this task, can be used as dependency object */ | |||
| 46 | @Override |
|
50 | @Override | |
| 47 | public Object reference() { |
|
51 | public Object reference() { | |
| 48 | return taskProvider; |
|
52 | return taskProvider; | |
| 49 | } |
|
53 | } | |
| 50 |
|
54 | |||
| 51 |
private static Object[] references(Task |
|
55 | private static Object[] references(TaskDependency[] other) { | |
| 52 |
return Stream.of(other).map(Task |
|
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
