Skip to content

Commit

Permalink
Make Auto-Activation and Projects plugins play nice together (#175)
Browse files Browse the repository at this point in the history
Activating both auto-activation and projects means that the
virtual environment of the project will be deactivated automatically
when leaving the project directory. This is accomplished by 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
  • Loading branch information
cecep2 authored May 29, 2020
1 parent 6ea1267 commit f6f26d1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 23 additions & 1 deletion virtualfish/auto_activation.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f6f26d1

Please sign in to comment.