| @@ -0,0 +1,25 | |||
|
|
1 | package org.implab.gradle.common.dsl; | |
|
|
2 | ||
|
|
3 | import java.util.Objects; | |
|
|
4 | ||
|
|
5 | import org.gradle.api.Task; | |
|
|
6 | import org.gradle.api.tasks.TaskContainer; | |
|
|
7 | ||
|
|
8 | public abstract class TaskGroup implements TasksMixin { | |
|
|
9 | private final String groupName; | |
|
|
10 | ||
|
|
11 | protected abstract TaskContainer tasks(); | |
|
|
12 | ||
|
|
13 | public TaskGroup(String name) { | |
|
|
14 | Objects.requireNonNull(name, "The group name is required"); | |
|
|
15 | this.groupName = name; | |
|
|
16 | } | |
|
|
17 | ||
|
|
18 | @Override | |
|
|
19 | public <T extends Task> TaskSpec<T> task(String taskName, Class<T> clazz) { | |
|
|
20 | var provider = tasks().register(taskName, clazz); | |
|
|
21 | provider.configure(t -> t.setGroup(groupName)); | |
|
|
22 | return new TaskSpec<>(provider); | |
|
|
23 | } | |
|
|
24 | ||
|
|
25 | } | |
| @@ -0,0 +1,22 | |||
|
|
1 | package org.implab.gradle.common.dsl; | |
|
|
2 | ||
|
|
3 | import org.gradle.api.Buildable; | |
|
|
4 | import org.gradle.api.tasks.TaskProvider; | |
|
|
5 | ||
|
|
6 | public interface TaskReference { | |
|
|
7 | ||
|
|
8 | Object reference(); | |
|
|
9 | ||
|
|
10 | public static TaskReference named(String name) { | |
|
|
11 | return () -> name; | |
|
|
12 | } | |
|
|
13 | ||
|
|
14 | public static TaskReference provider(TaskProvider<?> provider) { | |
|
|
15 | return () -> provider; | |
|
|
16 | } | |
|
|
17 | ||
|
|
18 | public static TaskReference buildable(Buildable dependency) { | |
|
|
19 | return () -> dependency; | |
|
|
20 | } | |
|
|
21 | ||
|
|
22 | } | |
| @@ -0,0 +1,54 | |||
|
|
1 | package org.implab.gradle.common.dsl; | |
|
|
2 | ||
|
|
3 | import java.util.stream.Stream; | |
|
|
4 | ||
|
|
5 | import org.gradle.api.Action; | |
|
|
6 | import org.gradle.api.Task; | |
|
|
7 | import org.gradle.api.tasks.TaskProvider; | |
|
|
8 | ||
|
|
9 | public class TaskSpec<T extends Task> implements TaskReference { | |
|
|
10 | private final TaskProvider<T> taskProvider; | |
|
|
11 | ||
|
|
12 | public TaskSpec(TaskProvider<T> taskProvider) { | |
|
|
13 | this.taskProvider = taskProvider; | |
|
|
14 | } | |
|
|
15 | ||
|
|
16 | public TaskSpec<T> configure(Action<T> configure) { | |
|
|
17 | taskProvider.configure(configure); | |
|
|
18 | return this; | |
|
|
19 | } | |
|
|
20 | ||
|
|
21 | public TaskSpec<T> dependsOn(TaskReference... other) { | |
|
|
22 | taskProvider.configure(t -> t.dependsOn(references(other))); | |
|
|
23 | return this; | |
|
|
24 | } | |
|
|
25 | ||
|
|
26 | public TaskSpec<T> finalizedBy(TaskReference... other) { | |
|
|
27 | taskProvider.configure(t -> t.finalizedBy(references(other))); | |
|
|
28 | return this; | |
|
|
29 | } | |
|
|
30 | ||
|
|
31 | public TaskSpec<T> mustRunAfter(TaskReference... other) { | |
|
|
32 | taskProvider.configure(t -> t.mustRunAfter(references(other))); | |
|
|
33 | return this; | |
|
|
34 | } | |
|
|
35 | ||
|
|
36 | public TaskSpec<T> doLast(Action<T> action) { | |
|
|
37 | taskProvider.configure(t -> t.doLast(self -> action.execute(t))); | |
|
|
38 | return this; | |
|
|
39 | } | |
|
|
40 | ||
|
|
41 | public TaskSpec<T> doFirst(Action<T> action) { | |
|
|
42 | taskProvider.configure(t -> t.doFirst(self -> action.execute(t))); | |
|
|
43 | return this; | |
|
|
44 | } | |
|
|
45 | ||
|
|
46 | @Override | |
|
|
47 | public Object reference() { | |
|
|
48 | return taskProvider; | |
|
|
49 | } | |
|
|
50 | ||
|
|
51 | private static Object[] references(TaskReference[] other) { | |
|
|
52 | return Stream.of(other).map(TaskReference::reference).toArray(Object[]::new); | |
|
|
53 | } | |
|
|
54 | } | |
| @@ -0,0 +1,12 | |||
|
|
1 | package org.implab.gradle.common.dsl; | |
|
|
2 | ||
|
|
3 | import org.gradle.api.Task; | |
|
|
4 | ||
|
|
5 | public interface TasksMixin { | |
|
|
6 | ||
|
|
7 | default TaskSpec<Task> task(String name) { | |
|
|
8 | return task(name, Task.class); | |
|
|
9 | } | |
|
|
10 | ||
|
|
11 | <T extends Task> TaskSpec<T> task(String name, Class<T> clazz); | |
|
|
12 | } | |
| @@ -0,0 +1,29 | |||
|
|
1 | package org.implab.gradle.common.tasks; | |
|
|
2 | ||
|
|
3 | import org.gradle.api.Project; | |
|
|
4 | import org.gradle.api.Task; | |
|
|
5 | import org.gradle.api.logging.Logger; | |
|
|
6 | import org.gradle.api.specs.Spec; | |
|
|
7 | import org.gradle.api.tasks.Internal; | |
|
|
8 | ||
|
|
9 | /** Task methods available by default, this interface is used by mixins to | |
|
|
10 | * interact with their task. | |
|
|
11 | */ | |
|
|
12 | public interface TaskExtra { | |
|
|
13 | @Internal | |
|
|
14 | Project getProject(); | |
|
|
15 | ||
|
|
16 | void onlyIf(Spec<? super Task> spec); | |
|
|
17 | ||
|
|
18 | @Internal | |
|
|
19 | Logger getLogger(); | |
|
|
20 | ||
|
|
21 | default void onlyIfReason(String skipReason, Spec<? super Task> spec) { | |
|
|
22 | onlyIf(self -> { | |
|
|
23 | var satisfied = spec.isSatisfiedBy(self); | |
|
|
24 | if (!satisfied) | |
|
|
25 | getLogger().info("SKIP: {}", skipReason); | |
|
|
26 | return satisfied; | |
|
|
27 | }); | |
|
|
28 | } | |
|
|
29 | } | |
| @@ -0,0 +1,24 | |||
|
|
1 | package org.implab.gradle.common.utils; | |
|
|
2 | ||
|
|
3 | import org.gradle.api.Action; | |
|
|
4 | import org.gradle.api.artifacts.Configuration; | |
|
|
5 | ||
|
|
6 | public final class Configurations { | |
|
|
7 | ||
|
|
8 | public static final Action<Configuration> RESOLVABLE = Configurations::resolvable; | |
|
|
9 | ||
|
|
10 | public static final Action<Configuration> CONSUMABLE = Configurations::consumable; | |
|
|
11 | ||
|
|
12 | private Configurations() { | |
|
|
13 | } | |
|
|
14 | ||
|
|
15 | private static void resolvable(Configuration configuration) { | |
|
|
16 | configuration.setCanBeResolved(true); | |
|
|
17 | configuration.setCanBeConsumed(false); | |
|
|
18 | } | |
|
|
19 | ||
|
|
20 | private static void consumable(Configuration configuration) { | |
|
|
21 | configuration.setCanBeResolved(false); | |
|
|
22 | configuration.setCanBeConsumed(true); | |
|
|
23 | } | |
|
|
24 | } | |
| @@ -0,0 +1,14 | |||
|
|
1 | package org.implab.gradle.common.utils; | |
|
|
2 | ||
|
|
3 | import java.util.regex.Pattern; | |
|
|
4 | ||
|
|
5 | public class Strings { | |
|
|
6 | ||
|
|
7 | private static final Pattern firstLetter = Pattern.compile("^\\w"); | |
|
|
8 | ||
|
|
9 | public static String capitalize(String string) { | |
|
|
10 | if (string == null || string.length() == 0) | |
|
|
11 | return ""; | |
|
|
12 | return firstLetter.matcher(string).replaceFirst(m -> m.group().toUpperCase()); | |
|
|
13 | } | |
|
|
14 | } | |
| @@ -1,64 +1,66 | |||
|
|
1 | 1 | package org.implab.gradle.common; |
|
|
2 | 2 | |
|
|
3 | 3 | import javax.inject.Inject; |
|
|
4 | 4 | |
|
|
5 | 5 | import org.gradle.api.Action; |
|
|
6 | 6 | import org.gradle.api.NamedDomainObjectProvider; |
|
|
7 | 7 | import org.gradle.api.Plugin; |
|
|
8 | 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. |
|
|
|
12 | import org.implab.gradle.common.dsl.TaskSpec; | |
|
|
13 | import org.implab.gradle.common.dsl.TasksMixin; | |
|
|
13 | 14 | import org.implab.gradle.common.utils.ExtraProps; |
|
|
14 | 15 | |
|
|
15 | 16 | /** Project configuration traits */ |
|
|
16 | public interface ProjectMixin { | |
|
|
17 | public interface ProjectMixin extends TasksMixin { | |
|
|
17 | 18 | @Inject |
|
|
18 | 19 | Project getProject(); |
|
|
19 | 20 | |
|
|
20 | /** registers the new task */ | |
|
|
21 |
default <T extends Task> Task |
|
|
|
22 |
|
|
|
|
21 | @Override | |
|
|
22 | default <T extends Task> TaskSpec<T> task(String name, Class<T> clazz) { | |
|
|
23 | var provider = getProject().getTasks().register(name, clazz); | |
|
|
24 | return new TaskSpec<>(provider); | |
|
|
23 | 25 | } |
|
|
24 | 26 | |
|
|
25 | 27 | /** Registers the new configuration */ |
|
|
26 | 28 | default NamedDomainObjectProvider<Configuration> configuration(String name, Action<? super Configuration> configure) { |
|
|
27 | 29 | return getProject().getConfigurations().register(name, configure); |
|
|
28 | 30 | } |
|
|
29 | 31 | |
|
|
30 | 32 | /** Returns the project directory */ |
|
|
31 | 33 | default Directory projectDirectory() { |
|
|
32 | 34 | return getProject().getLayout().getProjectDirectory(); |
|
|
33 | 35 | } |
|
|
34 | 36 | |
|
|
35 | 37 | /** Applies and returns the specified plugin, the plugin is applied only once. */ |
|
|
36 | 38 | default <T extends Plugin<Project>> T plugin(Class<T> clazz) { |
|
|
37 | 39 | getProject().getPluginManager().apply(clazz); |
|
|
38 | 40 | return getProject().getPlugins().findPlugin(clazz); |
|
|
39 | 41 | } |
|
|
40 | 42 | |
|
|
41 | 43 | /** |
|
|
42 | 44 | * @param classes The list of classes to register as extensions |
|
|
43 | 45 | */ |
|
|
44 | 46 | default void exportClasses(Class<?>... classes) { |
|
|
45 | 47 | var props = ExtraProps.of(getProject()); |
|
|
46 | 48 | for (var clazz : classes) |
|
|
47 | 49 | props.put(clazz.getSimpleName(), clazz); |
|
|
48 | 50 | } |
|
|
49 | 51 | |
|
|
50 | 52 | /** Creates and register a new project extension. |
|
|
51 | 53 | * |
|
|
52 | 54 | * @param <T> The type of the extension |
|
|
53 | 55 | * @param extensionName The name of the extension in the project |
|
|
54 | 56 | * @param clazz The class of the extension |
|
|
55 | 57 | * @return the newly created extension |
|
|
56 | 58 | */ |
|
|
57 | 59 | default <T> T extension(String extensionName, Class<T> clazz) { |
|
|
58 | 60 | T extension = getProject().getObjects().newInstance(clazz); |
|
|
59 | 61 | getProject().getExtensions().add(extensionName, extension); |
|
|
60 | 62 | return extension; |
|
|
61 | 63 | } |
|
|
62 | 64 | |
|
|
63 | 65 | } |
|
|
64 | 66 | |
General Comments 0
You need to be logged in to leave comments.
Login now
