Skip to content

Commit 28cd6f1

Browse files
committed
💅 Clean up version sorting and add test
1 parent d3d4606 commit 28cd6f1

File tree

3 files changed

+38
-49
lines changed

3 files changed

+38
-49
lines changed

libexec/rbenv-versions

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -63,62 +63,47 @@ if [ -d "$versions_dir" ]; then
6363
versions_dir="$(realpath "$versions_dir")"
6464
fi
6565

66+
list_versions() {
67+
shopt -s nullglob
68+
for path in "$versions_dir"/*; do
69+
if [ -d "$path" ]; then
70+
if [ -n "$skip_aliases" ] && [ -L "$path" ]; then
71+
target="$(realpath "$path")"
72+
[ "${target%/*}" != "$versions_dir" ] || continue
73+
fi
74+
echo "${path##*/}"
75+
fi
76+
done
77+
shopt -u nullglob
78+
}
79+
6680
if [ -n "$bare" ]; then
67-
hit_prefix=""
68-
miss_prefix=""
69-
current_version=""
70-
include_system=""
71-
else
72-
hit_prefix="* "
73-
miss_prefix=" "
74-
current_version="$(rbenv-version-name || true)"
75-
include_system="1"
81+
list_versions
82+
exit 0
7683
fi
7784

78-
num_versions=0
79-
80-
print_version() {
81-
if [ "$1" == "$current_version" ]; then
82-
echo "${hit_prefix}$(rbenv-version 2>/dev/null)"
83-
else
84-
echo "${miss_prefix}$1"
85-
fi
86-
num_versions=$((num_versions + 1))
87-
}
88-
8985
sort_versions() {
9086
sed 'h; s/[+-]/./g; s/.p\([[:digit:]]\)/.z.\1/; s/$/.z/; G; s/\n/ /' | \
9187
LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n | awk '{print $2}'
9288
}
9389

94-
# Include "system" in the non-bare output, if it exists
95-
if [ -n "$include_system" ] && RBENV_VERSION=system rbenv-which ruby >/dev/null 2>&1; then
96-
print_version system
97-
fi
98-
99-
shopt -s nullglob
100-
101-
versions=($(
102-
for path in "$versions_dir"/*; do
103-
if [ -d "$path" ]; then
104-
if [ -n "$skip_aliases" ] && [ -L "$path" ]; then
105-
target="$(realpath "$path")"
106-
[ "${target%/*}" != "$versions_dir" ] || continue
107-
fi
108-
echo "${path##*/}"
90+
versions="$(
91+
if RBENV_VERSION=system rbenv-which ruby >/dev/null 2>&1; then
92+
echo system
10993
fi
110-
done
111-
))
112-
113-
sorted_versions=($(printf "%s\n" ${versions[@]} | sort_versions))
114-
115-
for version in ${sorted_versions[@]}; do
116-
print_version $version
117-
done
118-
119-
shopt -u nullglob
94+
list_versions | sort_versions
95+
)"
12096

121-
if [ "$num_versions" -eq 0 ] && [ -n "$include_system" ]; then
97+
if [ -z "$versions" ]; then
12298
echo "Warning: no Ruby detected on the system" >&2
12399
exit 1
124100
fi
101+
102+
current_version="$(rbenv-version-name || true)"
103+
while read -r version; do
104+
if [ "$version" == "$current_version" ]; then
105+
echo "* $(rbenv-version 2>/dev/null)"
106+
else
107+
echo " $version"
108+
fi
109+
done <<<"$versions"

test/test_helper.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ path_without() {
119119
if [ "$found" != "${RBENV_ROOT}/shims" ]; then
120120
alt="${RBENV_TEST_DIR}/$(echo "${found#/}" | tr '/' '-')"
121121
mkdir -p "$alt"
122-
for util in bash head cut readlink greadlink; do
122+
for util in bash head cut readlink greadlink sed sort awk; do
123123
if [ -x "${found}/$util" ]; then
124124
ln -s "${found}/$util" "${alt}/$util"
125125
fi

test/versions.bats

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,19 @@ OUT
5656
@test "multiple versions" {
5757
stub_system_ruby
5858
create_version "1.8.7"
59-
create_version "1.9.3"
59+
create_version "1.9.3-p13"
60+
create_version "1.9.3-p2"
6061
create_version "2.2.10"
6162
create_version "2.2.3"
63+
create_version "2.2.3-pre.2"
6264
run rbenv-versions
6365
assert_success
6466
assert_output <<OUT
6567
* system
6668
1.8.7
67-
1.9.3
69+
1.9.3-p2
70+
1.9.3-p13
71+
2.2.3-pre.2
6872
2.2.3
6973
2.2.10
7074
OUT

0 commit comments

Comments
 (0)