45 lines
1.3 KiB
Java
45 lines
1.3 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>");
|
|
System.exit(1);
|
|
return;
|
|
}
|
|
|
|
String filePath = args[0];
|
|
String rulesetPath = args[1];
|
|
|
|
Path tempFile = null;
|
|
if ("-".equals(filePath)) {
|
|
String code = new String(System.in.readAllBytes());
|
|
tempFile = Files.createTempFile("pmd-stdin-", ".java");
|
|
Files.writeString(tempFile, code);
|
|
filePath = tempFile.toString();
|
|
}
|
|
|
|
try {
|
|
PMDConfiguration config = new PMDConfiguration();
|
|
config.setInputFilePath(Path.of(filePath));
|
|
config.addRuleSet(Path.of(rulesetPath));
|
|
config.setReportFormat("json");
|
|
|
|
StringWriter writer = new StringWriter();
|
|
config.setReportWriter(writer);
|
|
|
|
PmdAnalysis pmd = PmdAnalysis.create(config);
|
|
pmd.performAnalysis();
|
|
|
|
System.out.print(writer.toString());
|
|
} finally {
|
|
if (tempFile != null) {
|
|
Files.deleteIfExists(tempFile);
|
|
}
|
|
}
|
|
}
|
|
}
|