forked from junit-team/junit5-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BUILDING
112 lines (105 loc) · 3.64 KB
/
BUILDING
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.nio.file.*
import java.util.function.*
import java.util.spi.*
class Arguments {
List<String> list = new ArrayList<>();
Arguments add(Object o) {
list.add(String.valueOf(o));
return this;
}
Arguments addPath(Predicate<Path> predicate, String... roots) throws Exception {
List<String> paths = new ArrayList<>();
for (String root : roots) {
Files.walk(Paths.get(root)).filter(predicate).map(Object::toString).forEach(paths::add);
}
add(String.join(File.pathSeparator, paths));
return this;
}
Arguments addPath(String... paths) {
add(String.join(File.pathSeparator, paths));
return this;
}
Arguments addAllFiles(String root, String extension) throws Exception {
return addAllFiles(root, path -> path.getFileName().toString().endsWith(extension));
}
Arguments addAllFiles(String root, Predicate<Path> predicate) throws Exception {
Files.walk(Paths.get(root)).filter(predicate).forEach(this::add);
return this;
}
String[] toArray() {
return list.toArray(new String[0]);
}
}
int run(String tool, String... args) {
printCommandDetails("run", tool, args);
return ToolProvider.findFirst(tool).get().run(System.out, System.err, args);
}
int exe(String executable, String... args) throws Exception {
printCommandDetails("exe", executable, args);
ProcessBuilder processBuilder = new ProcessBuilder(executable);
Arrays.stream(args).forEach(processBuilder.command()::add);
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
process.getInputStream().transferTo(System.out);
return process.waitFor();
}
void printCommandDetails(String context, String command, String... args) {
if (args.length < 2) {
System.out.printf("[%s] %s%s%n", context, command, (args.length == 1 ? " " + args[0] : ""));
System.out.println();
return;
}
System.out.printf("[%s] %s with %d arguments%n", context, command, args.length);
boolean simple = true;
int indent = 8;
for (String arg : args) {
indent = simple ? 8 : arg.startsWith("-") ? 8 : 10;
simple = !arg.startsWith("-");
if (arg.length() > 100) {
if (arg.contains(File.pathSeparator)) {
for (String path : arg.split(File.pathSeparator)) {
System.out.printf("%-10s%s%n", "", path);
}
continue;
}
arg = arg.substring(0, 96) + "[...]";
}
System.out.printf("%-" + indent + "s%s%n", "", arg);
}
System.out.println();
}
void del(String directory) throws Exception {
System.out.printf("[del] %s%n", directory);
Path root = Paths.get(directory);
if (Files.notExists(root)) {
return;
}
try (Stream<Path> stream = Files.walk(root)) {
Stream<Path> selected = stream.sorted((p, q) -> -p.compareTo(q));
for (Path path : selected.collect(Collectors.toList())) {
Files.deleteIfExists(path);
}
}
}
Path get(String directory, URI uri) throws Exception {
String uriPath = uri.getPath();
int begin = uriPath.lastIndexOf('/') + 1;
String file = uriPath.substring(begin).split("\\?")[0].split("#")[0];
Path folder = Paths.get(directory);
Path target = folder.resolve(file);
if (Files.exists(target)) {
return target;
}
System.out.printf("[get] %s%n", target);
Files.createDirectories(folder);
try (InputStream sourceStream = uri.toURL().openStream(); OutputStream targetStream = Files.newOutputStream(target)) {
sourceStream.transferTo(targetStream);
}
return target;
}
Path get(String directory, String group, String artifact, String version) throws Exception {
String repo = "http://central.maven.org/maven2";
String file = artifact + "-" + version + ".jar";
URI uri = URI.create(String.join("/", repo, group.replace('.', '/'), artifact, version, file));
return get(directory, uri);
}