Skip to content

Commit

Permalink
fix cjdev2#14 Allow using custom jshint.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Crydust committed Feb 20, 2014
1 parent 2470797 commit 2ea3b90
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
25 changes: 19 additions & 6 deletions src/main/java/com/cj/jshintmojo/Mojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public class Mojo extends AbstractMojo {
*/
private String reportFile = "";

/**
* @parameter property="customJSHint"
*/
private File customJSHint = null;

/**
* @parameter expression="${jshint.version}"
*/
Expand Down Expand Up @@ -110,13 +115,21 @@ public Mojo(String options, String globals, File basedir, List<String> directori
this.reporter = reporter;
this.reportFile = reportFile;
}

public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("using jshint version " + version);

final String jshintCode = getEmbeddedJshintCode(version);

final JSHint jshint = new JSHint(jshintCode);
public void execute() throws MojoExecutionException, MojoFailureException {
final JSHint jshint;
if (customJSHint == null) {
getLog().info("using jshint version " + version);
final String jshintCode = getEmbeddedJshintCode(version);
jshint = new JSHint(jshintCode);
} else {
getLog().info("using customJSHint " + customJSHint);
try {
jshint = new JSHint(customJSHint);
} catch (IOException e) {
throw new MojoExecutionException("Could not load customJSHint", e);
}
}

final Config config = readConfig(this.options, this.globals, this.configFile, this.basedir, getLog());
final Cache.Hash cacheHash = new Cache.Hash(config.options, config.globals, this.version, this.configFile, this.directories, this.excludes);
Expand Down
21 changes: 16 additions & 5 deletions src/main/java/com/cj/jshintmojo/jshint/JSHint.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.cj.jshintmojo.jshint;

import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.mozilla.javascript.EcmaError;
import org.mozilla.javascript.NativeArray;
Expand All @@ -16,22 +19,30 @@ public class JSHint {

private final Rhino rhino;

public JSHint(File customJSHint) throws IOException {
rhino = createRhino(FileUtils.readFileToString(customJSHint, "UTF-8"));
}

public JSHint(String jshintCode) {

rhino = new Rhino();
rhino = createRhino(resourceAsString(jshintCode));
}

private static Rhino createRhino(final String code) {
Rhino result = new Rhino();
try {
rhino.eval(
result.eval(
"print=function(){};" +
"quit=function(){};" +
"arguments=[];");

rhino.eval(commentOutTheShebang(resourceAsString(jshintCode)));
result.eval(commentOutTheShebang(code));
} catch (EcmaError e) {
throw new RuntimeException("Javascript eval error:" + e.getScriptStackTrace(), e);
}
return result;
}

private String commentOutTheShebang(String code) {
private static String commentOutTheShebang(String code) {
String minusShebang = code.startsWith("#!")?"//" + code : code;
return minusShebang;
}
Expand Down

0 comments on commit 2ea3b90

Please sign in to comment.