zshlibs/global.funcs.zsh

199 lines
4.7 KiB
Bash
Raw Normal View History

2021-12-20 14:08:22 +00:00
typeset -g globaldots_path="${HOME}/.global"
if [[ -a $(realpath -m $(dirname $0)/../../.globaldir ) ]]
then
typeset -g globaldots_path=$(realpath -m $(dirname $0)/../../)
fi
if [[ -n ${GLOBALDOTS_PATH} ]]
then
typeset -g globaldots_path=${GLOBALDOTS_PATH}
fi
2021-11-10 14:41:19 +00:00
if [[ -z $globaldots_path ]]; then
echo 'functions require $globaldots_path to be set!'
fi
2021-12-20 14:08:22 +00:00
#### BASIC ZSHLIBS TOOLS
2021-12-20 14:21:55 +00:00
function zshlibs_update()
2021-12-20 14:08:22 +00:00
{
zshlibs_log zlibs_update "preparing self update …"
2021-12-20 14:08:22 +00:00
if [[ -n "$zshlibs_install" ]]
then
zshlibs_log zlibs_update "cd into '$zshlibs_install' …"
2021-12-20 14:29:01 +00:00
pushd $zshlibs_install 2>&1 > /dev/null
zshlibs_log zlibs_update "git pull …"
2021-12-20 14:24:45 +00:00
(
git pull 2>&1 |zshlibs_log zlibs_update & # > "${HOME}/.log/z_zshlibs_update" &
2021-12-20 14:29:01 +00:00
)
zshlibs_log zlibs_update "update done"
2021-12-20 14:29:01 +00:00
popd 2>&1 > /dev/null
2021-12-20 14:08:22 +00:00
else
echo "\$zshlibs_install not set!"
return 1
zlibs_install
fi
}
function zshlibs_ensure()
2021-12-20 14:08:22 +00:00
{
target=${2}
typ=${1}
if ! test -e ${target}
then
zshlibs_log zshlibs_ensure "creating '${target}' ($typ) …"
2021-12-20 14:08:22 +00:00
case $typ in
-d)
mkdir -p ${target} \
2022-01-11 15:20:34 +00:00
|| zshlibs_log $0 "unable to create directory '${target}'" \
2021-12-20 14:08:22 +00:00
&& return 2
;;
-f)
touch ${target} \
2022-01-11 15:20:34 +00:00
|| zshlibs_log $0 "unable to create file '${target}'" \
2021-12-20 14:08:22 +00:00
&& return 2
esac
zshlibs_log zshlibs_ensure "target '${target}' created"
2021-12-20 14:08:22 +00:00
elif test ${typ} ${target}
then
zshlibs_log zshlibs_ensure "target '${target}' ($typ) present"
2021-12-20 14:08:22 +00:00
return 0
else
zshlibs_log zshlibs_ensure "target '${target}' already exists and it is not a directory"
2021-12-20 14:08:22 +00:00
return 1
fi
}
function zshlibs_log()
{
local facility=$1
shift
if [[ -n "${@}" ]]
then
echo "$(date -Ins) ${@}" >> "${HOME}/.log/z_$facility.log"
else
echo "$(date -Ins) $(cat)" >> "${HOME}/.log/z_$facility.log"
fi
}
2022-01-11 15:01:21 +00:00
function zshlibs_path_add()
2021-12-20 14:08:22 +00:00
{
2022-01-11 15:01:21 +00:00
local pathtoadd="${1}"
zshlibs_log $0 "adding path '$pathtoadd' to \$PATH"
if [[ ! ${path[(r)${pathtoadd}]} ]]
then
path+=(${pathtoadd})
fi
}
2021-12-20 14:08:22 +00:00
2022-01-11 15:01:21 +00:00
function zshlibs_fpath_add()
{
local fpathtoadd="${1}"
zshlibs_log $0 "adding fpath '$fpathtoadd' to \$fpath"
if [[ ! ${fpath[(r)${fpathtoadd}]} ]]
then
path+=(${fpathtoadd})
fi
2021-12-20 14:08:22 +00:00
}
2022-01-11 15:01:21 +00:00
2022-01-11 15:20:34 +00:00
##### GLOBAL BUCKET STUFF
2022-01-11 15:01:21 +00:00
2021-12-20 14:08:22 +00:00
2021-11-10 14:41:19 +00:00
function global_path_expand()
{
local bin_base=${globaldots_path}/bin
for pathnew in ${*}; do
2022-01-11 15:20:34 +00:00
zshlibs_path_add "${bin_base}/${pathnew}"
2021-11-10 14:41:19 +00:00
done;
}
function global_fpath_expand()
{
local bin_base=${globaldots_path}/bin
for pathnew in ${*}; do
2022-01-11 15:20:34 +00:00
zshlibs_fpath_add "${bin_base}/${pathnew}"
2021-11-10 14:41:19 +00:00
done;
}
2022-01-11 15:20:34 +00:00
##### CONFIGURATION
2021-12-04 21:34:16 +00:00
2021-11-10 14:41:19 +00:00
function zconfig_checkload()
{
2021-12-20 14:08:22 +00:00
typeset -a -g zconfig_loaded
if [[ ! ${zconfig_loaded[(r)${1}]} ]]
2021-11-10 14:41:19 +00:00
then
zshlibs_log zbootstrap "sourcing config '$1'"
. -- $1 2>&1 >(zshlibs_log zbootstrap) # >> "${HOME}/.log/z_zbootstrap.log"
2021-12-20 14:08:22 +00:00
zconfig_loaded+=("$1")
zshlibs_log zbootstrap "end sourcing config '$1'"
2021-11-10 14:41:19 +00:00
else
zshlibs_log zbootstrap "config '$1' already loaded"
2021-11-10 14:41:19 +00:00
fi
}
function zconfigs_load()
{
for configfile in ${*}; do
zconfig_checkload $configfile
done
}
function zconfigs_local_load()
{
2022-01-11 15:20:34 +00:00
if [[ ! -a ${zconfig_local_path} ]]; then
zshlibs_log $0 "local config missing, recreating from '${zconfig_shared_path}/zshconfig.example'"
mkdir -p -m 700 ${zconfig_local_path}
cp ${zconfig_shared_path}/zshconfig.example ${zconfig_local_path}/local.config.zsh
fi
if [[ ! -d ${zconfig_local_path} ]]; then
zshlibs_log zconfigs ""
zconfig_checkload ${zconfig_local_path}
else
zconfigs_load ${zconfig_local_path}/*.config.zsh;
fi
2021-11-10 14:41:19 +00:00
}
2021-12-20 14:08:22 +00:00
function zshlibs_boot()
2021-11-10 14:41:19 +00:00
{
# ensure log directory
zshlibs_log zbootstrap "ensure directory '${HOME}/.log/'"
zshlibs_ensure -d "${HOME}/.log/"
2021-11-10 14:41:19 +00:00
# loading configs
2021-12-20 14:08:22 +00:00
typeset -a -g zconfig_loaded
2021-11-10 14:41:19 +00:00
typeset -a -g path_extra
typeset -a -g config_extra
2021-12-20 14:08:22 +00:00
typeset -g zconfig_shared_path="${zshlibs_install}/config"
typeset -g zconfig_local_path="${HOME}/.config/zsh"
}
function zbootstrap_noninteractive()
{
zshlibs_log zbootstrap 'load global config'
2021-12-20 14:39:03 +00:00
zconfig_checkload ${zconfig_shared_path}/global.config.zsh
zshlibs_log zbootstrap 'load local configs'
2021-11-10 14:41:19 +00:00
zconfigs_local_load
for zconfig in $config_extra; do
zshlibs_log zbootstrap 'load config extra $zconfig'
2021-12-20 14:39:03 +00:00
zconfig_checkload ${zconfig_shared_path}/${zconfig}.config.zsh
2021-11-10 14:41:19 +00:00
done
# expanding settings
zshlibs_log zbootstrap "expanding path with '$path_extra'"
2021-11-10 14:41:19 +00:00
global_path_expand $path_extra
zshlibs_log zbootstrap "expanding fpath with '$fpath_extra'"
2021-11-10 14:41:19 +00:00
global_fpath_expand vanilla
zshlibs_log zbootstrap "done loading noninteractive config"
2021-11-10 14:41:19 +00:00
}
function zbootstrap ()
{
2021-12-20 14:08:22 +00:00
zshlibs_boot
2021-11-10 14:41:19 +00:00
zbootstrap_noninteractive
2021-12-20 14:54:09 +00:00
zconfig_checkload ${zconfig_shared_path}/global.style.zsh
2021-12-20 14:20:33 +00:00
zshlibs_update
zshlibs_log zbootstrap "done loading interactive config"
2021-11-10 14:41:19 +00:00
}