autoenv.plugin.zsh 2.0 KB

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