Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to JRubyExec & project.jrubyexec to handle '-S' #64

Merged
merged 3 commits into from
Oct 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,15 @@ All other methods should work.
### Running a Ruby PATH command

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

```groovy
task rake( type :JRubyExec ) {
jrubyArgs '-S', 'rake'
script '/path/to/Rakefile'
scriptArgs 'target1', 'target2'
task rake( type : JRubyExec ) {
jrubyArgs '-S'
script 'rake'
scriptArgs '/path/to/Rakefile', 'target1', 'target2'
}
```

Expand All @@ -153,9 +154,9 @@ or even
ext {
rake = { String target ->
jrubyexec {
jrubyArgs '-S', 'rake'
script '/path/to/Rakefile'
scriptArgs target
jrubyArgs '-S'
script 'rake'
scriptArgs '/path/to/Rakefile', target
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ test {
systemProperties TEST_GEM_DIR : new File(projectDir,'src/integTest/resources/gems').absolutePath
systemProperties 'logback.configurationFile' : new File(projectDir,'src/test/resources/logback-test.xml').absolutePath

if(gradle.startParameter.isOffline()) {
systemProperties 'TESTS_ARE_OFFLINE' : '1'
}
}

integrationTest {
Expand Down
28 changes: 15 additions & 13 deletions src/main/groovy/com/github/jrubygradle/JRubyExec.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.jrubygradle

import com.github.jrubygradle.internal.JRubyExecUtils
import org.gradle.api.InvalidUserDataException
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.file.FileCollection
Expand Down Expand Up @@ -38,6 +39,7 @@ class JRubyExec extends JavaExec {
/** Script to execute.
*
*/
@Input
File script

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

/** getArgs gets overridden in order to add JRuby options, script name and script argumens in the correct order
/** getArgs gets overridden in order to add JRuby options, script name and script arguments in the correct order.
*
* There are three modes of behaviour
* <ul>
* <li> script set. no jrubyArgs, or jrubyArgs does not contain {@code -S} - Normal way to execute script. A check
* whether the script exists will be performed.
* <li> script set. jrubyArgs contains {@code -S} - If script is not absolute, no check will be performed to see
* if the script exists and will be assumed that the script can be found using the default ruby path mechanism.
* <li> script not set, but jrubyArgs set - Set up to execute jruby with no script. This should be a rarely used otion.
* </ul>
*
* @throw {@code org.gradle.api.InvalidUserDataException} if mode of behaviour cannot be determined.
*/
@Override
List<String> getArgs() {
def cmdArgs = []

if ((script == null) && (jrubyArgs.size() == 0)) {
throw new TaskInstantiationException('Cannot instantiate a JRubyExec instance without either `script` or `jrubyArgs` set')
}
cmdArgs.addAll(jrubyArgs)

if (script != null) {
cmdArgs.add(script.absolutePath)
}
cmdArgs.addAll(scriptArgs)
cmdArgs as List<String>
JRubyExecUtils.buildArgs(jrubyArgs,script,scriptArgs)
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,7 @@ class JRubyExecDelegate {
/** buildArgs creates a list of arguments to pass to the JVM
*/
List<String> buildArgs() {
def cmdArgs = []
cmdArgs.addAll(jrubyArgs)
cmdArgs.add(script.absolutePath)
cmdArgs.addAll(scriptArgs)
cmdArgs as List<String>
JRubyExecUtils.buildArgs(jrubyArgs,script,scriptArgs)
}

@PackageScope
Expand Down Expand Up @@ -117,7 +113,6 @@ class JRubyExecDelegate {
cl2.delegate = proxy
cl2.call()

proxy.validate()
File gemDir=new File(project.jruby.gemInstallDir)
Configuration config = project.configurations.getByName(JRUBYEXEC_CONFIG)
GemUtils.OverwriteAction overwrite = project.gradle.startParameter.refreshDependencies ? GemUtils.OverwriteAction.OVERWRITE : GemUtils.OverwriteAction.SKIP
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.github.jrubygradle.internal

import groovy.transform.CompileStatic
import org.gradle.api.InvalidUserDataException
import org.gradle.api.artifacts.Configuration
import org.gradle.api.file.FileCollection

/**
* @author Schalk W. Cronjé.
*/
@CompileStatic
class JRubyExecUtils {

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

static List<String> buildArgs( List<Object> jrubyArgs, File script, List<Object> scriptArgs ) {
def cmdArgs = []

boolean useBinPath = jrubyArgs.contains('-S')
cmdArgs.addAll(jrubyArgs)

if(script!=null && !useBinPath) {
if(!script.exists()) {
throw new InvalidUserDataException("${script} does not exist")
}
cmdArgs.add(script.absolutePath)
} else if(script!=null && useBinPath ) {
if(script.isAbsolute() && !script.exists()) {
throw new InvalidUserDataException("${script} does not exist")
}
cmdArgs.add(script.toString())
} else if(script==null && jrubyArgs.size() == 0 ) {
throw new InvalidUserDataException('Cannot instantiate a JRubyExec instance without either `script` or `jrubyArgs` set')
}
cmdArgs.addAll(scriptArgs)
cmdArgs as List<String>
}
}
7 changes: 4 additions & 3 deletions src/test/groovy/com/github/jrubygradle/JRubyExecSpec.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.jrubygradle

import org.gradle.api.InvalidUserDataException
import org.gradle.api.tasks.TaskInstantiationException
import org.gradle.testfixtures.ProjectBuilder
import spock.lang.*
Expand Down Expand Up @@ -135,12 +136,12 @@ class JRubyExecSpec extends Specification {
when:
project.configure(execTask) {
scriptArgs '-s1','-s2','-s3'
jrubyArgs '-j1','-j2','-j3'
jrubyArgs '-j1','-j2','-j3','-S'
script "${TEST_SCRIPT_DIR}/helloWorld.rb"
}

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

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

then: "An exception should be thrown"
thrown(TaskInstantiationException)
thrown(InvalidUserDataException)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import spock.lang.IgnoreIf
import spock.lang.Specification

import static org.gradle.api.logging.LogLevel.LIFECYCLE
import org.gradle.api.InvalidUserDataException

// ===============================================
// *** DO NOT CAll jrubyexec IN THIS UNITTEST ***
Expand Down Expand Up @@ -33,8 +34,8 @@ class JRubyExecDelegateSpec extends Specification {
def "When just passing script, scriptArgs, jrubyArgs, expect local properties to be updated"() {
given:
def cl = {
script '/path/to/file'
jrubyArgs 'c','d'
script 'path/to/file'
jrubyArgs 'c','d','-S'
scriptArgs '-x'
scriptArgs '-y','-z'
jrubyArgs 'a','b'
Expand All @@ -44,10 +45,28 @@ class JRubyExecDelegateSpec extends Specification {

expect:
jred.passthrough.size() == 0
jred.script == '/path/to/file'
jred.script == 'path/to/file'
jred.scriptArgs == ['-x','-y','-z']
jred.jrubyArgs == ['c','d','a','b']
jred.buildArgs() == ['c','d','a','b','/path/to/file','-x','-y','-z']
jred.jrubyArgs == ['c','d','-S','a','b']
jred.buildArgs() == ['c','d','-S','a','b','path/to/file','-x','-y','-z']
}

def "When passing absolute file and absolute file, expect check for existence to be executed"() {
given:
def cl = {
script '/path/to/file'
jrubyArgs 'c','d','-S'
scriptArgs '-x'
scriptArgs '-y','-z'
jrubyArgs 'a','b'
}
cl.delegate = jred
cl.call()
when:
jred.buildArgs()

then:
thrown(InvalidUserDataException)
}

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