Skip to content

Commit

Permalink
Add method to find all exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
jamo committed Nov 11, 2015
1 parent 60ad888 commit f7f611d
Showing 1 changed file with 76 additions and 14 deletions.
90 changes: 76 additions & 14 deletions tmc-langs-cli/src/main/java/fi/helsinki/cs/tmc/langs/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public final class Main {

Expand All @@ -33,16 +39,18 @@ public final class Main {
private static final String OUTPUT_PATH = "outputPath";

private static final Map<String, Integer> COMMAND_ARGUMENT_COUNTS =
ImmutableMap.of(
"checkstyle", 2,
"scan-exercise", 2,
"run-tests", 2,
"prepare-stub", 1,
"prepare-solution", 1);
new ImmutableMap.Builder<String, Integer>()
.put("checkstyle", 2)
.put("scan-exercises", 2)
.put("run-tests", 2)
.put("prepare-stub", 1)
.put("prepare-solution", 1)
.put("find-exercises", 2)
.build();

@VisibleForTesting
static final String HELP_TEXT =
" Usage: Main <command> [<command-arguments>] \n\n"
" Usage: Main <command> [<command-arguments>] \n\n"
+ " Commands:\n"
+ " checkstyle <exercise path> <output path>"
+ " Run checkstyle or similar plugin to project if applicable.\n"
Expand All @@ -54,14 +62,16 @@ public final class Main {
+ " Prepare a stub exercise from the original.\n"
+ " run-tests <exercise path> <output path>"
+ " Run the tests for the exercise.\n"
+ " scan-exercise <exercise path> <output path>"
+ " Produce an exercise description of an exercise directory.";

+ " scan-exercises <exercise path> <output path>"
+ " Produce an exercise description of an exercise directory."
+ " find-exercises <scan path> <output path>"
+ " Produce list of found exercises.";

/**
* Main entry point for the CLI.
*/
public static void main(String[] args) {
System.out.println(Arrays.deepToString(args));
if (args == null || args.length == 0) {
printHelpAndExit();
}
Expand Down Expand Up @@ -115,8 +125,11 @@ private static void run(String[] args) {
case "checkstyle":
runCheckCodeStyle(paths);
break;
case "scan-exercise":
runScanExercise(paths);
case "scan-exercises":
runScanExercises(paths);
break;
case "find-exercises":
runFindExercises(paths);
break;
case "run-tests":
runTests(paths);
Expand Down Expand Up @@ -156,8 +169,8 @@ private static void runCheckCodeStyle(Map<String, Path> paths) {
}
}

private static void runScanExercise(Map<String, Path> paths) {
// Exercise name, should it be something else than directory name?
private static void runScanExercises(Map<String, Path> paths) {
System.out.println(paths);
String exerciseName = paths.get(EXERCISE_PATH).toFile().getName();
Optional<ExerciseDesc> exerciseDesc = Optional.absent();
try {
Expand Down Expand Up @@ -186,6 +199,55 @@ private static void runScanExercise(Map<String, Path> paths) {
}
}

private static void runFindExercises(Map<String, Path> paths) {
Path clonePath = paths.get(EXERCISE_PATH);
final Set<String> exercises = new HashSet<String>();
try {
Files.walkFileTree(
clonePath,
new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(
Path dir, BasicFileAttributes attrs) throws IOException {
if (executor.isExerciseRootDirectory(dir)) {
exercises.add(dir.toAbsolutePath().toString());
}
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc)
throws IOException {
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
throws IOException {
return FileVisitResult.CONTINUE;
}
});

} catch (IOException e) {
System.out.println(e);
System.exit(1);
}

try {
JsonWriter.writeObjectIntoJsonFormat(exercises, paths.get(OUTPUT_PATH));
System.out.println("Results can be found in " + paths.get(OUTPUT_PATH));
} catch (IOException e) {
logger.error("Could not write output to {}", paths.get(OUTPUT_PATH), e);
printErrAndExit("ERROR: Could not write the results to the given file.");
}
}

private static void runTests(Map<String, Path> paths) {
RunResult runResult = null;
try {
Expand Down

0 comments on commit f7f611d

Please sign in to comment.