autoenv.plugin.zsh 1.8 KB

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