-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwb_sources
executable file
·91 lines (82 loc) · 2.17 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
#!/usr/bin/env bash
declare PREFIX=#PREFIX#
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" ]]; }
list_sources()
{
local -a sources=( "${PWB_PATH}/"* )
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
declare func="$2"
declare prefix="${func%%_*}"
declare path="${PREFIX}/lib/${prefix}_sources/$func"
if [ -f "$path" ]; then
enable "$prefix"
source "$path"
declare PWB_READ_ARGS_SHOW_USAGE=1
"$func"
show_func_doc "$path"
else
printf $'Unrecognized scriptlet "\e[32;1m%s\e[m"\n' "$func"
fi
elif [ "$#" -eq 1 ]; then
return_source_name "$1"
else
return_sources_script "$@"
fi