Skip to content

Commit

Permalink
Issue checkstyle#3312: Add sourcepath property to Ant task
Browse files Browse the repository at this point in the history
  • Loading branch information
soon committed Apr 29, 2017
1 parent 2d32ded commit ae6e040
Show file tree
Hide file tree
Showing 10 changed files with 306 additions and 84 deletions.
75 changes: 72 additions & 3 deletions config/ant-phase-verify.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<tstamp>
<format property="STARTED" pattern="dd/MM/yyyy hh:mm:ss aa" />
</tstamp>
<echo>Checkstyle started: ${STARTED}</echo>
<echo>Checkstyle started (checkstyle_checks.xml): ${STARTED}</echo>

<checkstyle config="${check.config}"
failOnViolation="false"
Expand All @@ -39,9 +39,78 @@
</checkstyle>

<tstamp>
<format property="FINISHED" pattern="dd/MM/yyyy hh:mm:ss aa" />
<format property="FINISHED" pattern="dd/MM/yyyy hh:mm:ss aa"/>
</tstamp>
<echo>Checkstyle finished: ${FINISHED}</echo>
<echo>Checkstyle finished (checkstyle_checks.xml) : ${FINISHED}</echo>

<tstamp>
<format property="STARTED" pattern="dd/MM/yyyy hh:mm:ss aa"/>
</tstamp>
<echo>Checkstyle started (checkstyle_non_main_files_checks.xml): ${STARTED}</echo>

<checkstyle config="config/checkstyle_non_main_files_checks.xml"
failOnViolation="true"
failureProperty="checkstyle.failure.property">
<path>
<fileset dir="${basedir}" includes="**/*">
<!--<exclude name="src/it/resources/**/*" />-->
<!--<exclude name="src/test/resources/**/*" />-->
<!--<exclude name="src/test/resources-noncompilable/**/*"/>-->

<exclude name=".git/**/*"/>
<exclude name="src/main/**/*"/>
<exclude name="src/test/java/**/*"/>
<exclude name="src/site/resources/images/**/*"/>

<!-- Eclipse project files -->
<exclude name=".settings/**/*"/>
<exclude name=".externalToolBuilders/**/*"/>
<exclude name=".classpath"/>
<exclude name=".project"/>

<!-- m2e-code-quality Eclipse IDE plugin temporary configuration files for Eclipse CS Checkstyle / PMD / FindBugs Plug-Ins -->
<exclude name=".checkstyle/**/*"/>
<exclude name=".pmd/**/*"/>
<exclude name=".pmdruleset.xml"/>
<exclude name=".fbExcludeFilterFile/**/*"/>

<!-- NetBeans project files -->
<exclude name="**/nbactions.xml"/>
<exclude name="**/nb-configuration.xml"/>

<!-- Maven build folder -->
<exclude name="target/**/*" />
<exclude name="bin/**/*" />

<!-- IDEA project files -->
<exclude name="*.iml"/>
<exclude name="*.ipr"/>
<exclude name="*.iws"/>
<exclude name=".idea/**/*"/>

<!-- Temp files -->
<exclude name="*~" />

<!-- Java Virtual machine crash logs -->
<exclude name="hs_err_pid*"/>
<exclude name="replay_pid*"/>

<!-- Apple MAC OSX hidden file -->
<exclude name=".DS_Store" />

<!-- NonDex files -->
<exclude name=".nondex/**/*" />
</fileset>
</path>
<formatter type="plain"/>
<formatter type="xml" toFile="${mvn.project.build.directory}/cs_non_main_files_errors.xml"/>
<classpath path="${mvn.runtime_classpath}"/>
</checkstyle>

<tstamp>
<format property="FINISHED" pattern="dd/MM/yyyy hh:mm:ss aa"/>
</tstamp>
<echo>Checkstyle finished (checkstyle_non_main_files_checks.xml): ${FINISHED}</echo>

<fail if="checkstyle.failure.property"
message="Checkstyle failed: ${checkstyle.failure.property}"
Expand Down
1 change: 1 addition & 0 deletions config/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<suppress checks="ClassDataAbstractionCoupling" files="(CheckerTest|TreeWalkerTest|BaseCheckTestSupport|XdocsPagesTest|CheckstyleAntTaskTest)\.java"/>
<suppress checks="ClassDataAbstractionCoupling" files="PropertyCacheFile\.java"/>
<suppress checks="ClassFanOutComplexity" files="[\\/]Main\.java"/>
<suppress checks="ClassFanOutComplexity" files="CheckstyleAntTask\.java"/>
<!-- a lot of GUI elements is OK -->
<suppress checks="ClassDataAbstractionCoupling" files="(TreeTable|MainFrame)\.java"/>

Expand Down
57 changes: 1 addition & 56 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -492,62 +492,6 @@
<goal>check</goal>
</goals>
</execution>
<execution>
<id>checkstyle-non-main-check</id>
<phase>verify</phase>
<configuration>
<configLocation>${project.basedir}/config/checkstyle_non_main_files_checks.xml</configLocation>
<failOnViolation>true</failOnViolation>
<includeResources>false</includeResources>
<includeTestResources>true</includeTestResources>
<logViolationsToConsole>true</logViolationsToConsole>
<maxAllowedViolations>0</maxAllowedViolations>
<violationSeverity>error</violationSeverity>
<propertyExpansion>project.basedir=${project.basedir}</propertyExpansion>
<sourceDirectory>${project.basedir}</sourceDirectory>
<includes>**/*</includes>
<!-- All .gitignore files should be added to exclusions -->
<excludes>
.git/**/*,
src/main/**/*,
src/test/java/**/*,
src/site/resources/images/**/*,
<!-- Eclipse project files -->
.settings/**/*,
.externalToolBuilders/**/*,
.classpath,
.project,
<!-- m2e-code-quality Eclipse IDE plugin temporary configuration files for Eclipse CS Checkstyle / PMD / FindBugs Plug-Ins -->
.checkstyle/**/*,
.pmd/**/*,
.pmdruleset.xml,
.fbExcludeFilterFile/**/*,
<!-- NetBeans project files -->
nbactions.xml,
nb-configuration.xml,
<!-- Maven build folder -->
target/**/*,
bin/**/*,
<!-- IDEA project files -->
*.iml,
*.ipr,
*.iws,
.idea/**/*,
<!-- Temp files -->
*~,
<!-- Java Virtual machine crash logs -->
hs_err_pid*,
replay_pid*,
<!-- Apple MAC OSX hidden file -->
.DS_Store,
<!-- NonDex files -->
.nondex/**/*,
</excludes>
</configuration>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>

Expand Down Expand Up @@ -895,6 +839,7 @@
<property name="mvn.project.build.directory" value="${project.build.directory}" />
<property name="mvn.project.version" value="${project.version}" />
<property name="mvn.runtime_classpath" refid="maven.runtime.classpath" />
<property name="project.basedir" value="${project.basedir}" />
<ant antfile="config/ant-phase-verify.xml" />
</target>
</configuration>
Expand Down
130 changes: 113 additions & 17 deletions src/main/java/com/puppycrawl/tools/checkstyle/ant/CheckstyleAntTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.stream.Collectors;

import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.BuildException;
Expand Down Expand Up @@ -72,6 +74,9 @@ public class CheckstyleAntTask extends Task {
/** Suffix for time string. */
private static final String TIME_SUFFIX = " ms.";

/** Contains the paths to process. */
private final List<Path> paths = new ArrayList<>();

/** Contains the filesets to process. */
private final List<FileSet> fileSets = new ArrayList<>();

Expand Down Expand Up @@ -151,6 +156,14 @@ public void setMaxWarnings(int maxWarnings) {
this.maxWarnings = maxWarnings;
}

/**
* Adds a path.
* @param path the path to add.
*/
public void addPath(Path path) {
paths.add(path);
}

/**
* Adds set of files (nested fileset attribute).
* @param fileSet the file set to add
Expand Down Expand Up @@ -296,9 +309,11 @@ public void execute() {
log("compiled on " + compileTimestamp, Project.MSG_VERBOSE);

// Check for no arguments
if (fileName == null && fileSets.isEmpty()) {
if (fileName == null
&& fileSets.isEmpty()
&& paths.isEmpty()) {
throw new BuildException(
"Must specify at least one of 'file' or nested 'fileset'.",
"Must specify at least one of 'file' or nested 'fileset' or 'path'.",
getLocation());
}
if (configLocation == null) {
Expand Down Expand Up @@ -359,7 +374,7 @@ private static void destroyRootModule(RootModule rootModule) {
private void processFiles(RootModule rootModule, final SeverityLevelCounter warningCounter,
final String checkstyleVersion) {
final long startTime = System.currentTimeMillis();
final List<File> files = scanFileSets();
final List<File> files = getFilesToCheck();
final long endTime = System.currentTimeMillis();
log("To locate the files took " + (endTime - startTime) + TIME_SUFFIX,
Project.MSG_VERBOSE);
Expand Down Expand Up @@ -509,33 +524,114 @@ private AuditListener[] getListeners() {

/**
* Returns the list of files (full path name) to process.
* @return the list of files included via the filesets.
* @return the list of files included via the fileName, filesets and paths.
*/
protected List<File> scanFileSets() {
final List<File> list = new ArrayList<>();
protected List<File> getFilesToCheck() {
final List<File> allFiles = new ArrayList<>();
if (fileName != null) {
// oops we've got an additional one to process, don't
// forget it. No sweat, it's fully resolved via the setter.
log("Adding standalone file for audit", Project.MSG_VERBOSE);
list.add(new File(fileName));
allFiles.add(new File(fileName));
}

final List<File> filesFromFileSets = scanFileSets();
allFiles.addAll(filesFromFileSets);

final List<File> filesFromPaths = scanPaths();
allFiles.addAll(filesFromPaths);

return allFiles;
}

/**
* Retrieves all files from the defined paths.
* @return a list of files defined via paths.
*/
private List<File> scanPaths() {
final List<File> allFiles = new ArrayList<>();

for (int i = 0; i < paths.size(); i++) {
final Path currentPath = paths.get(i);
final List<File> pathFiles = scanPath(currentPath, i + 1);
allFiles.addAll(pathFiles);
}

return allFiles;
}

/**
* Scans the given path and retrieves all files for the given path.
*
* @param path A path to scan.
* @param pathIndex The index of the given path. Used in log messages only.
* @return A list of files, extracted from the given path.
*/
private List<File> scanPath(Path path, int pathIndex) {
final String[] resources = path.list();
log(pathIndex + ") Scanning path " + path, Project.MSG_VERBOSE);
final List<File> allFiles = new ArrayList<>();
int concreteFilesCount = 0;

for (String resource : resources) {
final File file = new File(resource);
if (file.isFile()) {
concreteFilesCount++;
allFiles.add(file);
}
else {
final DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir(file);
scanner.scan();
final List<File> scannedFiles = retrieveAllScannedFiles(scanner, pathIndex);
allFiles.addAll(scannedFiles);
}
}

if (concreteFilesCount > 0) {
log(String.format(Locale.ROOT, "%d) Adding %d files from path %s",
pathIndex, concreteFilesCount, path), Project.MSG_VERBOSE);
}

return allFiles;
}

/**
* Returns the list of files (full path name) to process.
* @return the list of files included via the filesets.
*/
protected List<File> scanFileSets() {
final List<File> allFiles = new ArrayList<>();

for (int i = 0; i < fileSets.size(); i++) {
final FileSet fileSet = fileSets.get(i);
final DirectoryScanner scanner = fileSet.getDirectoryScanner(getProject());
scanner.scan();

final String[] names = scanner.getIncludedFiles();
log(i + ") Adding " + names.length + " files from directory "
+ scanner.getBasedir(), Project.MSG_VERBOSE);

for (String element : names) {
final String pathname = scanner.getBasedir() + File.separator
+ element;
list.add(new File(pathname));
}
final List<File> scannedFiles = retrieveAllScannedFiles(scanner, i);
allFiles.addAll(scannedFiles);
}

return list;
return allFiles;
}

/**
* Retrieves all matched files from the given scanner.
*
* @param scanner A directory scanner. Note, that {@link DirectoryScanner#scan()}
* must be called before calling this method.
* @param logIndex A log entry index. Used only for log messages.
* @return A list of files, retrieved from the given scanner.
*/
private List<File> retrieveAllScannedFiles(DirectoryScanner scanner, int logIndex) {
final String[] fileNames = scanner.getIncludedFiles();
log(String.format(Locale.ROOT, "%d) Adding %d files from directory %s",
logIndex, fileNames.length, scanner.getBasedir()), Project.MSG_VERBOSE);

return Arrays.stream(fileNames)
.map(name -> scanner.getBasedir() + File.separator + name)
.map(File::new)
.collect(Collectors.toList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package com.puppycrawl.tools.checkstyle;

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

import com.puppycrawl.tools.checkstyle.api.AuditListener;
Expand All @@ -29,6 +31,7 @@

public class TestRootModuleChecker implements RootModule {
private static boolean processed;
private static List<File> filesToCheck;

@Override
public void configure(Configuration configuration) throws CheckstyleException {
Expand All @@ -43,6 +46,7 @@ public void destroy() {
@Override
public int process(List<File> files) throws CheckstyleException {
processed = true;
filesToCheck = new ArrayList<>(files);
return 0;
}

Expand All @@ -62,5 +66,10 @@ public static boolean isProcessed() {

public static void reset() {
processed = false;
filesToCheck = null;
}

public static List<File> getFilesToCheck() {
return Collections.unmodifiableList(filesToCheck);
}
}
Loading

0 comments on commit ae6e040

Please sign in to comment.