198 lines
4.3 KiB
Bash
198 lines
4.3 KiB
Bash
# self module
|
|
|
|
# meta doc
|
|
rwx_doc() {
|
|
local name="${1}"
|
|
[ -n "${name}" ] || return
|
|
local constant doc func line module
|
|
while IFS= read -r line; do
|
|
case "${line}" in
|
|
"#!"*) doc="" ;;
|
|
"#."*)
|
|
doc=""
|
|
module="$(echo "${line}" | sed "s|#\\. \\(.*\\)|\\1|")"
|
|
;;
|
|
*"#"*)
|
|
[ -n "${doc}" ] && doc="${doc}
|
|
"
|
|
doc="${doc}$(echo "${line}" | sed "s|${RWX_REGEX_TARGET_DOC}|\\1|")"
|
|
;;
|
|
*"="*)
|
|
constant="$(echo "${line}" | sed "s|${RWX_REGEX_TARGET_CONSTANT}|\\1|")"
|
|
if [ "${constant}" = "${name}" ]; then
|
|
echo "${doc}"
|
|
return
|
|
else
|
|
doc=""
|
|
fi
|
|
;;
|
|
*"("*")"*"{"*)
|
|
func="$(echo "${line}" | sed "s|${RWX_REGEX_TARGET_FUNCTION}|\\1|")"
|
|
if [ "${func}" = "${name}" ]; then
|
|
echo "${doc}"
|
|
return
|
|
else
|
|
doc=""
|
|
fi
|
|
;;
|
|
*)
|
|
if [ "${module}" = "${name}" ]; then
|
|
echo "${doc}"
|
|
return
|
|
else
|
|
doc=""
|
|
fi
|
|
;;
|
|
esac
|
|
done <<EOF
|
|
${RWX_CODE}
|
|
EOF
|
|
}
|
|
|
|
# ╭──────┬───────╮
|
|
# │ self │ check │
|
|
# ╰──────┴───────╯
|
|
|
|
# check source code
|
|
rwx_self_check() {
|
|
# check format
|
|
rwx_log
|
|
rwx_shfmt "${RWX_ROOT_SYSTEM}"
|
|
# check syntax
|
|
rwx_log
|
|
rwx_shellcheck "${RWX_ROOT_SYSTEM}"
|
|
}
|
|
|
|
# ╭──────┬──────────╮
|
|
# │ self │ commands │
|
|
# ╰──────┴──────────╯
|
|
|
|
# get commands from root
|
|
rwx_self_commands() {
|
|
grep \
|
|
--directories "recurse" \
|
|
--no-filename \
|
|
"^${RWX_SELF_COMMAND}" "${RWX_ROOT_SYSTEM}" |
|
|
cut --delimiter "(" --fields 1 |
|
|
sed "s|^${RWX_SELF_COMMAND}||"
|
|
}
|
|
|
|
# ╭──────┬───────────╮
|
|
# │ self │ functions │
|
|
# ╰──────┴───────────╯
|
|
|
|
# get functions from root
|
|
rwx_self_functions() {
|
|
grep \
|
|
--directories "recurse" \
|
|
--no-filename \
|
|
"()" "${RWX_ROOT_SYSTEM}" |
|
|
cut --delimiter "(" --fields 1
|
|
}
|
|
|
|
# ╭──────┬──────╮
|
|
# │ self │ help │
|
|
# ╰──────┴──────╯
|
|
|
|
# output help message
|
|
rwx_self_help() {
|
|
rwx_log \
|
|
"rwx_… = functions" \
|
|
" a__… = aliases" \
|
|
" u__… = user"
|
|
}
|
|
|
|
# ╭──────┬──────╮
|
|
# │ self │ init │
|
|
# ╰──────┴──────╯
|
|
|
|
rwx_self_init() {
|
|
# run interactive extras
|
|
if rwx_shell_interactive; then
|
|
# help
|
|
rwx_log
|
|
rwx_self_help
|
|
fi
|
|
}
|
|
|
|
# ╭──────┬─────────╮
|
|
# │ self │ install │
|
|
# ╰──────┴─────────╯
|
|
|
|
_rwx_cmd_rwx_install() { rwx_self_install "${@}"; }
|
|
rwx_self_install() {
|
|
local target="${1}"
|
|
local command file name root
|
|
# code
|
|
if [ -n "${target}" ]; then
|
|
root="${target}${RWX_ROOT_SYSTEM}"
|
|
rwx_remove "${root}"
|
|
cp --recursive "${RWX_ROOT_SYSTEM}" "${root}"
|
|
fi
|
|
# commands
|
|
root="${target}/usr/local/bin"
|
|
name="${RWX_SELF_NAME}.sh"
|
|
file="${root}/${name}"
|
|
rwx_remove "${file}"
|
|
rwx_link "${file}" "${RWX_MAIN_PATH}"
|
|
for command in $(rwx_self_commands); do
|
|
file="${root}/${command}"
|
|
rwx_remove "${file}"
|
|
rwx_link "${file}" "${name}"
|
|
done
|
|
# sh
|
|
file="${target}/etc/profile.d/${RWX_SELF_NAME}.sh"
|
|
rwx_remove "${file}"
|
|
rwx_file_write "${file}" "\
|
|
export ENV=\"${RWX_MAIN_PATH}\"
|
|
"
|
|
# bash
|
|
file="${target}/etc/bash.bashrc"
|
|
rwx_remove "${file}"
|
|
rwx_link "${file}" "${RWX_MAIN_PATH}"
|
|
}
|
|
|
|
# ╭──────┬────────╮
|
|
# │ self │ subset │
|
|
# ╰──────┴────────╯
|
|
|
|
rwx_self_subset() {
|
|
local argument file root
|
|
for argument in "${@}"; do
|
|
root="${RWX_ROOT_SYSTEM}/${argument}"
|
|
file="${argument}.sh"
|
|
if [ -d "${root}" ]; then
|
|
local file
|
|
for file in $(rwx_find_shell "${root}"); do
|
|
echo "${argument}/${file}"
|
|
done
|
|
elif [ -f "${RWX_ROOT_SYSTEM}/${file}" ]; then
|
|
echo "${file}"
|
|
fi
|
|
done
|
|
}
|
|
|
|
# ╭──────┬───────╮
|
|
# │ self │ write │
|
|
# ╰──────┴───────╯
|
|
|
|
rwx_self_write() {
|
|
local target="${1}"
|
|
if [ -n "${target}" ]; then
|
|
shift
|
|
local file text
|
|
text="#! /usr/bin/env sh
|
|
"
|
|
local files="$(rwx_self_subset "${@}")"
|
|
while IFS= read -r file; do
|
|
text="${text}
|
|
$(cat "${RWX_ROOT_SYSTEM}/${file}")
|
|
"
|
|
done <<EOF
|
|
${files}
|
|
EOF
|
|
rwx_file_write "${target}" "${text}"
|
|
rwx_shfmt "${target}"
|
|
rwx_shellcheck_file "${target}"
|
|
fi
|
|
}
|