Skip to content

Commit 1cdf4d1

Browse files
author
R. Tyler Croy
committed
Merge pull request #27 from ysb33r/master
Added project.jrubyexec support
2 parents a877250 + b9a14c3 commit 1cdf4d1

File tree

11 files changed

+395
-58
lines changed

11 files changed

+395
-58
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ build/
22
*.sw*
33
.ruby-*
44
.gradle/
5+
*.iml
6+
.idea

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,27 @@ Additional ```JRubyExec``` methods for controlling the JVM instance
241241
* ```debug``` - ```Boolean```. See [debug](http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.JavaExec.html#org.gradle.api.tasks.JavaExec:debug)
242242
* ```copyTo``` - ```JavaForkOptions```. See [copyTo](http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.JavaExec.html)
243243
* ```executable``` - ```Object``` (Usually ```File``` or ```String```). See [executable](http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.JavaExec.html#org.gradle.api.tasks.JavaExec:executable)
244+
245+
## jrubyexec extension
246+
247+
Similar to ```javaexec``` and ```exec``` it is possible to add the execution of a jruby script within another task
248+
249+
```groovy
250+
task needSomeRubyLove {
251+
jrubyexec {
252+
script 'scripts/runme.rb'
253+
scriptArgs '-x', '-y'
254+
}
255+
}
256+
```
257+
258+
The behaviour of ```project.jrubyexec``` is slightly different to that of ```JRubyExec```.
259+
260+
* The version of ```jruby-complete``` is strictly tied to to ```jruby.defaultVersion```. Therefore trying to set ```jrubyVersion```
261+
in the ```jrubyexec``` closure will cause a failure
262+
* GEMs and additional JARs are only taken from the ```jrubyExec``` configuration.
263+
* It is not possible to supply a ```configuration``` parameter to the ```jrubyexec``` closure.
264+
* GEMs will be installed to ```jruby.gemInstallDir```. Existing gems will not be overwritten.
265+
266+
As with ```JRubyExec```, ```args```, ```setArgs``` and ```main``` are illegal within the ```jrubyexec``` closure.
267+
All other methods should work.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.lookout.jruby
2+
3+
import org.gradle.testfixtures.ProjectBuilder
4+
import spock.lang.Ignore
5+
import spock.lang.IgnoreIf
6+
import spock.lang.Specification
7+
8+
import static org.gradle.api.logging.LogLevel.LIFECYCLE
9+
10+
/**
11+
* @author Schalk W. Cronjé
12+
*/
13+
class JRubyExecExtensionIntegrationSpec extends Specification {
14+
static final boolean TESTS_ARE_OFFLINE = System.getProperty('TESTS_ARE_OFFLINE') != null
15+
static final File TEST_SCRIPT_DIR = new File( System.getProperty('TESTS_SCRIPT_DIR') ?: 'src/integTest/resources/scripts')
16+
static final File TESTROOT = new File(System.getProperty('TESTROOT') ?: 'build/tmp/test/integration-tests')
17+
18+
def project
19+
20+
void setup() {
21+
project = ProjectBuilder.builder().build()
22+
project.with {
23+
buildDir = TESTROOT
24+
logging.level = LIFECYCLE
25+
apply plugin: 'com.lookout.jruby'
26+
evaluate()
27+
}
28+
}
29+
30+
31+
@IgnoreIf({TESTS_ARE_OFFLINE})
32+
def "Run a script with minimum parameters"() {
33+
given:
34+
def output = new ByteArrayOutputStream()
35+
36+
when:
37+
project.jrubyexec {
38+
script "${TEST_SCRIPT_DIR}/helloWorld.rb"
39+
standardOutput output
40+
}
41+
42+
then:
43+
output.toString() == "Hello, World\n"
44+
}
45+
46+
@IgnoreIf({TESTS_ARE_OFFLINE})
47+
def "Run a script containing a conditional"() {
48+
given:
49+
def output = new ByteArrayOutputStream()
50+
51+
when: "we have an 'if' clause"
52+
project.jrubyexec {
53+
script "${TEST_SCRIPT_DIR}/helloName.rb"
54+
if(input == 0) {
55+
scriptArgs 'Stan'
56+
} else {
57+
scriptArgs 'man'
58+
}
59+
standardOutput output
60+
}
61+
62+
then: "only the appropriate parameters should be passed"
63+
output.toString() == expected
64+
65+
where:
66+
input | expected
67+
0 | "Hello, Stan\n"
68+
1 | "Hello, man\n"
69+
70+
}
71+
72+
@IgnoreIf({TESTS_ARE_OFFLINE})
73+
def "Running a script that requires a gem, a separate jRuby and a separate configuration"() {
74+
given:
75+
def output = new ByteArrayOutputStream()
76+
project.with {
77+
dependencies {
78+
jrubyExec 'rubygems:credit_card_validator:1.1.0'
79+
}
80+
jrubyexec {
81+
script "${TEST_SCRIPT_DIR}/requiresGem.rb"
82+
standardOutput output
83+
jrubyArgs '-T1'
84+
}
85+
}
86+
87+
when:
88+
project.evaluate()
89+
90+
then:
91+
output.toString() == "Not valid\n"
92+
}
93+
94+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
puts "Hello, " + ARGV[0]

src/main/groovy/com/lookout/jruby/GemUtils.groovy

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import org.gradle.api.file.DuplicateFileCopyingException
1010
* @author Schalk W. Cronjé
1111
*/
1212
class GemUtils {
13+
enum OverwriteAction { FAIL, SKIP, OVERWRITE }
14+
1315
static Boolean extractGem(Project p, File gem) {
1416
String gemName = gemFullNameFromFile(gem.getName())
1517
String installDir = p.jruby.gemInstallDir
@@ -38,15 +40,20 @@ class GemUtils {
3840
def jRubyClasspath,
3941
File gem,
4042
File destDir,
41-
boolean overwrite) {
43+
GemUtils.OverwriteAction overwrite) {
4244
String gemName = gemFullNameFromFile(gem.name)
4345
File extractDir = new File(destDir, gemName)
4446

4547
if (extractDir.exists()) {
46-
if(overwrite) {
47-
project.delete extractDir
48-
} else {
49-
throw new DuplicateFileCopyingException("Gem ${gem.name} already exists")
48+
49+
switch (overwrite) {
50+
case GemUtils.OverwriteAction.SKIP:
51+
return
52+
case GemUtils.OverwriteAction.OVERWRITE:
53+
project.delete extractDir
54+
break
55+
case GemUtils.OverwriteAction.FAIL:
56+
throw new DuplicateFileCopyingException("Gem ${gem.name} already exists")
5057
}
5158
}
5259

@@ -63,6 +70,15 @@ class GemUtils {
6370
}
6471
}
6572

73+
static void extractGems(Project project,def jRubyClasspath, Configuration gemConfig,File destDir,GemUtils.OverwriteAction action ) {
74+
gemConfig.files.findAll { File f ->
75+
f.name.endsWith('.gem')
76+
}.each { File f ->
77+
GemUtils.extractGem(project,jRubyClasspath,f,destDir,action)
78+
}
79+
80+
}
81+
6682
/** Take the given .gem filename (e.g. rake-10.3.2.gem) and just return the
6783
* gem "full name" (e.g. rake-10.3.2)
6884
*/

src/main/groovy/com/lookout/jruby/JRubyExec.groovy

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.lookout.jruby
22

3-
3+
import com.lookout.jruby.internal.JRubyExecUtils
44
import org.gradle.api.Project
55
import org.gradle.api.artifacts.Configuration
66
import org.gradle.api.file.FileCollection
@@ -143,12 +143,12 @@ class JRubyExec extends JavaExec {
143143
configurations.getByName(configuration).files.findAll { File f ->
144144
f.name.endsWith('.gem')
145145
}.each { File f ->
146-
GemUtils.extractGem(project,jrubyCompletePath,f,gemDir,true)
146+
GemUtils.extractGem(project,jrubyCompletePath,f,gemDir,GemUtils.OverwriteAction.OVERWRITE)
147147
}
148148
}
149149
}
150150

151-
super.classpath project.configurations.getByName(jrubyConfigurationName).files
151+
super.classpath JRubyExecUtils.classpathFromConfiguration(project.configurations.getByName(jrubyConfigurationName))
152152
super.setArgs(getArgs())
153153
super.exec()
154154
}
@@ -200,8 +200,8 @@ class JRubyExec extends JavaExec {
200200

201201
private File tmpGemDir() {
202202
String ext = FileUtils.toSafeFileName(jrubyConfigurationName)
203-
if(configuration) {
204-
ext= "${ext}-${FileUtils.toSafeFileName(configuration)}"
203+
if(configuration && configuration!=jrubyConfigurationName) {
204+
ext= ext + "-${FileUtils.toSafeFileName(configuration)}"
205205
}
206206
new File( project.buildDir, "tmp/${ext}").absoluteFile
207207
}

src/main/groovy/com/lookout/jruby/JRubyPlugin.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lookout.jruby
22

3+
import com.lookout.jruby.internal.JRubyExecDelegate
34
import org.gradle.api.Plugin
45
import org.gradle.api.Project
56
import org.gradle.api.tasks.Copy
@@ -33,6 +34,7 @@ class JRubyPlugin implements Plugin<Project> {
3334
gems
3435
}
3536
project.configurations.create(JRubyExec.JRUBYEXEC_CONFIG)
37+
JRubyExecDelegate.addToProject(project)
3638

3739
// In order for jrubyWar to work we'll need to pull in the warbler
3840
// bootstrap code from this artifact

0 commit comments

Comments
 (0)