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