#!/bin/bash
# lib - Loadable library support

set -e


######
# library interface

core_lib_config_init() {
	lib_setting_arrays lib_loaded lib_inited
}


######
# Public Functions

# lib_load_check - Check library loads, logging messages
lib_load_check() {
	local name="$lib_name"
	local -a script=( "${lib_name[@]}" )
	list_reverse script

	# check for built-in library
	if in_list "$name" "${package_libs[@]}"; then
		debug "$name: loading library"
	else
		info "$name: loading user library"
	fi

	local file
	file=$(lib_filename "$name")
	[ -f "$file" ] || error "$file: library not found"
}

# lib_load_dispatch() - Loads library ($1) and runs command in namespace ($2)
lib_load_dispatch_ns() {
	local name=$1
	local namespace=$2
	shift 2

	lib_load "$name"
	lib_cmd_dispatch "$namespace" "$@"
}

# lib_load_dispatch() - Loads library ($1) and runs command in default namespace
lib_load_dispatch() {
	lib_load_dispatch_ns "$1" "${1#*/}" "$@"
}


######
# Library settings

lib_setting_vars() { settings_add_vars "$(lib_symbol $lib_name)" "$@"; }
lib_setting_arrays() { settings_add_arrays "$(lib_symbol $lib_name)" "$@"; }
lib_setting_assocs() { settings_add_assocs "$(lib_symbol $lib_name)" "$@"; }
lib_setting_funcs() { settings_add_funcs "$(lib_symbol $lib_name)" "$@"; }


######
# Library command dispatching

lib_cmd_name() {
	local -a a=( "${lib_name[@]}" "$@" )
	list_map lib_symbol a
	echo "$(IFS="_"; echo "${a[*]}")"
}

lib_cmd_exists() {
	min_args 2 "$@"
	local name=$1
	shift

	local -a lib_name=( "$name" )
	is_function $(lib_cmd_name "$@")
}

lib_cmd_exec() {
	min_args 2 "$@"
	local name=$1
	local cmd=$2
	shift 2

	if lib_cmd_exists "$name" "$cmd"; then
		local -a lib_name=( "$name" )
		$(lib_cmd_name $cmd) "$@"
	fi
}

lib_cmd_dispatch() {
	min_args 1 "$@"
	local name=$1
	shift
	local -a script=( $name )
	cmd_dispatch "$@"
}

for_each_lib() { for_each "$*" "${lib_inited[@]}"; }
for_each_lib_loaded() { for_each "$*" "${lib_loaded[@]}"; }


######
# Library testing

lib_check() {
	local name=$1
	if is_function "$(lib_symbol $name)_lib_check"; then
		app_echo "$name: checking library..."
		lib_cmd_exec "$1" lib_check
	fi
}

lib_check_all() {
	app_echo "checking libraries..."
	for_each_lib lib_check
	app_echo "... done checking libraries"
}

View the Script Reference Index


Generated on Tue Apr 25 21:20:08 PDT 2017 by mcsh i7 v0.18.0.