Skip to content

Commit

Permalink
Add support for PMD 7 (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
adangel authored Jan 26, 2024
1 parent de2e9b1 commit ec33690
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 37 deletions.
6 changes: 5 additions & 1 deletion ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# pmd-bluej Release Notes

## ??-?????-?? - 5.1.0
## ??-?????-?? - 5.0.1

* Support PMD 7 (in addition to PMD 6)
* Changed default PMD options to `-f text -R rulesets/java/quickstart.xml`. Note: You might need to adjust this,
if migrating from PMD 6 to PMD 7. The new default options work with both versions.
* Fixed [#7: PMD distribution changed the name of the entry point script from run.sh to pmd](https://github.com/pmd/pmd-bluej/issues/7)

## 25-February-2021 - 5.0.0

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-bluej</artifactId>
<version>5.1.0-SNAPSHOT</version>
<version>5.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>pmd-bluej</name>
<url>http://pmd.sourceforge.net</url>
Expand Down
86 changes: 57 additions & 29 deletions src/main/java/net/sourceforge/pmd/MenuBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
*/
package net.sourceforge.pmd;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;

import bluej.extensions2.BClass;
import bluej.extensions2.MenuGenerator;
Expand All @@ -15,11 +17,11 @@
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;

public class MenuBuilder extends MenuGenerator {

Expand Down Expand Up @@ -71,13 +73,8 @@ public void handle(ActionEvent event) {
}

try {
String mycommand = preferences.getPMDPath() + "/bin/run.sh pmd " + preferences.getPMDOptions() + " -d " + javaFileName;

if (SystemUtils.isWindows()) {
mycommand = preferences.getPMDPath() + "\\bin\\pmd.bat " + preferences.getPMDOptions() + " -d " + javaFileName;
}

String output = runPMD(mycommand);
String command = determinePmdCommandLine();
String output = runPMD(command);

StringBuilder msg = new StringBuilder(1000);
msg.append("PMD run finished.").append(System.lineSeparator()).append(System.lineSeparator());
Expand All @@ -91,10 +88,34 @@ public void handle(ActionEvent event) {
alert.showAndWait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}

private String determinePmdCommandLine() {
boolean isPMD7 = new File(preferences.getPMDPath(), "bin/pmd").exists();

String mycommand = preferences.getPMDPath();

if (SystemUtils.isWindows()) {
if (isPMD7) {
mycommand += "\\bin\\pmd.bat check";
} else {
mycommand += "\\bin\\pmd.bat";
}
} else {
// linux/macos
if (isPMD7) {
mycommand += "/bin/pmd check";
} else {
mycommand += "/bin/run.sh pmd";
}
}

// always add the options/flags
mycommand += " " + preferences.getPMDOptions() + " -d " + javaFileName;
return mycommand;
}

private void showResults(StringBuilder msg) {
Dialog<ButtonType> resultDialog = new Dialog<>();
BorderPane dialogContent = new BorderPane();
Expand All @@ -112,28 +133,35 @@ private String runPMD(String mycommand) throws IOException, InterruptedException
pb.redirectErrorStream(false);
final Process p = pb.start();

final StringBuilder output = new StringBuilder();
Thread reader = new Thread(new Runnable() {
public void run() {
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s;
try {
while ((s = stdInput.readLine()) != null ){
output.append(s);
output.append(System.lineSeparator());
}
StringWriter stdout = new StringWriter();
new Thread(() -> {
try (Reader reader = new InputStreamReader(p.getInputStream())) {
reader.transferTo(stdout);
} catch (IOException e) {
output.append(e.toString());
e.printStackTrace();
} finally {
try { stdInput.close(); } catch (IOException e) { /* quiet */ }
throw new RuntimeException(e);
}
}).start();
StringWriter stderr = new StringWriter();
new Thread(() -> {
try (Reader reader = new InputStreamReader(p.getErrorStream())) {
reader.transferTo(stderr);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
reader.setDaemon(true);
reader.start();
p.waitFor();
return output.toString();

}).start();

int exitCode = p.waitFor();

if (exitCode == 1 || exitCode == 2) {
return "Executed command: " + pb.command() + System.lineSeparator()
+ "PMD Exited with: " + exitCode + System.lineSeparator() + stderr;
}
if (exitCode == 0) {
return "No problems found";
}

return stdout.toString();
}
}
}
16 changes: 10 additions & 6 deletions src/main/java/net/sourceforge/pmd/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package net.sourceforge.pmd;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import bluej.extensions2.BlueJ;
import bluej.extensions2.PreferenceGenerator;
Expand All @@ -27,7 +29,7 @@ public class Preferences implements PreferenceGenerator {
private BlueJ bluej;
public static final String PROPERTY_PMD_PATH = "PMD.Path";
public static final String PROPERTY_PMD_OPTIONS = "PMD.Options";
private static final String PMD_OPTIONS_DEFAULT = "-format text -R java-basic,java-design -language java";
private static final String PMD_OPTIONS_DEFAULT = "-f text -R rulesets/java/quickstart.xml";

public Preferences(BlueJ bluej) {
this.bluej = bluej;
Expand Down Expand Up @@ -70,7 +72,7 @@ private void renderPane() {
pmdPath.setText(selectedDirectory.getAbsolutePath());
} else {
Alert alert = new Alert(AlertType.ERROR, "The selected path " + selectedDirectory + " doesn't seem to be"
+ " a PMD installation. E.g. the file bin/pmd.bat or bin/run.sh is missing.");
+ " a PMD installation. E.g. the file bin/pmd.bat or bin/pmd (or bin/run.sh) is missing.");
alert.showAndWait();
}
}
Expand All @@ -80,13 +82,15 @@ private void renderPane() {
}

private boolean verifyPMDPath(File selectedFile) {
File pathToExecutable;
List<File> executables = new ArrayList<>();

if (SystemUtils.isWindows()) {
pathToExecutable = new File(selectedFile, "bin/pmd.bat");
executables.add(new File(selectedFile, "bin/pmd.bat"));
} else {
pathToExecutable = new File(selectedFile, "bin/run.sh");
executables.add(new File(selectedFile, "bin/run.sh")); // PMD 5, PMD 6
executables.add(new File(selectedFile, "bin/pmd")); // PMD 7
}
return pathToExecutable.exists();
return executables.stream().anyMatch(File::exists);
}

@Override
Expand Down

0 comments on commit ec33690

Please sign in to comment.