From e7c24ccc480ac3b5209a610dcae81424ba38eb8d Mon Sep 17 00:00:00 2001 From: cecep2 Date: Wed, 20 May 2020 15:48:42 +0200 Subject: [PATCH 1/3] Make projects and auto activation work together This patch ensures that that the auto activation and projects plugin play nice together: - Activating a venv and switching directory using `vf workon` now sets VF_AUTO_ACTIVATED flag if auto activation plugin is sourced - In the auto activation plugin, check if projects plugin is sourced. If so, check if PWD is a project directory to make sure changing into project subdirectories does not deactivate venv --- virtualfish/auto_activation.fish | 14 +++++++++++++- virtualfish/projects.fish | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/virtualfish/auto_activation.fish b/virtualfish/auto_activation.fish index 877a18c..aa2bb29 100644 --- a/virtualfish/auto_activation.fish +++ b/virtualfish/auto_activation.fish @@ -6,13 +6,25 @@ 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 "" while test $activation_root != "" if test -f "$activation_root/$VIRTUALFISH_ACTIVATION_FILE" set new_virtualenv_name (command cat "$activation_root/$VIRTUALFISH_ACTIVATION_FILE") break + # If the projects plugin is used alongside auto activation, switching into project directories + # using `vf workon` causes auto activation even if no activation file is present. In this case, + # we make sure the virtualenv is not deactivated when changing into project subdirectories by + # setting new_virtualenv_name to the basename of activation_root if project_path = activation_root + else if type -q __vf_workon + if test -e "$VIRTUAL_ENV/.project" + set -l project_path (command cat "$VIRTUAL_ENV/.project") + if test $project_path = $activation_root + set new_virtualenv_name (command basename $activation_root) + break + end + end end # this strips the last path component from the path. set activation_root (echo $activation_root | sed 's|/[^/]*$||') diff --git a/virtualfish/projects.fish b/virtualfish/projects.fish index 4c01b9a..b55c796 100644 --- a/virtualfish/projects.fish +++ b/virtualfish/projects.fish @@ -11,6 +11,11 @@ function __vf_workon --description "Work on a project" echo "You must specify a project or virtual environment." return 1 end + # Check if auto activation plugin is used alongside projects plugin. + # If it is, set VF_AUTO_ACTIVATED flag when switching into project directory + if type -q __vfsupport_auto_activate + set -l sourced_auto_activation + end # Matches a virtualenv name and possibly a project name if [ -d $VIRTUALFISH_HOME/$argv[1] ] vf activate $argv[1] @@ -18,12 +23,18 @@ function __vf_workon --description "Work on a project" set -l project_file_path (command cat $VIRTUAL_ENV/.project) if [ -d $project_file_path ] cd $project_file_path + if test -z "$sourced_auto_activation" + set -g VF_AUTO_ACTIVATED + end else echo ".project file path does not exist: $project_file_path" return 2 end else if [ -d $PROJECT_HOME/$argv[1] ] cd $PROJECT_HOME/$argv[1] + if test -z "$sourced_auto_activation" + set -g VF_AUTO_ACTIVATED + end end # Matches a project name but not a virtualenv name else if [ -d $PROJECT_HOME/$argv[1] ] @@ -31,6 +42,9 @@ function __vf_workon --description "Work on a project" set -l venv_file "$PROJECT_HOME/$project_name/$VIRTUALFISH_ACTIVATION_FILE" if [ -f $venv_file ] vf activate (command cat $venv_file) + if test -z "$sourced_auto_activation" + set -g VF_AUTO_ACTIVATED + end else echo "No virtual environment found." end From 9802417ec20ecf9bb1e58545d93ec84546e6b97c Mon Sep 17 00:00:00 2001 From: cecep2 Date: Wed, 20 May 2020 16:10:24 +0200 Subject: [PATCH 2/3] Add note to doc about auto-activation and projects Explain that using both auto-activation and projects means that the virtual environment of the project will be deactivated automatically when leaving the project directory. --- docs/plugins.rst | 3 +++ 1 file changed, 3 insertions(+) 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 From cd7701c5205bd25f1dd64ba943d9e3d5528c2898 Mon Sep 17 00:00:00 2001 From: cecep2 Date: Fri, 22 May 2020 20:29:27 +0200 Subject: [PATCH 3/3] Remove changes to Projects, expand Auto-Activation Taking back changes to the Projects plugin, but expanding the Auto-activation plugin to better detect projects in three steps: 1. Check if currently active virtualenv is connected to a project and save its path 2. For cases where no activation file exists in the project directory, additionally check if activation root is in project path to set new_virtualenv_name 3. Finally, additionally check if any virtualenv is active while project path doesn't match activation root. If so, auto-deactivate virtualenv --- virtualfish/auto_activation.fish | 34 +++++++++++++++++++++----------- virtualfish/projects.fish | 14 ------------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/virtualfish/auto_activation.fish b/virtualfish/auto_activation.fish index aa2bb29..4fb4ea1 100644 --- a/virtualfish/auto_activation.fish +++ b/virtualfish/auto_activation.fish @@ -9,23 +9,27 @@ function __vfsupport_auto_activate --on-variable PWD # 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 - # If the projects plugin is used alongside auto activation, switching into project directories - # using `vf workon` causes auto activation even if no activation file is present. In this case, - # we make sure the virtualenv is not deactivated when changing into project subdirectories by - # setting new_virtualenv_name to the basename of activation_root if project_path = activation_root - else if type -q __vf_workon - if test -e "$VIRTUAL_ENV/.project" - set -l project_path (command cat "$VIRTUAL_ENV/.project") - if test $project_path = $activation_root - set new_virtualenv_name (command basename $activation_root) - break - end - end + + # 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 @@ -41,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 diff --git a/virtualfish/projects.fish b/virtualfish/projects.fish index b55c796..4c01b9a 100644 --- a/virtualfish/projects.fish +++ b/virtualfish/projects.fish @@ -11,11 +11,6 @@ function __vf_workon --description "Work on a project" echo "You must specify a project or virtual environment." return 1 end - # Check if auto activation plugin is used alongside projects plugin. - # If it is, set VF_AUTO_ACTIVATED flag when switching into project directory - if type -q __vfsupport_auto_activate - set -l sourced_auto_activation - end # Matches a virtualenv name and possibly a project name if [ -d $VIRTUALFISH_HOME/$argv[1] ] vf activate $argv[1] @@ -23,18 +18,12 @@ function __vf_workon --description "Work on a project" set -l project_file_path (command cat $VIRTUAL_ENV/.project) if [ -d $project_file_path ] cd $project_file_path - if test -z "$sourced_auto_activation" - set -g VF_AUTO_ACTIVATED - end else echo ".project file path does not exist: $project_file_path" return 2 end else if [ -d $PROJECT_HOME/$argv[1] ] cd $PROJECT_HOME/$argv[1] - if test -z "$sourced_auto_activation" - set -g VF_AUTO_ACTIVATED - end end # Matches a project name but not a virtualenv name else if [ -d $PROJECT_HOME/$argv[1] ] @@ -42,9 +31,6 @@ function __vf_workon --description "Work on a project" set -l venv_file "$PROJECT_HOME/$project_name/$VIRTUALFISH_ACTIVATION_FILE" if [ -f $venv_file ] vf activate (command cat $venv_file) - if test -z "$sourced_auto_activation" - set -g VF_AUTO_ACTIVATED - end else echo "No virtual environment found." end