Skip to content

Commit

Permalink
Initial Require.JS Task Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Fitzgerald committed Feb 8, 2013
1 parent 3dd2abe commit 0a80433
Show file tree
Hide file tree
Showing 5 changed files with 24,058 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/main/groovy/com/eriwen/gradle/js/JsPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class JsPlugin implements Plugin<Project> {
void apply(final Project project) {
project.extensions.create(ClosureCompilerExtension.NAME, ClosureCompilerExtension)
project.extensions.create(JsDocExtension.NAME, JsDocExtension)
project.extensions.create(RequireJsExtension.NAME, RequireJsExtension)
project.extensions.create(Props2JsExtension.NAME, Props2JsExtension)
project.extensions.create(JavaScriptExtension.NAME, JavaScriptExtension, project)

Expand All @@ -39,6 +40,7 @@ class JsPlugin implements Plugin<Project> {
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 All @@ -50,9 +52,9 @@ class JsPlugin implements Plugin<Project> {
}
project.dependencies {
rhino 'org.mozilla:rhino:1.7R4'
// rhino 'com.google.code.gson:gson:2.2.1'
//add javaScript.gradlePublicJavaScriptRepository
//add javaScript.googleApisRepository
// rhino 'com.google.code.gson:gson:2.2.1'
// add javaScript.gradlePublicJavaScriptRepository
// add javaScript.googleApisRepository
}
// TODO: have 'check' depend on jshint
}
Expand Down
24 changes: 24 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,24 @@
/**
* 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 LinkedHashMap<String, Object> options = []
}
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
import org.mozilla.javascript.*;

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

0 comments on commit 0a80433

Please sign in to comment.