Skip to content

Commit

Permalink
Fix checkstyle report generation support, with checkstyle option to c…
Browse files Browse the repository at this point in the history
…ontrol it. Increment revision. Add opt flag to rhino.
  • Loading branch information
levsa committed Mar 26, 2013
1 parent 18dbcf8 commit 5f0f2a6
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 31 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ targetCompatibility = '1.6'

defaultTasks 'clean', 'build'

version = '1.4.1-SNAPSHOT'
version = '1.4.2'
group = 'com.eriwen'
ext.archivesBaseName = 'gradle-js-plugin'
ext.isSnapshot = version.endsWith("-SNAPSHOT")
Expand Down
2 changes: 1 addition & 1 deletion src/main/groovy/com/eriwen/gradle/js/RhinoExec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class RhinoExec {
def execOptions = {
main = RHINO_MAIN_CLASS
classpath = project.configurations.rhino
args = execargs
args = ["-opt", "9"] + execargs
workingDir = workingDirIn
ignoreExitValue = ignoreExitCode
standardOutput = out
Expand Down
13 changes: 5 additions & 8 deletions src/main/groovy/com/eriwen/gradle/js/tasks/JsHintTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import org.gradle.api.tasks.SourceTask
import org.gradle.api.tasks.OutputFile

class JsHintTask extends SourceTask {
private static final String JSHINT_PATH = 'jshint-rhino-r12.js'
private static final String JSHINT_CS_PATH = 'jshint-rhino-cs-r12.js'
private static final String JSHINT_PATH = 'jshint-rhino-cs-r12.js'
private static final String TMP_DIR = "tmp${File.separator}js"
private static final ResourceUtil RESOURCE_UTIL = new ResourceUtil()
private final RhinoExec rhino = new RhinoExec(project)
Expand All @@ -42,14 +41,12 @@ class JsHintTask extends SourceTask {
def run() {
final File jshintJsFile = RESOURCE_UTIL.extractFileToDirectory(
new File(project.buildDir, TMP_DIR), JSHINT_PATH)
final File jshintCsJsFile = RESOURCE_UTIL.extractFileToDirectory(
new File(project.buildDir, TMP_DIR), JSHINT_CS_PATH)
final File jsFile = jshintJsFile
final List<String> args = [jshintJsFile.canonicalPath]
args.addAll(source.files.collect { it.canonicalPath })
if (checkstyle) {
jsFile = jshintCsJsFile
logger.debug("reporter=checkstyle")
args.add("reporter=checkstyle")
}
final List<String> args = [jsFile.canonicalPath]
args.addAll(source.files.collect { it.canonicalPath })
LinkedHashMap<String, Object> options = project.jshint.options
if (options != null && options.size() > 0) {
def optionsArg = ""
Expand Down
68 changes: 48 additions & 20 deletions src/main/resources/jshint-rhino-cs-r12.js
Original file line number Diff line number Diff line change
Expand Up @@ -4972,13 +4972,23 @@ var reportWithReporter = function (reporter, file) {

(function (args) {
var filenames = [];
var reporter;
var reporterModule;
var optstr; // arg1=val1,arg2=val2,...
var predef; // global1=true,global2,global3,...
var opts = {};
var retval = 0;
var results = [];
var data = [];
var lintData;

args.forEach(function (arg) {
if (arg.indexOf("=") > -1) {
// Check first for reporter option
if (arg.split("=")[0] === "reporter") {
reporter = arg.split("=")[1];
return;
}
if (!optstr) {
// First time it's the options.
optstr = arg;
Expand Down Expand Up @@ -5031,28 +5041,46 @@ var reportWithReporter = function (reporter, file) {
});
}

filenames.forEach(function (name) {
var input = readFile(name);
filenames.forEach(function (file) {
var input = readFile(file);

if (!input) {
print("jshint: Couldn't open file " + file);
quit(1);
}

if (!JSHINT(input, opts)) {
JSHINT.errors.forEach(function (err) {
if (err) {
results.push({ file: file, error: err });
}
});
retval = 1;
}

if (!input) {
print("jshint: Couldn't open file " + name);
quit(1);
}
lintData = JSHINT.data();

if (!JSHINT(input, opts)) {
if (checkstyleReporter) {
reportWithReporter(checkstyleReporter.reporter, name);
}
else {
for (var i = 0, err; err = JSHINT.errors[i]; i += 1) {
print(err.reason + " (" + name + ":" + err.line + ":" + err.character + ")");
print("> " + (err.evidence || "").replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"));
print("");
}
}
retval = 1;
}
});
if (lintData) {
lintData.file = file;
data.push(lintData);
}
});

if (reporter === "checkstyle") {
reporterModule = checkstyleReporter;
}

if (reporterModule) {
reporterModule.reporter(results, data);
} else {
for (var i = 0; i < results.length; i += 1) {
var file = results[i].file;
var err = results[i].error;
print(err.reason + " (" + file + ":" + err.line + ":" + err.character + ")");
print("> " + (err.evidence || "").replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"));
print("");
}
}

quit(retval);
}(arguments));
30 changes: 29 additions & 1 deletion src/test/groovy/com/eriwen/gradle/js/JsHintTaskTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ class JsHintTaskTest extends Specification {
Project project = ProjectBuilder.builder().build()
def task
def src
def dest

def setup() {
project.apply(plugin: JsPlugin)
task = project.tasks.jshint
src = dir.newFolder()
dest = dir.newFile()
task.source = src
task.dest = dir.newFile()
task.dest = dest
}

def "build ignores result by default"() {
Expand Down Expand Up @@ -89,6 +91,32 @@ class JsHintTaskTest extends Specification {
notThrown ExecException
}

def "does not generate checkstyle report when disabled"() {
given:
task.checkstyle = false
addFile("invalid.js", "var b = 5")

when:
task.run()

then:
def contents = new File(dest as String).text
assert ! (contents =~ "<checkstyle")
}

def "generates checkstyle report when enabled"() {
given:
task.checkstyle = true
addFile("invalid.js", "var b = 5")

when:
task.run()

then:
def contents = new File(dest as String).text
assert contents =~ "<checkstyle"
}

def addValidFile() {
addFile("valid.js", "var a = 5;")
}
Expand Down

0 comments on commit 5f0f2a6

Please sign in to comment.