]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/pyenv/pyenv.plugin.zsh
initial
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / pyenv / pyenv.plugin.zsh
1 pyenv_config_warning() {
2   [[ "$ZSH_PYENV_QUIET" != true ]] || return 0
3
4   local reason="$1"
5   local pyenv_root="${PYENV_ROOT/#$HOME/\$HOME}"
6   cat >&2 <<EOF
7 Found pyenv, but it is badly configured ($reason). pyenv might not
8 work correctly for non-interactive shells (for example, when run from a script).
9 ${(%):-"%B%F{yellow}"}
10 To fix this message, add these lines to the '.profile' and '.zprofile' files
11 in your home directory:
12 ${(%):-"%f"}
13 export PYENV_ROOT="$pyenv_root"
14 export PATH="\$PYENV_ROOT/bin:\$PATH"
15 eval "\$(pyenv init --path)"
16 ${(%):-"%F{yellow}"}
17 You'll need to restart your user session for the changes to take effect.${(%):-%b%f}
18 For more information go to https://github.com/pyenv/pyenv/#installation.
19 EOF
20 }
21
22 # This plugin loads pyenv into the current shell and provides prompt info via
23 # the 'pyenv_prompt_info' function. Also loads pyenv-virtualenv if available.
24
25 # Look for pyenv in $PATH and verify that it's not a part of pyenv-win in WSL
26 if ! command -v pyenv &>/dev/null; then
27   FOUND_PYENV=0
28 elif [[ "${commands[pyenv]}" = */pyenv-win/* && "$(uname -r)" = *icrosoft* ]]; then
29   FOUND_PYENV=0
30 else
31   FOUND_PYENV=1
32 fi
33
34 # Look for pyenv and try to load it (will only work on interactive shells)
35 if [[ $FOUND_PYENV -ne 1 ]]; then
36   pyenvdirs=("$HOME/.pyenv" "/usr/local/pyenv" "/opt/pyenv" "/usr/local/opt/pyenv")
37   for dir in $pyenvdirs; do
38     if [[ -d "$dir/bin" ]]; then
39       FOUND_PYENV=1
40       break
41     fi
42   done
43
44   if [[ $FOUND_PYENV -ne 1 ]]; then
45     if (( $+commands[brew] )) && dir=$(brew --prefix pyenv 2>/dev/null); then
46       if [[ -d "$dir/bin" ]]; then
47         FOUND_PYENV=1
48       fi
49     fi
50   fi
51
52   # If we found pyenv, load it but show a caveat about non-interactive shells
53   if [[ $FOUND_PYENV -eq 1 ]]; then
54     # Configuring in .zshrc only makes pyenv available for interactive shells
55     export PYENV_ROOT="$dir"
56     export PATH="$PYENV_ROOT/bin:$PATH"
57     eval "$(pyenv init --path)"
58
59     # Show warning due to bad pyenv configuration
60     pyenv_config_warning 'pyenv command not found in $PATH'
61   fi
62 fi
63
64 if [[ $FOUND_PYENV -eq 1 ]]; then
65   if [[ -z "$PYENV_ROOT" ]]; then
66     # This is only for backwards compatibility with users that previously relied
67     # on this plugin exporting it. pyenv itself does not require it to be exported
68     export PYENV_ROOT="$(pyenv root)"
69   fi
70
71   # Add pyenv shims to $PATH if not already added
72   if [[ -z "${path[(Re)$(pyenv root)/shims]}" ]]; then
73     eval "$(pyenv init --path)"
74     pyenv_config_warning 'missing pyenv shims in $PATH'
75   fi
76
77   # Load pyenv
78   eval "$(pyenv init - --no-rehash zsh)"
79
80   # If pyenv-virtualenv exists, load it
81   if [[ "$(pyenv commands)" =~ "virtualenv-init" && "$ZSH_PYENV_VIRTUALENV" != false ]]; then
82     eval "$(pyenv virtualenv-init - zsh)"
83   fi
84
85   function pyenv_prompt_info() {
86     local version="$(pyenv version-name)"
87     echo "${version:gs/%/%%}"
88   }
89 else
90   # Fall back to system python
91   function pyenv_prompt_info() {
92     local version="$(python3 -V 2>&1 | cut -d' ' -f2)"
93     echo "system: ${version:gs/%/%%}"
94   }
95 fi
96
97 unset FOUND_PYENV pyenvdirs dir
98 unfunction pyenv_config_warning