Skip to content

Commit

Permalink
Added automatic mode selection
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoOfTwelve committed Jan 29, 2025
1 parent 56197a8 commit 2525705
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
29 changes: 29 additions & 0 deletions cli/src/main/java/de/jplag/cli/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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();
}
}
}
Expand Down Expand Up @@ -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<File> 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<File> getAllInputs() {
List<File> 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)) {
Expand Down
5 changes: 3 additions & 2 deletions cli/src/main/java/de/jplag/cli/options/CliOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion cli/src/main/java/de/jplag/cli/options/JPlagMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

0 comments on commit 2525705

Please sign in to comment.