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

Natural sort RUBIES by version. #278

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

havenwood
Copy link
Collaborator

chruby_rubies()

Extracts detecting and sorting RUBIES into a testable function. The function takes the default Ruby auto-detection directories as arguments and returns a natural Ruby version sorted list of Ruby directories.

Description

First find searches the auto-detection directories one level deep for any Ruby directories (using -mindepth and -maxdepth due to portability issues with -d). Then the detected Ruby directories are sorted using a slight variation of @mpapis’ natural version sorting code from #277 (closes #277, see @mpapis’ explanation of the code there for details). Replaced find and sed with a shell implementation.

The originally detected Ruby directory paths are stored, a copy is mapped to its basename with the first dash replaced with a period, and the stored original is appended to its corresponding sort-friendly basename. Finally, the basenames are dictionary sorted by Ruby name and then numeric sorted by MAJOR, MINOR, and TINY versions.

The PATCH version column is not numeric sorted since a simple numeric sort is ineffective due to the "p" prefix and it's incidentally correctly ordered by find for all supported patch versions. I think it's okay to leave it at that since with adoption of semantic versioning there's no worry about new patch versions incoming.

Unverified

The email in this signature doesn’t match the committer email.
- Uses the find command for building the RUBIES array.
- Uses a variant of mpapis's sed/sort code for natural version sorting.

Unverified

The email in this signature doesn’t match the committer email.
@havenwood havenwood force-pushed the find-then-natural-sort branch from 1871180 to 9e7f0e4 Compare September 15, 2014 07:56
@jrochkind
Copy link

any ETA for a merge and release? It's troublesome that chruby jruby gets us jruby 1.7.9 instead of later versions, I had scripts (and muscle memory) counting on chruby jruby getting me latest installed.

@postmodern
Copy link
Owner

@jrochkind random question, but why do you keep around older ruby versions?

@jrochkind
Copy link

@postmodern good question.

I think you're right that in this case it was just absent-mindedness, and I can take care of my immediate problem by removing old versions. And remembering to remove old versions every time I install a new version.

There have been about 2 times in the past when i was reporting a bug to jruby, and needed to switch back and forth among jruby versions to determine exactly where the bug exhibited. My bug-work may have been off-and-on over a couple weeks, in the midst of which I was doing other 'normal' work and wanted chruby jruby to just find the most recent version for my normal work.

And of course I keep old versions of MRI around in order to run tests of gems I work on against multiple versions of ruby, but since they would be invoked with chruby 1.9.3 or chruby 2.0, it probably wouldn't be related to this issue.

There might be other weird edge cases I'm not thinking of now. You're right they are edge cases, and remembering to clean up old rubies would take care of the bulk of the annoyance. But they do crop up from time to time, and it has been very nice that I don't need to think about "is this an edge case? Do I have old versions around for a reason or for no reason? How might it effect chruby?", and could just count on chruby jruby to give me the latest. It's a great feature of chruby, and I'd love it back even in the midst of multi-digit jruby patch numbers!

Make sense?

@nateberkopec
Copy link

Was reading this PR looking for a way to chruby <LATEST> (I guess it's just "chruby ruby"), but wanted to chime in about old versions.

As a gem author, I frequently need to use old versions as far back as 1.9 to reproduce build failures on my local machine. I think it's a valid use-case.

@schneems
Copy link

Ran into this again today (merry christmas!) #387

@jrochkind
Copy link

Anyone have a good way to "monkey-patch" this into chruby, while still being able to update to any new releases of chruby, if postmodern is still not interested in merging? Or maybe new releases of chruby don't come out so much that I should be scared to just use a fork? My life would still be easier with this feature.

@havenwood
Copy link
Collaborator Author

A nice turn of events is that macOS and the BSDs are now supporting -V, --version-sort, like GNU. Relying on --version-sort would let us do something like the following.

function chruby_rubies()
{
  local rubies
  rubies=()
  for dir in "$@"; do
    [[ -d "$dir" && -n "$(command ls -A "$dir")" ]] && rubies+=("$dir"/*)
  done

  printf "%b\n" "${rubies[@]}" | sort --version-sort | xargs
}

RUBIES=("$(chruby_rubies "$PREFIX/opt/rubies" "$HOME/.rubies")")

@havenwood
Copy link
Collaborator Author

@jrochkind You can save the snippet above as chruby_rubies.sh and source it after chruby.sh to get the functionality now. A plus side of it being a function is you can also run chruby_rubies to detect a newly installed Ruby instead of restarting the shell.

@postmodern
Copy link
Owner

FYI we are considering controlling RUBIES via a function, which can allow monkey patching, for 1.0.0. So expected something like the above example to become standard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants