rwx/sh/main.sh

249 lines
5.5 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"
2025-07-06 04:21:03 +02:00
# prefix for command functions
RWX_SELF_COMMAND="_${RWX_SELF_NAME}_cmd_"
# ╭──────┬───────────╮
# │ main │ variables │
# ╰──────┴───────────╯
RWX_COMMAND_ARGUMENT="${0}"
RWX_SHELL="$(cat "/proc/${$}/comm")"
RWX_COMMAND_NAME="$(basename "${RWX_COMMAND_ARGUMENT}" |
sed "s|^-||")"
case "${RWX_COMMAND_NAME}" in
"bash" | "dash" | "sh") unset RWX_COMMAND_NAME ;;
*) ;;
esac
RWX_ROOT_SYSTEM="/usr/local/lib/${RWX_SELF_NAME}"
RWX_SELF_USER="${HOME}/${RWX_SELF_NAME}"
2025-07-06 06:08:39 +02:00
RWX_AWK="${RWX_ROOT_SYSTEM}/${RWX_MAIN_NAME%.sh}.awk"
RWX_MAIN_PATH="${RWX_ROOT_SYSTEM}/${RWX_MAIN_NAME}"
# ╭──────┬──────╮
2025-06-28 04:50:53 +02:00
# │ main │ main │
# ╰──────┴──────╯
2025-06-28 04:50:53 +02:00
# run initial steps
rwx_main() {
2025-07-03 21:11:39 +02:00
# cache main
rwx_cache "${RWX_ROOT_SYSTEM}" "${RWX_MAIN_NAME}"
2025-06-28 04:50:53 +02:00
# source system root
2025-07-03 20:42:59 +02:00
if ! rwx_source "${RWX_ROOT_SYSTEM}" "${RWX_MAIN_NAME}"; then
2025-06-28 04:50:53 +02:00
__rwx_log "Not a directory: ${RWX_ROOT_SYSTEM}"
return 1
fi
# source user root
rwx_source "${RWX_SELF_USER}"
2025-07-04 00:47:18 +02:00
# parse code cache
2025-07-04 07:44:31 +02:00
rwx_parse_code
2025-06-28 04:50:53 +02:00
# context / command
if [ -n "${RWX_COMMAND_NAME}" ]; then
"${RWX_SELF_COMMAND}${RWX_COMMAND_NAME}" "${@}"
# context / shell
else
rwx_self_init
fi
}
2025-07-03 21:11:39 +02:00
# ╭──────┬───────╮
# │ main │ cache │
# ╰──────┴───────╯
2025-07-06 05:59:23 +02:00
# cache of all sourced code modules
_rwx_code=""
2025-07-04 03:35:51 +02:00
# cache source code of a module
2025-07-06 03:16:41 +02:00
# inside a global code variable
2025-07-03 21:11:39 +02:00
rwx_cache() {
local root="${1}"
local module="${2}"
local name="${module%.sh}"
local path="${root}/${module}"
2025-07-04 03:35:51 +02:00
local text
2025-07-03 21:47:57 +02:00
text="$(cat "${path}")"
2025-07-04 08:13:29 +02:00
# all source code
2025-07-06 05:59:23 +02:00
_rwx_code="${_rwx_code}\
2025-07-04 20:24:58 +02:00
#. ${name}
2025-07-04 01:17:12 +02:00
${text}
"
2025-07-03 21:11:39 +02:00
}
2025-06-28 04:50:53 +02:00
# ╭──────┬────────╮
# │ main │ source │
# ╰──────┴────────╯
# source code from file path
rwx_source() {
2025-07-03 18:14:56 +02:00
local root="${1}"
[ -d "${root}" ] ||
2025-06-28 04:50:53 +02:00
return 1
2025-07-03 20:42:59 +02:00
local file="${2}"
2025-06-28 04:50:53 +02:00
local count module
count=0
__rwx_log "" \
2025-07-03 18:14:56 +02:00
". ${root}"
2025-07-04 04:34:26 +02:00
local modules="$(rwx_find_shell "${root}" "${file}")"
while IFS= read -r module; do
2025-06-28 04:50:53 +02:00
count=$((count + 1))
__rwx_log "$(printf "%02d" "${count}") ${module%.sh}"
# shellcheck disable=SC1090
2025-07-03 21:11:39 +02:00
. "${root}/${module}"
# cache code
rwx_cache "${root}" "${module}"
2025-07-04 04:34:26 +02:00
done <<EOF
${modules}
EOF
}
# ╭──────┬─────╮
2025-06-28 04:50:53 +02:00
# │ main │ log │
# ╰──────┴─────╯
2025-06-28 04:50:53 +02:00
__rwx_log() {
if rwx_shell_interactive; then
[ ${#} -gt 0 ] || set -- ""
local line
for line in "${@}"; do
echo "${line}"
done
fi
}
2025-06-28 04:41:33 +02:00
# ╭──────┬───────╮
# │ main │ shell │
# ╰──────┴───────╯
# test if active shell is in interactive mode
rwx_shell_interactive() {
case "${-}" in
*i*) ;;
*) return 1 ;;
esac
}
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
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-04 00:26:44 +02:00
# ╭──────┬───────╮
# │ main │ parse │
# ╰──────┴───────╯
2025-07-04 06:14:08 +02:00
RWX_REGEX_CONSTANT="[_A-Z][_0-9A-Z]*"
RWX_REGEX_FUNCTION="[_A-Za-z][_0-9A-Za-z]*"
RWX_REGEX_SET="=.*"
RWX_REGEX_SPACES="[[:space:]]*"
RWX_REGEX_BEGIN="^${RWX_REGEX_SPACES}"
RWX_REGEX_OPEN="\
${RWX_REGEX_SPACES}(${RWX_REGEX_SPACES})${RWX_REGEX_SPACES}{.*"
2025-07-04 08:43:18 +02:00
RWX_REGEX_TARGET_CONSTANT="\
${RWX_REGEX_BEGIN}\\(${RWX_REGEX_CONSTANT}\\)${RWX_REGEX_SET}"
2025-07-04 09:07:36 +02:00
RWX_REGEX_TARGET_DOC="\
${RWX_REGEX_BEGIN}# \\(.*\\)${RWX_REGEX_SPACES}\$"
2025-07-04 08:43:18 +02:00
RWX_REGEX_TARGET_FUNCTION="\
${RWX_REGEX_BEGIN}\\(${RWX_REGEX_FUNCTION}\\)${RWX_REGEX_OPEN}"
2025-07-04 07:44:31 +02:00
rwx_parse_code() {
2025-07-06 06:41:44 +02:00
# parse aliases
local line
RWX_ALIASES="$(rwx_parse_aliases)"
while IFS= read -r line; do
2025-07-06 07:53:27 +02:00
eval "${line/ = /() { } \"\${@}\"; }"
2025-07-06 06:41:44 +02:00
done <<EOF
${RWX_ALIASES}
EOF
2025-07-04 06:14:08 +02:00
# parse constants
2025-07-04 07:44:31 +02:00
RWX_CONSTANTS="$(rwx_parse_constants)"
# parse functions
RWX_FUNCTIONS="$(rwx_parse_functions)"
}
2025-07-06 06:41:44 +02:00
rwx_parse_aliases() {
printf "%s" "${_rwx_code}" |
awk \
--assign action="alias" \
--file "${RWX_AWK}"
}
2025-07-04 07:44:31 +02:00
rwx_parse_constants() {
2025-07-06 05:59:23 +02:00
printf "%s\n" "${_rwx_code}" |
2025-07-05 15:57:24 +02:00
sed --silent "s|${RWX_REGEX_TARGET_CONSTANT}|\\1|p"
2025-07-04 07:44:31 +02:00
}
rwx_parse_functions() {
2025-07-06 05:59:23 +02:00
printf "%s\n" "${_rwx_code}" |
2025-07-05 15:57:24 +02:00
sed --silent "s|${RWX_REGEX_TARGET_FUNCTION}|\\1|p"
2025-07-04 00:26:44 +02:00
}
2025-07-06 04:11:06 +02:00
# ╭──────┬──────╮
# │ main │ test │
# ╰──────┴──────╯
rwx_test() {
local item
# TODO CODE
# TODO CONSTANTS
# TODO functions
# TODO variables
set \
"main" \
2025-07-06 05:31:51 +02:00
"alias/git" \
2025-07-06 04:11:06 +02:00
\
"RWX_MAIN_NAME" \
\
2025-07-06 05:59:23 +02:00
"_rwx_code" \
\
2025-07-06 04:11:06 +02:00
"rwx_cache" \
\
"gsc"
for item in "${@}"; do
echo
rwx_doc "${item}"
done
}
2025-06-28 04:45:29 +02:00
# ╭──────┬─────╮
# │ main │ run │
# ╰──────┴─────╯
# run main function
rwx_main "${@}"