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