forked from elastic/logstash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrubyUtils.gradle
251 lines (222 loc) · 8.43 KB
/
rubyUtils.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.yaml:snakeyaml:1.23'
classpath "de.undercouch:gradle-download-task:3.2.0"
classpath "org.jruby:jruby-complete:9.2.7.0"
}
}
import de.undercouch.gradle.tasks.download.Download
import de.undercouch.gradle.tasks.download.Verify
import org.yaml.snakeyaml.Yaml
import org.jruby.Ruby
import org.jruby.embed.PathType
import org.jruby.embed.ScriptingContainer
import java.nio.file.Files
import java.nio.file.Paths
ext {
bundle = this.&bundle
bundleWithEnv = this.&bundleWithEnv
gem = this.&gem
buildGem = this.&buildGem
rake = this.&rake
versionMap = new HashMap()
}
/**
* Executes a bundler bin script with given parameters.
* @param projectDir Gradle projectDir
* @param buildDir Gradle buildDir
* @param pwd Current worker directory to execute in
* @param bundleBin Bundler Bin Script
* @param args CLI Args to Use with Bundler
*/
void bundle(File projectDir, File buildDir, String pwd, String bundleBin, Iterable<String> args) {
bundleWithEnv(projectDir, buildDir, pwd, bundleBin, args, Collections.emptyMap())
}
/**
* Executes a bundler bin script with given parameters.
* @param projectDir Gradle projectDir
* @param buildDir Gradle buildDir
* @param pwd Current worker directory to execute in
* @param bundleBin Bundler Bin Script
* @param args CLI Args to Use with Bundler
* @param env Environment Variables to Set
*/
void bundleWithEnv(File projectDir, File buildDir, String pwd, String bundleBin, Iterable<String> args, Map<String, String> env) {
executeJruby projectDir, buildDir, { ScriptingContainer jruby ->
jruby.environment.putAll(env)
jruby.currentDirectory = pwd
jruby.argv = args.toList().toArray()
jruby.runScriptlet(PathType.ABSOLUTE, bundleBin)
}
}
/**
* Installs a Gem with the given version to the given path.
* @param projectDir Gradle projectDir
* @param buildDir Gradle buildDir
* @param gem Gem Name
* @param version Version to Install
* @param path Path to Install to
*/
void gem(File projectDir, File buildDir, String gem, String version, String path) {
executeJruby projectDir, buildDir, { ScriptingContainer jruby ->
jruby.currentDirectory = projectDir
jruby.runScriptlet("""
require 'rubygems/commands/install_command'
cmd = Gem::Commands::InstallCommand.new
cmd.handle_options ['--no-ri', '--no-rdoc', '${gem}', '-v', '${version}', '-i', '${path}']
begin
cmd.execute
rescue Gem::SystemExitException => e
raise e unless e.exit_code == 0
end
"""
)
}
}
void buildGem(File projectDir, File buildDir, String gemspec) {
executeJruby projectDir, buildDir, { ScriptingContainer jruby ->
jruby.currentDirectory = projectDir
jruby.runScriptlet("""
require 'rubygems/commands/build_command'
cmd = Gem::Commands::BuildCommand.new
cmd.handle_options ['${gemspec}']
begin
cmd.execute
rescue Gem::SystemExitException => e
raise e unless e.exit_code == 0
end
"""
)
}
}
/**
* Executes RSpec for a given plugin.
* @param projectDir Gradle projectDir
* @param buildDir Gradle buildDir
* @param plugin Plugin to run specs for
* @param args CLI arguments to pass to rspec
*/
void rake(File projectDir, File buildDir, String task) {
executeJruby projectDir, buildDir, { ScriptingContainer jruby ->
jruby.currentDirectory = projectDir
jruby.runScriptlet("require 'rake'")
jruby.runScriptlet("""
rake = Rake.application
rake.init
rake.load_rakefile
rake['${task}'].invoke
"""
)
}
}
/**
* Executes Closure using a fresh JRuby environment, safely tearing it down afterwards.
* @param projectDir Gradle projectDir
* @param buildDir Gradle buildDir
* @param block Closure to run
*/
Object executeJruby(File projectDir, File buildDir, Closure<?> /* Object*/ block) {
def jruby = new ScriptingContainer()
def env = jruby.environment
def gemDir = "${projectDir}/vendor/bundle/jruby/2.5.0".toString()
env.put "USE_RUBY", "1"
env.put "GEM_HOME", gemDir
env.put "GEM_SPEC_CACHE", "${buildDir}/cache".toString()
env.put "GEM_PATH", gemDir
try {
block(jruby)
} finally {
jruby.terminate()
Ruby.clearGlobalRuntime()
}
}
//===============================================================================
// Ruby variables
//===============================================================================
def versionsPath = project.hasProperty("LOGSTASH_CORE_PATH") ? LOGSTASH_CORE_PATH + "/../versions.yml" : "${projectDir}/versions.yml"
versionMap = (Map) (new Yaml()).load(new File("${versionsPath}").text)
String jRubyURL
String jRubyVersion
String jRubySha1
Boolean doChecksum
if (versionMap["jruby-runtime-override"]) {
jRubyVersion = versionMap["jruby-runtime-override"]["version"]
jRubyURL = versionMap["jruby-runtime-override"]["url"]
doChecksum = false
} else {
jRubyVersion = versionMap["jruby"]["version"]
jRubySha1 = versionMap["jruby"]["sha1"]
jRubyURL = "https://repo1.maven.org/maven2/org/jruby/jruby-dist/${jRubyVersion}/jruby-dist-${jRubyVersion}-bin.tar.gz"
doChecksum = true
}
def jrubyTarPath = "${projectDir}/vendor/_/jruby-dist-${jRubyVersion}-bin.tar.gz"
def customJRubyDir = project.hasProperty("custom.jruby.path") ? project.property("custom.jruby.path") : ""
def customJRubyVersion = customJRubyDir == "" ? "" : Files.readAllLines(Paths.get(customJRubyDir, "VERSION")).get(0).trim()
def customJRubyTar = customJRubyDir == "" ? "" : (customJRubyDir + "/maven/jruby-dist/target/jruby-dist-${customJRubyVersion}-bin.tar.gz")
task downloadJRuby(type: Download) {
description "Download JRuby artifact from this specific URL: ${jRubyURL}"
src jRubyURL
onlyIfNewer true
inputs.file("${projectDir}/versions.yml")
outputs.file(jrubyTarPath)
dest new File("${projectDir}/vendor/_", "jruby-dist-${jRubyVersion}-bin.tar.gz")
}
downloadJRuby.onlyIf { customJRubyDir == "" }
task verifyFile(dependsOn: downloadJRuby, type: Verify) {
description "Verify the SHA1 of the download JRuby artifact"
inputs.file(jrubyTarPath)
outputs.file(jrubyTarPath)
src new File(jrubyTarPath)
algorithm 'SHA-1'
checksum jRubySha1
}
verifyFile.onlyIf { customJRubyDir == "" }
verifyFile.onlyIf { doChecksum }
task buildCustomJRuby(type: Exec) {
description "Build tar.gz and .jar artifacts from JRuby source directory"
workingDir (customJRubyDir == "" ? "./" : customJRubyDir)
commandLine './mvnw', 'clean', 'install', '-Pdist', '-Pcomplete'
standardOutput = new ByteArrayOutputStream()
errorOutput = new ByteArrayOutputStream()
ext.output = {
standardOutput.toString() + errorOutput.toString()
}
}
buildCustomJRuby.onlyIf { customJRubyDir != "" }
task installCustomJRuby(dependsOn: buildCustomJRuby, type: Copy) {
description "Install custom built JRuby in the vendor directory"
inputs.file(customJRubyTar)
outputs.dir("${projectDir}/vendor/jruby")
from tarTree(customJRubyTar == "" ? jrubyTarPath : customJRubyTar)
eachFile { f ->
f.path = f.path.replaceFirst("^jruby-${customJRubyVersion}", '')
}
exclude "**/stdlib/rdoc/**"
includeEmptyDirs = false
into "${projectDir}/vendor/jruby"
}
installCustomJRuby.onlyIf { customJRubyDir != "" }
task downloadAndInstallJRuby(dependsOn: [verifyFile, installCustomJRuby], type: Copy) {
description "Install JRuby in the vendor directory"
inputs.file(jrubyTarPath)
outputs.dir("${projectDir}/vendor/jruby")
from tarTree(downloadJRuby.dest)
eachFile { f ->
f.path = f.path.replaceFirst("^jruby-${jRubyVersion}", '')
}
exclude "**/stdlib/rdoc/**"
exclude "**/stdlib/bundler/**"
exclude "**/stdlib/bundler.rb"
exclude "**/bundler-1.16.6/*"
exclude "**/bundler-1.16.6.*"
includeEmptyDirs = false
into "${projectDir}/vendor/jruby"
doLast {
gem(projectDir, buildDir, "rake", "12.3.1", "${projectDir}/vendor/bundle/jruby/2.5.0")
gem(projectDir, buildDir, "json", "1.8.6", "${projectDir}/vendor/bundle/jruby/2.5.0")
}
}
downloadAndInstallJRuby.onlyIf { customJRubyDir == "" }