You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
3.0 KiB

  1. #!/usr/bin/env bash
  2. # Summary: Configure the shell environment for pyenv
  3. # Usage: eval "$(pyenv init [-|--path] [--no-rehash] [<shell>])"
  4. set -e
  5. [ -n "$PYENV_DEBUG" ] && set -x
  6. # Provide pyenv completions
  7. if [ "$1" = "--complete" ]; then
  8. echo -
  9. echo --path
  10. echo --no-rehash
  11. echo bash
  12. echo fish
  13. echo ksh
  14. echo zsh
  15. exit
  16. fi
  17. mode="help"
  18. no_rehash=""
  19. for args in "$@"
  20. do
  21. if [ "$args" = "-" ]; then
  22. mode="print"
  23. shift
  24. fi
  25. if [ "$args" = "--path" ]; then
  26. mode="path"
  27. shift
  28. fi
  29. if [ "$args" = "--no-rehash" ]; then
  30. no_rehash=1
  31. shift
  32. fi
  33. done
  34. shell="$1"
  35. if [ -z "$shell" ]; then
  36. shell="$(ps -p "$PPID" -o 'args=' 2>/dev/null || true)"
  37. shell="${shell%% *}"
  38. shell="${shell##-}"
  39. shell="${shell:-$SHELL}"
  40. shell="${shell##*/}"
  41. shell="${shell%%-*}"
  42. fi
  43. root="${0%/*}/.."
  44. function main() {
  45. case "$mode" in
  46. "help")
  47. help_
  48. exit 1
  49. ;;
  50. "path")
  51. print_path
  52. exit 0
  53. ;;
  54. "print")
  55. init_dirs
  56. print_env
  57. print_completion
  58. print_shell_function
  59. exit 0
  60. ;;
  61. esac
  62. # should never get here
  63. exit 2
  64. }
  65. function help_() {
  66. case "$shell" in
  67. bash )
  68. profile='~/.bash_profile'
  69. rc='~/.bashrc'
  70. ;;
  71. zsh )
  72. profile='~/.zprofile'
  73. rc='~/.zshrc'
  74. ;;
  75. ksh )
  76. profile='~/.profile'
  77. rc='~/.profile'
  78. ;;
  79. * )
  80. profile='your shell'\''s login startup file'
  81. rc='your shell'\''s interactive startup file'
  82. ;;
  83. esac
  84. {
  85. echo
  86. echo '# See the README for instructions on how to set up'
  87. echo '# your shell environment for Pyenv.'
  88. echo
  89. } >&2
  90. }
  91. function init_dirs() {
  92. mkdir -p "${PYENV_ROOT}/"{shims,versions}
  93. }
  94. function print_path() {
  95. # Need to use the login shell rather than the current one
  96. case "$shell" in
  97. fish )
  98. echo "set -gx PATH '${PYENV_ROOT}/shims' \$PATH"
  99. ;;
  100. * )
  101. echo 'export PATH="'${PYENV_ROOT}'/shims:${PATH}"'
  102. ;;
  103. esac
  104. }
  105. function print_env() {
  106. case "$shell" in
  107. fish )
  108. echo "set -gx PYENV_SHELL $shell"
  109. ;;
  110. * )
  111. echo "export PYENV_SHELL=$shell"
  112. ;;
  113. esac
  114. }
  115. function print_completion() {
  116. completion="${root}/completions/pyenv.${shell}"
  117. if [ -r "$completion" ]; then
  118. echo "source '$completion'"
  119. fi
  120. if [ -z "$no_rehash" ]; then
  121. echo 'command pyenv rehash 2>/dev/null'
  122. fi
  123. }
  124. function print_shell_function() {
  125. commands=(`pyenv-commands --sh`)
  126. case "$shell" in
  127. fish )
  128. cat <<EOS
  129. function pyenv
  130. set command \$argv[1]
  131. set -e argv[1]
  132. switch "\$command"
  133. case ${commands[*]}
  134. source (pyenv "sh-\$command" \$argv|psub)
  135. case '*'
  136. command pyenv "\$command" \$argv
  137. end
  138. end
  139. EOS
  140. ;;
  141. ksh )
  142. cat <<EOS
  143. function pyenv {
  144. typeset command
  145. EOS
  146. ;;
  147. * )
  148. cat <<EOS
  149. pyenv() {
  150. local command
  151. EOS
  152. ;;
  153. esac
  154. if [ "$shell" != "fish" ]; then
  155. IFS="|"
  156. cat <<EOS
  157. command="\${1:-}"
  158. if [ "\$#" -gt 0 ]; then
  159. shift
  160. fi
  161. case "\$command" in
  162. ${commands[*]})
  163. eval "\$(pyenv "sh-\$command" "\$@")"
  164. ;;
  165. *)
  166. command pyenv "\$command" "\$@"
  167. ;;
  168. esac
  169. }
  170. EOS
  171. fi
  172. }
  173. main