Add option for PMD command line for a comma delimited list of files

This commit is contained in:
mrb
2016-06-09 13:57:03 -04:00
parent 3b2b813b44
commit c2b8e1872d
3 changed files with 91 additions and 49 deletions

View File

@ -3,7 +3,9 @@
*/
package net.sourceforge.pmd;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
@ -85,7 +87,7 @@ public class PMD {
/**
* Create a PMD instance using the specified Configuration.
*
*
* @param configuration
* The runtime Configuration of PMD to use.
*/
@ -158,7 +160,7 @@ public class PMD {
/**
* Create a report, filter out any defective rules, and keep a record of
* them.
*
*
* @param rs the rules
* @param ctx the rule context
* @param fileName the filename of the source file, which should appear in the report
@ -179,7 +181,7 @@ public class PMD {
/**
* Remove and return the misconfigured rules from the rulesets and log them
* for good measure.
*
*
* @param ruleSets
* RuleSets
* @return Set<Rule>
@ -202,7 +204,7 @@ public class PMD {
/**
* Get the runtime configuration. The configuration can be modified to
* affect how PMD behaves.
*
*
* @return The configuration.
* @see PMDConfiguration
*/
@ -220,7 +222,7 @@ public class PMD {
/**
* This method is the main entry point for command line usage.
*
*
* @param configuration the configure to use
* @return number of violations found.
*/
@ -299,14 +301,14 @@ public class PMD {
/**
* A callback that would be implemented by IDEs keeping track of PMD's
* progress as it evaluates a set of files.
*
*
* @author Brian Remedios
*/
public interface ProgressMonitor {
/**
* A status update reporting on current progress. Implementers will
* return true if it is to continue, false otherwise.
*
*
* @param total total number of files to be analyzed
* @param totalDone number of files, that have been done analyzing.
* @return <code>true</code> if the execution of PMD should continue, <code>false</code> if the execution
@ -318,7 +320,7 @@ public class PMD {
/**
* An entry point that would typically be used by IDEs intent on providing
* ongoing feedback and the ability to terminate it at will.
*
*
* @param configuration the PMD configuration to use
* @param ruleSetFactory ruleset factory
* @param files the files to analyze
@ -337,7 +339,7 @@ public class PMD {
/**
* Run PMD on a list of files using multiple threads - if more than one is
* available
*
*
* @param configuration
* Configuration
* @param ruleSetFactory
@ -417,6 +419,29 @@ public class PMD {
throw new RuntimeException("Problem with DBURI: " + uriString, ex);
}
}
if (null != configuration.getInputFilePath()) {
String inputFilePath = configuration.getInputFilePath();
File file = new File(inputFilePath);
try {
if (!file.exists()) {
LOG.log(Level.SEVERE, "Problem with Input File Path", inputFilePath);
throw new RuntimeException("Problem with Input File Path: " + inputFilePath);
} else {
BufferedReader br = new BufferedReader(new FileReader(file));
String filePaths = br.readLine();
if (null == filePaths){
LOG.log(Level.SEVERE, "Problem with Input File Path", inputFilePath);
throw new RuntimeException("Problem with Input File Path: " + inputFilePath);
}
files.addAll(FileUtil.collectFiles(filePaths, fileSelector));
}
} catch (IOException ex) {
LOG.log(Level.SEVERE, "Problem with Input File", ex);
throw new RuntimeException("Problem with Input File Path: " + inputFilePath, ex);
}
}
return files;
}
@ -442,7 +467,7 @@ public class PMD {
/**
* Entry to invoke PMD as command line tool
*
*
* @param args command line arguments
*/
public static void main(String[] args) {

View File

@ -29,6 +29,9 @@ public class PMDParameters {
@Parameter(names = { "-dir", "-d" }, description = "Root directory for sources.", required = false)
private String sourceDir;
@Parameter(names = { "-filelist" }, description = "Path to a file containing a list of files to analyze.", required = false)
private String fileListPath;
@Parameter(names = { "-format", "-f" }, description = "Report format type.")
private String format = "text"; // Enhance to support other usage
@ -121,12 +124,13 @@ public class PMDParameters {
}
public static PMDConfiguration transformParametersIntoConfiguration(PMDParameters params) {
if (null == params.getSourceDir() && null == params.getUri()) {
if (null == params.getSourceDir() && null == params.getUri() && null == params.getFileListPath()) {
throw new IllegalArgumentException(
"Please provide either source root directory (-dir or -d) or database URI (-uri or -u) parameter");
"Please provide a parameter for source root directory (-dir or -d), database URI (-uri or -u), or file list path (-filelist).");
}
PMDConfiguration configuration = new PMDConfiguration();
configuration.setInputPaths(params.getSourceDir());
configuration.setInputFilePath(params.getFileListPath());
configuration.setInputUri(params.getUri());
configuration.setReportFormat(params.getFormat());
configuration.setBenchmark(params.isBenchmark());
@ -231,6 +235,10 @@ public class PMDParameters {
return sourceDir;
}
public String getFileListPath() {
return fileListPath;
};
public String getFormat() {
return format;
}