rwx/sh/main.sh
Marc Beninca 09bf59c3ba
All checks were successful
/ job (push) Successful in 2m54s
module/main
2025-07-09 05:15:32 +02:00

138 lines
3 KiB
Bash
Executable file

#! /usr/bin/env sh
# ╭──────╮
# │ main │
# ╰──────╯
# main module
# ╭──────┬───────────╮
# │ main │ constants │
# ╰──────┴───────────╯
# extension of shell modules
RWX_MAIN_EXTENSION="sh"
# name of the entrypoint module
RWX_MAIN_NAME="main"
# name of the project itself
RWX_SELF_NAME="rwx"
# ╭──────┬───────────╮
# │ main │ variables │
# ╰──────┴───────────╯
# cache of all sourced code modules
_rwx_code=""
# TODO variablize
# system root directory of the project
RWX_ROOT_SYSTEM="/usr/local/lib/${RWX_SELF_NAME}"
# ╭──────┬──────╮
# │ main │ find │
# ╰──────┴──────╯
# find directory’s shell files
#| find
#| sed
#| sort
rwx_main_find() {
local root="${1}"
find \
"${root}" \
-name "*.${RWX_MAIN_EXTENSION}" \
-type "f" \
-printf "%P\n" |
sed "s|\\.[^.]*\$||" |
sort
}
# ╭──────┬───────╮
# │ main │ shell │
# ╰──────┴───────╯
# test if active shell is in interactive mode
rwx_main_interactive() {
case "${-}" in
*i*) ;;
*) return 1 ;;
esac
}
# ╭──────┬─────╮
# │ main │ log │
# ╰──────┴─────╯
_rwx_main_log() {
if rwx_main_interactive; then
[ ${#} -gt 0 ] || set -- ""
local line
for line in "${@}"; do
echo "${line}"
done
fi
}
# ╭──────┬────────╮
# │ main │ source │
# ╰──────┴────────╯
# source code from root path
rwx_main_source() {
local root="${1}"
local main="${2}"
[ -d "${root}" ] ||
return 1
local module modules
# cache main
[ -n "${main}" ] && rwx_main_cache "${root}" "${main}"
modules="$(rwx_main_find "${root}")"
while IFS= read -r module; do
if [ "${module}" != "${main}" ]; then
# shellcheck disable=SC1090
. "${root}/${module}.${RWX_MAIN_EXTENSION}"
# cache code
rwx_main_cache "${root}" "${module}"
fi
done <<EOF
${modules}
EOF
}
# ╭──────┬───────╮
# │ main │ cache │
# ╰──────┴───────╯
# cache source code of a module
# inside a global code variable
#| cat
rwx_main_cache() {
local root="${1}"
local module="${2}"
local path="${root}/${module}.${RWX_MAIN_EXTENSION}"
local text
text="$(cat "${path}")"
# all source code
_rwx_code="${_rwx_code}\
#. ${module}
${text}
"
}
# ╭──────┬──────╮
# │ main │ main │
# ╰──────┴──────╯
# run initial steps
#< core/code
rwx_main_main() {
# source system root
if ! rwx_main_source "${RWX_ROOT_SYSTEM}" "${RWX_MAIN_NAME}"; then
_rwx_main_log "Not a directory: ${RWX_ROOT_SYSTEM}"
return 1
fi
# run code main function
rwx_code_main "${@}"
}
# run main function
rwx_main_main "${@}"