Skip to content

Commit

Permalink
local libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
filiparag committed Aug 19, 2023
1 parent 7848fe1 commit 5e878cb
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 26 deletions.
32 changes: 31 additions & 1 deletion libwmrc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

WMRC_VERSION='2.1.0'
WMRC_LOG_LEVEL="${WMRC_LOG_LEVEL:-warn}"
WMRC_CHECK_DEPS="${WMRC_CHECK_DEPS:-true}"
LOG_FILE="/tmp/wmrc@$(whoami)${DISPLAY}.log"
WMRC_CONFIG="$HOME/.config/wmrc"
_pid_file="/tmp/wmrc::$(echo "$_module" | sed 's,/,::,g')@$(whoami)${DISPLAY}.pid"
Expand All @@ -10,6 +11,7 @@ export _module
export DAEMON_PID
export WMRC_VERSION
export WMRC_LOG_LEVEL
export WMRC_CHECK_DEPS
export WMRC_CONFIG

_stderr() {
Expand Down Expand Up @@ -57,6 +59,29 @@ debug() {
)"
}

_module_libraries() {
debug 'Module libraries' "$1"
if ! test -f "$WMRC_CONFIG/modules/$1"; then
error 'Module not found' "$WMRC_CONFIG/modules/$1"
exit 1
fi
libraries="$(
sh -c ". '$WMRC_CONFIG/modules/$1' && echo \$WMRC_LIBRARIES" | sed 's/ \{1,\}/\n/g'
)"
if [ -n "$libraries" ]; then
debug 'Found libraries' "$(echo "$libraries" | sed -z 's/\n/, /g;s/, $/\n/')"
if [ "$WMRC_CHECK_DEPS" != 'false' ]; then
for l in $libraries; do
debug 'Test library file' "$WMRC_CONFIG/libs/$l"
if ! test -f "$WMRC_CONFIG/libs/$l"; then
error 'Library not found' "$WMRC_CONFIG/libs/$l"
exit 1
fi
done
fi
fi
}

call() {
if [ -z "$1" ]; then
error 'Module name not provided'
Expand All @@ -75,7 +100,12 @@ call() {
_params="$_params${_params:+ }'$1'"
shift 1
done
sh -c "_module='$_callee' && . '$WMRC_DIR/libwmrc.sh' && . '$WMRC_CONFIG/modules/$_callee' && $_params"
_libs=''
_module_libraries "$_callee"
if [ -n "$libraries" ]; then
_libs="$(echo "$libraries" | xargs printf ". '$WMRC_CONFIG/libs/%s' &&")"
fi
sh -c "_module='$_callee' && . '$WMRC_DIR/libwmrc.sh' && $_libs. '$WMRC_CONFIG/modules/$_callee' && $_params"
_status="$?"
if [ "$_status" != 0 ]; then
error 'Error executing call' "$_callee::$(echo "$_params" | sed "s/^\(\w\) ?.*$/\1/;s:'::g")"
Expand Down
29 changes: 24 additions & 5 deletions wmrc.man
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Call module's method with arguments
When module name is provided only, calls init method without arguments

.HP
.B start|stop|restart|status
.B start | stop | restart | status
<group>/<module>
.br
Control module's background daemons
Expand Down Expand Up @@ -93,9 +93,11 @@ When accessing variables inside modules, they have
.B WMRC_
prefix added

.SS Logging
%LOG_LEVEL = <level>
.PP
.SS Configuration variables
.HP
.B %LOG_LEVEL
= <level>
.br
Logs are printed to stdout and to file
.I /tmp/wmrc@${USER}${DISPLAY}.log
.br
Expand All @@ -104,6 +106,12 @@ Available levels:
(default),
.B info, debug

.HP
.B %CHECK_DEPS
= true | false (default)
.br
Check dependencies on every module call, with minor performance impact


.SS Units
[unit_name]
Expand All @@ -129,6 +137,17 @@ Wait the call to complete and abort further execution on failure

.SH MODULES
.SS Dependencies
External dependencies are listed for every module as space separated values in
External dependencies and called modules are listed for every module
as space separated values in
.B WMRC_DEPENDENCIES
global variable

.SS Libraries
Local libraries from
.I $HOME/.config/wmrc/libs
are loaded into the module according to the space separated values in
.B WMRC_LIBRARIES
global variable
.PP
Libraries may also list external dependencies and called modules,
but not other libraries
100 changes: 80 additions & 20 deletions wmrc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,117 @@ _module='wmrc'
. "$WMRC_DIR/libwmrc.sh"

module_exec() {
check_dependencies "$1"
if [ "$WMRC_CHECK_DEPS" != 'false' ]; then
check_dependencies "$1"
fi
eval "call $*"
}

module_list() {
debug 'List modules'
debug 'Test module directory' "$WMRC_CONFIG/modules"
if ! test -d "$WMRC_CONFIG/modules"; then
error "Modules directory not found: $WMRC_CONFIG/modules"
error 'Modules directory not found' "$WMRC_CONFIG/modules"
exit 1
fi
modules="$(find "$WMRC_CONFIG/modules" -type f -printf '%P\n')"
debug 'Found modules' "$(echo "$modules" | sed -z 's/\n/, /g;s/, $/\n/')"
if [ -n "$modules" ]; then
debug 'Found modules' "$(echo "$modules" | sed -z 's/\n/, /g;s/, $/\n/')"
fi
}

library_list() {
debug 'List libraries'
debug 'Test library directory' "$WMRC_CONFIG/libs"
if ! test -d "$WMRC_CONFIG/libs"; then
error 'Libraries directory not found' "$WMRC_CONFIG/libs"
exit 1
fi
libraries="$(find "$WMRC_CONFIG/libs" -type f -printf '%P\n')"
if [ -n "$libraries" ]; then
debug 'Found libraries' "$(echo "$libraries" | sed -z 's/\n/, /g;s/, $/\n/')"
fi
}

get_dependencies() {
if [ -z "$1" ]; then
debug 'Get all dependencies'
module_list
library_list
else
debug 'Get dependencies for' "$1"
debug 'Get dependencies' "$1"
_module_libraries "$1"
modules="$1"
fi
dependencies=''
for m in $modules; do
_missing=''
_libs=''
_mods=''
if [ -n "$libraries" ]; then
_libs="$(echo "$libraries" | sed 's/^/libs\//g')"
fi
if [ -n "$modules" ]; then
_mods="$(echo "$modules" | sed 's/^/modules\//g')"
fi
for f in $_mods $_libs; do
_deps="$(
sh -c ". '$WMRC_CONFIG/modules/$m' && echo \$WMRC_DEPENDENCIES" | sed 's/ \{1,\}/:/g'
sh -c ". '$WMRC_CONFIG/$f' && echo \$WMRC_DEPENDENCIES" | sed 's/ \{1,\}/:/g'
)"
dependencies="$dependencies${dependencies:+:}$_deps"
_missing="$_missing${_missing:+:}$_deps"
done
debug 'Found dependencies' "$(echo "$dependencies" | sed 's|:|, |g')"
dependencies="$(echo "$dependencies" | sed 's|:|\n|g' | sort | uniq)"
debug 'Found dependencies' "$(echo "$_missing" | sed 's|:|, |g')"
dependencies="$(echo "$_missing" | sed 's|:|\n|g' | sort | uniq)"
}

get_libraries() {
if [ -z "$1" ]; then
debug 'Get all libraries'
module_list
else
debug 'Get libraries' "$1"
modules="$1"
fi
_libs=''
for m in $modules; do
_module_libraries "$m"
_libs="$_libs${_libs:+:}$libraries"
done
libraries="$(echo "$_libs" | sed 's|:|\n|g' | sort | uniq)"
}

check_dependencies() {
debug 'Check dependencies'
get_dependencies "$1" || return 1
module_list || return 1
_missing=''
debug 'Check dependencies' "$1"
_missing_libs=''
get_libraries "$1"
for l in $(echo "$_libs" | sed 's|:|\n|g' | sort | uniq); do
if ! test -f "$WMRC_CONFIG/libs/$l"; then
_missing_libs="$_missing_libs${_missing_libs:+:}$l"
fi
done
get_dependencies "$1"
module_list
_missing_mods=''
_missing_deps=''
for d in $dependencies; do
if echo "$d" | grep -qE '\w+/\w+'; then
if echo "$modules" | grep -qvF "$d"; then
_missing="$_missing${_missing:+, }wmrc::$d"
if ! echo "$modules" | grep -qF "$d"; then
_missing_mods="$_missing_mods${_missing_mods:+, }$d"
fi
else
if ! command -v "$d" 1>/dev/null; then
_missing="$_missing${_missing:+, }$d"
_missing_deps="$_missing_deps${_missing_deps:+, }$d"
fi
fi
done
if [ -n "$_missing" ]; then
error 'Missing dependencies' "$_missing"
if [ -n "$_missing_libs" ]; then
error 'Missing libraries' "$_missing_libs"
fi
if [ -n "$_missing_mods" ]; then
error 'Missing modules' "$_missing_mods"
fi
if [ -n "$_missing_deps" ]; then
error 'Missing dependencies' "$_missing_deps"
fi
if [ -n "$_missing_mods" ] || [ -n "$_missing_libs" ] || [ -n "$_missing_deps" ]; then
exit 1
fi
}
Expand All @@ -81,7 +140,7 @@ read_config_variables() {
}

config_unit_list() {
debug 'List all units'
debug 'List units'
debug 'Test configuration file' "$WMRC_CONFIG/rc.conf"
if ! test -f "$WMRC_CONFIG/rc.conf"; then
error 'Configuration file not found' "$WMRC_CONFIG/rc.conf"
Expand Down Expand Up @@ -255,6 +314,7 @@ case "$1" in
echo "$dependencies" | grep -vE '\w+/\w+'
;;
'check-deps')
export WMRC_CHECK_DEPS=true
check_dependencies
;;
*)
Expand Down

0 comments on commit 5e878cb

Please sign in to comment.