diff --git a/bin/pyenv-sh-activate b/bin/pyenv-sh-activate index 7d17293..c3f23f8 100755 --- a/bin/pyenv-sh-activate +++ b/bin/pyenv-sh-activate @@ -25,6 +25,30 @@ resolve_link() { unset FORCE unset QUIET +# Define `before_activate` and `after_activate` functions that allow +# plugin hooks to register a string of code for execution before or +# after activating a virtualenv. +declare -a before_hooks after_hooks + +before_activate() { + local hook="$1" + before_hooks["${#before_hooks[@]}"]="$hook" +} + +after_activate() { + local hook="$1" + after_hooks["${#after_hooks[@]}"]="$hook" +} + +# Load plugin hooks. +OLDIFS="$IFS" +IFS=$'\n' scripts=(`pyenv-hooks activate`) +IFS="$OLDIFS" +for script in "${scripts[@]}"; do source "$script"; done + +# Execute `before_activate` hooks. +for hook in "${before_hooks[@]}"; do eval "$hook"; done + while [ $# -gt 0 ]; do case "$1" in "--complete" ) @@ -258,3 +282,6 @@ if [ -d "${prefix}/conda-meta" ] || esac shopt -u nullglob fi + +# Execute `after_activate` hooks. +for hook in "${after_hooks[@]}"; do eval "$hook"; done diff --git a/test/activate.bats b/test/activate.bats index 8e74ce6..c644e4d 100644 --- a/test/activate.bats +++ b/test/activate.bats @@ -16,6 +16,11 @@ setup() { unset PYENV_VIRTUAL_ENV_DISABLE_PROMPT unset VIRTUAL_ENV_DISABLE_PROMPT unset _OLD_VIRTUAL_PS1 + stub pyenv-hooks "" +} + +teardown() { + unstub pyenv-hooks } @test "activate virtualenv from current version" { diff --git a/test/hooks.bats b/test/hooks.bats index 44352fe..88fffd5 100644 --- a/test/hooks.bats +++ b/test/hooks.bats @@ -40,3 +40,35 @@ OUT unstub pyenv-rehash teardown_version "3.5.1" } + +@test "pyenv-sh-activate hooks" { + cat > "${HOOK_PATH}/activate.bash" <