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.

154 lines
2.4 KiB

  1. #!/usr/bin/env bash
  2. # Summary: Configure the shell environment for pyenv
  3. # Usage: eval "$(pyenv init - [--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 --no-rehash
  10. echo bash
  11. echo fish
  12. echo ksh
  13. echo zsh
  14. exit
  15. fi
  16. print=""
  17. no_rehash=""
  18. for args in "$@"
  19. do
  20. if [ "$args" = "-" ]; then
  21. print=1
  22. shift
  23. fi
  24. if [ "$args" = "--no-rehash" ]; then
  25. no_rehash=1
  26. shift
  27. fi
  28. done
  29. shell="$1"
  30. if [ -z "$shell" ]; then
  31. shell="$(ps -p "$PPID" -o 'args=' 2>/dev/null || true)"
  32. shell="${shell%% *}"
  33. shell="${shell##-}"
  34. shell="${shell:-$SHELL}"
  35. shell="${shell##*/}"
  36. fi
  37. root="${0%/*}/.."
  38. if [ -z "$print" ]; then
  39. case "$shell" in
  40. bash )
  41. if [ -f "${HOME}/.bashrc" ] && [ ! -f "${HOME}/.bash_profile" ]; then
  42. profile='~/.bashrc'
  43. else
  44. profile='~/.bash_profile'
  45. fi
  46. ;;
  47. zsh )
  48. profile='~/.zshrc'
  49. ;;
  50. ksh )
  51. profile='~/.profile'
  52. ;;
  53. fish )
  54. profile='~/.config/fish/config.fish'
  55. ;;
  56. * )
  57. profile='your profile'
  58. ;;
  59. esac
  60. { echo "# Load pyenv automatically by appending"
  61. echo "# the following to ${profile}:"
  62. echo
  63. case "$shell" in
  64. fish )
  65. echo 'status --is-interactive; and source (pyenv init -|psub)'
  66. ;;
  67. * )
  68. echo 'eval "$(pyenv init -)"'
  69. ;;
  70. esac
  71. echo
  72. } >&2
  73. exit 1
  74. fi
  75. mkdir -p "${PYENV_ROOT}/"{shims,versions}
  76. case "$shell" in
  77. fish )
  78. echo "set -gx PATH '${PYENV_ROOT}/shims' \$PATH"
  79. echo "set -gx PYENV_SHELL $shell"
  80. ;;
  81. * )
  82. echo 'export PATH="'${PYENV_ROOT}'/shims:${PATH}"'
  83. echo "export PYENV_SHELL=$shell"
  84. ;;
  85. esac
  86. completion="${root}/completions/pyenv.${shell}"
  87. if [ -r "$completion" ]; then
  88. echo "source '$completion'"
  89. fi
  90. if [ -z "$no_rehash" ]; then
  91. echo 'command pyenv rehash 2>/dev/null'
  92. fi
  93. commands=(`pyenv-commands --sh`)
  94. case "$shell" in
  95. fish )
  96. cat <<EOS
  97. function pyenv
  98. set command \$argv[1]
  99. set -e argv[1]
  100. switch "\$command"
  101. case ${commands[*]}
  102. source (pyenv "sh-\$command" \$argv|psub)
  103. case '*'
  104. command pyenv "\$command" \$argv
  105. end
  106. end
  107. EOS
  108. ;;
  109. ksh )
  110. cat <<EOS
  111. function pyenv {
  112. typeset command
  113. EOS
  114. ;;
  115. * )
  116. cat <<EOS
  117. pyenv() {
  118. local command
  119. EOS
  120. ;;
  121. esac
  122. if [ "$shell" != "fish" ]; then
  123. IFS="|"
  124. cat <<EOS
  125. command="\${1:-}"
  126. if [ "\$#" -gt 0 ]; then
  127. shift
  128. fi
  129. case "\$command" in
  130. ${commands[*]})
  131. eval "\$(pyenv "sh-\$command" "\$@")";;
  132. *)
  133. command pyenv "\$command" "\$@";;
  134. esac
  135. }
  136. EOS
  137. fi