Skip to content
Closed
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
7 changes: 5 additions & 2 deletions bin/chruby-exec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ source "$chruby_sh"

case "$1" in
-h|--help)
echo "usage: chruby-exec RUBY [RUBYOPTS] -- COMMAND [ARGS...]"
echo "usage: chruby-exec RUBY [RUBYOPTS] [\"RUBIES=(...)\"] -- COMMAND [ARGS...]"
exit
;;
-V|--version)
Expand All @@ -24,7 +24,9 @@ argv=()
for arg in "$@"; do
shift

if [[ "$arg" == "--" ]]; then break
if echo "$arg" | grep -q '^RUBIES=('; then
CUSTOM_RUBIES="$arg"
elif [[ "$arg" == "--" ]]; then break
else argv+=($arg)
fi
done
Expand All @@ -38,6 +40,7 @@ shell_opts=("-l")
[[ -t 0 ]] && shell_opts+=("-i")

source_command="command -v chruby >/dev/null || source $chruby_sh"
[[ -n "$CUSTOM_RUBIES" ]] && source_command+="; $CUSTOM_RUBIES"
chruby_command="chruby $(printf "%q " "${argv[@]}")"
sub_command="$(printf "%q " "$@")"
command="$source_command; $chruby_command && $sub_command"
Expand Down
84 changes: 71 additions & 13 deletions share/chruby/chruby.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,53 @@
CHRUBY_VERSION="0.3.9"
RUBIES=()

for dir in "$PREFIX/opt/rubies" "$HOME/.rubies"; do
[[ -d "$dir" && -n "$(ls -A "$dir")" ]] && RUBIES+=("$dir"/*)
done
unset dir
function chruby_list_rubies()
{
local chruby_entry
for chruby_entry in "$@"; do
[[ -x "$chruby_entry/bin/ruby" ]] && echo "$chruby_entry"
done
}

function chruby_detect_rubies()
{
if [[ -d "$1" ]]; then
{ chruby_list_rubies "$1"/* ;} 2>/dev/null || true
else
chruby_list_rubies $1
fi
}

function chruby_stable_version_sort() {
local dir ruby engine ver old_ver v0 v1 v2 v3 v4
sort | uniq | while read -r dir; do
ruby="${dir##*/}"; engine="${ruby%%-*}"; version="${ruby#*-}"
v0="${ver%%\.*}"; ver="${ver#*\.}"
v1="${ver%%\.*}"; old_ver="$ver"; ver="${ver#*\.}"
[[ "$ver" == "$old_ver" ]] && ver=""
v2="${ver%%\.*}"; old_ver="$ver"; ver="${ver#*\.}"
[[ "$ver" == "$old_ver" ]] && ver=""
v3="${ver%%\.*}"; old_ver="$ver"; ver="${ver#*\.}"
[[ "$ver" == "$old_ver" ]] && ver=""
v4="${v2#*-p}"
[[ "$v2" == "$v4" ]] && v4=""
echo "$engine:$v0:$v1:$v2:$v3:$v4:$dir"
done | sort -n -t: -k1,1 -k2,2 -k3,3 -k4,4 -k5,5 -k6,6 | cut -d: -f7-
}

function chruby_detect_all_rubies()
{
{
chruby_detect_rubies "$PREFIX/opt/rubies"
chruby_detect_rubies "$HOME/.rubies"
if [[ -n "$RUBIES" ]]; then
local chruby_rubies_dir
for chruby_rubies_dir in "${RUBIES[@]}"; do
chruby_detect_rubies "$chruby_rubies_dir"
done
fi
} | chruby_stable_version_sort
}


function chruby_reset()
{
Expand All @@ -30,6 +73,22 @@ function chruby_reset()
hash -r
}

function chruby_generate_cached_env()
{
"$SHELL" -c "unset JAVACMD JRUBY_OPTS; RUBYGEMS_GEMDEPS='' '$RUBY_ROOT/bin/ruby' -" <<'SCRIPT'
env = {
:RUBY_ENGINE => Object.const_defined?(:RUBY_ENGINE) ? RUBY_ENGINE : 'ruby',
:RUBY_VERSION => RUBY_VERSION,
}
begin
require 'rubygems'
env[:GEM_ROOT] = Gem.default_dir
rescue LoadError
end
env.each { |k,v| puts "#{k}='#{v}'; export #{k}" }
SCRIPT
}

function chruby_use()
{
if [[ ! -x "$1/bin/ruby" ]]; then
Expand All @@ -42,13 +101,12 @@ function chruby_use()
export RUBY_ROOT="$1"
export RUBYOPT="$2"
export PATH="$RUBY_ROOT/bin:$PATH"
local ruby_cached_env="$RUBY_ROOT/.ruby_env"
if [[ ! -e "$ruby_cached_env" ]]; then
chruby_generate_cached_env > "$ruby_cached_env"
fi
. "$ruby_cached_env"

eval "$(RUBYGEMS_GEMDEPS="" "$RUBY_ROOT/bin/ruby" - <<EOF
puts "export RUBY_ENGINE=#{Object.const_defined?(:RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'};"
puts "export RUBY_VERSION=#{RUBY_VERSION};"
begin; require 'rubygems'; puts "export GEM_ROOT=#{Gem.default_dir.inspect};"; rescue LoadError; end
EOF
)"
export PATH="${GEM_ROOT:+$GEM_ROOT/bin:}$PATH"

if (( UID != 0 )); then
Expand All @@ -71,7 +129,7 @@ function chruby()
;;
"")
local dir ruby
for dir in "${RUBIES[@]}"; do
chruby_detect_all_rubies | while read -r dir; do
dir="${dir%%/}"; ruby="${dir##*/}"
if [[ "$dir" == "$RUBY_ROOT" ]]; then
echo " * ${ruby} ${RUBYOPT}"
Expand All @@ -84,7 +142,7 @@ function chruby()
system) chruby_reset ;;
*)
local dir ruby match
for dir in "${RUBIES[@]}"; do
chruby_detect_all_rubies | while read -r dir; do
dir="${dir%%/}"; ruby="${dir##*/}"
case "$ruby" in
"$1") match="$dir" && break ;;
Expand Down