mirror of https://gitlab.com/meliurwen/dotvenv
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
3.1 KiB
109 lines
3.1 KiB
3 years ago
|
#!/bin/sh
|
||
|
|
||
|
set -e
|
||
|
|
||
|
# We need to detect if this script is being sourced or not.
|
||
|
# Unfortunately, if we want to maintain POSIX compliancy and portability to
|
||
|
# other shells there is not a 100% accurate and clean method;
|
||
|
# this is the closer we can get... at the moment.
|
||
|
# Source: https://stackoverflow.com/a/28776166
|
||
|
|
||
|
THIS_SCRIPTNAME="activate"
|
||
|
IS_SOURCED=0
|
||
|
if [ -n "$ZSH_EVAL_CONTEXT" ]; then
|
||
|
case $ZSH_EVAL_CONTEXT in *:file) IS_SOURCED=1;; esac
|
||
|
elif [ -n "$KSH_VERSION" ]; then
|
||
|
[ \
|
||
|
"$(cd "$(dirname -- "$0")" && \
|
||
|
pwd -P)/$(basename -- "$0")" != "$(cd "$(dirname -- "${.sh.file}")" && \
|
||
|
pwd -P)/$(basename -- "${.sh.file}")" \
|
||
|
] && IS_SOURCED=1
|
||
|
elif [ -n "$BASH_VERSION" ]; then
|
||
|
(return 0 2>/dev/null) && IS_SOURCED=1
|
||
|
else
|
||
|
# All other shells: examine $0 for known shell binary filenames
|
||
|
# Detects `sh` and `dash`; add additional shell filenames as needed.
|
||
|
case ${0##*/} in
|
||
|
sh|dash)
|
||
|
IS_SOURCED=1
|
||
|
;;
|
||
|
*)
|
||
|
[ "${0##*/}" != "$THIS_SCRIPTNAME" ] && IS_SOURCED=1
|
||
|
;;
|
||
|
esac
|
||
|
fi
|
||
|
|
||
|
if [ "$IS_SOURCED" = "0" ]; then
|
||
|
echo "This script can only be sourced. Aborting..."
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
set +e
|
||
|
|
||
|
# Thank you python's venv for the useful snippets <3
|
||
|
|
||
|
deactivate () {
|
||
|
# reset old environment variables
|
||
|
unset DOTSCRIPTS_D
|
||
|
|
||
|
if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then
|
||
|
PATH="${_OLD_VIRTUAL_PATH:-}"
|
||
|
export PATH
|
||
|
unset _OLD_VIRTUAL_PATH
|
||
|
fi
|
||
|
|
||
|
# This should detect bash and zsh, which have a hash command that must
|
||
|
# be called to get it to forget past commands. Without forgetting
|
||
|
# past commands the $PATH changes we made may not be respected
|
||
|
if [ -n "${BASH:-}" ] || [ -n "${ZSH_VERSION:-}" ] ; then
|
||
|
hash -r 2> /dev/null
|
||
|
fi
|
||
|
|
||
|
if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then
|
||
|
PS1="${_OLD_VIRTUAL_PS1:-}"
|
||
|
export PS1
|
||
|
unset _OLD_VIRTUAL_PS1
|
||
|
fi
|
||
|
|
||
|
if [ ! "${1:-}" = "nondestructive" ] ; then
|
||
|
# Self destruct!
|
||
|
unset -f deactivate
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
if [ -f "./.dotenv" ]; then
|
||
|
. "./.dotenv"
|
||
|
fi
|
||
|
|
||
|
if [ -z "${DOTSCRIPTS_D}" ]; then
|
||
|
#echo "No DOTSCRIPTS_D defined. Applying the default one..."
|
||
|
# In case of sourcing using a relative path there are a couple of cases to
|
||
|
# handle:
|
||
|
# - with `$. ./path/activate` I get `/some/./path`
|
||
|
# - with `$. ./path/activate` I get `/some/../path`
|
||
|
# At the moment only the first case is handled
|
||
|
DOTSCRIPTS_D="$(printf "%s" "$PWD/$(dirname -- "$0")" | sed 's|/./|/|')"
|
||
|
fi
|
||
|
if [ ! -d "$DOTSCRIPTS_D" ]; then
|
||
|
echo "The directory '$DOTSCRIPTS_D' does not exist. Aborting..."
|
||
|
exit 1
|
||
|
fi
|
||
|
export DOTSCRIPTS_D
|
||
|
|
||
|
_OLD_VIRTUAL_PATH="$PATH"
|
||
|
PATH="$DOTSCRIPTS_D:$PATH"
|
||
|
export PATH
|
||
|
|
||
|
# This should detect bash and zsh, which have a hash command that must
|
||
|
# be called to get it to forget past commands. Without forgetting
|
||
|
# past commands the $PATH changes we made may not be respected
|
||
|
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
|
||
|
hash -r 2> /dev/null
|
||
|
fi
|
||
|
|
||
|
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
|
||
|
_OLD_VIRTUAL_PS1="${PS1:-}"
|
||
|
PS1="(dotvenv) ${PS1:-}"
|
||
|
export PS1
|
||
|
fi
|