Skip to content

Commit

Permalink
Merge joefitzgerald/requirejs-support into master
Browse files Browse the repository at this point in the history
  • Loading branch information
eriwen committed Feb 9, 2013
2 parents a13c2bc + 4d5c66f commit 93a3e41
Show file tree
Hide file tree
Showing 17 changed files with 35,002 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/groovy/com/eriwen/gradle/js/JsPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class JsPlugin implements Plugin<Project> {
project.extensions.create(ClosureCompilerExtension.NAME, ClosureCompilerExtension)
project.extensions.create(JsDocExtension.NAME, JsDocExtension)
project.extensions.create(JsHintExtension.NAME, JsHintExtension)
project.extensions.create(RequireJsExtension.NAME, RequireJsExtension)
project.extensions.create(Props2JsExtension.NAME, Props2JsExtension)
project.extensions.create(JavaScriptExtension.NAME, JavaScriptExtension, project)

Expand All @@ -33,12 +34,14 @@ class JsPlugin implements Plugin<Project> {
}

void applyTasks(final Project project) {
//project.task('coffee', type: TranspileCoffeeScriptTask, group: 'Build', description: 'Transpile CoffeeScript to JavaScript') {}
project.task('combineJs', type: CombineJsTask, group: 'Build', description: 'Combine many JavaScript files into one') {}
project.task('minifyJs', type: MinifyJsTask, group: 'Build', description: 'Minify JavaScript using Closure Compiler') {}
project.task('gzipJs', type: GzipJsTask, group: 'Build', description: 'GZip a given JavaScript file') {}
project.task('jshint', type: JsHintTask, group: 'Verification', description: 'Analyze JavaScript sources with JSHint') {}
project.task('jsdoc', type: JsDocTask, group: 'Documentation', description: 'Produce HTML documentation with JSDoc 3') {}
project.task('props2js', type: Props2JsTask, group: 'Build', description: 'Convert Java properties files for use with JavaScript') {}
project.task('requireJs', type: RequireJsTask, group: 'Build', description: 'Run the r.js Optimizer to produce Require.js output') {}
}

void configureDependencies(final Project project) {
Expand Down
26 changes: 26 additions & 0 deletions src/main/groovy/com/eriwen/gradle/js/RequireJsExtension.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright 2012 Joe Fitzgerald
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.eriwen.gradle.js

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
67 changes: 67 additions & 0 deletions src/main/groovy/com/eriwen/gradle/js/tasks/RequireJsTask.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright 2012 Joe Fitzgerald
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.eriwen.gradle.js.tasks

import org.gradle.api.tasks.TaskAction
import com.eriwen.gradle.js.ResourceUtil
import com.eriwen.gradle.js.RhinoExec
import org.gradle.api.tasks.SourceTask
import org.gradle.api.tasks.OutputFile

class RequireJsTask extends SourceTask {
private static final String REQUIREJS_PATH = 'r.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)

@OutputFile def dest

def ignoreExitCode = false;

File getDest() {
project.file(dest)
}

@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, 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 && project.requirejs.buildprofile.exists()) {
args.add("${project.requirejs.buildprofile.canonicalPath}")
}

def outAdded = false
if (!options.containsKey("out")) {
args.add("out=${getDest().canonicalPath}")
outAdded = true
}
options.each() { key, value ->
logger.debug("${key} == ${value}")
if (key.equalsIgnoreCase("out") & !outAdded) {
args.add("out=${getDest().canonicalPath}")
outAdded = true
} else {
args.add("${key}=${value}")
}
}

rhino.execute(args, [ignoreExitCode: ignoreExitCode, workingDir: project.projectDir.canonicalPath])
}
}
Loading

0 comments on commit 93a3e41

Please sign in to comment.