rbenvの切り替えの仕組み…と、他言語での実験


rbenv使 - 
rbenv0.2.1
rbenv使.rbenv-versionrubyruby調
rbenv
$ which ruby
/Users/sugyan/.rbenv/shims/ruby

${RBENV_ROOT}/shimsrubyPATH$HOME/.rbenv/libexec/rbenv-init
echo 'export PATH="'${RBENV_ROOT}'/shims:${PATH}"'

eval "$(rbenv init -)"


${RBENV_ROOT}/shims/ruby
#!/usr/bin/env bash
set -e
export RBENV_ROOT="/Users/sugyan/.rbenv"
exec rbenv exec "${0##*/}" "$@"

gemirbrakePATH

set -e

bash


Exit immediately if a pipeline (see Pipelines), which may consist of a single simple command (see Simple Commands), a subshell command enclosed in parentheses (see Command Grouping), or one of the commands executed as part of a command list enclosed by braces (see Command Grouping) returns a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test in an if statement, part of any command executed in a && or || list except the command following the final && or ||, any command in a pipeline but the last, or if the command's return status is being inverted with !. A trap on ERR, if set, is executed before the shell exits.
 http://www.gnu.org/software/bash/manual/bashref.html#The-Set-Builtin  


#!/usr/bin/env bash
set -e
test -e $0
echo 'finish!'

"finish!"
#!/usr/bin/env bash
set -e
test -d $0
echo 'finish!'

3test0"finish!"
set -e



export RBENV_ROOT="/Users/sugyan/.rbenv"
exec rbenv exec "${0##*/}" "$@"

${0##*/}


The word is expanded to produce a pattern just as in filename expansion (see Filename Expansion). If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the # case) or the longest matching pattern (the ## case) deleted. If parameter is @ or *, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list. 
http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion  

"/"${RBENV_ROOT}/shims/ruby
#!/usr/bin/env bash
set -e
export RBENV_ROOT="/Users/sugyan/.rbenv"
echo $0
echo ${0##*/}
# exec rbenv exec "${0##*/}" "$@"

${RBENV_ROOT}/shims/ruby
$ ruby
/Users/sugyan/.rbenv/shims/ruby
ruby

${RBENV_ROOT}/shims/ruby
exec rbenv exec ruby "$@"

rbenv exec${RBENV_ROOT}/libexec/rbenv-execexecrbenv-exec
...
RBENV_COMMAND="$1"
...
RBENV_COMMAND_PATH="$(rbenv-which "$RBENV_COMMAND")"
RBENV_BIN_PATH="${RBENV_COMMAND_PATH%/*}"
...
shift 1
export PATH="${RBENV_BIN_PATH}:${PATH}"
exec -a "$RBENV_COMMAND" "$RBENV_COMMAND_PATH" "$@"

rbenv-which"rubyPATHexec
${RBENV_ROOT}/libexec/rbenv-which
...
RBENV_VERSION="$(rbenv-version-name)"
RBENV_COMMAND="$1"
...
if [ "$RBENV_VERSION" = "system" ]; then
  PATH="$(remove_from_path "${RBENV_ROOT}/shims")"
  RBENV_COMMAND_PATH="$(command -v "$RBENV_COMMAND")"
else
  RBENV_COMMAND_PATH="${RBENV_ROOT}/versions/${RBENV_VERSION}/bin/${RBENV_COMMAND}"
fi
...

if [ -x "$RBENV_COMMAND_PATH" ]; then
  echo "$RBENV_COMMAND_PATH"
else
...

ruby$(rbenv-version-name)
${RBENV_ROOT}/libexec/rbenv-version-name
...
if [ -z "$RBENV_VERSION" ]; then
  RBENV_VERSION_FILE="$(rbenv-version-file)"
  RBENV_VERSION="$(rbenv-version-file-read "$RBENV_VERSION_FILE" || true)"
fi

if [ -z "$RBENV_VERSION" ] || [ "$RBENV_VERSION" = "system" ]; then
  echo "system"
  exit
fi
...

${RBENV_VERSION}$(rbenv-version-file)${RBENV_VERSION}global, local使
$ rbenv global
1.9.3-p0
$ rbenv local
system
$ ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin10.0]
$ RBENV_VERSION=1.9.2-p290 ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0]

${RBENV_ROOT}/libexec/rbenv-version-file
...
root="$RBENV_DIR"
while [ -n "$root" ]; do
  if [ -e "${root}/.rbenv-version" ]; then
    echo "${root}/.rbenv-version"
    exit
  fi
  root="${root%/*}"
done
...

${RBENV_DIR}.rbenv-version使


要するに


rbenv使ruby


(一)${RBENV_ROOT}/shims/ruby

(二)rbenv-exec

(三)rbenv-whichruby

(四)rbenv-version-nameruby

(五)rbenv-version-file.rbenv-version調


.rbenv-version


(一)rbenv-version-file

(二)rbenv-version-name${RBENV_VERSION}

(三)rbenv-which${RBENV_COMMAND_PATH}

(四)rbenv-exec





ということは


ruby使
Perl
rbenvplenvclone
$ cd
$ git clone git://github.com/sstephenson/rbenv.git .plenv

s/rbenv/plenv/g plenv-*
$ for i in $(find .plenv/libexec -name 'rbenv*'); do perl -pe 's/rbenv/plenv/g;s/RBENV/PLENV/g' $i > ${i/rbenv/plenv}; chmod u+x ${i/rbenv/plenv}; done


$ ln -s ../libexec/plenv .plenv/bin

$HOME/.zshenvPATH
# plenv
path=($HOME/.plenv/bin(N) $path)
eval "$(plenv init -)"

plenv使
versionsperlbrewinstallperl()
$ ln -s $HOME/perl5/perlbrew/perls/perl-5.12.1 $HOME/.plenv/versions/5.12.1
$ ln -s $HOME/perl5/perlbrew/perls/perl-5.14.2 $HOME/.plenv/versions/5.14.2


$ plenv versions
  5.12.1
  5.14.2


$ plenv global 5.14.2
$ plenv versions
  5.12.1
* 5.14.2 (set by /Users/sugyan/.plenv/version)

global$HOME/.plenv/shims/perl
#!/usr/bin/env bash
set -e
export PLENV_ROOT="/Users/sugyan/.plenv"
exec plenv exec "${0##*/}" "$@"


$ perl -v

This is perl 5, version 14, subversion 2 (v5.14.2) built for darwin-2level
...

$ PLENV_VERSION=5.12.1 perl -v

This is perl 5, version 12, subversion 1 (v5.12.1) built for darwin-2level
...


$ PLENV_VERSION=system perl -v

This is perl, v5.10.1 (*) built for darwin-2level
...

$ cd ~/temp
$ ls .plenv-version
ls: .plenv-version: No such file or directory
$ perl -v

This is perl 5, version 14, subversion 2 (v5.14.2) built for darwin-2level
...

$ plenv local 5.12.1
$ ls .plenv-version
.plenv-version
$ cat .plenv-version
5.12.1
$ perl -v

This is perl 5, version 12, subversion 1 (v5.12.1) built for darwin-2level
...