Skip to content

Commit d3793c9

Browse files
author
R. Tyler Croy
committed
Merge pull request #64 from ysb33r/master
Updates to JRubyExec & project.jrubyexec to handle '-S'
2 parents 668262f + e4b9710 commit d3793c9

File tree

7 files changed

+82
-36
lines changed

7 files changed

+82
-36
lines changed

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,15 @@ All other methods should work.
136136
### Running a Ruby PATH command
137137

138138
Because `JRubyExec` checks for the existence of the script, it might look at first whether running Ruby commands from
139-
`PATH` could be difficult. However, this is totally possible by utilising `jrubyArgs`. Here is an example of running
139+
`PATH` could be difficult. However, this is totally possible by utilising `jrubyArgs` and passing `-S` as one would do
140+
when using `ruby` or `jruby` on the command-line. Here is an example of running
140141
`rake` as task.
141142

142143
```groovy
143-
task rake( type :JRubyExec ) {
144-
jrubyArgs '-S', 'rake'
145-
script '/path/to/Rakefile'
146-
scriptArgs 'target1', 'target2'
144+
task rake( type : JRubyExec ) {
145+
jrubyArgs '-S'
146+
script 'rake'
147+
scriptArgs '/path/to/Rakefile', 'target1', 'target2'
147148
}
148149
```
149150

@@ -153,9 +154,9 @@ or even
153154
ext {
154155
rake = { String target ->
155156
jrubyexec {
156-
jrubyArgs '-S', 'rake'
157-
script '/path/to/Rakefile'
158-
scriptArgs target
157+
jrubyArgs '-S'
158+
script 'rake'
159+
scriptArgs '/path/to/Rakefile', target
159160
}
160161
}
161162
}

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ test {
8383
systemProperties TEST_GEM_DIR : new File(projectDir,'src/integTest/resources/gems').absolutePath
8484
systemProperties 'logback.configurationFile' : new File(projectDir,'src/test/resources/logback-test.xml').absolutePath
8585

86+
if(gradle.startParameter.isOffline()) {
87+
systemProperties 'TESTS_ARE_OFFLINE' : '1'
88+
}
8689
}
8790

8891
integrationTest {

src/main/groovy/com/github/jrubygradle/JRubyExec.groovy

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.jrubygradle
22

33
import com.github.jrubygradle.internal.JRubyExecUtils
4+
import org.gradle.api.InvalidUserDataException
45
import org.gradle.api.Project
56
import org.gradle.api.artifacts.Configuration
67
import org.gradle.api.file.FileCollection
@@ -38,6 +39,7 @@ class JRubyExec extends JavaExec {
3839
/** Script to execute.
3940
*
4041
*/
42+
@Input
4143
File script
4244

4345
/** Configuration to copy gems from. If {@code jRubyVersion} has not been set, {@code jRubyExec} will used as
@@ -152,22 +154,22 @@ class JRubyExec extends JavaExec {
152154
super.exec()
153155
}
154156

155-
/** getArgs gets overridden in order to add JRuby options, script name and script argumens in the correct order
157+
/** getArgs gets overridden in order to add JRuby options, script name and script arguments in the correct order.
158+
*
159+
* There are three modes of behaviour
160+
* <ul>
161+
* <li> script set. no jrubyArgs, or jrubyArgs does not contain {@code -S} - Normal way to execute script. A check
162+
* whether the script exists will be performed.
163+
* <li> script set. jrubyArgs contains {@code -S} - If script is not absolute, no check will be performed to see
164+
* if the script exists and will be assumed that the script can be found using the default ruby path mechanism.
165+
* <li> script not set, but jrubyArgs set - Set up to execute jruby with no script. This should be a rarely used otion.
166+
* </ul>
167+
*
168+
* @throw {@code org.gradle.api.InvalidUserDataException} if mode of behaviour cannot be determined.
156169
*/
157170
@Override
158171
List<String> getArgs() {
159-
def cmdArgs = []
160-
161-
if ((script == null) && (jrubyArgs.size() == 0)) {
162-
throw new TaskInstantiationException('Cannot instantiate a JRubyExec instance without either `script` or `jrubyArgs` set')
163-
}
164-
cmdArgs.addAll(jrubyArgs)
165-
166-
if (script != null) {
167-
cmdArgs.add(script.absolutePath)
168-
}
169-
cmdArgs.addAll(scriptArgs)
170-
cmdArgs as List<String>
172+
JRubyExecUtils.buildArgs(jrubyArgs,script,scriptArgs)
171173
}
172174

173175
@Override

src/main/groovy/com/github/jrubygradle/internal/JRubyExecDelegate.groovy

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,7 @@ class JRubyExecDelegate {
7777
/** buildArgs creates a list of arguments to pass to the JVM
7878
*/
7979
List<String> buildArgs() {
80-
def cmdArgs = []
81-
cmdArgs.addAll(jrubyArgs)
82-
cmdArgs.add(script.absolutePath)
83-
cmdArgs.addAll(scriptArgs)
84-
cmdArgs as List<String>
80+
JRubyExecUtils.buildArgs(jrubyArgs,script,scriptArgs)
8581
}
8682

8783
@PackageScope
@@ -117,7 +113,6 @@ class JRubyExecDelegate {
117113
cl2.delegate = proxy
118114
cl2.call()
119115

120-
proxy.validate()
121116
File gemDir=new File(project.jruby.gemInstallDir)
122117
Configuration config = project.configurations.getByName(JRUBYEXEC_CONFIG)
123118
GemUtils.OverwriteAction overwrite = project.gradle.startParameter.refreshDependencies ? GemUtils.OverwriteAction.OVERWRITE : GemUtils.OverwriteAction.SKIP

src/main/groovy/com/github/jrubygradle/internal/JRubyExecUtils.groovy

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.github.jrubygradle.internal
22

3+
import groovy.transform.CompileStatic
4+
import org.gradle.api.InvalidUserDataException
35
import org.gradle.api.artifacts.Configuration
46
import org.gradle.api.file.FileCollection
57

68
/**
79
* @author Schalk W. Cronjé.
810
*/
11+
@CompileStatic
912
class JRubyExecUtils {
1013

1114
/** Extract a list of files from a configuration that is suitable for a jruby classpath
@@ -32,7 +35,29 @@ class JRubyExecUtils {
3235
* @return Returns the classpath as a File or null if the jar was not found
3336
*/
3437
static FileCollection jrubyJar(FileCollection fc) {
35-
fc.filter { File f -> it.name.startsWith('jruby-complete-') }
38+
fc.filter { File f -> f.name.startsWith('jruby-complete-') }
3639
}
3740

41+
static List<String> buildArgs( List<Object> jrubyArgs, File script, List<Object> scriptArgs ) {
42+
def cmdArgs = []
43+
44+
boolean useBinPath = jrubyArgs.contains('-S')
45+
cmdArgs.addAll(jrubyArgs)
46+
47+
if(script!=null && !useBinPath) {
48+
if(!script.exists()) {
49+
throw new InvalidUserDataException("${script} does not exist")
50+
}
51+
cmdArgs.add(script.absolutePath)
52+
} else if(script!=null && useBinPath ) {
53+
if(script.isAbsolute() && !script.exists()) {
54+
throw new InvalidUserDataException("${script} does not exist")
55+
}
56+
cmdArgs.add(script.toString())
57+
} else if(script==null && jrubyArgs.size() == 0 ) {
58+
throw new InvalidUserDataException('Cannot instantiate a JRubyExec instance without either `script` or `jrubyArgs` set')
59+
}
60+
cmdArgs.addAll(scriptArgs)
61+
cmdArgs as List<String>
62+
}
3863
}

src/test/groovy/com/github/jrubygradle/JRubyExecSpec.groovy

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.jrubygradle
22

3+
import org.gradle.api.InvalidUserDataException
34
import org.gradle.api.tasks.TaskInstantiationException
45
import org.gradle.testfixtures.ProjectBuilder
56
import spock.lang.*
@@ -135,12 +136,12 @@ class JRubyExecSpec extends Specification {
135136
when:
136137
project.configure(execTask) {
137138
scriptArgs '-s1','-s2','-s3'
138-
jrubyArgs '-j1','-j2','-j3'
139+
jrubyArgs '-j1','-j2','-j3','-S'
139140
script "${TEST_SCRIPT_DIR}/helloWorld.rb"
140141
}
141142

142143
then:
143-
execTask.getArgs() == ['-j1','-j2','-j3',new File(TEST_SCRIPT_DIR,'helloWorld.rb').absolutePath,'-s1','-s2','-s3']
144+
execTask.getArgs() == ['-j1','-j2','-j3','-S',new File(TEST_SCRIPT_DIR,'helloWorld.rb').toString(),'-s1','-s2','-s3']
144145
}
145146

146147
def "Properly handle the lack of a `script` argument"() {
@@ -160,7 +161,7 @@ class JRubyExecSpec extends Specification {
160161
execTask.getArgs()
161162

162163
then: "An exception should be thrown"
163-
thrown(TaskInstantiationException)
164+
thrown(InvalidUserDataException)
164165
}
165166

166167
}

src/test/groovy/com/github/jrubygradle/internal/JRubyExecDelegateSpec.groovy

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import spock.lang.IgnoreIf
66
import spock.lang.Specification
77

88
import static org.gradle.api.logging.LogLevel.LIFECYCLE
9+
import org.gradle.api.InvalidUserDataException
910

1011
// ===============================================
1112
// *** DO NOT CAll jrubyexec IN THIS UNITTEST ***
@@ -33,8 +34,8 @@ class JRubyExecDelegateSpec extends Specification {
3334
def "When just passing script, scriptArgs, jrubyArgs, expect local properties to be updated"() {
3435
given:
3536
def cl = {
36-
script '/path/to/file'
37-
jrubyArgs 'c','d'
37+
script 'path/to/file'
38+
jrubyArgs 'c','d','-S'
3839
scriptArgs '-x'
3940
scriptArgs '-y','-z'
4041
jrubyArgs 'a','b'
@@ -44,10 +45,28 @@ class JRubyExecDelegateSpec extends Specification {
4445

4546
expect:
4647
jred.passthrough.size() == 0
47-
jred.script == '/path/to/file'
48+
jred.script == 'path/to/file'
4849
jred.scriptArgs == ['-x','-y','-z']
49-
jred.jrubyArgs == ['c','d','a','b']
50-
jred.buildArgs() == ['c','d','a','b','/path/to/file','-x','-y','-z']
50+
jred.jrubyArgs == ['c','d','-S','a','b']
51+
jred.buildArgs() == ['c','d','-S','a','b','path/to/file','-x','-y','-z']
52+
}
53+
54+
def "When passing absolute file and absolute file, expect check for existence to be executed"() {
55+
given:
56+
def cl = {
57+
script '/path/to/file'
58+
jrubyArgs 'c','d','-S'
59+
scriptArgs '-x'
60+
scriptArgs '-y','-z'
61+
jrubyArgs 'a','b'
62+
}
63+
cl.delegate = jred
64+
cl.call()
65+
when:
66+
jred.buildArgs()
67+
68+
then:
69+
thrown(InvalidUserDataException)
5170
}
5271

5372
def "When just passing arbitrary javaexec, expect them to be stored"() {

0 commit comments

Comments
 (0)