##// END OF EJS Templates
Changed TaskEnvSpecMixin , Fixed bug in ExtraProps extension when updating extra pros of objects.
cin -
r16:0e291927620c default
parent child
Show More
@@ -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 }
@@ -1,41 +1,41
1 1 plugins {
2 2 id "java-library"
3 3 id "ivy-publish"
4 4 }
5 5
6 6 java {
7 7 withJavadocJar()
8 8 withSourcesJar()
9 toolchain {
9 toolchain {
10 10 languageVersion = JavaLanguageVersion.of(17)
11 11 }
12 12 }
13 13
14 14 dependencies {
15 15 compileOnly libs.jdt.annotations
16 16
17 17 api gradleApi()
18 18 }
19 19
20 20 task printVersion{
21 21 doLast {
22 22 println "project: $project.group:$project.name:$project.version"
23 23 println "jar: ${->jar.archiveFileName.get()}"
24 24 }
25 25 }
26 26
27 27 publishing {
28 28 repositories {
29 29 ivy {
30 30 url "${System.properties["user.home"]}/ivy-repo"
31 31 }
32 32 }
33 33 publications {
34 34 ivy(IvyPublication) {
35 35 from components.java
36 36 descriptor.description {
37 37 text = providers.provider({ description })
38 38 }
39 39 }
40 40 }
41 41 } No newline at end of file
@@ -1,52 +1,47
1 1 package org.implab.gradle.common.dsl;
2 2
3 3 import java.util.HashMap;
4 4 import java.util.Map;
5 5
6 6 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
13 14 import groovy.lang.Closure;
14 15
15 16 /**
16 17 * Configuration properties of the execution shell. This object specifies a
17 18 * working directory and environment variables for the processes started within
18 19 * this shell.
19 20 */
20 21 public interface TaskEnvSpecMixin {
21 22
22 23 /** Inherit environment from current process */
23 24 Property<Boolean> getInheritEnvironment();
24 25
25 26 /** Working directory */
26 27 DirectoryProperty getWorkingDirectory();
27 28
28 29 /** Environment variables */
29 30 MapProperty<String, String> getEnvironment();
30 31
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, Object>> configure) {
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) {
50 45 env(Closures.action(configure));
51 46 }
52 47 }
@@ -1,16 +1,25
1 1 package org.implab.gradle.common.utils;
2 2
3 import java.util.Map;
3 import java.util.Optional;
4 import java.util.Set;
4 5
5 6 import org.gradle.api.plugins.ExtensionAware;
6 7
7 public class ExtraProps {
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,93 +1,123
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;
6 7 import java.util.Map.Entry;
7 8 import java.util.Optional;
8 9 import java.util.function.Function;
9 10 import java.util.function.Supplier;
10 11 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 {
17 20
18 21 private Values() {
19 22 }
20 23
21 24 /**
22 25 * Converts values in the specified map
23 *
26 *
24 27 * @param <K>
25 28 * @param <V>
26 29 * @param <U>
27 30 * @param map
28 31 * @param mapper
29 32 * @return
30 33 */
31 34 public static <K, V, U> Map<K, U> mapValues(Map<K, V> map, Function<V, U> mapper) {
32 35 Function<Entry<K, V>, V> getter = Entry::getValue;
33 36
34 37 return map.entrySet().stream()
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 */
41 71 public static String toString(Object value) {
42 72 if (value == null) {
43 73 return null;
44 74 } else if (value instanceof String string) {
45 75 return string;
46 76 } else if (value instanceof Provider<?> provider) {
47 77 return toString(provider.getOrNull());
48 78 } else if (value instanceof Supplier<?> supplier) {
49 79 return toString(supplier.get());
50 80 } else {
51 81 return value.toString();
52 82 }
53 83 }
54 84
55 85 public static <T> Stream<T> stream(Iterator<T> remaining) {
56 86 return StreamSupport.stream(
57 87 Spliterators.spliteratorUnknownSize(remaining, 0),
58 88 false);
59 89 }
60 90
61 91 public static <T> Optional<T> take(Iterator<T> iterator) {
62 92 return iterator.hasNext() ? Optional.of(iterator.next()) : Optional.empty();
63 93 }
64 94
65 95 public static <T> Iterable<T> iterable(T[] values) {
66 96 return () -> new ArrayIterator<>(values);
67 97 }
68 98
69 99 public static <T> Optional<T> optional(Provider<T> provider) {
70 100 return provider.isPresent() ? Optional.of(provider.get()) : Optional.empty();
71 101 }
72 102
73 103 private static class ArrayIterator<T> implements Iterator<T> {
74 104 private final T[] data;
75 105
76 106 private int pos = 0;
77 107
78 108 ArrayIterator(T[] data) {
79 109 this.data = data;
80 110 }
81 111
82 112 @Override
83 113 public boolean hasNext() {
84 114 return pos < data.length;
85 115 }
86 116
87 117 @Override
88 118 public T next() {
89 119 return data[pos++];
90 120 }
91 121 }
92 122
93 123 }
General Comments 0
You need to be logged in to leave comments. Login now