Skip to content
Snippets Groups Projects
Commit 223f18ed authored by Florian Angerer's avatar Florian Angerer
Browse files

Implemented option '--outDir'.

parent db621357
No related branches found
No related tags found
No related merge requests found
......@@ -68,17 +68,33 @@ public class PTAMain {
OptionsParser parser = new OptionsParser();
String[] remainingArgs = parser.parseOptions(args);
if (remainingArgs.length != 1) {
System.err.println("Unknown arguments: " + Arrays.toString(remainingArgs));
printHelp();
System.exit(1);
}
ftw(Paths.get(remainingArgs[0]), parser.get("--glob", "*"));
Path outDir = Paths.get(parser.get("--outDir", "html"));
ftw(Paths.get(remainingArgs[0]), outDir, parser.get("--glob", "*"));
}
private static final String LF = System.lineSeparator();
private static void ftw(Path root, String glob) throws IOException {
private static void ftw(Path root, Path outDir, String glob) throws IOException {
// TODO FS checking
HtmlDumper htmlDumper = new HtmlDumper(outDir);
// fail early
try {
if (!htmlDumper.createAndCheckOutDir()) {
LOGGER.severe("Cannot write to output directory: " + outDir);
System.exit(1);
}
} catch (IOException e) {
LOGGER.severe(String.format("Cannot create output directory: %s ", e.getMessage()));
System.exit(1);
}
try (DirectoryStream<Path> stream = Files.newDirectoryStream(root, glob)) {
Collection<RPackage> pkgs = new LinkedList<>();
for (Path p : stream) {
......@@ -88,7 +104,7 @@ public class PTAMain {
}
}
Collection<Problem> allProblems = collectAllProblems(pkgs);
new HtmlDumper(Paths.get("html")).dump(allProblems);
htmlDumper.dump(allProblems);
}
}
......
......@@ -47,13 +47,21 @@ public class HtmlDumper extends AbstractDumper {
this.destDir = Objects.requireNonNull(destDir);
}
/**
* Creates the output directory if it does not exists and checks if the directory is writable.
* This method may throw an {@link IOException} if it cannot create the directory.
*/
public boolean createAndCheckOutDir() throws IOException {
if (!Files.exists(destDir)) {
Files.createDirectories(destDir);
}
return Files.isWritable(destDir);
}
@Override
public void dump(Collection<Problem> problems) {
try {
if (!Files.exists(destDir)) {
Files.createDirectory(destDir);
}
createAndCheckOutDir();
dumpIndexFile(problems);
} catch (IOException e) {
e.printStackTrace();
......@@ -63,8 +71,7 @@ public class HtmlDumper extends AbstractDumper {
private void dumpIndexFile(Collection<Problem> problems) {
Path indexFile = destDir.resolve("index.html");
try (BufferedWriter bw = Files.newBufferedWriter(indexFile, StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
try (BufferedWriter bw = Files.newBufferedWriter(indexFile, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
PrintWriter writer = new PrintWriter(bw);
writer.println(
......
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