Skip to content

Commit

Permalink
Support JRuby
Browse files Browse the repository at this point in the history
  • Loading branch information
yanecc committed Jun 10, 2024
1 parent 3c47818 commit 53b7f7c
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 26 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

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

For Linux and macos, both [Ruby(conda-forge)](https://github.com/conda-forge/ruby-feedstock) and [TruffleRuby](https://www.graalvm.org/ruby/) are provided.
## Requirement

| Branch | Dependencies |
| :---------: | :-----------------------------------------: |
| Ruby | none |
| JRuby | JRE v8 or higher |
| TruffleRuby | `bash`, `make`, `gcc`, `g++` and `zlib-dev` |

## Install

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

``` shell
vfox install ruby@latest
vfox install ruby@9.4.5.0 # JRuby
vfox install ruby@24.0.1 # TruffleRuby
vfox install ruby@24.0.1.jvm # TruffleRuby-jvm
```

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

- **Why is there a lack of updated versions?** <br>
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.

- **Are there any dependencies required to use this plugin?** <br>
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.
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.
121 changes: 101 additions & 20 deletions lib/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ function fetchAvailable(noCache)
else
result = fetchForUnix()
end
for _, v in ipairs(fetchJRubyVersions()) do
table.insert(result, {
version = v,
note = "jruby",
})
end

return result
end
Expand All @@ -83,7 +89,7 @@ function fetchForWindows()
error("Failed to request: " .. err)
end
if resp.status_code ~= 200 then
error("Failed to get information: " .. err .. "\nstatus_code => " .. resp.status_code)
error("Failed to get Ruby versions: " .. err .. "\nstatus_code => " .. resp.status_code)
end

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

function fetchJRubyVersions()
local versions = {}
local patterns = {
"(9%.1%.1[6-9]%.0)</a>",
"(9%.2%.2%d%.%d)</a>",
"(9%.3%.1%d%.%d)</a>",
"(9%.[4-9]%.%d+%.%d)</a>",
}
local resp, err = http.get({
url = "https://www.jruby.org/files/downloads/index.html",
})
if err ~= nil then
error("Failed to request: " .. err)
end
if resp.status_code ~= 200 then
error("Failed to get JRuby versions: " .. err .. "\nstatus_code => " .. resp.status_code)
end

for _, pattern in ipairs(patterns) do
for match in resp.body:gmatch(pattern) do
table.insert(versions, match)
end
end
sortVersions(versions)

return versions
end

-- pre_install.lua
function getDownloadInfo(version)
local file
Expand Down Expand Up @@ -183,15 +217,12 @@ function getLatestVersion()
end

function generateURL(version, osType, archType)
local file
local sha256
local githubURL = os.getenv("GITHUB_URL") or "https://github.com/"
if osType == "windows" then
local bit = archType == "amd64" and "64" or "86"
file = githubURL:gsub("/$", "")
.. "/oneclick/rubyinstaller2/releases/download/RubyInstaller-%s-1/rubyinstaller-%s-1-x%s.7z"
file = file:format(version, version, bit)
sha256 = getSha256ForWindows(version, bit)
local file, sha256

if compareVersion(version, "9") == 0 then
file, sha256 = generateJRuby(version)
elseif osType == "windows" then
file, sha256 = generateWindowsRuby(version, archType)
elseif osType ~= "darwin" and osType ~= "linux" then
print("Unsupported OS: " .. osType)
os.exit(1)
Expand All @@ -207,7 +238,42 @@ function generateURL(version, osType, archType)
return file, sha256
end

function getSha256ForWindows(version, bit)
function generateJRuby(version)
local file, sha256
if not hasValue(fetchJRubyVersions(), version) then
print("Unsupported version: " .. version)
os.exit(1)
end

if os.getenv("GITHUB_URL") then
file = os.getenv("GITHUB_URL"):gsub("/$", "") .. "/jruby/jruby/releases/download/%s/jruby-bin-%s.tar.gz"
else
file = "https://repo1.maven.org/maven2/org/jruby/jruby-dist/%s/jruby-dist-%s-bin.tar.gz"
end
file = file:format(version, version)
sha256 = "https://repo1.maven.org/maven2/org/jruby/jruby-dist/%s/jruby-dist-%s-bin.tar.gz.sha256"
local resp, err = http.get({
url = sha256:format(version, version),
})
if err ~= nil then
error("Failed to request: " .. err)
end
if resp.status_code ~= 200 then
error("Failed to get sha256: " .. err .. "\nstatus_code => " .. resp.status_code)
end
sha256 = resp.body

return file, sha256
end

function generateWindowsRuby(version, archType)
local file
local bit = archType == "amd64" and "64" or "86"
local githubURL = os.getenv("GITHUB_URL") or "https://github.com/"
file = githubURL:gsub("/$", "")
.. "/oneclick/rubyinstaller2/releases/download/RubyInstaller-%s-1/rubyinstaller-%s-1-x%s.7z"
file = file:format(version, version, bit)

local resp, err = http.get({
url = "https://rubyinstaller.org/downloads/archives/",
})
Expand All @@ -219,7 +285,7 @@ function getSha256ForWindows(version, bit)
end
local sha256 = resp.body:match(version .. "%-1%-x" .. bit .. '.7z[%s%S]-value="([0-9a-z]+)')

return sha256
return file, sha256
end

function hasValue(table, value)
Expand Down Expand Up @@ -281,15 +347,31 @@ end

-- post_install.lua
function unixInstall(path, version)
if compareVersion(version, "20.0.0") >= 0 then
patchTruffleRuby(path)
elseif hasValue(HomebrewRubyVersions, version) then
if hasValue(HomebrewRubyVersions, version) then
patchHomebrewRuby(path, version)
elseif compareVersion(version, "20.0.0") >= 0 then
patchTruffleRuby(path)
elseif compareVersion(version, "9") == 0 then
patchJRuby(path)
else
mambaInstall(path, version)
end
end

function patchHomebrewRuby(path, version)
local command1 = "mv " .. path .. "/" .. version .. "/* " .. path
local command2 = "mkdir -p " .. path .. "/share/gems/bin"
local command3 = "rm -rf " .. path .. "/.brew " .. path .. "/" .. version

for _, command in ipairs({ command1, command2, command3 }) do
local status = os.execute(command)
if status ~= 0 then
print("Failed to execute command: " .. command)
os.exit(1)
end
end
end

function patchTruffleRuby(path)
local command1 = path .. "/lib/truffle/post_install_hook.sh > /dev/null"
local command2 = "mkdir -p " .. path .. "/share/gems/bin"
Expand All @@ -304,12 +386,11 @@ function patchTruffleRuby(path)
end
end

function patchHomebrewRuby(path, version)
local command1 = "mv " .. path .. "/" .. version .. "/* " .. path
local command2 = "mkdir -p " .. path .. "/share/gems/bin"
local command3 = "rm -rf " .. path .. "/.brew " .. path .. "/" .. version
function patchJRuby(path)
local command1 = "mkdir -p " .. path .. "/share/gems/bin"
local command2 = "rm -f " .. path .. "/bin/*.exe " .. path .. "/bin/*.bat " .. path .. "/bin/*.dll"

for _, command in ipairs({ command1, command2, command3 }) do
for _, command in ipairs({ command1, command2 }) do
local status = os.execute(command)
if status ~= 0 then
print("Failed to execute command: " .. command)
Expand Down
2 changes: 1 addition & 1 deletion metadata.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ PLUGIN = {}
--- Plugin name
PLUGIN.name = "ruby"
--- Plugin version
PLUGIN.version = "0.4.1"
PLUGIN.version = "0.4.2"
--- Plugin homepage
PLUGIN.homepage = "https://github.com/yanecc/vfox-ruby"
--- Plugin license, please choose a correct license according to your needs.
Expand Down

0 comments on commit 53b7f7c

Please sign in to comment.