Newer
Older
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
package support;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Consumer;
import java.util.stream.Stream;
/**
* Process all files test/solutions/*.java into
* corresponding files in test/exercises/*.java.
*/
public class Grind {
static class Sink implements Consumer<String> {
boolean removing = false;
final PrintStream out;
Sink(PrintStream out) { this.out = out; }
public void accept(String line) {
if (line.contains("//BEGINREMOVE")) {
removing = true;
} else if (line.contains("//ENDREMOVE")) {
removing = false;
} else if (!removing) {
out.println(line);
}
}
}
String subst(String line) {
return line.replace("package solutions;", "package exercises;")
.replace("@Test", "@Test @Ignore")
.replace("//UNCOMMENT//", "");
}
void processFile(Path input) {
Path output = Paths.get("test", "exercises")
.resolve(input.getName(input.getNameCount() - 1));
System.out.println(input + " => " + output);
try (Stream<String> lines = Files.lines(input);
PrintStream out = new PrintStream(output.toFile())) {
lines.map(this::subst)
.forEachOrdered(new Sink(out));
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
}
void run() throws IOException {
Path dir = Paths.get("test", "solutions");
try (Stream<Path> paths = Files.list(dir)) {
paths.filter(p -> p.toString().endsWith(".java"))
.forEachOrdered(this::processFile);
}
}
public static void main(String[] args) throws IOException {
new Grind().run();
}
}