162 lines
3.6 kB
1
# meta doc
2
rwx_doc() {
3
local name="${1}"
4
[ -n "${name}" ] || return
5
local doc line module
6
rwx_ifs_set
7
for module in $(rwx_find_shell "${RWX_ROOT_SYSTEM}"); do
8
while read -r line; do
9
case "${line}" in
10
"#"*) doc="${doc}${line}" ;;
11
"${name}() {")
12
echo "${doc}"
13
return
14
;;
15
*) doc="" ;;
16
esac
17
done <"${RWX_ROOT_SYSTEM}/${module}"
18
done
19
rwx_ifs_unset
20
}
21
22
# ╭──────┬───────╮
23
# │ self │ check │
24
# ╰──────┴───────╯
25
26
# check source code
27
rwx_self_check() {
28
# check format
29
rwx_log
30
rwx_shfmt "${RWX_ROOT_SYSTEM}"
31
# check syntax
32
rwx_log
33
rwx_shellcheck "${RWX_ROOT_SYSTEM}"
34
}
35
36
# ╭──────┬──────────╮
37
# │ self │ commands │
38
# ╰──────┴──────────╯
39
40
# get commands from root
41
rwx_self_commands() {
42
grep \
43
--directories "recurse" \
44
--no-filename \
45
"^${RWX_SELF_COMMAND}" "${RWX_ROOT_SYSTEM}" |
46
cut --delimiter "(" --fields 1 |
47
sed "s|^${RWX_SELF_COMMAND}||"
48
}
49
50
# ╭──────┬───────────╮
51
# │ self │ functions │
52
# ╰──────┴───────────╯
53
54
# get functions from root
55
rwx_self_functions() {
56
grep \
57
--directories "recurse" \
58
--no-filename \
59
"()" "${RWX_ROOT_SYSTEM}" |
60
cut --delimiter "(" --fields 1
61
}
62
63
# ╭──────┬──────╮
64
# │ self │ help │
65
# ╰──────┴──────╯
66
67
# output help message
68
rwx_self_help() {
69
rwx_log \
70
"rwx_… = functions" \
71
" a__… = aliases" \
72
" u__… = user"
73
}
74
75
# ╭──────┬──────╮
76
# │ self │ init │
77
# ╰──────┴──────╯
78
79
rwx_self_init() {
80
# run interactive extras
81
if rwx_shell_interactive; then
82
# help
83
rwx_log
84
rwx_self_help
85
fi
86
}
87
88
# ╭──────┬─────────╮
89
# │ self │ install │
90
# ╰──────┴─────────╯
91
92
_rwx_cmd_rwx_install() { rwx_self_install "${@}"; }
93
rwx_self_install() {
94
local target="${1}"
95
local command file root
96
# code
97
if [ -n "${target}" ]; then
98
root="${target}${RWX_ROOT_SYSTEM}"
99
rwx_remove "${root}"
100
cp --recursive "${RWX_ROOT_SYSTEM}" "${root}"
101
fi
102
# commands
103
root="${target}/usr/local/bin"
104
for command in $(rwx_self_commands); do
105
file="${root}/${command}"
106
rwx_remove "${file}"
107
rwx_link "${file}" "${RWX_MAIN_PATH}"
108
done
109
# sh
110
file="${target}/etc/profile.d/${RWX_SELF_NAME}.sh"
111
rwx_remove "${file}"
112
rwx_file_write "${file}" "\
113
export ENV=\"${RWX_MAIN_PATH}\"
114
"
115
# bash
116
file="${target}/etc/bash.bashrc"
117
rwx_remove "${file}"
118
rwx_link "${file}" "${RWX_MAIN_PATH}"
119
}
120
121
# ╭──────┬────────╮
122
# │ self │ subset │
123
# ╰──────┴────────╯
124
125
rwx_self_subset() {
126
local argument path
127
for argument in "${@}"; do
128
path="${RWX_ROOT_SYSTEM}/${argument}"
129
if [ -d "${path}" ]; then
130
local file
131
for file in $(rwx_find_shell "${path}"); do
132
echo "${argument}/${file}"
133
done
134
elif [ -f "${path}" ]; then
135
echo "${argument}"
136
fi
137
done
138
}
139
140
# ╭──────┬───────╮
141
# │ self │ write │
142
# ╰──────┴───────╯
143
144
rwx_self_write() {
145
local target="${1}"
146
if [ -n "${target}" ]; then
147
shift
148
local file text
149
text="#! /usr/bin/env sh
150
"
151
rwx_ifs_set
152
for file in $(rwx_self_subset "${@}"); do
153
text="${text}
154
$(cat "${RWX_ROOT_SYSTEM}/${file}")
155
"
156
done
157
rwx_ifs_unset
158
rwx_file_write "${target}" "${text}"
159
rwx_shfmt "${target}"
160
rwx_shellcheck_file "${target}"
161
fi
162
}
163