Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added automatic mode selection #2168

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
TwoOfTwelve marked this conversation as resolved.
Show resolved Hide resolved
List<File> inputs = this.getAllInputs();

if (inputs.isEmpty()) {
this.runViewer(null);
return;
}

if (inputs.size() == 1 && inputs.getFirst().getName().endsWith(ZIP_FILE_ENDING)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a small comment what happens here, why we give an input file to the that is normally a submission to the viewer

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));
TwoOfTwelve marked this conversation as resolved.
Show resolved Hide resolved
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 the given input files. If none are given the report viewer will open on the file upload page. If a single result zip is given it will be opened in the report viewer directly. Otherwise, JPlag will check the given submissions and show the result in the report 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,
}
Loading