b62911660d3743d3dfb0116f5b05aabd67a24a06

Author
Marc Cornellà <marc.cornella@live.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

Organize error checking in virtualenwrapper plugin

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh
  2index 9a0616b72dfa1cdf7497e26f1747f5459e9f9498..0ad33691743d993b5cdee7b8b0b46528e6afb31f 100644
  3--- a/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh
  4+++ b/plugins/virtualenvwrapper/virtualenvwrapper.plugin.zsh
  5@@ -6,67 +6,70 @@ elif [[ -f "/etc/bash_completion.d/virtualenvwrapper" ]]; then
  6   virtualenvwrapper="/etc/bash_completion.d/virtualenvwrapper"
  7   source "/etc/bash_completion.d/virtualenvwrapper"
  8 else
  9-  print "zsh virtualenvwrapper plugin: Cannot find ${virtualenvwrapper}. Please install with \`pip install virtualenvwrapper\`."
 10+  print "zsh virtualenvwrapper plugin: Cannot find ${virtualenvwrapper}.\n"\
 11+        "Please install with \`pip install virtualenvwrapper\`" >&2
 12+  return
 13+fi
 14+if ! type workon &>/dev/null; then
 15+  print "zsh virtualenvwrapper plugin: shell function 'workon' not defined.\n"\
 16+        "Please check ${virtualenvwrapper}" >&2
 17   return
 18 fi
 19 
 20-if type workon 2>&1 >/dev/null; then
 21-  if [[ "$WORKON_HOME" == "" ]]; then
 22-    echo "\$WORKON_HOME is not defined so ZSH plugin virtualenvwrapper will not work"
 23-  else
 24-    if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
 25-      # Automatically activate Git projects's virtual environments based on the
 26-      # directory name of the project. Virtual environment name can be overridden
 27-      # by placing a .venv file in the project root with a virtualenv name in it
 28-      function workon_cwd {
 29-        if [ ! $WORKON_CWD ]; then
 30-          WORKON_CWD=1
 31-          # Check if this is a Git repo
 32-          PROJECT_ROOT=`git rev-parse --show-toplevel 2> /dev/null`
 33-          if (( $? != 0 )); then
 34-            PROJECT_ROOT="."
 35-          fi
 36-          # Check for virtualenv name override
 37-          if [[ -f "$PROJECT_ROOT/.venv" ]]; then
 38-            ENV_NAME=`cat "$PROJECT_ROOT/.venv"`
 39-          elif [[ -f "$PROJECT_ROOT/.venv/bin/activate" ]];then
 40-            ENV_NAME="$PROJECT_ROOT/.venv"
 41-          elif [[ "$PROJECT_ROOT" != "." ]]; then
 42-            ENV_NAME=`basename "$PROJECT_ROOT"`
 43-          else
 44-            ENV_NAME=""
 45-          fi
 46-          if [[ "$ENV_NAME" != "" ]]; then
 47-            # Activate the environment only if it is not already active
 48-            if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then
 49-              if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
 50-                workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
 51-              elif [[ -e "$ENV_NAME/bin/activate" ]]; then
 52-                source $ENV_NAME/bin/activate && export CD_VIRTUAL_ENV="$ENV_NAME"
 53-              fi
 54-            fi
 55-          elif [[ -n $CD_VIRTUAL_ENV && -n $VIRTUAL_ENV ]]; then
 56-            # We've just left the repo, deactivate the environment
 57-            # Note: this only happens if the virtualenv was activated automatically
 58-            deactivate && unset CD_VIRTUAL_ENV
 59-          fi
 60-          unset PROJECT_ROOT
 61-          unset WORKON_CWD
 62-        fi
 63-      }
 64+if [[ "$WORKON_HOME" == "" ]]; then
 65+  print "\$WORKON_HOME is not defined so ZSH plugin virtualenvwrapper will not work" >&2
 66+  return
 67+fi
 68 
 69-      # Append workon_cwd to the chpwd_functions array, so it will be called on cd
 70-      # http://zsh.sourceforge.net/Doc/Release/Functions.html
 71-      # TODO: replace with 'add-zsh-hook chpwd workon_cwd' when oh-my-zsh min version is raised above 4.3.4
 72-      if (( ${+chpwd_functions} )); then
 73-        if (( $chpwd_functions[(I)workon_cwd] == 0 )); then
 74-          set -A chpwd_functions $chpwd_functions workon_cwd
 75-        fi
 76+if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
 77+  # Automatically activate Git projects's virtual environments based on the
 78+  # directory name of the project. Virtual environment name can be overridden
 79+  # by placing a .venv file in the project root with a virtualenv name in it
 80+  function workon_cwd {
 81+    if [ ! $WORKON_CWD ]; then
 82+      WORKON_CWD=1
 83+      # Check if this is a Git repo
 84+      PROJECT_ROOT=`git rev-parse --show-toplevel 2> /dev/null`
 85+      if (( $? != 0 )); then
 86+        PROJECT_ROOT="."
 87+      fi
 88+      # Check for virtualenv name override
 89+      if [[ -f "$PROJECT_ROOT/.venv" ]]; then
 90+        ENV_NAME=`cat "$PROJECT_ROOT/.venv"`
 91+      elif [[ -f "$PROJECT_ROOT/.venv/bin/activate" ]];then
 92+        ENV_NAME="$PROJECT_ROOT/.venv"
 93+      elif [[ "$PROJECT_ROOT" != "." ]]; then
 94+        ENV_NAME=`basename "$PROJECT_ROOT"`
 95       else
 96-        set -A chpwd_functions workon_cwd
 97+        ENV_NAME=""
 98+      fi
 99+      if [[ "$ENV_NAME" != "" ]]; then
100+        # Activate the environment only if it is not already active
101+        if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then
102+          if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
103+            workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
104+          elif [[ -e "$ENV_NAME/bin/activate" ]]; then