Skip to content
Snippets Groups Projects
Commit d748deb7 authored by Stuart Marks's avatar Stuart Marks
Browse files

Rewrite source processor script into Java.

parent 305b3e63
No related branches found
No related tags found
No related merge requests found
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();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment