Permalink
Browse files

Adding the beginnings of an _experimental_ JavaScript plugin.

Right now, this is really just some Rhino infrastructure. This is heading towards CoffeeScript compilation.
  • Loading branch information...
ldaley committed May 17, 2012
1 parent b7dcb5b commit 37366957a4a401a30f8e9f79a94953d9e7d4f829
@@ -21,4 +21,9 @@
<suppress checks="Javadoc.*"
files=".*[/\\]subprojects[/\\]internal-.+[/\\]src[/\\]main[/\\].+"/>
+ <!-- JavaScript plugin is incubating -->
+ <suppress checks="Javadoc.*"
+ files=".*[/\\]subprojects[/\\]javascript[/\\].+"/>
+
+
</suppressions>
View
@@ -43,6 +43,7 @@ include 'internalTesting'
include 'internalIntegTesting'
include 'website'
include 'performance'
+include 'javascript'
rootProject.name = 'gradle'
rootProject.children.each {project ->
@@ -34,7 +34,7 @@ abstract class WellBehavedPluginTest extends AbstractIntegrationSpec {
def "plugin does not force creation of build dir during configuration"() {
given:
- buildFile << "apply plugin: '${getPluginId()}'"
+ applyPlugin()
when:
run "tasks"
@@ -45,9 +45,13 @@ abstract class WellBehavedPluginTest extends AbstractIntegrationSpec {
def "plugin can build with empty project"() {
given:
- buildFile << "apply plugin: '${getPluginId()}'"
+ applyPlugin()
expect:
succeeds mainTask
}
+
+ protected applyPlugin(File target = buildFile) {
+ target << "apply plugin: '${getPluginId()}'\n"
+ }
}
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2012 the original author or authors.
+ *
+ * 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.
+ */
+
+dependencies {
+ groovy libraries.groovy
+
+ compile project(':core'), project(":plugins")
+}
+
+useTestFixtures()
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2012 the original author or authors.
+ *
+ * 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 org.gradle.plugins.javascript.base
+
+import org.gradle.integtests.fixtures.WellBehavedPluginTest
+
+class JavaScriptBasePluginIntegrationTest extends WellBehavedPluginTest {
+
+ @Override
+ String getPluginId() {
+ "javascript-base"
+ }
+
+}
@@ -0,0 +1,131 @@
+/*
+ * Copyright 2012 the original author or authors.
+ *
+ * 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 org.gradle.plugins.javascript.rhino
+
+import org.gradle.integtests.fixtures.WellBehavedPluginTest
+
+class RhinoPluginIntegrationTest extends WellBehavedPluginTest {
+
+ def setup() {
+ applyPlugin()
+
+ buildFile << """
+ repositories {
+ mavenCentral()
+ }
+ """
+ }
+
+
+ def "can use default rhino dependency"() {
+ when:
+ buildFile << """
+ task resolve(type: Copy) {
+ from javaScript.rhino.configuration
+ into "deps"
+ }
+ """
+
+ then:
+ succeeds("resolve")
+
+ and:
+ file("deps/rhino-${RhinoPlugin.DEFAULT_RHINO_VERSION}.jar").exists()
+ }
+
+ def "can run rhino exec task"() {
+ given:
+ file("some.js") << """
+ print("rhino js-version: " + version())
+ print("rhino arg: " + arguments[0])
+ """
+
+ buildFile << """
+ task rhino(type: ${RhinoShellExec.name}) {
+ rhinoOptions "-version", "160"
+ script "some.js"
+ scriptArgs "foo"
+ }
+ """
+
+ when:
+ run "rhino"
+
+ then:
+ output.contains "rhino js-version: 160"
+ output.contains "rhino arg: foo"
+ }
+
+ def "compile failure fails task"() {
+ given:
+ file("some.js") << " ' "
+
+ buildFile << """
+ task rhino(type: ${RhinoShellExec.name}) {
+ script "some.js"
+ }
+ """
+
+ expect:
+ fails "rhino"
+ }
+
+ def "can run rhino actions"() {
+ given:
+ file("some.js") << """
+ print("rhino js-version: " + version())
+ print("rhino arg: " + arguments[0])
+ """
+
+ buildFile << """
+ task rhino << {
+ javaScript.rhino.exec {
+ args "-version", "160", file("some.js").absolutePath, "foo"
+ }
+ }
+ """
+
+ when:
+ run "rhino"
+
+ then:
+ output.contains "rhino js-version: 160"
+ output.contains "rhino arg: foo"
+ }
+
+ def "can use older rhino version"() {
+ given:
+ buildFile << """
+ javaScript {
+ rhino {
+ dependencies 'rhino:js:1.6R6'
+ }
+ }
+
+ task rhino(type: ${RhinoShellExec.name}) {
+ rhinoOptions "-e", "print('rhinoClasspath: ' + environment['java.class.path'])"
+ }
+ """
+
+ when:
+ run "rhino"
+
+ then:
+ output.readLines().any { it ==~ /rhinoClasspath:.+js-1.6R6.jar/ }
+ }
+
+}
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2012 the original author or authors.
+ *
+ * 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 org.gradle.plugins.javascript.base
+
+import org.gradle.api.Project
+import org.gradle.api.Plugin
+import org.gradle.api.plugins.BasePlugin
+
+class JavaScriptBasePlugin implements Plugin<Project> {
+
+ void apply(Project project) {
+ project.apply(plugin: BasePlugin)
+ JavaScriptExtension extension = project.extensions.create(JavaScriptExtension.NAME, JavaScriptExtension)
+ }
+}
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2012 the original author or authors.
+ *
+ * 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 org.gradle.plugins.javascript.base;
+
+public class JavaScriptExtension {
+
+ public static final String NAME = "javaScript";
+
+}
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2012 the original author or authors.
+ *
+ * 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 org.gradle.plugins.javascript.rhino;
+
+import groovy.lang.Closure;
+import org.gradle.api.Action;
+import org.gradle.api.artifacts.Configuration;
+import org.gradle.api.artifacts.Dependency;
+import org.gradle.api.artifacts.ResolvableDependencies;
+import org.gradle.api.artifacts.dsl.DependencyHandler;
+import org.gradle.api.internal.file.FileResolver;
+import org.gradle.process.ExecResult;
+import org.gradle.process.JavaExecSpec;
+import org.gradle.process.internal.DefaultJavaExecAction;
+import org.gradle.process.internal.JavaExecAction;
+import org.gradle.util.ConfigureUtil;
+
+public class RhinoExtension {
+
+ public static final String NAME = "rhino";
+ public static final String RHINO_SHELL_MAIN = "org.mozilla.javascript.tools.shell.Main";
+
+ private final FileResolver fileResolver;
+ private final DependencyHandler dependencyHandler;
+ private final Configuration configuration;
+ private final Dependency defaultDependency;
+
+ public RhinoExtension(FileResolver fileResolver, DependencyHandler dependencyHandler, Configuration configuration, Dependency defaultDependency) {
+ this.fileResolver = fileResolver;
+ this.dependencyHandler = dependencyHandler;
+ this.configuration = configuration;
+ this.defaultDependency = defaultDependency;
+
+ configureConfiguration(configuration);
+ }
+
+ public void dependencies(Object notation) {
+ dependencies(notation, null);
+ }
+
+ public void dependencies(Object notation, Closure closure) {
+ configuration.getDependencies().add(dependencyHandler.create(notation, closure));
+ }
+
+ public Configuration getConfiguration() {
+ return configuration;
+ }
+
+ private void configureConfiguration(final Configuration configuration) {
+ configuration.getIncoming().beforeResolve(new Action<ResolvableDependencies>() {
+ public void execute(ResolvableDependencies resolvableDependencies) {
+ if (resolvableDependencies.getDependencies().isEmpty()) {
+ configuration.getDependencies().add(defaultDependency);
+ }
+ }
+ });
+ }
+
+ public JavaExecAction exec() {
+ JavaExecAction action = new DefaultJavaExecAction(fileResolver);
+ action.setMain(RHINO_SHELL_MAIN);
+ action.setClasspath(getConfiguration());
+ return action;
+ }
+
+ public ExecResult exec(Action<JavaExecSpec> action) {
+ JavaExecAction exec = exec();
+ action.execute(exec);
+ return exec.execute();
+ }
+
+ public ExecResult exec(Closure<?> action) {
+ return ConfigureUtil.configure(action, exec()).execute();
+ }
+
+}
Oops, something went wrong.

0 comments on commit 3736695

Please sign in to comment.