rbenvを使ってみる - すぎゃーんメモã®ç¶ãã
ç¾æç¹ã§ã®rbenvã®ãã¼ã¸ã§ã³ã¯0.2.1ã
rbenvã使ã£ã¦ããã¨.rbenv-version
ãã¡ã¤ã«ã®æç¡ã§rubyã³ãã³ãæã£ãã¨ãã«å®è¡ãããrubyãéããã®ã«ãªããã¨ããã®ãã¡ãã£ã¨æ°é®®ã§ãããã¯ã©ãããä»çµã¿ã§åãã¦ããã®ã ããï¼ã¨æã£ã¦å°ã調ã¹ã¦ã¿ãã
ä¸è¨è¨äºã®ããã«rbenvã®è¨å®ãããç°å¢ã§ã¯ã
$ which ruby /Users/sugyan/.rbenv/shims/ruby
ã¨ãªãã${RBENV_ROOT}/shims
以ä¸ã®rubyãæããã¨ã«ãªããããã¸ã®PATHã¯$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##*/}" "$@"
ã¨ã ãæ¸ãã¦ãããããã¯åãã£ã¬ã¯ããªã«ããgem
ãirb
ãrake
ãªã©ãå
¨é¨åãå
容ãçµå±ãã®PATHã«å
¥ã£ã¦ããã³ãã³ããå¼ã°ããå ´åã¯ãã¹ã¦åãããã«å¦çããããã¨ãããã¨ã«ãªãã
ãã®ã·ã§ã«ã¹ã¯ãªããã®å
容ã¨ãã¦ã¯
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!'
ã ã¨3è¡ç®ã®testã³ãã³ãã0ã§ãªãçµäºã³ãã³ããè¿ãã®ã§ããã®æç¹ã§ãã®ã·ã§ã«ã¹ã¯ãªãããçµäºãã¦"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-exec
ãexec
ããå½¢ã«ãªã£ã¦ãã¦ããã®rbenv-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
ããåå¾ã§ãã"å®è¡ãã¹ãrubyã®å®ä½ã¸ã®PATHâãåå¾ãã¦ãexec
ãã¦å¼æ°ããããã¦ãããã¨ãããã¨ããã¦ããããã ã
ã§ã${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-which
ã§å®è¡ãã¹ãrubyãæ¢ãããããã¯ä¸ã§rbenv-version-name
ã§å®è¡ãã¹ãrubyã®ãã¼ã¸ã§ã³ãç¹å®ããããã¤ãä¸ã§rbenv-version-file
ã§.rbenv-version
ãã¡ã¤ã«ããããã©ãã調ã¹ã
ã¨ããæµãã«ãªãã.rbenv-version
ãã¡ã¤ã«ãè¦ã¤ããã°ã
rbenv-version-file
ããã®ãã¡ã¤ã«ãã¹ãè¿ããrbenv-version-name
ãããããèªã¿åã£ã${RBENV_VERSION}
ãè¿ããrbenv-which
ããã®ãã¼ã¸ã§ã³ã«å¯¾å¿ãã${RBENV_COMMAND_PATH}
ãè¿ããrbenv-exec
ããã®ã³ãã³ããå®éã«å®è¡ãã
ã¨ãããããããªããªãæ·±ãâ¦ã
ã¨ãããã¨ã¯
ç¹ã«rubyç¹æã®ä»çµã¿ã使ã£ã¦ããããã»ã¼ã·ã§ã«ã¹ã¯ãªããã ãã§å®ç¾ããã¦ããã®ã§ãä»ã®ä¼¼ããããªã¹ã¯ãªããè¨èªã§ããã®ä»çµã¿ã¯é©ç¨ã§ãããã
ã¨ãããã¨ã§Perlã§å®é¨ãã¦ã¿ãã
ã¾ãã¯rbenvã¨ã¾ã£ããåããã®ãplenvã¨ããååã§cloneããã
$ 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/.zshenv
ã«PATHã¨è¨å®ã追è¨ã
# plenv path=($HOME/.plenv/bin(N) $path) eval "$(plenv init -)"
ããã§plenv
ã³ãã³ãã¯ä½¿ããããã«ãªãã
versions以ä¸ãããperlbrewã§installæ¸ã¿ã®perlã«ã·ã³ããªãã¯ãªã³ã¯ãè²¼ã£ã¦ãã(æ°è¦ã¤ã³ã¹ãã¼ã«ã§ãè¯ãã¯ã)ã
$ 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 ...
åããããªãããã§åãæ¿ãã§ãã¾ããã