-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsandboxer.sh
88 lines (72 loc) · 2.32 KB
/
sandboxer.sh
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
85
86
#!/bin/bash
if [ -z "$SANDBOXER_ROOT" ]; then
export SANDBOXER_ROOT="$HOME/.sandboxer"
fi
_activate() {
if [ ! -e $SANDBOXER_ROOT/$1 ]; then
echo "$SANDBOXER_ROOT/$1 does not exist"
return
fi
if [ ! -z "$SANDBOXER_BOX" ]; then
_deactivate
fi
export SANDBOXER_BOX=$1
export SANDBOXER_OLD_PATH=$PATH
export SANDBOXER_OLD_GHC_PACKAGE_PATH=$GHC_PACKAGE_PATH
export SANDBOXER_REAL_CABAL_DEV=$(which cabal-dev)
export PATH=$SANDBOXER_ROOT/$SANDBOXER_BOX/bin:$SANDBOXER_ROOT/$SANDBOXER_BOX/sandboxer:$PATH
# we need to set GHC_PACKAGE_PATH, and when run non-interactively
# ghc-pkg list includes colons at the end
local system=$(cabal-dev ghc-pkg list | grep "^/" | head -n 1)
local user=$(cabal-dev ghc-pkg list | grep "^/" | tail -n 1)
export GHC_PACKAGE_PATH="$user${system%?}"
}
_deactivate() {
if [ -z "$SANDBOXER_BOX" ]; then
echo "Not currently in a sandbox."
return
fi
echo "Deactivating sandbox $SANDBOXER_BOX."
export PATH=$SANDBOXER_OLD_PATH
export GHC_PACKAGE_PATH=$SANDBOXER_OLD_GHC_PACKAGE_PATH
unset SANDBOXER_REAL_CABAL_DEV
unset SANDBOXER_BOX
unset SANDBOXER_OLD_PATH
}
function sandboxer() {
case $1 in
init)
echo "Making sandbox $2"
local sandbox=$HOME/.sandboxer/$2
mkdir -p $sandbox/sandboxer
# activate first so we get SANDBOXER_REAL_CABAL_DEV
_activate $2
local target=$sandbox/sandboxer/cabal-dev
# create the fake cabal-dev file, escaping the
# REAL_CABAL_DEV so that moving cabal-dev around doesn't
# completely break existing cabal-dev sandboxes
cat > $target <<EOF
#!/bin/bash
\$SANDBOXER_REAL_CABAL_DEV \$* --sandbox=$sandbox
EOF
chmod 0755 $target;
hash -r
;;
activate)
_activate $2
;;
deactivate)
_deactivate $2
;;
*)
cat <<EOF
sandboxer is a shell script that helps you manage cabal-dev sandboxes even in
sandbox-unaware applications.
Usage:
sandboxer init <name> -- create a new sandbox and activate it
sandboxer activate <name> -- activate a sandbox
sandboxer deactivate <name> -- deactivate the current sandbox
EOF
;;
esac
}