rwx/sh/main.sh

140 lines
3 KiB
Bash
Raw Normal View History

#! /usr/bin/env sh
2025-06-28 04:36:16 +02:00
# ╭──────╮
# │ main │
# ╰──────╯
2025-07-06 04:21:03 +02:00
# main module
2025-06-28 04:36:16 +02:00
# ╭──────┬───────────╮
# │ main │ constants │
# ╰──────┴───────────╯
2025-07-09 02:20:50 +02:00
# extension of shell modules
RWX_MAIN_EXTENSION="sh"
2025-07-09 02:04:14 +02:00
# name of the entrypoint module
RWX_MAIN_NAME="main"
2025-07-06 04:21:03 +02:00
# name of the project itself
RWX_SELF_NAME="rwx"
# ╭──────┬───────────╮
# │ main │ variables │
# ╰──────┴───────────╯
2025-07-08 00:44:25 +02:00
# cache of all sourced code modules
_rwx_code=""
2025-07-08 04:35:32 +02:00
# TODO variablize
2025-07-06 20:53:47 +02:00
# system root directory of the project
RWX_ROOT_SYSTEM="/usr/local/lib/${RWX_SELF_NAME}"
2025-06-28 04:40:19 +02:00
# ╭──────┬──────╮
2025-06-28 04:50:53 +02:00
# │ main │ find │
2025-06-28 04:40:19 +02:00
# ╰──────┴──────╯
2025-07-09 01:04:59 +02:00
# find directory’s shell files
2025-07-08 03:40:08 +02:00
#| find
2025-07-09 02:04:14 +02:00
#| sed
2025-07-08 03:40:08 +02:00
#| sort
2025-07-09 01:04:59 +02:00
rwx_main_find() {
local root="${1}"
2025-07-09 02:04:14 +02:00
find \
2025-06-28 04:50:53 +02:00
"${root}" \
2025-07-09 02:20:50 +02:00
-name "*.${RWX_MAIN_EXTENSION}" \
2025-07-09 02:04:14 +02:00
-type "f" \
2025-06-28 04:50:53 +02:00
-printf "%P\n" |
2025-07-09 02:10:12 +02:00
sed "s|\\.[^.]*\$||" |
sort
2025-06-28 04:50:53 +02:00
}
2025-07-08 01:15:21 +02:00
# ╭──────┬───────╮
# │ main │ shell │
# ╰──────┴───────╯
# test if active shell is in interactive mode
2025-07-09 00:18:57 +02:00
rwx_main_interactive() {
2025-07-08 01:15:21 +02:00
case "${-}" in
*i*) ;;
*) return 1 ;;
esac
}
2025-07-08 01:13:06 +02:00
# ╭──────┬─────╮
# │ main │ log │
# ╰──────┴─────╯
2025-07-08 05:50:46 +02:00
_rwx_main_log() {
2025-07-09 00:18:57 +02:00
if rwx_main_interactive; then
2025-07-08 01:13:06 +02:00
[ ${#} -gt 0 ] || set -- ""
local line
for line in "${@}"; do
echo "${line}"
done
fi
}
2025-07-08 01:12:02 +02:00
# ╭──────┬────────╮
# │ main │ source │
# ╰──────┴────────╯
2025-07-09 02:04:14 +02:00
# source code from root path
2025-07-08 06:59:55 +02:00
rwx_main_source() {
2025-07-08 01:12:02 +02:00
local root="${1}"
[ -d "${root}" ] ||
return 1
2025-07-09 04:57:58 +02:00
local module modules
2025-07-08 05:50:46 +02:00
_rwx_main_log "" \
2025-07-08 01:12:02 +02:00
". ${root}"
2025-07-09 02:04:14 +02:00
modules="$(rwx_main_find "${root}")"
2025-07-08 01:12:02 +02:00
while IFS= read -r module; do
2025-07-09 02:04:14 +02:00
if [ "${module}" != "${RWX_MAIN_NAME}" ]; then
# shellcheck disable=SC1090
2025-07-09 02:20:50 +02:00
. "${root}/${module}.${RWX_MAIN_EXTENSION}"
2025-07-09 02:04:14 +02:00
# cache code
rwx_main_cache "${root}" "${module}"
fi
2025-07-08 01:12:02 +02:00
done <<EOF
${modules}
EOF
}
2025-07-08 00:48:18 +02:00
# ╭──────┬───────╮
# │ main │ cache │
# ╰──────┴───────╯
# cache source code of a module
# inside a global code variable
2025-07-08 03:40:08 +02:00
#| cat
2025-07-08 07:03:26 +02:00
rwx_main_cache() {
2025-07-08 00:48:18 +02:00
local root="${1}"
local module="${2}"
2025-07-09 02:20:50 +02:00
local path="${root}/${module}.${RWX_MAIN_EXTENSION}"
2025-07-08 00:48:18 +02:00
local text
text="$(cat "${path}")"
# all source code
_rwx_code="${_rwx_code}\
2025-07-09 02:10:12 +02:00
#. ${module}
2025-07-08 00:48:18 +02:00
${text}
"
}
# ╭──────┬──────╮
# │ main │ main │
# ╰──────┴──────╯
# run initial steps
2025-07-08 22:13:38 +02:00
#< core/code
2025-07-08 05:50:46 +02:00
rwx_main_main() {
2025-07-08 00:48:18 +02:00
# cache main
2025-07-08 07:03:26 +02:00
rwx_main_cache "${RWX_ROOT_SYSTEM}" "${RWX_MAIN_NAME}"
2025-07-08 00:48:18 +02:00
# source system root
2025-07-09 02:04:14 +02:00
if ! rwx_main_source "${RWX_ROOT_SYSTEM}"; then
2025-07-08 05:50:46 +02:00
_rwx_main_log "Not a directory: ${RWX_ROOT_SYSTEM}"
2025-07-08 00:48:18 +02:00
return 1
fi
2025-07-08 04:35:32 +02:00
# run code main function
2025-07-08 07:07:11 +02:00
rwx_code_main "${@}"
2025-07-08 00:48:18 +02:00
}
# run main function
2025-07-08 05:50:46 +02:00
rwx_main_main "${@}"