Skip to content

Commit 79d0a8f

Browse files
author
Cerem Cem ASLAN
committed
added support for using TMUX's environment variables
1 parent 7cca7c2 commit 79d0a8f

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

venv

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
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-
#
102
set -e -o pipefail
113
safe_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
147
LOCAL_VENV="$_sdir/nodeenv"
15-
168
[[ -d "$LOCAL_VENV" ]] && SCADAJS_VENV_PATH=$LOCAL_VENV
179

10+
# Check if this script is sourced
1811
_sourced=
1912
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
2013
#echo "script ${BASH_SOURCE[0]} is being sourced ..."
@@ -23,6 +16,18 @@ else
2316
[[ -n ${1:-} ]] && SCADAJS_VENV_PATH="$1"
2417
fi
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+
2631
if [[ ! -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
4147
EOL
4248
fi

0 commit comments

Comments
 (0)