autoenv.plugin.zsh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Initialization: activate autoenv or report its absence
  2. () {
  3. local d autoenv_dir install_locations
  4. if ! type autoenv_init >/dev/null; then
  5. # Check if activate.sh is in $PATH
  6. if (( $+commands[activate.sh] )); then
  7. autoenv_dir="${commands[activate.sh]:h}"
  8. fi
  9. # Locate autoenv installation
  10. if [[ -z $autoenv_dir ]]; then
  11. install_locations=(
  12. ~/.autoenv
  13. ~/.local/bin
  14. /usr/local/opt/autoenv
  15. /opt/homebrew/opt/autoenv
  16. /usr/local/bin
  17. /usr/share/autoenv-git
  18. ~/Library/Python/bin
  19. )
  20. for d ( $install_locations ); do
  21. if [[ -e $d/activate.sh ]]; then
  22. autoenv_dir=$d
  23. break
  24. fi
  25. done
  26. fi
  27. # Look for Homebrew path as a last resort
  28. if [[ -z "$autoenv_dir" ]] && (( $+commands[brew] )); then
  29. d=$(brew --prefix)/opt/autoenv
  30. if [[ -e $d/activate.sh ]]; then
  31. autoenv_dir=$d
  32. fi
  33. fi
  34. # Complain if autoenv is not installed
  35. if [[ -z $autoenv_dir ]]; then
  36. cat <<END >&2
  37. -------- AUTOENV ---------
  38. Could not locate autoenv installation.
  39. Please check if autoenv is correctly installed.
  40. In the meantime the autoenv plugin is DISABLED.
  41. --------------------------
  42. END
  43. return 1
  44. fi
  45. # Load autoenv
  46. source $autoenv_dir/activate.sh
  47. fi
  48. }
  49. [[ $? != 0 ]] && return $?
  50. # The use_env call below is a reusable command to activate/create a new Python
  51. # virtualenv, requiring only a single declarative line of code in your .env files.
  52. # It only performs an action if the requested virtualenv is not the current one.
  53. use_env() {
  54. local venv
  55. venv="$1"
  56. if [[ "${VIRTUAL_ENV:t}" != "$venv" ]]; then
  57. if workon | grep -q "$venv"; then
  58. workon "$venv"
  59. else
  60. echo -n "Create virtualenv $venv now? (Yn) "
  61. read answer
  62. if [[ "$answer" == "Y" ]]; then
  63. mkvirtualenv "$venv"
  64. fi
  65. fi
  66. fi
  67. }