61ceec528533bdc2865cf3d04a9214dda6f0f173

Author
Jaiden Mispy <^_^@mispy.me>
Committer
Jaiden Mispy <^_^@mispy.me>
Date

Message

Plugin which loads Python virtualenvwrapper and activates virtualenvs on cd into git repos

Diff

 1diff --git a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh
 2new file mode 100644
 3index 0000000000000000000000000000000000000000..afdad1beaa401818fd4ac4162bc4e7a23afb83ff
 4--- /dev/null
 5+++ b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh
 6@@ -0,0 +1,47 @@
 7+WRAPPER_FOUND=0
 8+for wrapsource in "/usr/local/bin/virtualenvwrapper.sh" "/etc/bash_completion.d/virtualenvwrapper" ; do
 9+  if [[ -e $wrapsource ]] ; then
10+    WRAPPER_FOUND=1
11+    source $wrapsource
12+
13+    if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
14+      # Automatically activate Git projects' virtual environments based on the
15+      # directory name of the project. Virtual environment name can be overridden
16+      # by placing a .venv file in the project root with a virtualenv name in it
17+      function workon_cwd {
18+          # Check that this is a Git repo
19+          GIT_DIR=`git rev-parse --git-dir 2> /dev/null`
20+          if (( $? == 0 )); then
21+              # Find the repo root and check for virtualenv name override
22+              GIT_DIR=`readlink -f $GIT_DIR`
23+              PROJECT_ROOT=`dirname "$GIT_DIR"`
24+              ENV_NAME=`basename "$PROJECT_ROOT"`
25+              if [[ -f "$PROJECT_ROOT/.venv" ]]; then
26+                  ENV_NAME=`cat "$PROJECT_ROOT/.venv"`
27+              fi
28+              # Activate the environment only if it is not already active
29+              if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then
30+                  if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
31+                      workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
32+                  fi
33+              fi
34+          elif [ $CD_VIRTUAL_ENV ]; then
35+              # We've just left the repo, deactivate the environment
36+              # Note: this only happens if the virtualenv was activated automatically
37+              deactivate && unset CD_VIRTUAL_ENV
38+          fi
39+      }
40+
41+      # New cd function that does the virtualenv magic
42+      function cd {
43+          builtin cd "$@" && workon_cwd
44+      }
45+    fi
46+
47+    break
48+  fi
49+done
50+
51+if [ $WRAPPER_FOUND -eq 0 ] ; then
52+  print "zsh virtualenvwrapper plugin: Couldn't activate virtualenvwrapper. Please run \`pip install virtualenvwrapper\`."
53+fi