Skip to content

Commit 53b7f7c

Browse files
committed
Support JRuby
1 parent 3c47818 commit 53b7f7c

File tree

3 files changed

+113
-26
lines changed

3 files changed

+113
-26
lines changed

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
[Ruby](https://www.ruby-lang.org/) language plugin for [vfox](https://vfox.lhan.me).
44

5-
For Linux and macos, both [Ruby(conda-forge)](https://github.com/conda-forge/ruby-feedstock) and [TruffleRuby](https://www.graalvm.org/ruby/) are provided.
5+
## Requirement
6+
7+
| Branch | Dependencies |
8+
| :---------: | :-----------------------------------------: |
9+
| Ruby | none |
10+
| JRuby | JRE v8 or higher |
11+
| TruffleRuby | `bash`, `make`, `gcc`, `g++` and `zlib-dev` |
612

713
## Install
814

@@ -23,6 +29,9 @@ Install the latest stable version with `latest` tag.
2329

2430
``` shell
2531
vfox install ruby@latest
32+
vfox install ruby@9.4.5.0 # JRuby
33+
vfox install ruby@24.0.1 # TruffleRuby
34+
vfox install ruby@24.0.1.jvm # TruffleRuby-jvm
2635
```
2736

2837
Some environment variables are served as following:
@@ -44,7 +53,4 @@ export GITHUB_URL=https://mirror.ghproxy.com/https://github.com/
4453
## FAQ
4554

4655
- **Why is there a lack of updated versions?** <br>
47-
Currently, vfox-ruby uses precompiled packages from conda-forge and Homebrew on Linux and macOS. You could open an issue in the [ruby-feedstock](https://github.com/conda-forge/ruby-feedstock/issues) repository to remind the maintainers to provide the latest build. Once the latest version is available, the plugin will be updated soon.
48-
49-
- **Are there any dependencies required to use this plugin?** <br>
50-
On Windows, vfox-ruby uses standalone 7-ZIP archives provided by [RubyInstaller](https://github.com/oneclick/rubyinstaller2/wiki/faq). On Linux and macOS, installing Ruby requires no dependencies other than the built-in commands. Installing TruffleRuby requires `bash`, `make`, `gcc`, `g++` and `zlib-dev`. For more details, refer to the [dependencies](https://github.com/oracle/truffleruby/blob/master/README.md#Dependencies) section.
56+
Currently, vfox-ruby uses precompiled packages from conda-forge and Homebrew on Linux and macOS. You could open an issue in the [ruby-feedstock](https://github.com/conda-forge/ruby-feedstock) repository to remind the maintainers to provide the latest build. Once the latest version is available, the plugin will be updated soon.

lib/util.lua

Lines changed: 101 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ function fetchAvailable(noCache)
6565
else
6666
result = fetchForUnix()
6767
end
68+
for _, v in ipairs(fetchJRubyVersions()) do
69+
table.insert(result, {
70+
version = v,
71+
note = "jruby",
72+
})
73+
end
6874

6975
return result
7076
end
@@ -83,7 +89,7 @@ function fetchForWindows()
8389
error("Failed to request: " .. err)
8490
end
8591
if resp.status_code ~= 200 then
86-
error("Failed to get information: " .. err .. "\nstatus_code => " .. resp.status_code)
92+
error("Failed to get Ruby versions: " .. err .. "\nstatus_code => " .. resp.status_code)
8793
end
8894

8995
for version in resp.body:gmatch('7z">Ruby (%d.%d.%d+)%-1 %(x64%)') do
@@ -150,6 +156,34 @@ function fetchForUnix()
150156
return result
151157
end
152158

159+
function fetchJRubyVersions()
160+
local versions = {}
161+
local patterns = {
162+
"(9%.1%.1[6-9]%.0)</a>",
163+
"(9%.2%.2%d%.%d)</a>",
164+
"(9%.3%.1%d%.%d)</a>",
165+
"(9%.[4-9]%.%d+%.%d)</a>",
166+
}
167+
local resp, err = http.get({
168+
url = "https://www.jruby.org/files/downloads/index.html",
169+
})
170+
if err ~= nil then
171+
error("Failed to request: " .. err)
172+
end
173+
if resp.status_code ~= 200 then
174+
error("Failed to get JRuby versions: " .. err .. "\nstatus_code => " .. resp.status_code)
175+
end
176+
177+
for _, pattern in ipairs(patterns) do
178+
for match in resp.body:gmatch(pattern) do
179+
table.insert(versions, match)
180+
end
181+
end
182+
sortVersions(versions)
183+
184+
return versions
185+
end
186+
153187
-- pre_install.lua
154188
function getDownloadInfo(version)
155189
local file
@@ -183,15 +217,12 @@ function getLatestVersion()
183217
end
184218

185219
function generateURL(version, osType, archType)
186-
local file
187-
local sha256
188-
local githubURL = os.getenv("GITHUB_URL") or "https://github.com/"
189-
if osType == "windows" then
190-
local bit = archType == "amd64" and "64" or "86"
191-
file = githubURL:gsub("/$", "")
192-
.. "/oneclick/rubyinstaller2/releases/download/RubyInstaller-%s-1/rubyinstaller-%s-1-x%s.7z"
193-
file = file:format(version, version, bit)
194-
sha256 = getSha256ForWindows(version, bit)
220+
local file, sha256
221+
222+
if compareVersion(version, "9") == 0 then
223+
file, sha256 = generateJRuby(version)
224+
elseif osType == "windows" then
225+
file, sha256 = generateWindowsRuby(version, archType)
195226
elseif osType ~= "darwin" and osType ~= "linux" then
196227
print("Unsupported OS: " .. osType)
197228
os.exit(1)
@@ -207,7 +238,42 @@ function generateURL(version, osType, archType)
207238
return file, sha256
208239
end
209240

210-
function getSha256ForWindows(version, bit)
241+
function generateJRuby(version)
242+
local file, sha256
243+
if not hasValue(fetchJRubyVersions(), version) then
244+
print("Unsupported version: " .. version)
245+
os.exit(1)
246+
end
247+
248+
if os.getenv("GITHUB_URL") then
249+
file = os.getenv("GITHUB_URL"):gsub("/$", "") .. "/jruby/jruby/releases/download/%s/jruby-bin-%s.tar.gz"
250+
else
251+
file = "https://repo1.maven.org/maven2/org/jruby/jruby-dist/%s/jruby-dist-%s-bin.tar.gz"
252+
end
253+
file = file:format(version, version)
254+
sha256 = "https://repo1.maven.org/maven2/org/jruby/jruby-dist/%s/jruby-dist-%s-bin.tar.gz.sha256"
255+
local resp, err = http.get({
256+
url = sha256:format(version, version),
257+
})
258+
if err ~= nil then
259+
error("Failed to request: " .. err)
260+
end
261+
if resp.status_code ~= 200 then
262+
error("Failed to get sha256: " .. err .. "\nstatus_code => " .. resp.status_code)
263+
end
264+
sha256 = resp.body
265+
266+
return file, sha256
267+
end
268+
269+
function generateWindowsRuby(version, archType)
270+
local file
271+
local bit = archType == "amd64" and "64" or "86"
272+
local githubURL = os.getenv("GITHUB_URL") or "https://github.com/"
273+
file = githubURL:gsub("/$", "")
274+
.. "/oneclick/rubyinstaller2/releases/download/RubyInstaller-%s-1/rubyinstaller-%s-1-x%s.7z"
275+
file = file:format(version, version, bit)
276+
211277
local resp, err = http.get({
212278
url = "https://rubyinstaller.org/downloads/archives/",
213279
})
@@ -219,7 +285,7 @@ function getSha256ForWindows(version, bit)
219285
end
220286
local sha256 = resp.body:match(version .. "%-1%-x" .. bit .. '.7z[%s%S]-value="([0-9a-z]+)')
221287

222-
return sha256
288+
return file, sha256
223289
end
224290

225291
function hasValue(table, value)
@@ -281,15 +347,31 @@ end
281347

282348
-- post_install.lua
283349
function unixInstall(path, version)
284-
if compareVersion(version, "20.0.0") >= 0 then
285-
patchTruffleRuby(path)
286-
elseif hasValue(HomebrewRubyVersions, version) then
350+
if hasValue(HomebrewRubyVersions, version) then
287351
patchHomebrewRuby(path, version)
352+
elseif compareVersion(version, "20.0.0") >= 0 then
353+
patchTruffleRuby(path)
354+
elseif compareVersion(version, "9") == 0 then
355+
patchJRuby(path)
288356
else
289357
mambaInstall(path, version)
290358
end
291359
end
292360

361+
function patchHomebrewRuby(path, version)
362+
local command1 = "mv " .. path .. "/" .. version .. "/* " .. path
363+
local command2 = "mkdir -p " .. path .. "/share/gems/bin"
364+
local command3 = "rm -rf " .. path .. "/.brew " .. path .. "/" .. version
365+
366+
for _, command in ipairs({ command1, command2, command3 }) do
367+
local status = os.execute(command)
368+
if status ~= 0 then
369+
print("Failed to execute command: " .. command)
370+
os.exit(1)
371+
end
372+
end
373+
end
374+
293375
function patchTruffleRuby(path)
294376
local command1 = path .. "/lib/truffle/post_install_hook.sh > /dev/null"
295377
local command2 = "mkdir -p " .. path .. "/share/gems/bin"
@@ -304,12 +386,11 @@ function patchTruffleRuby(path)
304386
end
305387
end
306388

307-
function patchHomebrewRuby(path, version)
308-
local command1 = "mv " .. path .. "/" .. version .. "/* " .. path
309-
local command2 = "mkdir -p " .. path .. "/share/gems/bin"
310-
local command3 = "rm -rf " .. path .. "/.brew " .. path .. "/" .. version
389+
function patchJRuby(path)
390+
local command1 = "mkdir -p " .. path .. "/share/gems/bin"
391+
local command2 = "rm -f " .. path .. "/bin/*.exe " .. path .. "/bin/*.bat " .. path .. "/bin/*.dll"
311392

312-
for _, command in ipairs({ command1, command2, command3 }) do
393+
for _, command in ipairs({ command1, command2 }) do
313394
local status = os.execute(command)
314395
if status ~= 0 then
315396
print("Failed to execute command: " .. command)

metadata.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ PLUGIN = {}
55
--- Plugin name
66
PLUGIN.name = "ruby"
77
--- Plugin version
8-
PLUGIN.version = "0.4.1"
8+
PLUGIN.version = "0.4.2"
99
--- Plugin homepage
1010
PLUGIN.homepage = "https://github.com/yanecc/vfox-ruby"
1111
--- Plugin license, please choose a correct license according to your needs.

0 commit comments

Comments
 (0)