Skip to content

Commit

Permalink
fix cjdev2#33 Excludes should allow for glob patterns
Browse files Browse the repository at this point in the history
use a fileset to match files
  • Loading branch information
Crydust committed Feb 20, 2014
1 parent 2470797 commit 6086296
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
36 changes: 7 additions & 29 deletions src/main/java/com/cj/jshintmojo/Mojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.maven.model.FileSet;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand All @@ -27,8 +28,6 @@
import com.cj.jshintmojo.cache.Cache;
import com.cj.jshintmojo.cache.Result;
import com.cj.jshintmojo.jshint.EmbeddedJshintCode;
import com.cj.jshintmojo.jshint.FunctionalJava;
import com.cj.jshintmojo.jshint.FunctionalJava.Fn;
import com.cj.jshintmojo.jshint.JSHint;
import com.cj.jshintmojo.jshint.JSHint.Error;
import com.cj.jshintmojo.reporter.CheckStyleReporter;
Expand Down Expand Up @@ -185,25 +184,14 @@ private List<File> findFilesToCheck() {
if(!path.exists() && !path.isDirectory()){
getLog().warn("You told me to find tests in " + next + ", but there is nothing there (" + path.getAbsolutePath() + ")");
}else{
collect(path, javascriptFiles);
FileSet fs = new FileSet();
fs.setDirectory(path.getAbsolutePath());
fs.addInclude("**/*.js");
fs.setExcludes(excludes);
javascriptFiles.addAll(toFileList(fs));
}
}

List<File> matches = FunctionalJava.filter(javascriptFiles, new Fn<File, Boolean>(){
public Boolean apply(File i) {
for(String exclude : excludes){
File e = new File(basedir, exclude);
if(i.getAbsolutePath().startsWith(e.getAbsolutePath())){
getLog().warn("Excluding " + i);

return Boolean.FALSE;
}
}

return Boolean.TRUE;
}
});
return matches;
return javascriptFiles;
}

private static Map<String, Result> lintTheFiles(final JSHint jshint, final Cache cache, List<File> filesToCheck, final Config config, final Log log) throws FileNotFoundException {
Expand Down Expand Up @@ -368,16 +356,6 @@ private Cache readCache(File path, Cache.Hash hash){

return new Cache(hash);
}

private void collect(File directory, List<File> files) {
for(File next : directory.listFiles()){
if(next.isDirectory()){
collect(next, files);
}else if(next.getName().endsWith(".js")){
files.add(next);
}
}
}

/**
* Read contents of the specified config file and use the values defined there instead of the ones defined directly in pom.xml config.
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/com/cj/jshintmojo/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.maven.model.FileSet;

public class Util {

Expand Down Expand Up @@ -72,5 +74,28 @@ public static void mkdirs(File path) {
throw new RuntimeException("Could not create directory: " + path.getAbsolutePath());
}
}


public static List<File> toFileList(final FileSet fileSet) {
File directory = new File(fileSet.getDirectory());
String includes = toCommaSeparated(fileSet.getIncludes());
String excludes = toCommaSeparated(fileSet.getExcludes());
try {
return org.codehaus.plexus.util.FileUtils.getFiles(
directory, includes, excludes);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static String toCommaSeparated(final List<String> strings) {
StringBuilder sb = new StringBuilder();
for (String s : strings) {
sb.append(s).append(',');
}
if (sb.length() > 0) {
sb.setLength(sb.length() - 1);
}
return sb.toString();
}

}

0 comments on commit 6086296

Please sign in to comment.