Skip to content

Commit c4cf57d

Browse files
committed
Merge pull request #74 from ysb33r/master
Added support for gemWorkDir - Fixes #73
2 parents b4d8779 + 0b39b13 commit c4cf57d

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ if (System.env.RELEASE != '1') {
2121
version = "${version}-SNAPSHOT"
2222
}
2323
sourceCompatibility = '1.7'
24+
targetCompatibility = '1.7'
2425

2526
// --- Could be in a separate gradle file
2627
configurations {

src/integTest/groovy/com/github/jrubygradle/JRubyExecIntegrationSpec.groovy

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,26 @@ class JRubyExecIntegrationSpec extends Specification {
106106
output.toString() == "Not valid\n"
107107
}
108108

109+
@IgnoreIf({TESTS_ARE_OFFLINE})
110+
def "Running a script that has a custom gemdir"() {
111+
given:
112+
def output = new ByteArrayOutputStream()
113+
project.configure(execTask) {
114+
setEnvironment [:]
115+
script "${TEST_SCRIPT_DIR}/requiresGem.rb"
116+
standardOutput output
117+
gemWorkDir new File(TESTROOT,'customGemDir')
118+
}
119+
120+
when:
121+
project.dependencies.add(JRubyExec.JRUBYEXEC_CONFIG,'rubygems:credit_card_validator:1.2.0' )
122+
project.evaluate()
123+
execTask.exec()
124+
125+
then:
126+
output.toString() == "Not valid\n"
127+
new File(TESTROOT,'customGemDir').exists()
128+
129+
}
130+
109131
}

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.gradle.api.tasks.InputFile
1010
import org.gradle.api.tasks.InputFiles
1111
import org.gradle.api.tasks.JavaExec
1212
import org.gradle.api.tasks.Optional
13+
import org.gradle.api.tasks.OutputDirectory
1314
import org.gradle.api.tasks.TaskInstantiationException
1415
import org.gradle.internal.FileUtils
1516
import org.gradle.process.ExecResult
@@ -55,6 +56,9 @@ class JRubyExec extends JavaExec {
5556
@Input
5657
String jrubyVersion
5758

59+
@OutputDirectory
60+
File gemWorkDir
61+
5862
JRubyExec() {
5963
super()
6064
super.setMain 'org.jruby.Main'
@@ -86,6 +90,27 @@ class JRubyExec extends JavaExec {
8690
}
8791
}
8892

93+
/** Sets the working directory used by the task for installing GEMs.
94+
* By default every task will use its own private directory, so that tasks
95+
* can be run in isolation avoiding potential clashes between different versions
96+
* of the same GEM. If the user requires multiple tasks to share the same GEM area
97+
* then this configuration property can be used to to provide such a folder..
98+
*
99+
* @param fName Path to script
100+
*/
101+
void setGemWorkDir(Object fName) {
102+
switch (fName) {
103+
case File:
104+
gemWorkDir=fName
105+
break
106+
case String:
107+
gemWorkDir = new File(fName)
108+
break
109+
default:
110+
gemWorkDir = new File(fName.toString())
111+
}
112+
}
113+
89114
/** Returns a list of script arguments
90115
*/
91116
List<String> scriptArgs() {
@@ -148,17 +173,19 @@ class JRubyExec extends JavaExec {
148173

149174
GemUtils.OverwriteAction overwrite = project.gradle.startParameter.refreshDependencies ? GemUtils.OverwriteAction.OVERWRITE : GemUtils.OverwriteAction.SKIP
150175
def jrubyCompletePath = project.configurations.getByName(jrubyConfigurationName)
151-
File gemDir = tmpGemDir()
152-
gemDir.mkdirs()
153-
environment 'GEM_HOME' : gemDir,
176+
if(gemWorkDir == null) {
177+
gemWorkDir = tmpGemDir()
178+
}
179+
gemWorkDir.mkdirs()
180+
environment 'GEM_HOME' : gemWorkDir,
154181
'PATH' : getComputedPATH(System.env.PATH)
155182

156183
if (configuration != null) {
157184
GemUtils.extractGems(
158185
project,
159186
jrubyCompletePath,
160187
project.configurations.getByName(configuration),
161-
gemDir,
188+
gemWorkDir,
162189
overwrite
163190
)
164191
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,15 @@ class JRubyExecSpec extends Specification {
176176
then:
177177
execTask.getComputedPATH(System.env.PATH).contains('foo')
178178
}
179+
180+
def "Override the GEM working directory"() {
181+
when:
182+
project.configure(execTask) {
183+
gemWorkDir 'foobar'
184+
}
185+
186+
then:
187+
execTask.gemWorkDir == new File('foobar')
188+
}
189+
179190
}

0 commit comments

Comments
 (0)