From 2813077c03d3562b12b23f242f94c409064e3c26 Mon Sep 17 00:00:00 2001 From: Sunrise Date: Thu, 13 Jun 2024 23:41:46 +0800 Subject: [PATCH] Support building Ruby and mRuby with ruby-build --- README.md | 22 ++++++++++++++-------- lib/util.lua | 45 +++++++++++++++++++++++++++++++++++++++++++++ metadata.lua | 2 +- 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e79a64f..5364ee5 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 | diff --git a/lib/util.lua b/lib/util.lua index 71efa3e..f1e3bee 100644 --- a/lib/util.lua +++ b/lib/util.lua @@ -1,4 +1,5 @@ local http = require("http") +local json = require("json") local strings = require("vfox.strings") local HomebrewRubyVersions = { "3.3.2", @@ -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) @@ -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 @@ -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 @@ -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" diff --git a/metadata.lua b/metadata.lua index dbc1458..863ab80 100644 --- a/metadata.lua +++ b/metadata.lua @@ -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.