Skip to content

Commit

Permalink
Support building Ruby and mRuby with ruby-build
Browse files Browse the repository at this point in the history
  • Loading branch information
yanecc committed Jun 13, 2024
1 parent a173cf4 commit 2813077
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

## Requirement

| Distribution | Dependencies |
| :----------: | :--------------------------------------------------------------------: |
| Ruby | none |
| JRuby | JRE v8 or higher |
| TruffleRuby | `bash`, `make`, `gcc`, `g++`, `openssl-dev`, `yaml-dev` and `zlib-dev` |
| Distribution | Dependencies |
| :----------: | :---------------------------------------------------------------------------------------------------: |
| Ruby | none |
| JRuby | JRE v8 or higher |
| TruffleRuby | `bash`, `make`, `gcc`, `g++`, `openssl-dev`, `yaml-dev` and `zlib-dev` |
| Ruby-b | See [ruby-build's instructions](https://github.com/rbenv/ruby-build/wiki#suggested-build-environment) |
| mRuby-b | Ruby or TruffleRuby |

## Install

Expand All @@ -29,11 +31,15 @@ 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
vfox install ruby@9.4.5.0 # JRuby
vfox install ruby@24.0.1 # TruffleRuby
vfox install ruby@24.0.1.jvm # TruffleRuby-jvm
vfox install ruby@3.4.0-preview1.b # Ruby-build
vfox install ruby@mruby-3.3.0.b # mRuby-build
```

Compiling installation with ruby-build only works on Unix-like systems. All [versions](https://github.com/rbenv/ruby-build/tree/master/share/ruby-build) of Ruby and mRuby provided by ruby-build (except for the dev versions like `3.4-dev`) are supported to install and use with the `.b` suffix.

Some environment variables are served as following:

| Environment variables | Default value | Description |
Expand Down
45 changes: 45 additions & 0 deletions lib/util.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local http = require("http")
local json = require("json")
local strings = require("vfox.strings")
local HomebrewRubyVersions = {
"3.3.2",
Expand Down Expand Up @@ -223,6 +224,8 @@ function generateURL(version, osType, archType)
file, sha256 = generateJRuby(version)
elseif osType == "windows" then
file, sha256 = generateWindowsRuby(version, archType)
elseif version:sub(-1) == "b" then
file = generateRubyBuild(version)
elseif osType ~= "darwin" and osType ~= "linux" then
print("Unsupported OS: " .. osType)
os.exit(1)
Expand Down Expand Up @@ -288,6 +291,29 @@ function generateWindowsRuby(version, archType)
return file, sha256
end

function generateRubyBuild(version)
version = version:gsub("%.b$", "")
if not version:match("[1-3]%.%d%.%d%-?%w*") and not version:match("mruby%-[1-3]%.[0-9]%.[0-2]") then
print("Unsupported version: " .. version)
os.exit(1)
end
local file
local latestRubyBuild = "https://api.github.com/repos/rbenv/ruby-build/releases/latest"
local resp, err = http.get({
url = latestRubyBuild,
})
if err ~= nil then
error("Failed to request: " .. err)
end
if resp.status_code ~= 200 then
error("Failed to get latest ruby-build: " .. err .. "\nstatus_code => " .. resp.status_code)
end
latestRubyBuild = json.decode(resp.body)
file = latestRubyBuild.tarball_url .. "#/ruby-build.tar.gz"

return file
end

function hasValue(table, value)
for _, v in ipairs(table) do
if v == value then
Expand Down Expand Up @@ -349,6 +375,8 @@ end
function unixInstall(path, version)
if hasValue(HomebrewRubyVersions, version) then
patchHomebrewRuby(path, version)
elseif version:sub(-1) == "b" then
patchRubyBuild(path, version)
elseif compareVersion(version, "20.0.0") >= 0 then
patchTruffleRuby(path)
elseif compareVersion(version, "9") == 0 then
Expand All @@ -372,6 +400,23 @@ function patchHomebrewRuby(path, version)
end
end

function patchRubyBuild(path, version)
local rubyBuild = path .. "/bin/ruby-build"
local command1 = rubyBuild .. " " .. version:gsub("%.b$", "") .. " " .. path .. "/build > /dev/null"
local command2 = "find " .. path .. " -mindepth 1 -maxdepth 1 ! -name 'build' -exec rm -rf {} +"
local command3 = "mv " .. path .. "/build/* " .. path
local command4 = "mkdir -p " .. path .. "/share/gems/bin"
local command5 = "rm -rf " .. path .. "/build"

for _, command in ipairs({ command1, command2, command3, command4, command5 }) 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 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.3"
PLUGIN.version = "0.5.0"
--- 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 2813077

Please sign in to comment.