rwx/sh/code.sh
2025-07-07 03:01:21 +02:00

151 lines
2.9 KiB
Bash

# ╭──────╮
# │ code │
# ╰──────╯
# ╭──────┬───────────╮
# │ code │ variables │
# ╰──────┴───────────╯
# cache for the parsing awk script
_rwx_code_awk="$(cat "${RWX_ROOT_SYSTEM}/code.awk")"
# 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=""
# ╭──────┬───────╮
# │ code │ parts │
# ╰──────┴───────╯
# show the cached code
#= rc
rwx_code() {
echo "${_rwx_code}"
}
# show the cached awk script
rwx_code_awk() {
echo "${_rwx_code_awk}"
}
# show the cached aliases
#= rca
rwx_code_aliases() {
echo "${_rwx_code_aliases}"
}
# show the cached aliases and functions
#= rcaf
rwx_code_aliases_functions() {
echo "${_rwx_code_aliases_functions}"
}
# show the cached constants
#= rcc
rwx_code_constants() {
echo "${_rwx_code_constants}"
}
# show the cached functions
#= rcf
rwx_code_functions() {
echo "${_rwx_code_functions}"
}
# show the cached variables
#= rcv
rwx_code_variables() {
echo "${_rwx_code_variables}"
}
# ╭──────┬───────╮
# │ code │ parse │
# ╰──────┴───────╯
# check source code
rwx_code_check() {
# check format
rwx_log
rwx_shfmt "${RWX_ROOT_SYSTEM}"
# check syntax
rwx_log
rwx_shellcheck "${RWX_ROOT_SYSTEM}"
}
# fetch matching doc for given name
#= rcd
rwx_code_doc() {
local name="${1}"
[ -n "${name}" ] || return
rwx_code |
awk \
--assign action="doc" \
--assign target="${name}" \
"${_rwx_code_awk}"
}
rwx_code_load() {
# parse aliases
_rwx_code_aliases="$(rwx_parse_aliases)"
# parse aliases functions
local line text
_rwx_code_aliases_functions="$(rwx_parse_aliases_functions)"
while IFS= read -r line; do
text="$(echo "${line}" | sed "s| |() { |")"
text="${text} \"\${@}\"; }"
eval "${text}"
done <<EOF
${_rwx_code_aliases_functions}
EOF
# parse constants
_rwx_code_constants="$(rwx_parse_constants)"
# parse functions
_rwx_code_functions="$(rwx_parse_functions)"
# parse variables
_rwx_code_variables="$(rwx_parse_variables)"
}
rwx_code_parse() {
local action="${1}"
rwx_code |
awk \
--assign action="${action}" \
"${_rwx_code_awk}"
}
rwx_parse_aliases() {
rwx_code |
awk \
--assign action="aliases" \
"${_rwx_code_awk}"
}
rwx_parse_aliases_functions() {
rwx_code |
awk \
--assign action="aliases functions" \
"${_rwx_code_awk}"
}
rwx_parse_constants() {
rwx_code |
awk \
--assign action="constants" \
"${_rwx_code_awk}"
}
rwx_parse_functions() {
rwx_code |
awk \
--assign action="functions" \
"${_rwx_code_awk}"
}
rwx_parse_variables() {
rwx_code |
awk \
--assign action="variables" \
"${_rwx_code_awk}"
}