rwx/sh/code.sh

121 lines
2.5 KiB
Bash
Raw Normal View History

2025-07-06 23:15:48 +02:00
# ╭──────╮
# │ code │
# ╰──────╯
2025-07-07 01:13:06 +02:00
# ╭──────┬───────────╮
# │ code │ variables │
# ╰──────┴───────────╯
2025-07-07 02:45:58 +02:00
# cache for the parsing awk script
2025-07-07 01:13:06 +02:00
_rwx_code_awk="$(cat "${RWX_ROOT_SYSTEM}/code.awk")"
2025-07-07 02:45:58 +02:00
# cache for code aliases
_rwx_code_aliases=""
# cache for code aliases functions
_rwx_code_aliases_functions=""
# cache for code constants
_rwx_code_constants=""
# cache for code functions
_rwx_code_functions=""
# cache for code variables
_rwx_code_variables=""
2025-07-07 01:13:06 +02:00
# ╭──────┬───────╮
# │ code │ parts │
# ╰──────┴───────╯
2025-07-06 23:09:41 +02:00
# show the cached code
#= rc
rwx_code() {
echo "${_rwx_code}"
}
2025-07-06 23:10:52 +02:00
2025-07-07 02:45:58 +02:00
# show the cached awk script
rwx_code_awk() {
echo "${_rwx_code_awk}"
}
2025-07-07 02:11:32 +02:00
# show the cached aliases
#= rca
rwx_code_aliases() {
echo "${_rwx_code_aliases}"
}
2025-07-07 00:29:50 +02:00
# show the cached aliases and functions
#= rcaf
rwx_code_aliases_functions() {
echo "${_rwx_code_aliases_functions}"
2025-07-07 00:17:58 +02:00
}
2025-07-06 23:10:52 +02:00
# show the cached constants
#= rcc
rwx_code_constants() {
echo "${_rwx_code_constants}"
}
2025-07-06 23:11:25 +02:00
# show the cached functions
#= rcf
rwx_code_functions() {
echo "${_rwx_code_functions}"
}
2025-07-06 23:15:48 +02:00
2025-07-07 00:01:06 +02:00
# show the cached variables
#= rcv
rwx_code_variables() {
echo "${_rwx_code_variables}"
}
2025-07-06 23:15:48 +02:00
# ╭──────┬───────╮
2025-07-07 01:13:06 +02:00
# │ code │ parse │
2025-07-06 23:15:48 +02:00
# ╰──────┴───────╯
# check source code
rwx_code_check() {
# check format
rwx_log
rwx_shfmt "${RWX_ROOT_SYSTEM}"
# check syntax
rwx_log
rwx_shellcheck "${RWX_ROOT_SYSTEM}"
}
2025-07-07 00:29:50 +02:00
# fetch matching doc for given name
#= rcd
rwx_code_doc() {
local name="${1}"
[ -n "${name}" ] || return
2025-07-07 02:45:58 +02:00
rwx_code |
2025-07-07 00:29:50 +02:00
awk \
--assign action="doc" \
--assign target="${name}" \
2025-07-07 02:23:26 +02:00
"${_rwx_code_awk}"
2025-07-07 00:29:50 +02:00
}
2025-07-07 02:58:39 +02:00
rwx_code_load() {
# parse aliases
2025-07-07 03:07:28 +02:00
_rwx_code_aliases="$(rwx_code_parse "aliases")"
2025-07-07 02:58:39 +02:00
# parse aliases functions
local line text
2025-07-07 03:07:28 +02:00
_rwx_code_aliases_functions="$(rwx_code_parse "aliases functions")"
2025-07-07 02:58:39 +02:00
while IFS= read -r line; do
text="$(echo "${line}" | sed "s| |() { |")"
text="${text} \"\${@}\"; }"
eval "${text}"
done <<EOF
${_rwx_code_aliases_functions}
EOF
# parse constants
2025-07-07 03:07:28 +02:00
_rwx_code_constants="$(rwx_code_parse "constants")"
2025-07-07 02:58:39 +02:00
# parse functions
2025-07-07 03:07:28 +02:00
_rwx_code_functions="$(rwx_code_parse "functions")"
2025-07-07 02:58:39 +02:00
# parse variables
2025-07-07 03:07:28 +02:00
_rwx_code_variables="$(rwx_code_parse "variables")"
2025-07-07 02:58:39 +02:00
}
2025-07-07 03:01:21 +02:00
rwx_code_parse() {
local action="${1}"
rwx_code |
awk \
--assign action="${action}" \
"${_rwx_code_awk}"
}