-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwb_sources
executable file
·134 lines (115 loc) · 3.36 KB
/
pwb_sources
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env bash
declare PREFIX=#PREFIX#
# Get app prefix from first three characters of the called function,
# accommodating either 'pwb_sources' or 'ate_sources':
declare APP_PREFIX="${0##*/}"
APP_PREFIX="${APP_PREFIX:0:3}"
if [[ "$APP_PREFIX" != "pwb" ]] && [[ "$APP_PREFIX" != "ate" ]]; then
echo "APP_PREFIX '$APP_PREFIX' breaks nameing convention necessary for success." >&2
exit 1
fi
declare ATE_PATH="${PREFIX}/lib/ate_sources"
declare PWB_PATH="${PREFIX}/lib/pwb_sources"
script_exists() { [ -f "${SOURCE_PATH}/$1" ]; }
function_loaded() { [[ $( type -f "$1" 2>/dev/null ) == "function" ]]; }
show_usage()
{
local sname
printf -v sname $'\e[1m%s\e[22m' "${APP_PREFIX}_sources"
cat <<EOF
USAGE:
$sname [help] [source_name ...]
The first three calls return information:
Display this usage message:
$sname help
Display a list of all available sources:
$sname
Display usage message for source ${APP_PREFIX}_exit_on_error
$sname help ${APP_PREFIX}_exit_on_error
The final format returns a code fragment that will load and
of the listed sources that haven't already been loaded:
Use this form in a script like this, omitting the '...'
or replacing it with a continuing list of sources:
$sname ${APP_PREFIX}_exit_on_error ...
An example of using the final form in a script:
source <( $sname ${APP_PREFIX}_exit_on_error )
EOF
}
list_sources()
{
local -a sources=( "${PREFIX}/lib/${APP_PREFIX}_sources/"* )
local OIFS="$IFS"
local IFS='/'
for func in "${sources[@]}"; do
local -a parts=( $func )
echo "${parts[@]: -1}"
done
IFS="$OIFS"
}
return_source_name()
{
if ! function_loaded "$1"; then
local prefix="${1%%_*}"
local path="${PREFIX}/lib/${prefix}_sources/$1"
if [ -f "$path" ]; then
echo -n "$path"
fi
fi
}
return_sources_script()
{
for func in "$@"; do
if ! function_loaded "$func"; then
local prefix="${func%%_*}"
local path="${PREFIX}/lib/${prefix}_sources/$func"
if [ -f "$path" ]; then
echo "if ! declare -f $func >/dev/null; then source $path; fi"
fi
fi
done
}
show_func_doc()
{
local path="$1"
local IFS=$'\n'
local -a lines=( $( grep ^##\\\([[:space:]].*\\\)\\?$ "$path" ) )
local -i progress=0
local line cline
for line in "${lines[@]}"; do
[[ "$line" =~ ^##[[:space:]](.*)$ ]]
cline="${BASH_REMATCH[1]}"
if [ "$progress" -eq 0 ] && [[ "$cline" == "BEGIN_DOC" ]]; then
echo
(( ++progress ))
elif [ "$progress" -eq 1 ]; then
if [[ "$cline" != "END_DOC" ]]; then
printf "%s\n" "$cline"
else
(( ++progress ))
break
fi
fi
done
}
if [ "$#" -eq 0 ]; then
list_sources
elif [[ "$1" == "help" ]]; then
if [ "$#" -eq 1 ]; then
show_usage
else
declare func="$2"
declare prefix="${func%%_*}"
declare path="${PREFIX}/lib/${prefix}_sources/$func"
if [ -f "$path" ]; then
enable "$prefix"
source "$path"
declare READ_ARGS_SHOW_USAGE=1
"$func"
show_func_doc "$path"
else
printf $'Unrecognized scriptlet "\e[32;1m%s\e[m"\n' "$func"
fi
fi
else
return_sources_script "$@"
fi