56 lines
1.8 KiB
Java
56 lines
1.8 KiB
Java
import java.io.*;
|
|
import java.nio.file.*;
|
|
import net.sourceforge.pmd.*;
|
|
import net.sourceforge.pmd.renderers.*;
|
|
|
|
public class PmdRunner {
|
|
public static void main(String[] args) throws Exception {
|
|
if (args.length < 2) {
|
|
System.err.println("Usage: PmdRunner <filePath|- for stdin> <rulesetPath> [extension]");
|
|
System.exit(1);
|
|
return;
|
|
}
|
|
|
|
String filePath = args[0];
|
|
String rulesetPath = args[1];
|
|
String extension = args.length >= 3 ? args[2] : "java";
|
|
|
|
Path tempFile = null;
|
|
if ("-".equals(filePath)) {
|
|
String code = new String(System.in.readAllBytes());
|
|
tempFile = Files.createTempFile("pmd-stdin-", "." + extension);
|
|
Files.writeString(tempFile, code);
|
|
filePath = tempFile.toString();
|
|
}
|
|
|
|
try {
|
|
PMDConfiguration config = new PMDConfiguration();
|
|
config.addRuleSet(rulesetPath);
|
|
|
|
String auxcp = System.getenv("PMD_AUXCP");
|
|
if (auxcp == null || auxcp.isEmpty()) {
|
|
auxcp = System.getenv("PMD_AUX_CLASSPATH");
|
|
}
|
|
if (auxcp != null && !auxcp.isEmpty()) {
|
|
config.prependAuxClasspath(auxcp);
|
|
}
|
|
|
|
Writer writer = new StringWriter();
|
|
JsonRenderer renderer = new JsonRenderer();
|
|
renderer.setWriter(writer);
|
|
|
|
try (PmdAnalysis pmd = PmdAnalysis.create(config)) {
|
|
pmd.files().addFile(Path.of(filePath));
|
|
pmd.addRenderer(renderer);
|
|
pmd.performAnalysis();
|
|
}
|
|
|
|
System.out.print(writer.toString());
|
|
} finally {
|
|
if (tempFile != null) {
|
|
Files.deleteIfExists(tempFile);
|
|
}
|
|
}
|
|
}
|
|
}
|