Skip to content

Commit b5c94d0

Browse files
committed
Add support for installing precompiled rubies from jdx/ruby
1 parent 1c6a957 commit b5c94d0

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-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: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
local checksum_url="${url}.sha256"
50+
local checksum_path="${download_path}.sha256"
51+
if curl -fLsS -o "$checksum_path" "$checksum_url" 2>/dev/null; then
52+
local expected actual
53+
expected="$(cut -d ' ' -f1 <"$checksum_path")"
54+
if command -v sha256sum >/dev/null 2>&1; then
55+
actual="$(sha256sum "$download_path" | cut -d ' ' -f1)"
56+
elif command -v shasum >/dev/null 2>&1; then
57+
actual="$(shasum -a 256 "$download_path" | cut -d ' ' -f1)"
58+
else
59+
# No checksum tool is available, so skip verification
60+
rm -f "$checksum_path"
61+
return 0
62+
fi
63+
rm -f "$checksum_path"
64+
if [ "$expected" != "$actual" ]; then
65+
echo >&2 "Checksum mismatch for precompiled binary"
66+
return 1
67+
fi
68+
fi
69+
70+
return 0
71+
}
72+
73+
install_precompiled() {
74+
local version="$1"
75+
local install_path="$2"
76+
77+
local platform
78+
if ! platform="$(precompiled_platform)"; then
79+
return 1
80+
fi
81+
82+
if ! is_version_precompilable "$version"; then
83+
return 1
84+
fi
85+
86+
local url
87+
url="$(precompiled_url "$version" "$platform")"
88+
89+
local tmp_dir
90+
tmp_dir="$(mktemp -d)"
91+
local tarball="${tmp_dir}/ruby-${version}.tar.gz"
92+
93+
echo "Looking for precompiled Ruby ${version} for ${platform}..."
94+
95+
if ! download_precompiled "$url" "$tarball"; then
96+
rm -rf "$tmp_dir"
97+
return 1
98+
fi
99+
100+
mkdir -p "$install_path"
101+
if ! tar -xzf "$tarball" -C "$install_path" --strip-components=1; then
102+
rm -rf "$tmp_dir" "$install_path"
103+
return 1
104+
fi
105+
106+
rm -rf "$tmp_dir"
107+
echo "Installed precompiled Ruby ${version} successfully."
108+
return 0
109+
}

0 commit comments

Comments
 (0)