##// END OF EJS Templates
WIP minor changes
cin -
r12:9ce58f1128fc default
parent child
Show More
@@ -5,26 +5,54 import java.util.stream.Stream;
5 import org.eclipse.jdt.annotation.NonNullByDefault;
5 import org.eclipse.jdt.annotation.NonNullByDefault;
6 import org.implab.gradle.common.utils.Values;
6 import org.implab.gradle.common.utils.Values;
7
7
8 /** Command builder interface, used to specify the executable and parameters */
8 @NonNullByDefault
9 @NonNullByDefault
9 public interface CommandBuilder {
10 public interface CommandBuilder {
10
11
12 /** Sets the executable, the parameters are left intact. */
11 CommandBuilder executable(String executable);
13 CommandBuilder executable(String executable);
12
14
15 /** Sets the specified executable and parameters, old executable and parameters
16 * are discarded.
17 */
13 default CommandBuilder commandLine(String executable, String... args) {
18 default CommandBuilder commandLine(String executable, String... args) {
14 return executable(executable)
19 return executable(executable)
15 .arguments(args);
20 .arguments(args);
16 }
21 }
17
22
18 default CommandBuilder arg(String arg0, String... args) {
23 /** Sets the specified executable and parameters, old executable and parameters
24 * are discarded.
25 *
26 * @param command The command line. Must contain at least one element (executable).
27 *
28 */
29 default CommandBuilder commandLine(Iterable<? extends String> command) {
30 var iterator = command.iterator();
31
32 // set executable
33 executable(Values.take(iterator).orElseThrow());
34 // cleat arguments
35 arguments();
36 // set new arguments
37 while (iterator.hasNext())
38 addArguments(iterator.next());
39
40 return this;
41 }
42
43 /** Adds the specified arguments to this builder */
44 default CommandBuilder addArguments(String arg0, String... args) {
19 return arguments(() -> Stream
45 return arguments(() -> Stream
20 .concat(Stream.of(arg0), Stream.of(args))
46 .concat(Stream.of(arg0), Stream.of(args))
21 .iterator());
47 .iterator());
22 }
48 }
23
49
50 /** Replaces arguments in the builder with the specified one. */
24 default CommandBuilder arguments(String... args) {
51 default CommandBuilder arguments(String... args) {
25 return arguments(Values.iterable(args));
52 return arguments(Values.iterable(args));
26 }
53 }
27
54
55 /** Replaces arguments in the builder with the specified one. */
28 CommandBuilder arguments(Iterable<String> args);
56 CommandBuilder arguments(Iterable<String> args);
29
57
30 default CommandBuilder from(CommandSpec commandSpec) {
58 default CommandBuilder from(CommandSpec commandSpec) {
@@ -53,17 +53,8 public abstract class ExecBuilder implem
53 return this;
53 return this;
54 }
54 }
55
55
56 public ExecBuilder commandLine(Iterable<? extends String> cmd) {
57 requireNonNull(cmd, "cmd can't be null");
58
59 this.arguments.clear();
60 for (var arg : cmd)
61 this.arguments.add(arg);
62 return this;
63 }
64
65 @Override
56 @Override
66 public ExecBuilder arg(String arg0, String... args) {
57 public ExecBuilder addArguments(String arg0, String... args) {
67 requireNonNull(arg0, "arg0 parameter can't be null");
58 requireNonNull(arg0, "arg0 parameter can't be null");
68 arguments.add(arg0);
59 arguments.add(arg0);
69
60
@@ -2,10 +2,15 package org.implab.gradle.common.utils;
2
2
3 import java.util.Iterator;
3 import java.util.Iterator;
4 import java.util.Map;
4 import java.util.Map;
5 import java.util.Spliterator;
6 import java.util.Spliterators;
5 import java.util.Map.Entry;
7 import java.util.Map.Entry;
8 import java.util.Optional;
6 import java.util.function.Function;
9 import java.util.function.Function;
7 import java.util.function.Supplier;
10 import java.util.function.Supplier;
8 import java.util.stream.Collectors;
11 import java.util.stream.Collectors;
12 import java.util.stream.Stream;
13 import java.util.stream.StreamSupport;
9
14
10 import org.gradle.api.provider.Provider;
15 import org.gradle.api.provider.Provider;
11
16
@@ -46,6 +51,16 public final class Values {
46 }
51 }
47 }
52 }
48
53
54 public static <T> Stream<T> stream(Iterator<T> remaining) {
55 var spliterator = Spliterators.spliteratorUnknownSize(remaining, 0);
56
57 return StreamSupport.stream(spliterator, false);
58 }
59
60 public static <T> Optional<T> take(Iterator<T> iterator) {
61 return iterator.hasNext() ? Optional.of(iterator.next()) : Optional.empty();
62 }
63
49 public static <T> Iterable<T> iterable(T[] values) {
64 public static <T> Iterable<T> iterable(T[] values) {
50 return () -> new ArrayIterator<>(values);
65 return () -> new ArrayIterator<>(values);
51 }
66 }
General Comments 0
You need to be logged in to leave comments. Login now