Skip to content

Commit 79f64ee

Browse files
committed
Support building with version prefix
Commonly in CI pipelines and other scripts, users may want to install the latest from a specific series, e.g. `3.4` or `jruby-10.0`. ```bash $ bin/ruby-build 3.4 /tmp/3.4 ... Downloading ruby-3.4.9.tar.gz... $ bin/ruby-build 4.0 /tmp/4.0 ... Downloading ruby-4.0.2.tar.gz... ``` Also support `--resolve`: ```bash $ bin/ruby-build --resolve 3.4 3.4.9 ```
1 parent 703a7a3 commit 79f64ee

4 files changed

Lines changed: 37 additions & 45 deletions

File tree

bin/rbenv-install

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ DEFINITION="${ARGUMENTS[0]}"
144144
[ -n "$DEFINITION" ] || DEFINITION="$(rbenv-local 2>/dev/null || true)"
145145
[ -n "$DEFINITION" ] || usage 1 >&2
146146

147+
[ -n "$DEFINITION" ] && RESOLVED_DEFINITION="$(ruby-build --resolve "${DEFINITION}" 2>/dev/null || true)"
148+
149+
if [ -n "$RESOLVED_DEFINITION" ]; then
150+
DEFINITION="$RESOLVED_DEFINITION"
151+
fi
152+
147153
# Define `before_install` and `after_install` functions that allow
148154
# plugin hooks to register a string of code for execution before or
149155
# after the installation process.

bin/ruby-build

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# ruby-build --version
77
#
88
# -l, --list List latest stable releases for each Ruby
9-
# -r, --resolve Find the most recent version matching a prefix.
9+
# --resolve Find the most recent version matching a prefix.
1010
# --definitions List all local definitions, including outdated ones
1111
# --version Show version of ruby-build
1212
#
@@ -1405,47 +1405,24 @@ list_maintained_versions() {
14051405

14061406
filter_version_prefix()
14071407
{
1408-
match=1
1409-
prefix="$1"
1410-
if [[ "${prefix}" = "ruby" ]]; then
1411-
# Filter MRI versions
1412-
grep -e '^[0-9]'
1413-
elif echo "${prefix}" | grep -e '^\([0-9]\|[a-z\+]\+-[0-9]\)' > /dev/null; then
1414-
# Filter by version prefix
1415-
while IFS= read -r version; do
1416-
if [[ "${version}" = "${prefix}" ]] || [[ "${version}" = "${prefix}."* ]]; then
1417-
match=0
1418-
echo "${version}"
1419-
fi
1420-
done
1421-
else
1422-
# Filter by implementation
1423-
while IFS= read -r version; do
1424-
if [[ "${version}" = "${prefix}-"* ]]; then
1425-
match=0
1426-
echo "${version}"
1427-
fi
1428-
done
1429-
fi
1430-
return $match
1408+
pattern="${1//./\.}"
1409+
pattern="${pattern//+/\+}"
1410+
grep -e "^${pattern}[-.$]"
14311411
}
14321412

14331413
resolve() {
14341414
prefix="${1%.}"
14351415
prefix="${prefix#ruby-}"
14361416

1437-
list_definitions | \
1417+
version=$(list_definitions | \
14381418
grep -v -e '-rc[0-9]*$' -e '-preview[0-9]*$' -e '-dev$' | \
14391419
filter_version_prefix "${prefix}" | \
14401420
LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n | \
1441-
tail -n 1
1442-
}
1421+
tail -n 1)
1422+
1423+
[[ -z "${version}" ]] && return 1
1424+
[[ "${1}" = "ruby-"* ]] && version="ruby-${version}"
14431425

1444-
resolve_version() {
1445-
version=$(resolve "$@")
1446-
if [[ -z "${version}" ]]; then
1447-
exit 1
1448-
fi
14491426
echo "${version}"
14501427
}
14511428

@@ -1492,7 +1469,7 @@ for option in "${OPTIONS[@]}"; do
14921469
EARLY_EXIT=list_maintained_versions
14931470
;;
14941471
"resolve")
1495-
EARLY_EXIT=resolve_version
1472+
EARLY_EXIT=resolve
14961473
;;
14971474
"d" | "dir")
14981475
APPEND_DEFINITION_TO_PREFIX=true
@@ -1523,6 +1500,7 @@ for option in "${OPTIONS[@]}"; do
15231500
done
15241501

15251502
DEFINITION_PATH="${ARGUMENTS[0]}"
1503+
ORIGINAL_DEFINITION_PATH="${DEFINITION_PATH}"
15261504
PREFIX_PATH="${ARGUMENTS[1]}"
15271505

15281506
if [ -z "$EARLY_EXIT" ] && [ -z "$DEFINITION_PATH" ]; then
@@ -1559,7 +1537,7 @@ help )
15591537
echo
15601538
usage 0
15611539
;;
1562-
version | list_definitions | list_maintained_versions | resolve_version )
1540+
version | list_definitions | list_maintained_versions | resolve )
15631541
shift
15641542
"$EARLY_EXIT" "$@"
15651543
exit 0
@@ -1602,7 +1580,11 @@ if [[ ! -f "$DEFINITION_PATH" && ! -p "$DEFINITION_PATH" ]]; then
16021580
if [ -f "${DEFINITION_DIR}/${fully_qualified_version}" ]; then
16031581
DEFINITION_PATH="${DEFINITION_DIR}/${fully_qualified_version}"
16041582
if [ "$APPEND_DEFINITION_TO_PREFIX" = "true" ]; then
1605-
PREFIX_PATH="$ORIGINAL_PREFIX_PATH/$(basename "$DEFINITION_PATH")"
1583+
if [[ "${ORIGINAL_DEFINITION_PATH}" = ruby-* ]]; then
1584+
PREFIX_PATH="$ORIGINAL_PREFIX_PATH/ruby-$(basename "$DEFINITION_PATH")"
1585+
else
1586+
PREFIX_PATH="$ORIGINAL_PREFIX_PATH/$(basename "$DEFINITION_PATH")"
1587+
fi
16061588
fi
16071589
break
16081590
fi

test/definitions.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ NUM_DEFINITIONS="$(ls "$BATS_TEST_DIRNAME"/../share/ruby-build | wc -l)"
9999
touch "${TMP}/definitions/2.0.0"
100100

101101
run bin/ruby-build --resolve "ruby-1.9" "${TMP}/install"
102-
assert_success "1.9.10"
102+
assert_success "ruby-1.9.10"
103103
}
104104

105105
@test "resolve definition by implementation name" {

test/rbenv.bats

100644100755
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ stub_ruby_build() {
1616
}
1717

1818
@test "install proper" {
19-
stub_ruby_build 'echo ruby-build "$@"'
19+
stub_ruby_build 'echo 2.1.2' 'echo ruby-build "$@"'
2020

2121
run rbenv-install 2.1.2
2222
assert_success "ruby-build 2.1.2 ${RBENV_ROOT}/versions/2.1.2"
@@ -27,7 +27,7 @@ stub_ruby_build() {
2727
}
2828

2929
@test "install with flags" {
30-
stub_ruby_build 'echo "ruby-build $(inspect_args "$@")"'
30+
stub_ruby_build 'echo 2.1.2' 'echo "ruby-build $(inspect_args "$@")"'
3131

3232
run rbenv-install -kpv 2.1.2 -- --with-configure-opt="hello world"
3333
assert_success "ruby-build --keep --verbose --patch 2.1.2 ${RBENV_ROOT}/versions/2.1.2 -- \"--with-configure-opt=hello world\""
@@ -39,7 +39,7 @@ stub_ruby_build() {
3939

4040
@test "suggest running rbenv global after install" {
4141
rm -rf "$RBENV_ROOT/version"
42-
stub_ruby_build 'echo ruby-build "$@"'
42+
stub_ruby_build 'echo 2.1.2' 'echo ruby-build "$@"'
4343

4444
run rbenv-install 2.1.2
4545
assert_success <<OUT
@@ -52,7 +52,7 @@ OUT
5252
}
5353

5454
@test "install rbenv local version by default" {
55-
stub_ruby_build 'echo ruby-build "$1"'
55+
stub_ruby_build 'echo 2.1.2' 'echo ruby-build "$1"'
5656
stub rbenv-local 'echo 2.1.2'
5757

5858
run rbenv-install
@@ -100,7 +100,8 @@ OUT
100100
fi
101101

102102
stub_repeated brew false
103-
stub_ruby_build 'echo ERROR >&2 && exit 2' \
103+
stub_ruby_build 'exit 1' \
104+
'echo ERROR >&2 && exit 2' \
104105
"--definitions : echo 1.8.7 1.9.3-p0 1.9.3-p194 2.1.2 | tr ' ' $'\\n'"
105106

106107
run rbenv-install 1.9.3
@@ -127,10 +128,11 @@ OUT
127128

128129
@test "Homebrew upgrade instructions" {
129130
stub brew "--prefix : echo '${BATS_TEST_DIRNAME%/*}'"
130-
stub_ruby_build 'echo ERROR >&2 && exit 2' \
131+
stub_ruby_build 'exit 1' \
132+
'echo ERROR >&2 && exit 2' \
131133
"--definitions : true"
132134

133-
run rbenv-install 1.9.3
135+
run rbenv-install 1.9.10
134136
assert_failure
135137
assert_output <<OUT
136138
ERROR
@@ -148,7 +150,8 @@ OUT
148150

149151
@test "no build definitions from plugins" {
150152
refute [ -e "${RBENV_ROOT}/plugins" ]
151-
stub_ruby_build 'echo $RUBY_BUILD_DEFINITIONS'
153+
stub_ruby_build 'echo 2.1.2' \
154+
'echo $RUBY_BUILD_DEFINITIONS'
152155

153156
run rbenv-install 2.1.2
154157
assert_success ""
@@ -157,7 +160,8 @@ OUT
157160
@test "some build definitions from plugins" {
158161
mkdir -p "${RBENV_ROOT}/plugins/foo/share/ruby-build"
159162
mkdir -p "${RBENV_ROOT}/plugins/bar/share/ruby-build"
160-
stub_ruby_build "echo \$RUBY_BUILD_DEFINITIONS | tr ':' $'\\n'"
163+
stub_ruby_build 'echo 2.1.2' \
164+
"echo \$RUBY_BUILD_DEFINITIONS | tr ':' $'\\n'"
161165

162166
run rbenv-install 2.1.2
163167
assert_success

0 commit comments

Comments
 (0)