-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib-require.sh
More file actions
41 lines (37 loc) · 906 Bytes
/
lib-require.sh
File metadata and controls
41 lines (37 loc) · 906 Bytes
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
#!/usr/bin/false
if [[ -z "${BIN}" ]]
then
BIN="$(cd "$(dirname "$0")" ; pwd)"
fi
function get-var {
eval "echo \"\$$1\""
}
function set-var {
local VALUE="'$(echo "$2" | sed -e "s/'/'\\''/g")'"
eval "$1=${VALUE}"
}
function require() {
local STATUS
local MODULES=("$@")
set -- "${ARGV[@]}"
for M in "${MODULES[@]}"
do
M_UC="$(echo "${M}" | tr -- '-a-z' '_A-Z')"
VAR="LIB_${M_UC}_STATUS"
STATUS="$(get-var "${VAR}")"
if [[ -z "${STATUS}" ]]
then
set-var "${VAR}" 'loading'
SCRIPT_FILE="${BIN}/lib-${M}.sh"
## echo "Loading ${M}: ${SCRIPT_FILE}" >&2
source "${SCRIPT_FILE}"
set-var "${VAR}" 'loaded'
elif [[ ".${STATUS}" = '.loading' ]]
then
## echo "Recursively loading ${M} is not allowed. (Check if it possible to use 'require-lax'.)"
return 1
else
: ## echo "Module ${M} ${STATUS}" >&2
fi
done
}