Skip to content

Commit

Permalink
List ruby-build versions with --ruby-build option
Browse files Browse the repository at this point in the history
  • Loading branch information
yanecc committed Jul 1, 2024
1 parent 8496eb4 commit 6f860e0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 55 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ After installing [vfox](https://github.com/version-fox/vfox), install the plugin
vfox add ruby
```

Next, search and select the version to install. By default, vfox keeps cache for available versions, use `--no-cache` flag to search without cache and rebuild the cache.
Next, search and select the version to install. The `--ruby-build` flag is used to list all the versions that can be built and installed by ruby-build. By default, vfox keeps cache for available versions, use the `--no-cache` flag to delete the cache file.

``` shell
vfox search ruby
vfox search ruby --no-cache
vfox search ruby --ruby-build
vfox search ruby --no-cache && vfox search ruby
```

Install the latest stable version with `latest` tag.
Expand Down
86 changes: 34 additions & 52 deletions lib/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ local http = require("http")
local json = require("json")
local strings = require("vfox.strings")

function fetchVersions()
local rubyVersions, jrubyVersions, homebrewRubyVersions
function fetchManifest()
local manifest
local githubURL = os.getenv("GITHUB_URL") or "https://github.com/"
local resp, err = http.get({
url = githubURL:gsub("/$", "") .. "/yanecc/vfox-ruby/releases/manifest",
Expand All @@ -14,31 +14,31 @@ function fetchVersions()
if resp.status_code ~= 200 then
error("Failed to get versions: " .. err .. "\nstatus_code => " .. resp.status_code)
end
manifest = resp.body:match("<code>(.-)</code>")
manifest = json.decode(manifest)

local versionList = resp.body:match("<code>(.-)</code>")
local versionJson = json.decode(versionList)
rubyVersions = versionJson.ruby
jrubyVersions = versionJson.jruby
homebrewRubyVersions = versionJson.homebrew

return rubyVersions, jrubyVersions, homebrewRubyVersions
return manifest
end

local RubyVersions, JRubyVersions, HomebrewRubyVersions = fetchVersions()
local Manifest = fetchManifest()

-- available.lua
function fetchAvailable(buildArg)
local result
local result = {}

if buildArg then
result = fetchRubyVersions(resp.body)
end
if RUNTIME.osType == "windows" then
result = fetchForWindows()
elseif buildArg then
for _, v in ipairs(Manifest["ruby-build"]) do
table.insert(result, {
version = v,
})
end
return result
else
result = fetchForUnix()
end
for _, v in ipairs(JRubyVersions) do
for _, v in ipairs(Manifest.jruby) do
table.insert(result, {
version = v,
note = "jruby",
Expand All @@ -48,24 +48,6 @@ function fetchAvailable(buildArg)
return result
end

function fetchRubyBuildVersions()
local result = {}
local versions = {}
local resp, err = http.get({
url = "https://cache.ruby-lang.org/pub/ruby/index.txt",
})
if err ~= nil then
error("Failed to request: " .. err)
end
if resp.status_code ~= 200 then
error("Failed to get Ruby versions: " .. err .. "\nstatus_code => " .. resp.status_code)
end

for version in resp.body:gmatch("(%d.%d.%d.-)%s") do
table.insert(versions, version)
end
end

function fetchForWindows()
local result = {}
local versions = {}
Expand Down Expand Up @@ -123,7 +105,7 @@ end
function fetchForUnix()
local result = {}

for i, v in ipairs(RubyVersions) do
for i, v in ipairs(Manifest.ruby) do
if i == 1 then
table.insert(result, {
version = v,
Expand All @@ -146,7 +128,7 @@ end

function clearCache()
os.remove(RUNTIME.pluginDirPath .. "/available.cache")
exit()
os.exit()
end

-- pre_install.lua
Expand Down Expand Up @@ -176,7 +158,7 @@ function getLatestVersion()
end
version = resp.body:match("Ruby (%d.%d.%d+)%-1 %(x64%)")
else
version = RubyVersions[1]
version = Manifest.ruby[1]
end

return version
Expand All @@ -185,19 +167,19 @@ end
function generateURL(version, osType, archType)
local file, sha256

if hasValue(JRubyVersions, version) then
if hasValue(Manifest.jruby, version) then
file, sha256 = generateJRuby(version)
elseif osType == "windows" then
file, sha256 = generateWindowsRuby(version, archType)
elseif version:match("^[1-3]%.%d%.%d%-?%w*%.m?rb$") then
file = generateRubyBuild()
elseif hasValue(Manifest["ruby-build"], version) then
file = fetchRubyBuild()
elseif osType ~= "darwin" and osType ~= "linux" then
print("Unsupported OS: " .. osType)
os.exit(1)
elseif not hasValue(RubyVersions, version) then
elseif not hasValue(Manifest.ruby, version) then
print("Unsupported version: " .. version)
os.exit(1)
elseif hasValue(HomebrewRubyVersions, version) then
elseif hasValue(Manifest.homebrew, version) then
file = generateHomebrewRuby(version, osType, archType)
elseif compareVersion(version, "20.0.0") >= 0 then
file = generateTruffleRuby(version, osType, archType)
Expand Down Expand Up @@ -234,8 +216,8 @@ function generateWindowsRuby(version, archType)
local file, sha256
local bit = archType == "amd64" and "64" or "86"
local githubURL = os.getenv("GITHUB_URL") or "https://github.com/"
file = "rubyinstaller-%s-1-x%s.7z"
file = githubURL:gsub("/$", "") .. "/oneclick/rubyinstaller2/releases/download/RubyInstaller-%s-1/" .. file
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({
Expand All @@ -252,20 +234,20 @@ function generateWindowsRuby(version, archType)
return file, sha256
end

function generateRubyBuild()
local file
local latestRubyBuild = "https://api.github.com/repos/rbenv/ruby-build/releases/latest"
function fetchRubyBuild()
local githubURL = os.getenv("GITHUB_URL") or "https://github.com/"
local file = githubURL:gsub("/$", "") .. "/rbenv/ruby-build/tags"
local resp, err = http.get({
url = latestRubyBuild,
url = file,
})
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.tgz"
file = resp.body:match("(v%d+)</a>") .. ".tar.gz"
file = githubURL:gsub("/$", "") .. "/rbenv/ruby-build/archive/refs/tags/" .. file

return file
end
Expand Down Expand Up @@ -329,11 +311,11 @@ end

-- post_install.lua
function unixInstall(rootPath, path, version)
if hasValue(HomebrewRubyVersions, version) then
if hasValue(Manifest.homebrew, version) then
patchHomebrewRuby(path, version)
elseif hasValue(JRubyVersions, version) then
elseif hasValue(Manifest.jruby, version) then
patchJRuby(path)
elseif version:match("%.m?rb$") then
elseif hasValue(Manifest["ruby-build"], version) then
patchRubyBuild(path, version)
elseif compareVersion(version, "20.0.0") >= 0 then
patchTruffleRuby(path)
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.5.3"
PLUGIN.version = "0.6.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 6f860e0

Please sign in to comment.