Skip to content

Commit

Permalink
Automate updating ruby-build versions
Browse files Browse the repository at this point in the history
  • Loading branch information
yanecc committed Jun 30, 2024
1 parent 017fd23 commit 8496eb4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 19 deletions.
Binary file modified .github/UpdateVersionList
Binary file not shown.
25 changes: 24 additions & 1 deletion .github/UpdateVersionList.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@ require "http/client"
require "json"
require "option_parser"

record RBFile, name : String do
include JSON::Serializable
end

def fetchRubyBuildVersions
response = HTTP::Client.get "https://api.github.com/repos/rbenv/ruby-build/contents/share/ruby-build"
fileList = Array(RBFile).from_json response.body
versionList = fileList.map &.name
versionList.select! &.match_full /[1-3]\.\d\.\d-?\w*|mruby-\d\.\d\.\d/
mrubyList, rubyList = versionList.partition &.starts_with? "mruby-"
rubyList.map! { |version| version + ".rb" }
mrubyList.map! { |version| version.split("-").last + ".mrb" }
rubyList.reverse!
mrubyList.reverse!
versionList = rubyList + mrubyList
end

def getLatestVersion(url)
response = HTTP::Client.get url
latestRelease = response.headers["Location"]
latestVersion = latestRelease[/(\d[\.\d]+)/]
latestVersion = latestRelease[/\d[\.\d]+/]
end

def fetchVersionList
Expand Down Expand Up @@ -71,6 +88,12 @@ if vlist["jruby"] != fetchJRubyVersions
puts "JRuby version list is updated"
end

if vlist["ruby-build"] != fetchRubyBuildVersions
isUpdated = false
vlist["ruby-build"] = fetchRubyBuildVersions
puts "Ruby-build version list is updated"
end

unless isUpdated
vlist["ruby"] = vlist["homebrew"] + vlist["conda-forge"]
vlist["ruby"].sort! { |a, b| compareVersions(b, a) }
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ 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@3.3.3.rb # Ruby-build
vfox install ruby@3.3.0.mrb # mRuby-build
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.3.3.rb # Ruby-build
vfox install ruby@3.3.0.mrb # 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 supported by ruby-build (except for the development versions like `3.4-dev`) are available.
Expand Down
10 changes: 5 additions & 5 deletions hooks/available.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require("util")

function PLUGIN:Available(ctx)
local option = ctx.args[1]
local noCache = false
if option == "--no-cache" then
noCache = true
local buildArg = hasValue(ctx.args, "--ruby-build")
local cacheArg = hasValue(ctx.args, "--no-cache")
if cacheArg then
clearCache()
end
return fetchAvailable(noCache)
return fetchAvailable(buildArg)
end
35 changes: 27 additions & 8 deletions lib/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ end
local RubyVersions, JRubyVersions, HomebrewRubyVersions = fetchVersions()

-- available.lua
function fetchAvailable(noCache)
function fetchAvailable(buildArg)
local result

if noCache then
clearCache()
if buildArg then
result = fetchRubyVersions(resp.body)
end
if RUNTIME.osType == "windows" then
result = fetchForWindows()
Expand All @@ -48,8 +48,22 @@ function fetchAvailable(noCache)
return result
end

function clearCache()
os.remove(RUNTIME.pluginDirPath .. "/available.cache")
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()
Expand Down Expand Up @@ -130,6 +144,11 @@ function fetchForUnix()
return result
end

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

-- pre_install.lua
function getDownloadInfo(version)
local file, sha256
Expand Down Expand Up @@ -215,8 +234,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 = githubURL:gsub("/$", "")
.. "/oneclick/rubyinstaller2/releases/download/RubyInstaller-%s-1/rubyinstaller-%s-1-x%s.7z"
file = "rubyinstaller-%s-1-x%s.7z"
file = githubURL:gsub("/$", "") .. "/oneclick/rubyinstaller2/releases/download/RubyInstaller-%s-1/" .. file
file = file:format(version, version, bit)

local resp, err = http.get({
Expand Down Expand Up @@ -246,7 +265,7 @@ function generateRubyBuild()
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"
file = latestRubyBuild.tarball_url .. "#/ruby-build.tgz"

return file
end
Expand Down

0 comments on commit 8496eb4

Please sign in to comment.