Skip to content

Commit 49fe463

Browse files
committed
An update to @rtyler's commit that looks for -S in jrubyArgs of JRubyExec.
1 parent 668262f commit 49fe463

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
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: 26 additions & 5 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,19 +154,38 @@ 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() {
159172
def cmdArgs = []
160173

161-
if ((script == null) && (jrubyArgs.size() == 0)) {
162-
throw new TaskInstantiationException('Cannot instantiate a JRubyExec instance without either `script` or `jrubyArgs` set')
163-
}
174+
boolean useBinPath = jrubyArgs.contains('-S')
164175
cmdArgs.addAll(jrubyArgs)
165176

166-
if (script != null) {
177+
if(script!=null && !useBinPath) {
178+
if(!script.exists()) {
179+
throw new InvalidUserDataException("${script} does not exist")
180+
}
167181
cmdArgs.add(script.absolutePath)
182+
} else if(script!=null && useBinPath ) {
183+
if(script.isAbsolute() && !script.exists()) {
184+
throw new InvalidUserDataException("${script} does not exist")
185+
}
186+
cmdArgs.add(script.toString())
187+
} else if(script==null && jrubyArgs.size() == 0 ) {
188+
throw new InvalidUserDataException('Cannot instantiate a JRubyExec instance without either `script` or `jrubyArgs` set')
168189
}
169190
cmdArgs.addAll(scriptArgs)
170191
cmdArgs as List<String>

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
}

0 commit comments

Comments
 (0)