-
Notifications
You must be signed in to change notification settings - Fork 5
/
setup.bash
113 lines (94 loc) · 3.4 KB
/
setup.bash
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#! /usr/bin/env bash
# ------------------------------------------
# Helper function for checking if all env vars are set
function _tue-check-env-vars
{
[[ -n "${TUE_DIR}" ]] && [[ -n "${TUE_ENV}" ]] && [[ -n "${TUE_ENV_DIR}" ]] \
&& [[ -n "${TUE_BIN}" ]] && [[ -n "${TUE_ENV_TARGETS_DIR}" ]] && return 0
echo "[tue] Not all needed environment variables are set."
return 1
}
export -f _tue-check-env-vars
function _tue-env-main
{
# -----------------------------------------
# Set the TUE_DIR variable
TUE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export TUE_DIR
# -----------------------------------------
# Add `.local/bin` and TUE_DIR to PATH
export TUE_BIN=${TUE_DIR}/bin
# .local/bin is needed in the path for all user installs like pip. It gets added automatically on reboot but not on CI
if [[ :${PATH}: != *:${HOME}/.local/bin:* ]]
then
export PATH=${HOME}/.local/bin${PATH:+:${PATH}}
fi
if [[ :${PATH}: != *:${TUE_BIN}:* ]]
then
export PATH=${TUE_BIN}${PATH:+:${PATH}}
fi
# -----------------------------------------
# Load tue-env tool
# shellcheck disable=SC1091
source "${TUE_DIR}"/setup/tue-env.bash
# Load the (optional) default environment
if [[ -z "${TUE_ENV}" ]]
then
[[ -f "${TUE_DIR}"/user/config/default_env ]] || return 0 # Quietly return
TUE_ENV=$(cat "${TUE_DIR}"/user/config/default_env)
export TUE_ENV
if [[ ! -f "${TUE_DIR}"/user/envs/"${TUE_ENV}" ]]
then
echo "[tue] No such environment: '${TUE_ENV}'"
return 1
fi
fi
# -----------------------------------------
# Set the TUE_ENV_DIR and TUE_ENV_TARGETS_DIR variables
TUE_ENV_DIR=$(cat "${TUE_DIR}"/user/envs/"${TUE_ENV}")
export TUE_ENV_DIR
if [[ ! -d "${TUE_ENV_DIR}" ]]
then
echo "[tue] Environment directory '${TUE_ENV_DIR}' (environment '${TUE_ENV}') does not exist"
return 1
fi
export TUE_ENV_TARGETS_DIR=${TUE_ENV_DIR}/.env/targets
if [[ ! -d "${TUE_ENV_TARGETS_DIR}" ]]
then
echo "[tue] Targets directory '${TUE_ENV_TARGETS_DIR}' (environment '${TUE_ENV}') does not exist"
return 1
fi
# -----------------------------------------
# Load the user setup file
if [[ -f "${TUE_ENV_DIR}"/.env/setup/user_setup.bash ]]
then
# shellcheck disable=SC1091
source "${TUE_ENV_DIR}"/.env/setup/user_setup.bash
fi
# -----------------------------------------
# Load the python virtual environment if it exists
if [[ -d "${TUE_ENV_DIR}"/.venv/"${TUE_ENV}" ]]
then
# shellcheck disable=SC1090
source "${TUE_ENV_DIR}"/.venv/"${TUE_ENV}"/bin/activate
fi
# -----------------------------------------
# Load all the bash functions
# shellcheck disable=SC1091
source "${TUE_DIR}"/setup/tue-functions.bash
if [[ -f "${TUE_DIR}"/setup/tue-misc.bash ]]
then
# shellcheck disable=SC1091
source "${TUE_DIR}"/setup/tue-misc.bash
fi
# -----------------------------------------
# Load the target setup files
# The target setup files could depend on anything that was loaded above
if [[ -f "${TUE_ENV_DIR}"/.env/setup/target_setup.bash ]]
then
# shellcheck disable=SC1091
source "${TUE_ENV_DIR}"/.env/setup/target_setup.bash
fi
}
_tue-env-main "$@"
unset -f _tue-env-main