Skip to content

Commit 9168d8e

Browse files
committed
Add support for installing precompiled rubies from jdx/ruby
1 parent 384566b commit 9168d8e

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

bin/install

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ set -euo pipefail
44

55
# shellcheck source=/dev/null
66
source "$(dirname "$0")/../lib/utils.sh"
7+
# shellcheck source=/dev/null
8+
source "$(dirname "$0")/../lib/precompiled.sh"
79

810
install_ruby() {
911
ensure_ruby_build_setup
@@ -130,5 +132,16 @@ install_default_gems() {
130132
)
131133
}
132134

133-
install_ruby "$ASDF_INSTALL_TYPE" "$ASDF_INSTALL_VERSION" "$ASDF_INSTALL_PATH"
135+
should_compile_from_source() {
136+
local val="${ASDF_RUBY_COMPILE_FROM_SOURCE:-}"
137+
[[ "$val" == "1" || "$val" == "true" || "$val" == "yes" ]]
138+
}
139+
140+
if should_compile_from_source || [ "$ASDF_INSTALL_TYPE" != "version" ]; then
141+
install_ruby "$ASDF_INSTALL_TYPE" "$ASDF_INSTALL_VERSION" "$ASDF_INSTALL_PATH"
142+
elif ! install_precompiled "$ASDF_INSTALL_VERSION" "$ASDF_INSTALL_PATH"; then
143+
echoerr "Falling back to building Ruby from source..."
144+
install_ruby "$ASDF_INSTALL_TYPE" "$ASDF_INSTALL_VERSION" "$ASDF_INSTALL_PATH"
145+
fi
146+
134147
install_default_gems

lib/precompiled.sh

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env bash
2+
3+
PRECOMPILED_BASE_URL="${ASDF_RUBY_PRECOMPILED_URL:-https://github.com/jdx/ruby/releases/download}"
4+
5+
precompiled_platform() {
6+
local os arch
7+
os="$(uname -s)"
8+
arch="$(uname -m)"
9+
10+
case "$os" in
11+
Darwin)
12+
case "$arch" in
13+
arm64) echo "macos" ;;
14+
*) return 1 ;;
15+
esac
16+
;;
17+
Linux)
18+
case "$arch" in
19+
x86_64) echo "x86_64_linux" ;;
20+
aarch64) echo "arm64_linux" ;;
21+
*) return 1 ;;
22+
esac
23+
;;
24+
*) return 1 ;;
25+
esac
26+
}
27+
28+
is_version_precompilable() {
29+
local version="$1"
30+
# Only standard MRI/CRuby versions (e.g. 4.0.1) are available as precompiled
31+
# binaries. Ignore prefixed variants like jruby-*, truffleruby-*, mruby-*, etc
32+
[[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+([-.](preview|rc|dev)[0-9]*)?$ ]]
33+
}
34+
35+
precompiled_url() {
36+
local version="$1"
37+
local platform="$2"
38+
echo "${PRECOMPILED_BASE_URL}/${version}/ruby-${version}.${platform}.tar.gz"
39+
}
40+
41+
download_precompiled() {
42+
local url="$1"
43+
local download_path="$2"
44+
45+
if ! curl -fLsS -o "$download_path" "$url"; then
46+
return 1
47+
fi
48+
49+
return 0
50+
}
51+
52+
install_precompiled() {
53+
local version="$1"
54+
local install_path="$2"
55+
56+
local platform
57+
if ! platform="$(precompiled_platform)"; then
58+
return 1
59+
fi
60+
61+
if ! is_version_precompilable "$version"; then
62+
return 1
63+
fi
64+
65+
local url
66+
url="$(precompiled_url "$version" "$platform")"
67+
68+
local tmp_dir
69+
tmp_dir="$(mktemp -d)"
70+
local tarball="${tmp_dir}/ruby-${version}.tar.gz"
71+
72+
echo "Looking for precompiled Ruby ${version} for ${platform}..."
73+
74+
if ! download_precompiled "$url" "$tarball"; then
75+
rm -rf "$tmp_dir"
76+
return 1
77+
fi
78+
79+
mkdir -p "$install_path"
80+
if ! tar -xzf "$tarball" -C "$install_path" --strip-components=1; then
81+
rm -rf "$tmp_dir" "$install_path"
82+
return 1
83+
fi
84+
85+
rm -rf "$tmp_dir"
86+
echo "Installed precompiled Ruby ${version} successfully."
87+
return 0
88+
}

0 commit comments

Comments
 (0)