summaryrefslogtreecommitdiff
path: root/plugins/autoenv/autoenv.plugin.zsh
blob: 229a8a83404de0450f0d07d428684a8c23adbe9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Initialization: activate autoenv or report its absence
() {
local d autoenv_dir install_locations
if ! type autoenv_init >/dev/null; then
  # Check if activate.sh is in $PATH
  if (( $+commands[activate.sh] )); then
    autoenv_dir="${commands[activate.sh]:h}"
  fi

  # Locate autoenv installation
  if [[ -z $autoenv_dir ]]; then
    install_locations=(
      ~/.autoenv
      ~/.local/bin
      /usr/local/opt/autoenv
      /opt/homebrew/opt/autoenv
      /usr/local/bin
      /usr/share/autoenv-git
      ~/Library/Python/bin
    )
    for d ( $install_locations ); do
      if [[ -e $d/activate.sh ]]; then
        autoenv_dir=$d
        break
      fi
    done
  fi

  # Look for Homebrew path as a last resort
  if [[ -z "$autoenv_dir" ]] && (( $+commands[brew] )); then
    d=$(brew --prefix)/opt/autoenv
    if [[ -e $d/activate.sh ]]; then
      autoenv_dir=$d
    fi
  fi

  # Complain if autoenv is not installed
  if [[ -z $autoenv_dir ]]; then 
    cat <<END >&2
-------- AUTOENV ---------
Could not locate autoenv installation.
Please check if autoenv is correctly installed.
In the meantime the autoenv plugin is DISABLED.
--------------------------
END
    return 1
  fi
  # Load autoenv
  source $autoenv_dir/activate.sh
fi
}
[[ $? != 0 ]] && return $?

# The use_env call below is a reusable command to activate/create a new Python
# virtualenv, requiring only a single declarative line of code in your .env files.
# It only performs an action if the requested virtualenv is not the current one.

use_env() {
  local venv
  venv="$1"
  if [[ "${VIRTUAL_ENV:t}" != "$venv" ]]; then
    if workon | grep -q "$venv"; then
      workon "$venv"
    else
      echo -n "Create virtualenv $venv now? (Yn) "
      read answer
      if [[ "$answer" == "Y" ]]; then
        mkvirtualenv "$venv"
      fi
    fi
  fi
}