11#! /bin/bash
2- #
3- # Description
4- # -----------
5- # Switch to appropriate (or given) Virtual Environment.
6- # 1. Use the virtual environment given by the argument
7- # 2. If above is not possible, use the VENV "here" named "nodeenv"
8- # 3. If above is not possible, use the VENV pointed by $SCADAJS_X_VENV variable
9- #
102set -e -o pipefail
113safe_source () { [[ ! -z ${1:- } ]] && source $1 ; _dir=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) " ; _sdir=$( dirname " $( readlink -f " $0 " ) " ) ; }; safe_source
124_sdir=$( dirname $( realpath ${BASH_SOURCE[0]} ) )
135
6+ # Use ./nodeenv if exists
147LOCAL_VENV=" $_sdir /nodeenv"
15-
168[[ -d " $LOCAL_VENV " ]] && SCADAJS_VENV_PATH=$LOCAL_VENV
179
10+ # Check if this script is sourced
1811_sourced=
1912if [[ " ${BASH_SOURCE[0]} " != " ${0} " ]]; then
2013 # echo "script ${BASH_SOURCE[0]} is being sourced ..."
2316 [[ -n ${1:- } ]] && SCADAJS_VENV_PATH=" $1 "
2417fi
2518
19+ # For Tmux VirtualEnv support
20+ tmux_get_var (){
21+ local key=$1
22+ [[ -n " $TMUX " ]] && tmux showenv | awk -F= -v key=" $key " ' $1==key {print $2}'
23+ }
24+
25+ # Get environment variable from within the TMUX session
26+ venv=$( tmux_get_var " SCADAJS_VENV_PATH" ) || venv=" "
27+ if [ -n " $venv " ]; then
28+ SCADAJS_VENV_PATH=" $venv "
29+ fi
30+
2631if [[ ! -d " $SCADAJS_VENV_PATH " ]]; then
2732 if [[ -n $SCADAJS_VENV_PATH ]]; then
2833 echo " $SCADAJS_VENV_PATH variable is set but it's not a valid directory."
@@ -34,9 +39,10 @@ if [[ ! -d "$SCADAJS_VENV_PATH" ]]; then
3439
3540 Please do one of the followings:
3641
37- 1. Create a virtual environment in THIS directory and name it "nodeenv".
38- 2. Set \$ SCADAJS_VENV_PATH variable in your startup script.
39- 3. Pass the virtual environment path as the first argument to this script.
42+ 1. Create a virtual environment in THIS directory and name it as "nodeenv".
43+ 2. Set \$ SCADAJS_VENV_PATH environment variable beforehand.
44+ 3. Set \$ SCADAJS_VENV_PATH environment variable in TMUX.
45+ 4. Pass the virtual environment path as the first argument to this script.
4046
4147EOL
4248 fi
0 commit comments