Skip to content

Commit

Permalink
(tue-env)(rm) deactivate env when removing active env
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthijsBurgh committed Jul 4, 2024
1 parent a6be5a1 commit b886ed1
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions setup/tue-env.bash
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
#! /usr/bin/env bash

# ----------------------------------------------------------------------------------------------------
# TUE-ENV IMPLEMENTATION
# ----------------------------------------------------------------------------------------------------

function _tue-env-deactivate-current-env
{
# Deactivate the old virtualenv if it exists
if [[ -n ${VIRTUAL_ENV} ]]
then
echo "[tue-env](deactivate) deactivating old virtualenv"
deactivate || { echo "[tue-env](deactivate) Failed to deactivate the old virtualenv"; return 1; }
fi

echo "[tue-env](deactivate) Unsetting all TUE_ENV* of the old environment: '${TUE_ENV}'"
for var in ${!TUE_ENV*}
do
unset "${var}"
done

return 0
}


# ----------------------------------------------------------------------------------------------------
# TUE-ENV
# ----------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -190,6 +213,13 @@ options:

[[ -f "${TUE_DIR}"/user/envs/"${tue_env}" ]] || { echo "[tue-env](rm) No such environment: '${tue_env}'"; return 1; }

if [[ "${tue_env}" == "${TUE_ENV}" ]]
then
echo "[tue-env](rm) The environment '${tue_env}' is currently active. Deactivating it first."
tue-env unset-default "${tue_env}"
_tue-env-deactivate-current-env || { echo "[tue-env](rm) Failed to deactivate the current environment, don't use this terminal anymore, open a new terminal"; return 1; }
fi

local tue_env_dir
tue_env_dir=$(cat "${TUE_DIR}"/user/envs/"${tue_env}")
rm "${TUE_DIR}"/user/envs/"${tue_env}"
Expand Down Expand Up @@ -265,14 +295,7 @@ Environment directory '${tue_env_dir}' didn't exist (anymore)"""

[[ "${persistent}" == "true" ]] && tue-env set-default "${tue_env}"

# Deactivate the old virtualenv if it exists
[[ -n ${VIRTUAL_ENV} ]] && echo "[tue-env](switch) deactivating old virtualenv" && deactivate

echo "[tue-env](switch) Unsetting all TUE_ENV* of the old environment: '${TUE_ENV}'"
for var in ${!TUE_ENV*}
do
unset "${var}"
done
_tue-env-deactivate-current-env || { echo "[tue-env](switch) Failed to deactivate the current environment, don't use this terminal anymore, open a new terminal"; return 1; }

# Successful, so we can set the environment variables
TUE_ENV=${tue_env}
Expand Down

0 comments on commit b886ed1

Please sign in to comment.