From 25257054258ef1bd0f56e2bef328e6c43de098eb Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Wed, 29 Jan 2025 20:39:15 +0100 Subject: [PATCH] Added automatic mode selection --- cli/src/main/java/de/jplag/cli/CLI.java | 29 +++++++++++++++++++ .../java/de/jplag/cli/options/CliOptions.java | 5 ++-- .../java/de/jplag/cli/options/JPlagMode.java | 6 +++- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/cli/src/main/java/de/jplag/cli/CLI.java b/cli/src/main/java/de/jplag/cli/CLI.java index 263a9021ed..a0b462f372 100644 --- a/cli/src/main/java/de/jplag/cli/CLI.java +++ b/cli/src/main/java/de/jplag/cli/CLI.java @@ -3,6 +3,8 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import org.slf4j.ILoggerFactory; import org.slf4j.Logger; @@ -32,6 +34,8 @@ public final class CLI { private static final String OUTPUT_FILE_EXISTS = "The output file (also with suffixes e.g. results(1).zip) already exists. You can use --overwrite to overwrite the file."; private static final String OUTPUT_FILE_NOT_WRITABLE = "The output file (%s) cannot be written to."; + private static final String ZIP_FILE_ENDING = ".zip"; + private final CliInputHandler inputHandler; /** @@ -59,6 +63,7 @@ public void executeCli() throws ExitException, IOException { case RUN -> runJPlag(); case VIEW -> runViewer(null); case RUN_AND_VIEW -> runViewer(runJPlag()); + case AUTO -> selectModeAutomatically(); } } } @@ -115,6 +120,30 @@ public void runViewer(File zipFile) throws IOException { JPlagRunner.runInternalServer(zipFile, this.inputHandler.getCliOptions().advanced.port); } + private void selectModeAutomatically() throws IOException, ExitException { + List inputs = this.getAllInputs(); + + if (inputs.isEmpty()) { + this.runViewer(null); + return; + } + + if (inputs.size() == 1 && inputs.getFirst().getName().endsWith(ZIP_FILE_ENDING)) { + this.runViewer(inputs.getFirst()); + return; + } + + this.runViewer(this.runJPlag()); + } + + private List getAllInputs() { + List inputs = new ArrayList<>(); + inputs.addAll(List.of(this.inputHandler.getCliOptions().newDirectories)); + inputs.addAll(List.of(this.inputHandler.getCliOptions().oldDirectories)); + inputs.addAll(List.of(this.inputHandler.getCliOptions().rootDirectory)); + return inputs; + } + private void finalizeLogger() { ILoggerFactory factory = LoggerFactory.getILoggerFactory(); if (!(factory instanceof CollectedLoggerFactory collectedLoggerFactory)) { diff --git a/cli/src/main/java/de/jplag/cli/options/CliOptions.java b/cli/src/main/java/de/jplag/cli/options/CliOptions.java index 748a8f6c8b..7a363cd7df 100644 --- a/cli/src/main/java/de/jplag/cli/options/CliOptions.java +++ b/cli/src/main/java/de/jplag/cli/options/CliOptions.java @@ -53,8 +53,9 @@ public class CliOptions implements Runnable { "--result-file"}, description = "Name of the file in which the comparison results will be stored (default: ${DEFAULT-VALUE}). Missing .zip endings will be automatically added.") public String resultFile = "results"; - @Option(names = {"-M", "--mode"}, description = "The mode of JPlag. One of: ${COMPLETION-CANDIDATES} (default: ${DEFAULT_VALUE})") - public JPlagMode mode = JPlagMode.RUN_AND_VIEW; + @Option(names = {"-M", + "--mode"}, description = "The mode of JPlag. By default JPlag will automatically select the mode based on your input files. If none are selected the viewer will open on the file select screen. If a single result zip is selected it will be opened in the viewer directly. Otherwise JPlag will run on the submissions in the input files and show the result in the viewer. One of: ${COMPLETION-CANDIDATES} (default: ${DEFAULT_VALUE})") + public JPlagMode mode = JPlagMode.AUTO; @Option(names = {"--normalize"}, description = "Activate the normalization of tokens. Supported for languages: Java, C++.") public boolean normalize = false; diff --git a/cli/src/main/java/de/jplag/cli/options/JPlagMode.java b/cli/src/main/java/de/jplag/cli/options/JPlagMode.java index 8d1607d46b..c9fe1f9918 100644 --- a/cli/src/main/java/de/jplag/cli/options/JPlagMode.java +++ b/cli/src/main/java/de/jplag/cli/options/JPlagMode.java @@ -15,5 +15,9 @@ public enum JPlagMode { /** * Run JPlag and open the result in report viewer */ - RUN_AND_VIEW + RUN_AND_VIEW, + /** + * Choose the mode automatically from the given input files + */ + AUTO, }