| @@ -1,45 +1,45 | |||||
| 1 | package org.implab.gradle.common.dsl; |
|
1 | package org.implab.gradle.common.dsl; | |
| 2 |
|
2 | |||
| 3 | import java.util.Map; |
|
3 | import java.util.Map; | |
| 4 |
|
4 | |||
| 5 | import org.gradle.api.Action; |
|
5 | import org.gradle.api.Action; | |
| 6 | import org.gradle.api.file.DirectoryProperty; |
|
6 | import org.gradle.api.file.DirectoryProperty; | |
| 7 | import org.gradle.api.provider.MapProperty; |
|
7 | import org.gradle.api.provider.MapProperty; | |
| 8 | import org.gradle.api.provider.Property; |
|
8 | import org.gradle.api.provider.Property; | |
| 9 | import org.implab.gradle.common.utils.Closures; |
|
9 | import org.implab.gradle.common.utils.Closures; | |
| 10 | import org.implab.gradle.common.utils.Properties; |
|
10 | import org.implab.gradle.common.utils.Properties; | |
| 11 |
|
11 | |||
| 12 | import groovy.lang.Closure; |
|
12 | import groovy.lang.Closure; | |
| 13 |
|
13 | |||
| 14 | /** |
|
14 | /** | |
| 15 | * Configuration properties of the execution shell. This object specifies a |
|
15 | * Configuration properties of the execution shell. This object specifies a | |
| 16 | * working directory and environment variables for the processes started within |
|
16 | * working directory and environment variables for the processes started within | |
| 17 | * this shell. |
|
17 | * this shell. | |
| 18 | */ |
|
18 | */ | |
| 19 | public interface TaskEnvSpecMixin { |
|
19 | public interface TaskEnvSpecMixin { | |
| 20 |
|
20 | |||
| 21 | /** Inherit environment from current process */ |
|
21 | /** Inherit environment from current process */ | |
| 22 | Property<Boolean> getInheritEnvironment(); |
|
22 | Property<Boolean> getInheritEnvironment(); | |
| 23 |
|
23 | |||
| 24 | /** Working directory */ |
|
24 | /** Working directory */ | |
| 25 | DirectoryProperty getWorkingDirectory(); |
|
25 | DirectoryProperty getWorkingDirectory(); | |
| 26 |
|
26 | |||
| 27 | /** Environment variables */ |
|
27 | /** Environment variables */ | |
| 28 | MapProperty<String, String> getEnvironment(); |
|
28 | MapProperty<String, String> getEnvironment(); | |
| 29 |
|
29 | |||
| 30 | /** |
|
30 | /** | |
| 31 | * Configures the environment variable using the specified action. The |
|
31 | * Configures the environment variable using the specified action. The | |
| 32 | * action is called when the value is calculated; |
|
32 | * action is called when the value is calculated; | |
| 33 | * |
|
33 | * | |
| 34 | * <p> |
|
34 | * <p> | |
| 35 | * The configuration action is called immediately. To support lazy evaluation, |
|
35 | * The configuration action is called immediately. To support lazy evaluation, | |
| 36 | * properties may be assigned to providers. |
|
36 | * properties may be assigned to providers. | |
| 37 | */ |
|
37 | */ | |
| 38 |
default void env(Action<Map<String, |
|
38 | default void env(Action<Map<String, Object>> configure) { | |
| 39 | Properties.configureMap(getEnvironment(), configure, Object::toString); |
|
39 | Properties.configureMap(getEnvironment(), configure, Object::toString); | |
| 40 | } |
|
40 | } | |
| 41 |
|
41 | |||
| 42 | default void env(Closure<?> configure) { |
|
42 | default void env(Closure<?> configure) { | |
| 43 | env(Closures.action(configure)); |
|
43 | env(Closures.action(configure)); | |
| 44 | } |
|
44 | } | |
| 45 | } |
|
45 | } | |
| @@ -1,32 +1,32 | |||||
| 1 | package org.implab.gradle.common.utils; |
|
1 | package org.implab.gradle.common.utils; | |
| 2 |
|
2 | |||
| 3 | import groovy.lang.Closure; |
|
3 | import groovy.lang.Closure; | |
| 4 |
|
4 | |||
| 5 | import org.eclipse.jdt.annotation.NonNullByDefault; |
|
5 | import org.eclipse.jdt.annotation.NonNullByDefault; | |
| 6 | import org.gradle.api.Action; |
|
6 | import org.gradle.api.Action; | |
| 7 |
|
7 | |||
| 8 | @NonNullByDefault |
|
8 | @NonNullByDefault | |
| 9 | public final class Closures { |
|
9 | public final class Closures { | |
| 10 | private Closures() { |
|
10 | private Closures() { | |
| 11 | } |
|
11 | } | |
| 12 |
|
12 | |||
| 13 | /** |
|
13 | /** | |
| 14 | * Wraps {@link Action} around the specified closure. The parameter |
|
14 | * Wraps {@link Action} around the specified closure. The parameter | |
| 15 | * of the action will be used as delegate in the specified closure. |
|
15 | * of the action will be used as delegate in the specified closure. | |
| 16 | * |
|
16 | * | |
| 17 | * @param <T> The type of the action parameter |
|
17 | * @param <T> The type of the action parameter | |
| 18 | * @param closure The closure |
|
18 | * @param closure The closure | |
| 19 | * @return |
|
19 | * @return | |
| 20 | */ |
|
20 | */ | |
| 21 | public static <T> Action<T> action(Closure<?> closure) { |
|
21 | public static <T> Action<T> action(Closure<?> closure) { | |
| 22 | return arg -> apply(closure, arg); |
|
22 | return arg -> apply(closure, arg); | |
| 23 | } |
|
23 | } | |
| 24 |
|
24 | |||
| 25 | public static void apply(Closure<?> action, Object target) { |
|
25 | public static void apply(Closure<?> action, Object target) { | |
| 26 | var c = (Closure<?>)action.clone(); |
|
26 | var c = (Closure<?>)action.clone(); | |
| 27 |
c.setResolveStrategy( |
|
27 | c.setResolveStrategy(Closure.DELEGATE_FIRST); | |
| 28 | c.setDelegate(target); |
|
28 | c.setDelegate(target); | |
| 29 | c.call(target); |
|
29 | c.call(target); | |
| 30 |
|
30 | |||
| 31 | } |
|
31 | } | |
| 32 | } |
|
32 | } | |
| @@ -1,63 +1,63 | |||||
| 1 | package org.implab.gradle.common.utils; |
|
1 | package org.implab.gradle.common.utils; | |
| 2 |
|
2 | |||
| 3 | import java.util.HashMap; |
|
3 | import java.util.HashMap; | |
| 4 | import java.util.Map; |
|
4 | import java.util.Map; | |
| 5 | import java.util.function.Function; |
|
5 | import java.util.function.Function; | |
| 6 |
|
6 | |||
| 7 | import org.gradle.api.Action; |
|
7 | import org.gradle.api.Action; | |
| 8 | import org.gradle.api.provider.ListProperty; |
|
8 | import org.gradle.api.provider.ListProperty; | |
| 9 | import org.gradle.api.provider.MapProperty; |
|
9 | import org.gradle.api.provider.MapProperty; | |
| 10 | import org.gradle.api.provider.Provider; |
|
10 | import org.gradle.api.provider.Provider; | |
| 11 |
|
11 | |||
| 12 | public final class Properties { |
|
12 | public final class Properties { | |
| 13 | private Properties() { |
|
13 | private Properties() { | |
| 14 | } |
|
14 | } | |
| 15 |
|
15 | |||
| 16 | public static <K> void mergeMap(MapProperty<K, Object> property, Map<K, Object> map) { |
|
16 | public static <K> void mergeMap(MapProperty<K, Object> property, Map<K, Object> map) { | |
| 17 | map.forEach((k, v) -> { |
|
17 | map.forEach((k, v) -> { | |
| 18 | if (v instanceof Provider<?>) |
|
18 | if (v instanceof Provider<?>) | |
| 19 | property.put(k, (Provider<?>) v); |
|
19 | property.put(k, (Provider<?>) v); | |
| 20 | else |
|
20 | else | |
| 21 | property.put(k, v); |
|
21 | property.put(k, v); | |
| 22 | }); |
|
22 | }); | |
| 23 | } |
|
23 | } | |
| 24 |
|
24 | |||
| 25 | public static <K, V> void mergeMap(MapProperty<K, V> property, Map<K, ?> map, Function<Object, V> mapper) { |
|
25 | public static <K, V> void mergeMap(MapProperty<K, V> property, Map<K, ?> map, Function<Object, V> mapper) { | |
| 26 | map.forEach((k, v) -> { |
|
26 | map.forEach((k, v) -> { | |
| 27 | if (v instanceof Provider<?>) |
|
27 | if (v instanceof Provider<?>) | |
| 28 | property.put(k, ((Provider<?>) v).map(mapper::apply)); |
|
28 | property.put(k, ((Provider<?>) v).map(mapper::apply)); | |
| 29 | else |
|
29 | else | |
| 30 | property.put(k, mapper.apply(v)); |
|
30 | property.put(k, mapper.apply(v)); | |
| 31 | }); |
|
31 | }); | |
| 32 | } |
|
32 | } | |
| 33 |
|
33 | |||
| 34 | public static void mergeList(ListProperty<Object> property, Iterable<Object> values) { |
|
34 | public static void mergeList(ListProperty<Object> property, Iterable<Object> values) { | |
| 35 | values.forEach(v -> { |
|
35 | values.forEach(v -> { | |
| 36 | if (v instanceof Provider<?>) |
|
36 | if (v instanceof Provider<?>) | |
| 37 | property.add((Provider<?>) v); |
|
37 | property.add((Provider<?>) v); | |
| 38 | else |
|
38 | else | |
| 39 | property.add(v); |
|
39 | property.add(v); | |
| 40 | }); |
|
40 | }); | |
| 41 | } |
|
41 | } | |
| 42 |
|
42 | |||
| 43 | public static <V> void mergeList(ListProperty<V> property, Iterable<Object> values, Function<Object, V> mapper) { |
|
43 | public static <V> void mergeList(ListProperty<V> property, Iterable<Object> values, Function<Object, V> mapper) { | |
| 44 | values.forEach(v -> { |
|
44 | values.forEach(v -> { | |
| 45 | if (v instanceof Provider<?>) |
|
45 | if (v instanceof Provider<?>) | |
| 46 | property.add(((Provider<?>) v).map(mapper::apply)); |
|
46 | property.add(((Provider<?>) v).map(mapper::apply)); | |
| 47 | else |
|
47 | else | |
| 48 | property.add(mapper.apply(v)); |
|
48 | property.add(mapper.apply(v)); | |
| 49 | }); |
|
49 | }); | |
| 50 | } |
|
50 | } | |
| 51 |
|
51 | |||
| 52 |
public static <K> void configureMap(MapProperty<K, Object> prop, Action<Map<K, |
|
52 | public static <K> void configureMap(MapProperty<K, Object> prop, Action<Map<K, Object>> configure) { | |
| 53 | var map = new HashMap<K, Object>(); |
|
53 | var map = new HashMap<K, Object>(); | |
| 54 | configure.execute(map); |
|
54 | configure.execute(map); | |
| 55 | mergeMap(prop, map); |
|
55 | mergeMap(prop, map); | |
| 56 | } |
|
56 | } | |
| 57 |
|
57 | |||
| 58 |
public static <K, V> void configureMap(MapProperty<K, V> prop, Action<Map<K, |
|
58 | public static <K, V> void configureMap(MapProperty<K, V> prop, Action<Map<K, Object>> configure, Function<Object, V> mapper) { | |
| 59 | var map = new HashMap<K, Object>(); |
|
59 | var map = new HashMap<K, Object>(); | |
| 60 | configure.execute(map); |
|
60 | configure.execute(map); | |
| 61 | mergeMap(prop, map, mapper); |
|
61 | mergeMap(prop, map, mapper); | |
| 62 | } |
|
62 | } | |
| 63 | } |
|
63 | } | |
General Comments 0
You need to be logged in to leave comments.
Login now
