Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The list rbenv install -l is too long, show latest stable releases by default #1402

Merged
merged 16 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion bin/rbenv-install
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
# rbenv install -l|--list
# rbenv install --version
#
# -l/--list List all available versions
# -l/--list List latest stable versions for each Ruby
# -L/--list-all List all local versions
# -f/--force Install even if the version appears to be installed already
# -s/--skip-existing Skip if the version appears to be installed already
#
Expand Down Expand Up @@ -38,6 +39,7 @@ shopt -u nullglob
# Provide rbenv completions
if [ "$1" = "--complete" ]; then
echo --list
echo --list-all
echo --force
echo --skip-existing
echo --keep
Expand Down Expand Up @@ -77,6 +79,15 @@ for option in "${OPTIONS[@]}"; do
usage 0
;;
"l" | "list" )
ruby-build --list
{
echo
echo "Only latest stable releases for each Ruby implementation are shown."
echo "Use 'rbenv install --list-all' to show all local versions."
} 1>&2
metalefty marked this conversation as resolved.
Show resolved Hide resolved
exit
;;
"L" | "list-all" )
ruby-build --definitions
exit
;;
Expand Down
26 changes: 26 additions & 0 deletions bin/ruby-build
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# -4/--ipv4 Resolve names to IPv4 addresses only
# -6/--ipv6 Resolve names to IPv6 addresses only
# --definitions List all local definitions
# -l/--list List latest stable releases for each Ruby
# --version Show version of ruby-build
#

Expand Down Expand Up @@ -1197,13 +1198,34 @@ usage() {
[ -z "$1" ] || exit "$1"
}

# list all versions
list_definitions() {
{ for DEFINITION_DIR in "${RUBY_BUILD_DEFINITIONS[@]}"; do
[ -d "$DEFINITION_DIR" ] && ls "$DEFINITION_DIR"
done
} | sort_versions | uniq
}

# list only latest stable versions excluding RC, preview, dev and EoL'ed
list_maintained_versions() {
{ for DEFINITION_DIR in "${RUBY_BUILD_DEFINITIONS[@]}"; do
[ -d "$DEFINITION_DIR" ] && \
grep -L -e warn_eol "$DEFINITION_DIR"/* 2>/dev/null | \
sed 's|.*/||' | \
grep -v -e '-rc[0-9]*$' -e '-preview[0-9]*$' -e '-dev$'
done
} | extract_latest_versions | sort_versions | uniq
}

extract_latest_versions() {
# sort in this function looks redundunt but it is necessary
# rbx-3.99 appears latest unless the sort
sed 'h; s/[+-]/./g; s/.p\([[:digit:]]\)/.z.\1/; s/$/.z/; G; s/\n/ /' | \
LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n | \
Comment on lines +1223 to +1224
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we re-use sort_versions here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed.

Copy link
Contributor Author

@metalefty metalefty Mar 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, actually we can't. In that function, intermediate output of sort_versions is required (before awk { print $2 }). It cannot be simply replaced. Re-using common part both from sort_versions and extract_latest_versions is possible.

@mislav What do you think?

Copy link
Contributor Author

@metalefty metalefty Mar 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be commonalized like this but it looks me a little bit weird. Do you like this?

extract_latest_versions() {
  _some_briliant_name | \
    sed 's/[+.]/ /; s/[0-9].*z //; s/^\([0-9].[0-9]\)/mri\1 \1/' | \
    awk '{ latest[$1] =$2 } END{ for(key in latest) { print latest[key] } }'
}

sort_versions() {
    _some_briliant_name | awk '{print $2}'
}

_some_briliant_name() {
  sed 'h; s/[+-]/./g; s/.p\([[:digit:]]\)/.z.\1/; s/$/.z/; G; s/\n/ /' | \
    LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n
}

sed 's/[+.]/ /; s/[0-9].*z //; s/^\([0-9].[0-9]\)/mri\1 \1/' | \
awk '{ latest[$1] =$2 } END{ for(key in latest) { print latest[key] } }'
}

sort_versions() {
sed 'h; s/[+-]/./g; s/.p\([[:digit:]]\)/.z.\1/; s/$/.z/; G; s/\n/ /' | \
LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n | awk '{print $2}'
Expand Down Expand Up @@ -1234,6 +1256,10 @@ for option in "${OPTIONS[@]}"; do
list_definitions
exit 0
;;
"l" | "list")
list_maintained_versions
exit 0
;;
"k" | "keep" )
KEEP_BUILD_PATH=true
;;
Expand Down
65 changes: 65 additions & 0 deletions test/definitions.bats
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,71 @@ truffleruby-19.3.0"
assert_success "$expected"
}

@test "filtering previous Ruby versions" {
export RUBY_BUILD_ROOT="$TMP"
mkdir -p "${RUBY_BUILD_ROOT}/share/ruby-build"

all_versions="
2.4.0
2.4.1
2.4.2
2.4.3
2.4.4
2.4.5
2.4.6
2.4.7
2.4.8
2.4.9
2.5.0
2.5.1
2.5.2
2.5.3
2.5.4
2.5.5
2.5.6
2.5.7
2.6.0
2.6.1
2.6.2
2.6.3
2.6.4
2.6.5
2.7.0
jruby-1.5.6
jruby-9.2.7.0
jruby-9.2.8.0
jruby-9.2.9.0
maglev-1.0.0
mruby-1.4.1
mruby-2.0.0
mruby-2.0.1
mruby-2.1.0
rbx-3.104
rbx-3.105
rbx-3.106
rbx-3.107
truffleruby-19.2.0.1
truffleruby-19.3.0
truffleruby-19.3.0.2
truffleruby-19.3.1"

expected="2.4.9
2.5.7
2.6.5
2.7.0
jruby-9.2.9.0
maglev-1.0.0
mruby-2.1.0
rbx-3.107
truffleruby-19.3.1"

for ver in $all_versions; do
touch "${RUBY_BUILD_ROOT}/share/ruby-build/$ver"
done
run ruby-build --list
assert_success "$expected"
}

@test "removing duplicate Ruby versions" {
export RUBY_BUILD_ROOT="$TMP"
export RUBY_BUILD_DEFINITIONS="${RUBY_BUILD_ROOT}/share/ruby-build"
Expand Down
5 changes: 3 additions & 2 deletions test/rbenv.bats
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ stub_ruby_build() {
stub_ruby_build \
"--definitions : echo 1.8.7 1.9.3-p0 1.9.3-p194 2.1.2 | tr ' ' $'\\n'"

run rbenv-install --list
run rbenv-install --list-all
assert_success
assert_output <<OUT
1.8.7
Expand Down Expand Up @@ -122,7 +122,7 @@ OUT
mkdir -p "${RBENV_ROOT}/plugins/bar/share/ruby-build"
stub_ruby_build "--definitions : echo \$RUBY_BUILD_DEFINITIONS | tr ':' $'\\n'"

run rbenv-install --list
run rbenv-install --list-all
assert_success
assert_output <<OUT

Expand All @@ -140,6 +140,7 @@ OUT
assert_success
assert_output <<OUT
--list
--list-all
--force
--skip-existing
--keep
Expand Down