-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.sh
executable file
·66 lines (51 loc) · 1.37 KB
/
import.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
#!/bin/bash
# Loads all private functions of a given module
# $1 module name
_load_private_functions()
{
local pattern0="\(^function _[A-Za-z0-9_]+\ *{\?$\)"
local pattern1="\(^_[A-Za-z0-9_]+()\ *{\?$\)"
sed -n "/\($pattern0\|$pattern1\)/,/^}/p" "${1}.sh"
}
# Loads a public function of a particular module
# $1 module name
# $2 function name
_load_public_functions()
{
local pattern0="\(^function $2\ *{\?$\)"
local pattern1="\(^$2()\ *{\?$\)"
sed -n "/\($pattern0\|$pattern1\)/,/^}/p" "${1}.sh"
}
_isloaded()
{
echo "declare -F $1 > /dev/null; echo $?"
}
# usage: use <module>.[function][ as <alias>]
# $1 name of the module/function to include
# $2 the 'as' "keyword"
# $3 function alias
import()
{
local arg=(${1//./ })
local module="${arg[0]}"
local func="${arg[1]}"
local alias="$3"
# check if a function is specified
if [ -n "${func}" ]; then
local private_fn="$(_load_private_functions ${module})"
local public_fn="$(_load_public_functions ${module} ${func})"
# check if an alias was specified
if [ "$2" = "as" -a -n "${alias}" ]; then
public_fn="${public_fn//$func/$alias}"
fi
eval "$private_fn"
eval "$public_fn"
if [ "$(_isloaded $func)" = "$(_isloaded $alias)" ]; then
echo "unable to load function: $func" 1>&2
exit
fi
else
# loads the entire module
source "${module}.sh"
fi
}