Skip to content

Commit

Permalink
Merge branch 'develop' into feature/refactorRLang
Browse files Browse the repository at this point in the history
# Conflicts:
#	languages/rlang/src/main/java/de/jplag/rlang/RLanguage.java
#	languages/rlang/src/main/java/de/jplag/rlang/RParserAdapter.java
  • Loading branch information
TwoOfTwelve committed Oct 29, 2024
2 parents 1be60dc + 69cc90d commit a84ac1e
Show file tree
Hide file tree
Showing 156 changed files with 3,656 additions and 2,381 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/complete-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
node-version: "18"

- name: Build Assembly
run: mvn -Pwith-report-viewer -DskipTests clean package assembly:single
run: mvn -DskipTests clean package assembly:single

- name: Rename Jar
run: mv cli/target/jplag-*-jar-with-dependencies.jar cli/target/jplag.jar
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
- "**/pom.xml"
- "**.java"
- "**.g4"
- "report-viewer/**"
pull_request:
types: [opened, synchronize, reopened]
paths:
Expand Down
13 changes: 13 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ jobs:
with:
node-version: "18"

- name: Set version of Report Viewer
shell: bash
run: |
VERSION=$(grep "<revision>" pom.xml | grep -oPm1 "(?<=<revision>)[^-|<]+")
MAJOR=$(echo $VERSION | cut -d '.' -f 1)
MINOR=$(echo $VERSION | cut -d '.' -f 2)
PATCH=$(echo $VERSION | cut -d '.' -f 3)
json=$(cat report-viewer/src/version.json)
json=$(echo "$json" | jq --arg MAJOR "$MAJOR" --arg MINOR "$MINOR" --arg PATCH "$PATCH" '.report_viewer_version |= { "major": $MAJOR | tonumber, "minor": $MINOR | tonumber, "patch": $PATCH | tonumber }')
echo "$json" > report-viewer/src/version.json
echo "Version of Report Viewer:"
cat report-viewer/src/version.json
- name: Build JPlag
run: mvn -Pwith-report-viewer -U -B clean package assembly:single

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/report-viewer-demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ jobs:
npm run build-demo
- name: Deploy 🚀
uses: JamesIves/[email protected].1
uses: JamesIves/[email protected].8
with:
branch: gh-pages
folder: report-viewer/dist
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/report-viewer-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
npm run build-dev
- name: Deploy 🚀
uses: JamesIves/[email protected].1
uses: JamesIves/[email protected].8
with:
branch: gh-pages
folder: report-viewer/dist
Expand Down
43 changes: 0 additions & 43 deletions .github/workflows/report-viewer.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Subsequence Match Merging
--neighbor-length=<minimumNeighborLength>
Minimal length of neighboring matches to be merged (between 1 and minTokenMatch, default: 2).
Subcommands (supported languages):
Languages:
c
cpp
csharp
Expand Down
2 changes: 1 addition & 1 deletion cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.3.0</version>
<version>3.5.0</version>
<executions>
<execution>
<id>npm install</id>
Expand Down
2 changes: 2 additions & 0 deletions cli/src/main/java/de/jplag/cli/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public CLI(String[] args) {
*/
public void executeCli() throws ExitException, IOException {
logger.debug("Your version of JPlag is {}", JPlag.JPLAG_VERSION);
JPlagVersionChecker.printVersionNotification();

if (!this.inputHandler.parse()) {
CollectedLogger.setLogLevel(this.inputHandler.getCliOptions().advanced.logLevel);
Expand Down Expand Up @@ -110,6 +111,7 @@ public File runJPlag() throws ExitException, FileNotFoundException {
* @throws IOException If something went wrong with the internal server
*/
public void runViewer(File zipFile) throws IOException {
finalizeLogger(); // Prints the errors. The later finalizeLogger will print any errors logged after this point.
JPlagRunner.runInternalServer(zipFile, this.inputHandler.getCliOptions().advanced.port);
}

Expand Down
6 changes: 5 additions & 1 deletion cli/src/main/java/de/jplag/cli/JPlagRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public static void runInternalServer(File zipFile, int port) throws IOException
ReportViewer reportViewer = new ReportViewer(zipFile, port);
int actualPort = reportViewer.start();
logger.info("ReportViewer started on port http://localhost:{}", actualPort);
Desktop.getDesktop().browse(URI.create("http://localhost:" + actualPort + "/"));
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(URI.create("http://localhost:" + actualPort + "/"));
} else {
logger.info("Could not open browser. You can open the Report Viewer here: http://localhost:{}/", actualPort);
}

System.out.println("Press Enter key to exit...");
System.in.read();
Expand Down
91 changes: 91 additions & 0 deletions cli/src/main/java/de/jplag/cli/JPlagVersionChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package de.jplag.cli;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Optional;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.jplag.JPlag;
import de.jplag.reporting.reportobject.model.Version;

/**
* Handles the check for newer versions.
*/
public class JPlagVersionChecker {
private static final String API_URL = "https://api.github.com/repos/jplag/JPlag/releases";
private static final Logger logger = LoggerFactory.getLogger(JPlagVersionChecker.class);
private static final String EXPECTED_VERSION_FORMAT = "v\\d\\.\\d\\.\\d+";
private static final String WARNING_UNABLE_TO_FETCH = "Unable to fetch version information. New version notification will not work.";
private static final String NEWER_VERSION_AVAILABLE = "There is a newer version ({}) available. You can download the newest version here: https://github.com/jplag/JPlag/releases";
private static final String UNEXPECTED_ERROR = "There was an unexpected error, when checking for new versions. Please report this on: https://github.com/jplag/JPlag/issues";

private JPlagVersionChecker() {

}

/**
* Prints a warning if a newer version is available on GitHub.
*/
public static void printVersionNotification() {
Optional<Version> newerVersion = checkForNewVersion();
newerVersion.ifPresent(version -> logger.warn(NEWER_VERSION_AVAILABLE, version));
}

private static Optional<Version> checkForNewVersion() {
try {
JsonArray array = fetchApi();
Version newest = getNewestVersion(array);
Version current = JPlag.JPLAG_VERSION;

if (newest.compareTo(current) > 0) {
return Optional.of(newest);
}
} catch (IOException | URISyntaxException e) {
logger.info(WARNING_UNABLE_TO_FETCH);
} catch (Exception e) {
logger.warn(UNEXPECTED_ERROR, e);
}

return Optional.empty();
}

private static JsonArray fetchApi() throws IOException, URISyntaxException {
URL url = new URI(API_URL).toURL();
URLConnection connection = url.openConnection();

try (JsonReader reader = Json.createReader(connection.getInputStream())) {
return reader.readArray();
}
}

private static Version getNewestVersion(JsonArray apiResult) {
return apiResult.stream().map(JsonObject.class::cast).map(version -> version.getString("name"))
.filter(versionName -> versionName.matches(EXPECTED_VERSION_FORMAT)).limit(1).map(JPlagVersionChecker::parseVersion).findFirst()
.orElse(JPlag.JPLAG_VERSION);
}

/**
* Parses the version name.
* @param versionName The version name. The expected format is: v[major].[minor].[patch]
* @return The parsed version
*/
private static Version parseVersion(String versionName) {
String withoutPrefix = versionName.substring(1);
String[] parts = withoutPrefix.split("\\.");
return parseVersionParts(parts);
}

private static Version parseVersionParts(String[] parts) {
return new Version(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
}
}
14 changes: 10 additions & 4 deletions cli/src/main/java/de/jplag/cli/logger/CollectedLogger.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.jplag.cli.logger;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand All @@ -19,7 +20,6 @@ public class CollectedLogger extends AbstractLogger {
private static final String JPLAG_LOGGER_PREFIX = "de.jplag.";
private static final Level LOG_LEVEL_FOR_EXTERNAL_LIBRARIES = Level.ERROR;
private static final int MAXIMUM_MESSAGE_LENGTH = 32;
private static final PrintStream TARGET_STREAM = System.out;
private static Level currentLogLevel = Level.INFO;

private final transient SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss_SSS");
Expand Down Expand Up @@ -148,11 +148,10 @@ private StringBuilder prepareLogOutput(LogEntry entry) {

private void printLogEntry(LogEntry entry) {
StringBuilder output = prepareLogOutput(entry);
TARGET_STREAM.println(output);
DelayablePrinter.getInstance().println(output.toString());
if (entry.cause() != null) {
entry.cause().printStackTrace(TARGET_STREAM);
this.printStackTrace(entry.cause());
}
TARGET_STREAM.flush();
}

public static Level getLogLevel() {
Expand All @@ -162,4 +161,11 @@ public static Level getLogLevel() {
public static void setLogLevel(Level logLevel) {
currentLogLevel = logLevel;
}

private void printStackTrace(Throwable error) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
error.printStackTrace(new PrintStream(outputStream));
String stackTrace = outputStream.toString();
DelayablePrinter.getInstance().println(stackTrace);
}
}
73 changes: 73 additions & 0 deletions cli/src/main/java/de/jplag/cli/logger/DelayablePrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package de.jplag.cli.logger;

import java.io.PrintStream;
import java.util.PriorityQueue;
import java.util.Queue;

/**
* Prints strings to stdout. Provides the option to delay the actual printing.
*/
public class DelayablePrinter {
private final Queue<String> outputQueue;
private PrintStream targetStream;

private boolean isDelayed;

private static final class InstanceHolder {
private static final DelayablePrinter instance = new DelayablePrinter();
}

/**
* Threadsafe singleton getter
* @return The singleton instance
*/
public static DelayablePrinter getInstance() {
return InstanceHolder.instance;
}

private DelayablePrinter() {
this.outputQueue = new PriorityQueue<>();
this.targetStream = System.out;
this.isDelayed = false;
}

/**
* Prints the given string to the terminal appending a line-break
* @param output The string to print
*/
public synchronized void println(String output) {
this.outputQueue.offer(output);
this.printQueue();
}

/**
* Stops printing to the terminal until {@link #resume()} is called
*/
public synchronized void delay() {
this.isDelayed = true;
}

/**
* Resumes printing if {@link #delay()} was called
*/
public synchronized void resume() {
this.isDelayed = false;
this.printQueue();
}

/**
* Changes the output stream messages are written to
*/
public void setOutputStream(PrintStream printStream) {
this.targetStream = printStream;
}

private synchronized void printQueue() {
if (!this.isDelayed) {
while (!this.outputQueue.isEmpty()) {
this.targetStream.println(this.outputQueue.poll());
}
this.targetStream.flush();
}
}
}
6 changes: 3 additions & 3 deletions cli/src/main/java/de/jplag/cli/logger/IdleBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;

import de.jplag.logging.ProgressBar;

/**
* Prints an idle progress bar, that does not count upwards.
*/
public class IdleBar implements ProgressBar {
public class IdleBar extends LogDelayingProgressBar {
private final PrintStream output;

private final Thread runner;
Expand All @@ -27,6 +25,7 @@ public class IdleBar implements ProgressBar {
private boolean running = false;

public IdleBar(String text) {
super();
this.output = System.out;
this.runner = new Thread(this::run);
this.length = 50;
Expand Down Expand Up @@ -61,6 +60,7 @@ public void dispose() {
}
this.output.print('\r');
this.output.println(this.text + ": complete");
super.dispose();
}

private void run() {
Expand Down
Loading

0 comments on commit a84ac1e

Please sign in to comment.