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