-
Notifications
You must be signed in to change notification settings - Fork 0
/
activate
84 lines (71 loc) · 2.59 KB
/
activate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#https://unix.stackexchange.com/a/153061
VIRTUALIZE_SOURCED_NAME="${_:-$BASH_SOURCE}" # must do this first because of $_ ambiguity
if [[ ${BASH_SOURCE-} = $0 && ! $VIRTUALIZE_ACTIVATE_VIA_SCRIPT ]]; then
echo "you must source this script: \$ source $0" >&2
exit 33
fi
if [[ $VIRTUALIZE_ACTIVATE_VIA_SCRIPT ]]; then
# this assume the script doing the activating does a 'cd' into the
# directory where the activate file is is before sourcing it
VIRTUALIZE_SOURCED_NAME="./activate"
fi
if [[ $VIRTUALIZE_ROOT && ! $VIRTUALIZE_ACTIVATE_VIA_SCRIPT ]]; then
echo "this shell is already virtualized for $VIRTUALIZE_ROOT, type 'unactivate' to undo"
return
fi
export VIRTUALIZE_ROOT=$( cd -- "$( dirname -- "${VIRTUALIZE_SOURCED_NAME}" )" &> /dev/null && pwd )
echo "activating virtualize for $VIRTUALIZE_ROOT"
VIRTUALIZE_ORIG_PATH="$PATH"
VIRTUALIZE_ORIG_PS1="$PS1"
export PATH="$VIRTUALIZE_ROOT/bin:$VIRTUALIZE_ROOT/virtualize/bin:$PATH"
function sgr() { # still leaks (or overrides) this one function name
"$VIRTUALIZE_ROOT"/virtualize/bin/sgr $1
}
VIRTUALIZE_ROOT_DISPLAY=$( echo "$VIRTUALIZE_ROOT" | sed "s%^$HOME/%%" | sed 's/\//·/g' )
if [[ $ZSH_VERSION ]]; then
PS1="%{$(sgr boldblue)%}[$VIRTUALIZE_ROOT_DISPLAY]%{$(sgr reset)%}$PS1"
else # for bash (and others?)
PS1="$(sgr boldblue)[$VIRTUALIZE_ROOT_DISPLAY]$(sgr reset)\n$PS1"
fi
VIRTUALIZE_LIST=()
for activate in virtualize*/activate; do
if [[ $activate == "virtualize/activate" ]]; then
continue
fi
VIRTUALIZE_ACTIVATING=$(dirname $activate | sed 's/^virtualize-//')
echo "activating $VIRTUALIZE_ACTIVATING"
# workaround bash behavior when sourced files source other files
VIRTUALIZE_SUBSOURCE="$activate"
source $activate
unset VIRTUALIZE_SUBSOURCE
VIRTUALIZE_LIST+=($VIRTUALIZE_ACTIVATING)
unset VIRTUALIZE_ACTIVATING
done
unset activate
function unactivate() {
local unactivate
for unactivate in "${VIRTUALIZE_LIST[@]}"; do
echo "unactivating $unactivate"
"unactivate_${unactivate}"
unset -f unactivate_${unactivate}
done
unset VIRTUALIZE_LIST
PATH="$VIRTUALIZE_ORIG_PATH"
PS1="$VIRTUALIZE_ORIG_PS1"
unset VIRTUALIZE_ORIG_PATH
unset VIRTUALIZE_ORIG_PS1
unset VIRTUALIZE_ROOT_DISPLAY
unset VIRTUALIZE_SOURCED_NAME
local virtualize_root="$VIRTUALIZE_ROOT"
unset VIRTUALIZE_ROOT
unset -f unactivate
unalias deactivate
unset -f reactivate
echo "unactivated $virtualize_root"
}
alias deactivate=unactivate
function reactivate() {
unactivate
source ./activate
}
echo "type 'unactivate' to undo"