rwx/sh/main.sh

150 lines
3.2 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-04 03:35:51 +02:00
# name of the entrypoint file
RWX_MAIN_NAME="main.sh"
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-06-28 04:50:53 +02:00
# find directory’s files by extension
2025-07-08 03:40:08 +02:00
#| find
#| sort
2025-06-28 04:50:53 +02:00
rwx_find_extension() {
local extension="${1}"
local root="${2}"
local file="${3}"
set -- \
"${root}" \
-name "*.${extension}" \
-type "f"
[ -n "${file}" ] &&
set -- "${@}" \
-not \
-name "${file}"
find "${@}" \
-printf "%P\n" |
sort
}
# find directory’s sh files
rwx_find_shell() {
rwx_find_extension "sh" "${@}"
}
2025-07-08 01:15:21 +02:00
# ╭──────┬───────╮
# │ main │ shell │
# ╰──────┴───────╯
# test if active shell is in interactive mode
rwx_shell_interactive() {
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-08 01:13:06 +02:00
if rwx_shell_interactive; then
[ ${#} -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-08 06:59:55 +02:00
# source code from root path but file
rwx_main_source() {
2025-07-08 01:12:02 +02:00
local root="${1}"
[ -d "${root}" ] ||
return 1
local file="${2}"
local count module modules
count=0
2025-07-08 05:50:46 +02:00
_rwx_main_log "" \
2025-07-08 01:12:02 +02:00
". ${root}"
modules="$(rwx_find_shell "${root}" "${file}")"
while IFS= read -r module; do
count=$((count + 1))
2025-07-08 05:50:46 +02:00
_rwx_main_log "$(printf "%02d" "${count}") ${module%.sh}"
2025-07-08 01:12:02 +02:00
# shellcheck disable=SC1090
. "${root}/${module}"
# cache code
2025-07-08 07:03:26 +02:00
rwx_main_cache "${root}" "${module}"
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}"
local name="${module%.sh}"
local path="${root}/${module}"
local text
text="$(cat "${path}")"
# all source code
_rwx_code="${_rwx_code}\
#. ${name}
${text}
"
}
# ╭──────┬──────╮
# │ main │ main │
# ╰──────┴──────╯
# run initial steps
2025-07-08 04:35:32 +02:00
#< 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-08 06:59:55 +02:00
if ! rwx_main_source "${RWX_ROOT_SYSTEM}" "${RWX_MAIN_NAME}"; 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 "${@}"