|
| 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