| @@ -0,0 +1,43 | |||
|
|
1 | package org.implab.gradle.common.utils; | |
|
|
2 | ||
|
|
3 | import java.util.Optional; | |
|
|
4 | import java.util.Set; | |
|
|
5 | ||
|
|
6 | import org.eclipse.jdt.annotation.NonNullByDefault; | |
|
|
7 | import org.gradle.api.plugins.ExtraPropertiesExtension; | |
|
|
8 | ||
|
|
9 | @NonNullByDefault | |
|
|
10 | class ExtraPropsExtension implements ExtraProps { | |
|
|
11 | ||
|
|
12 | private final ExtraPropertiesExtension ext; | |
|
|
13 | ||
|
|
14 | public ExtraPropsExtension(ExtraPropertiesExtension ext) { | |
|
|
15 | this.ext = ext; | |
|
|
16 | } | |
|
|
17 | ||
|
|
18 | @Override | |
|
|
19 | public Set<String> keys() { | |
|
|
20 | return ext.getProperties().keySet(); | |
|
|
21 | } | |
|
|
22 | ||
|
|
23 | @Override | |
|
|
24 | public boolean has(String name) { | |
|
|
25 | return ext.has(name); | |
|
|
26 | } | |
|
|
27 | ||
|
|
28 | @Override | |
|
|
29 | public <T> Optional<T> get(String name, Class<T> clazz) { | |
|
|
30 | return Optional.ofNullable(ext.get(name)).map(clazz::cast); | |
|
|
31 | } | |
|
|
32 | ||
|
|
33 | @Override | |
|
|
34 | public void put(String name, Object value) { | |
|
|
35 | ext.set(name, value); | |
|
|
36 | } | |
|
|
37 | ||
|
|
38 | @Override | |
|
|
39 | public void remove(String name) { | |
|
|
40 | ext.set(name, null); | |
|
|
41 | } | |
|
|
42 | ||
|
|
43 | } | |
| @@ -0,0 +1,31 | |||
|
|
1 | package org.implab.gradle.common.utils; | |
|
|
2 | ||
|
|
3 | import java.util.Optional; | |
|
|
4 | import java.util.Set; | |
|
|
5 | ||
|
|
6 | public class ExtraPropsNone implements ExtraProps { | |
|
|
7 | ||
|
|
8 | @Override | |
|
|
9 | public Set<String> keys() { | |
|
|
10 | return Set.of(); | |
|
|
11 | } | |
|
|
12 | ||
|
|
13 | @Override | |
|
|
14 | public boolean has(String name) { | |
|
|
15 | return false; | |
|
|
16 | } | |
|
|
17 | ||
|
|
18 | @Override | |
|
|
19 | public <T> Optional<T> get(String name, Class<T> clazz) { | |
|
|
20 | return Optional.empty(); | |
|
|
21 | } | |
|
|
22 | ||
|
|
23 | @Override | |
|
|
24 | public void put(String name, Object value) { | |
|
|
25 | } | |
|
|
26 | ||
|
|
27 | @Override | |
|
|
28 | public void remove(String name) { | |
|
|
29 | } | |
|
|
30 | ||
|
|
31 | } | |
| @@ -7,6 +7,7 import org.gradle.api.Action; | |||
|
|
7 | 7 | import org.gradle.api.file.DirectoryProperty; |
|
|
8 | 8 | import org.gradle.api.provider.MapProperty; |
|
|
9 | 9 | import org.gradle.api.provider.Property; |
|
|
10 | import org.gradle.api.provider.Provider; | |
|
|
10 | 11 | import org.implab.gradle.common.utils.Closures; |
|
|
11 | 12 | import org.implab.gradle.common.utils.Values; |
|
|
12 | 13 | |
| @@ -31,19 +32,13 public interface TaskEnvSpecMixin { | |||
|
|
31 | 32 | /** |
|
|
32 | 33 | * Configures the environment variable using the specified action. The |
|
|
33 | 34 | * action is called when the value is calculated; |
|
|
35 | * | |
|
|
36 | * <p> | |
|
|
37 | * The configuration action is called immediately. To support lazy evaluation, | |
|
|
38 | * properties may be assigned to providers. | |
|
|
34 | 39 | */ |
|
|
35 |
default void env(Action<Map<String, |
|
|
|
36 | var provider = getEnvironment() | |
|
|
37 | .orElse(Map.of()) | |
|
|
38 | .map((base) -> { | |
|
|
39 | var props = new HashMap<String, Object>(base); | |
|
|
40 | ||
|
|
41 | configure.execute(props); | |
|
|
42 | ||
|
|
43 | return Values.mapValues(props, Values::toString); | |
|
|
44 | }); | |
|
|
45 | ||
|
|
46 | getEnvironment().set(provider); | |
|
|
40 | default void env(Action<Map<String, ?>> configure) { | |
|
|
41 | Values.configureMap(getEnvironment(), configure, Object.class, Values::toString); | |
|
|
47 | 42 | } |
|
|
48 | 43 | |
|
|
49 | 44 | default void env(Closure<?> configure) { |
| @@ -1,16 +1,25 | |||
|
|
1 | 1 | package org.implab.gradle.common.utils; |
|
|
2 | 2 | |
|
|
3 |
import java.util. |
|
|
|
3 | import java.util.Optional; | |
|
|
4 | import java.util.Set; | |
|
|
4 | 5 | |
|
|
5 | 6 | import org.gradle.api.plugins.ExtensionAware; |
|
|
6 | 7 | |
|
|
7 |
public |
|
|
|
8 | public interface ExtraProps { | |
|
|
9 | ||
|
|
10 | Set<String> keys(); | |
|
|
11 | ||
|
|
12 | boolean has(String name); | |
|
|
13 | ||
|
|
14 | <T> Optional<T> get(String name, Class<T> clazz); | |
|
|
8 | 15 | |
|
|
9 | public static Map<String, Object> of(Object target) { | |
|
|
10 | if (target instanceof ExtensionAware ext) | |
|
|
11 | return ext.getExtensions().getExtraProperties().getProperties(); | |
|
|
12 | else | |
|
|
13 | return Map.of(); | |
|
|
16 | void put(String name, Object value); | |
|
|
17 | ||
|
|
18 | void remove(String name); | |
|
|
19 | ||
|
|
20 | public static ExtraProps of(Object target) { | |
|
|
21 | return target instanceof ExtensionAware ext | |
|
|
22 | ? new ExtraPropsExtension(ext.getExtensions().getExtraProperties()) | |
|
|
23 | : new ExtraPropsNone(); | |
|
|
14 | 24 | } |
|
|
15 | ||
|
|
16 | 25 | } |
| @@ -1,5 +1,6 | |||
|
|
1 | 1 | package org.implab.gradle.common.utils; |
|
|
2 | 2 | |
|
|
3 | import java.util.HashMap; | |
|
|
3 | 4 | import java.util.Iterator; |
|
|
4 | 5 | import java.util.Map; |
|
|
5 | 6 | import java.util.Spliterators; |
| @@ -11,6 +12,8 import java.util.stream.Collectors; | |||
|
|
11 | 12 | import java.util.stream.Stream; |
|
|
12 | 13 | import java.util.stream.StreamSupport; |
|
|
13 | 14 | |
|
|
15 | import org.gradle.api.Action; | |
|
|
16 | import org.gradle.api.provider.MapProperty; | |
|
|
14 | 17 | import org.gradle.api.provider.Provider; |
|
|
15 | 18 | |
|
|
16 | 19 | public final class Values { |
| @@ -35,6 +38,33 public final class Values { | |||
|
|
35 | 38 | .collect(Collectors.toMap(Entry::getKey, getter.andThen(mapper))); |
|
|
36 | 39 | } |
|
|
37 | 40 | |
|
|
41 | public static <K, V> void mergeMap(MapProperty<K,V> prop, Map<K, ? > map, Class<V> clazz) { | |
|
|
42 | map.forEach((k,v) -> { | |
|
|
43 | if(v instanceof Provider<?>) | |
|
|
44 | prop.put(k, ((Provider<?>)v).map(clazz::cast)); | |
|
|
45 | else | |
|
|
46 | prop.put(k, clazz.cast(v)); | |
|
|
47 | }); | |
|
|
48 | } | |
|
|
49 | ||
|
|
50 | public static <K,V,U> void mergeMap(MapProperty<K,V> prop, Map<K, ?> map, Class<U> clazz, Function<U,V> mapper) { | |
|
|
51 | Function<Object, U> caster = clazz::cast; | |
|
|
52 | var castMap = caster.andThen(mapper); | |
|
|
53 | ||
|
|
54 | map.forEach((k,v) -> { | |
|
|
55 | if(v instanceof Provider<?>) | |
|
|
56 | prop.put(k, ((Provider<?>)v).map(x -> castMap.apply(x))); | |
|
|
57 | else | |
|
|
58 | prop.put(k, castMap.apply(v)); | |
|
|
59 | }); | |
|
|
60 | } | |
|
|
61 | ||
|
|
62 | public static <K,V,U> void configureMap(MapProperty<K,V> prop, Action<Map<K, ?>> configure, Class<U> clazz, Function<U,V> mapper) { | |
|
|
63 | var map = new HashMap<K,V>(); | |
|
|
64 | configure.execute(map); | |
|
|
65 | mergeMap(prop, map, clazz, mapper); | |
|
|
66 | } | |
|
|
67 | ||
|
|
38 | 68 | /** |
|
|
39 | 69 | * Converts the supplied value to a string. |
|
|
40 | 70 | */ |
General Comments 0
You need to be logged in to leave comments.
Login now
