Skip to content

Commit

Permalink
Completing RequireJS Task Implementation, Adding Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Fitzgerald committed Feb 8, 2013
1 parent 0a80433 commit 436717d
Show file tree
Hide file tree
Showing 14 changed files with 10,958 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ import org.gradle.api.tasks.Input
class RequireJsExtension {
public static final String NAME = 'requirejs'

@Input File buildprofile = null

@Input LinkedHashMap<String, Object> options = []
}
6 changes: 5 additions & 1 deletion src/main/groovy/com/eriwen/gradle/js/RhinoExec.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.eriwen.gradle.js

import org.gradle.api.Project
import org.gradle.process.ExecResult

/**
* Utility for executing JS with Rhino.
Expand All @@ -25,7 +26,10 @@ class RhinoExec {
standardOutput = out
}

project.javaexec(execOptions)
ExecResult result = project.javaexec(execOptions)
if (!ignoreExitCode) {
result.assertNormalExitValue()
}
}

public RhinoExec(final Project projectIn) {
Expand Down
31 changes: 12 additions & 19 deletions src/main/groovy/com/eriwen/gradle/js/tasks/RequireJsTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import com.eriwen.gradle.js.ResourceUtil
import com.eriwen.gradle.js.RhinoExec
import org.gradle.api.tasks.SourceTask
import org.gradle.api.tasks.OutputFile
import org.mozilla.javascript.*;

class RequireJsTask extends SourceTask {
private static final String REQUIREJS_PATH = 'r.js'
Expand All @@ -38,30 +37,24 @@ class RequireJsTask extends SourceTask {

@TaskAction
def run() {
final File requireJsFile = RESOURCE_UTIL.extractFileToDirectory(
new File(project.buildDir, TMP_DIR), REQUIREJS_PATH)
LinkedHashMap<String, Object> options = [optimize: "none", logLevel: 2, skipModuleInsertion: false, jamConfig: "jam/require.config.js", out: dest]
options.putAll(project.options)

// Set Some Options Based On The jam File
File jamFile = new File(options.jamConfig);
if (jamFile.exists()) {
Context cx = Context.enter();
try {
Scriptable scope = cx.initStandardObjects()
String s = jamFile.text
Object result = cx.evaluateString(scope, s, options.jamConfig, 1, null)
logger.debug(result.toString())
} finally {
Context.exit();
final File requireJsFile = RESOURCE_UTIL.extractFileToDirectory(new File(project.buildDir, TMP_DIR), REQUIREJS_PATH)
LinkedHashMap<String, Object> options = [] // [optimize: "none", logLevel: 2, skipModuleInsertion: false, out: dest]
options.putAll(project.requirejs.options)

final List<String> args = [requireJsFile.canonicalPath]
args.add("-o")
if (project.requirejs.buildprofile != null && project.requirejs.buildprofile.class == File) {
File buildprofile = project.requirejs.buildprofile
if (buildprofile.exists()) {
args.add(buildprofile.canonicalPath)
}
}

final List<String> args = []
options.each() { key, value ->
logger.debug("${key} == ${value}")
args.add("${key}=${value}")
}
rhino.execute(args, [ignoreExitCode: ignoreExitCode])

rhino.execute(args, [ignoreExitCode: ignoreExitCode, workingDir: project.projectDir.canonicalPath])
}
}
92 changes: 92 additions & 0 deletions src/test/groovy/com/eriwen/gradle/js/RequireJsTaskTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.eriwen.gradle.js

import org.gradle.api.Project
import org.gradle.process.internal.ExecException
import org.gradle.testfixtures.ProjectBuilder
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Specification

class RequireJsTaskTest extends Specification {
@Rule TemporaryFolder dir = new TemporaryFolder()

Project project = ProjectBuilder.builder().build()
def task
def src

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

def "runWithDefaults"() {
given:
project.requirejs.options = [baseUrl: ".", "paths.jquery": "jam/jquery/dist/jquery", name: "main", out: "main-built.js"]
task.ignoreExitCode = false
addMainFile()
addJamDir()

when:
task.run()

then:
notThrown ExecException
}

def "runWithInvalidMainJs"() {
given:
project.requirejs.options = [baseUrl: ".", "paths.jquery": "jam/jquery/dist/jquery", name: "main", out: "main-built.js"]
task.ignoreExitCode = false
addInvalidMainFile()
addJamDir()

when:
task.run()

then:
ExecException e = thrown()
}

def "runWithBuildProfile"() {
given:
project.requirejs.buildprofile = new File("build.js")
addBuildFile()
addMainFile()
addJamDir()

when:
task.run()

then:
notThrown ExecException
}

def addBuildFile() {
addFile("build.js", new File("src/test/resources/requirejs/build.js").text)
}

def addMainFile() {
addFile("main.js", new File("src/test/resources/requirejs/main.js").text)
}

def addInvalidMainFile() {
// call to invalidrequire[]
addFile("main.js", new File("src/test/resources/requirejs/invalidmain.js").text)
}

def addJamDir() {
new AntBuilder().copy(todir: new File("${project.projectDir.absolutePath}${File.separator}jam").canonicalPath) {
fileset(dir : new File("src/test/resources/requirejs/jam").canonicalPath)
}
}

def addFile(name,contents) {
def file = new File(project.projectDir as String, name)
file << contents
}
}


12 changes: 12 additions & 0 deletions src/test/resources/requirejs/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
({
baseUrl: ".",
packages: [
{
"name": "jquery",
"location": "jam/jquery",
"main": "dist/jquery.js"
}
],
name: "main",
out: "main-built.js"
})
6 changes: 6 additions & 0 deletions src/test/resources/requirejs/invalidmain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
;;&^%#&@%$#@badrequire(["jquery"], function(util) {
//This function is called when scripts/helper/util.js is loaded.
//If util.js calls define(), then this function is not fired until
//util's dependencies have loaded, and the util argument will hold
//the module value for "helper/util".
});
Loading

0 comments on commit 436717d

Please sign in to comment.