diff --git a/docs/plugins.rst b/docs/plugins.rst index d68e82a..8f871f7 100644 --- a/docs/plugins.rst +++ b/docs/plugins.rst @@ -136,6 +136,9 @@ directory. If you are using both the *Compatibility Aliases* and *Projects* plugins, ``workon`` will alias ``vf workon`` instead of ``vf activate``. + If you are using both the *Auto-activation* and *Projects* plugins, the + project's virtual environment will be deactivated automatically when you + leave the project's directory. Commands diff --git a/virtualfish/auto_activation.fish b/virtualfish/auto_activation.fish index 877a18c..4fb4ea1 100644 --- a/virtualfish/auto_activation.fish +++ b/virtualfish/auto_activation.fish @@ -6,14 +6,30 @@ function __vfsupport_auto_activate --on-variable PWD return end - # find an auto-activation file + # find an auto-activation file or determine whether inside a project directory set -l activation_root $PWD set -l new_virtualenv_name "" + + # Projects plugin compatibility: Enable auto-deactivation (1/3) + # Check if active virtualenv (if any) is connected to a project + if test -e "$VIRTUAL_ENV/.project" + set project_path (command cat "$VIRTUAL_ENV/.project") + end + while test $activation_root != "" if test -f "$activation_root/$VIRTUALFISH_ACTIVATION_FILE" set new_virtualenv_name (command cat "$activation_root/$VIRTUALFISH_ACTIVATION_FILE") break + + # Projects plugin compatibility: Enable auto-deactivation (2/3) + # vf workon might activate virtualenvs without activation files. To detect those instances + # here in the Auto-activation plugin, check if activation root is a project path. If so, + # set new_virtualenv_name to the basename of the project path + else if test "$project_path" = "$activation_root" + set new_virtualenv_name (command basename $project_path) + break end + # this strips the last path component from the path. set activation_root (echo $activation_root | sed 's|/[^/]*$||') end @@ -29,6 +45,12 @@ function __vfsupport_auto_activate --on-variable PWD # if there's an auto-activated virtualenv, deactivate it if set -q VIRTUAL_ENV VF_AUTO_ACTIVATED vf deactivate + + # Projects plugin compatibility: Enable auto-deactivation (3/3) + # vf workon doesn't set VF_AUTO_ACTIVATED. To deactivate virtualenv automatically when + # leaving project directory, we check if any virtualenv is active while not in project path + else if begin set -q VIRTUAL_ENV; and test "$project_path" != "$activation_root"; end + vf deactivate end end end