refactor install script

This commit is contained in:
thek4n 2024-09-02 16:25:04 +03:00
parent 31cf397c4e
commit 2c66dbab44

64
install
View File

@ -14,13 +14,13 @@ declare DOTFILES_ROOT
DOTFILES_ROOT="$(_detect_current_script_real_directory)"
readonly DOTFILES_ROOT
declare -xr SUB="$DOTFILES_ROOT/home/user"
declare -xr SUB="${DOTFILES_ROOT}/home/user"
source "$DOTFILES_ROOT/TARGETS.sh"
source "${DOTFILES_ROOT}/TARGETS.sh"
_die() {
echo "$0: $1" >&2
echo "${0}: ${1}" >&2
exit $2
}
@ -28,14 +28,14 @@ _link_files_in_sandbox() {
local targetfile
for targetfile in "$@"
do
echo "installing: $targetfile"
if [ "${targetfile::1}" = "%" ]; then
echo "installing: ${targetfile}"
if [[ "${targetfile::1}" = "%" ]]; then
_link_files_in_sandbox ${TARGETS["${targetfile:1}"]}
else
if [ ! "$(dirname "$targetfile")" = "." ]; then
mkdir -p "$SANDBOX_PATH/$(dirname "$targetfile")"
if [[ ! "$(dirname "$targetfile")" = "." ]]; then
mkdir -p "${SANDBOX_PATH}/$(dirname "$targetfile")"
fi
ln -sT "$SUB/$targetfile" "$SANDBOX_PATH/$targetfile"
ln -sT "${SUB}/${targetfile}" "${SANDBOX_PATH}/${targetfile}"
fi
done
}
@ -54,7 +54,7 @@ __install_from_sandbox() {
local comparisons
comparisons="$(_compare_sandbox_to_home)"
if [ -n "$comparisons" ]; then
if [[ -n "$comparisons" ]]; then
echo "$comparisons" >&2
_die "Found conflicting files. Exiting" 1
fi
@ -66,9 +66,12 @@ __install_from_sandbox() {
_execute_hook_if_executable() {
# all hooks gets SUB and SANDBOX_PATH env variables
local hook_path="$DOTFILES_ROOT/install-hooks/$1/$2"
if [ -x "$hook_path" ]; then
echo "Executing $2 for target '$1'"
local -r target="$1"
local -r hook_name="$2"
local hook_path="${DOTFILES_ROOT}/install-hooks/${target}/${hook_name}"
if [[ -x "$hook_path" ]]; then
echo "Executing ${hook_name} for target '${target}'"
"$hook_path"
fi
}
@ -81,7 +84,7 @@ recursive_execute_pre_hooks() {
local targetfile
for targetfile in ${TARGETS["$1"]}
do
if [ "${targetfile::1}" = "%" ]; then
if [[ "${targetfile::1}" = "%" ]]; then
recursive_execute_pre_hooks "${targetfile:1}"
execute_pre_hook "${targetfile:1}"
fi
@ -96,7 +99,7 @@ recursive_execute_post_hooks() {
local targetfile
for targetfile in ${TARGETS["$1"]}
do
if [ "${targetfile::1}" = "%" ]; then
if [[ "${targetfile::1}" = "%" ]]; then
recursive_execute_post_hooks "${targetfile:1}"
execute_post_hook "${targetfile:1}"
fi
@ -104,12 +107,14 @@ recursive_execute_post_hooks() {
}
install_target() {
execute_pre_hook "$1"
recursive_execute_pre_hooks "$1"
_link_files_in_sandbox ${TARGETS["$1"]}
local -r target="$1"
execute_pre_hook "$target"
recursive_execute_pre_hooks "$target"
_link_files_in_sandbox ${TARGETS["$target"]}
__install_from_sandbox
recursive_execute_post_hooks "$1"
execute_post_hook "$1"
recursive_execute_post_hooks "$target"
execute_post_hook "$target"
}
is_target_installed() {
@ -118,17 +123,17 @@ is_target_installed() {
local targetfile
for targetfile in ${TARGETS["$1"]}
do
if [ "${targetfile::1}" = "%" ]; then
if [[ "${targetfile::1}" = "%" ]]; then
is_target_installed "${targetfile:1}" || not_fully_installed=true
else
if [ ! -e "$TARGET_PATH/$targetfile" ]; then
echo "$targetfile not linked"
if [[ ! -e "$TARGET_PATH/$targetfile" ]]; then
echo "${targetfile} not linked"
not_fully_installed=true
fi
fi
done
if $not_fully_installed; then
echo "Target '$1' not fully installed"
echo "Target '${1}' not fully installed"
echo
return 1
fi
@ -139,7 +144,7 @@ find_targets_that_depend_on() {
local target
for target in "${!TARGETS[@]}"
do
if [[ " ${TARGETS["$target"]} " =~ " %$1 " ]]; then
if [[ " ${TARGETS["$target"]} " =~ " %${1} " ]]; then
echo "$target"
fi
done
@ -149,7 +154,7 @@ die_if_installed_targets_depend_on() {
for reverse_dependecy in $(find_targets_that_depend_on "$1")
do
if is_target_installed "$reverse_dependecy" >/dev/null; then
_die "target '$reverse_dependecy' is depends on installed target '$1'. Exiting..." 1
_die "target '${reverse_dependecy}' is depends on installed target '${1}'. Exiting..." 1
fi
done
}
@ -162,12 +167,12 @@ cmd_unlink() {
for targetfile in ${TARGETS["$target"]}
do
if [ "${targetfile::1}" = "%" ]; then
if [[ "${targetfile::1}" = "%" ]]; then
continue
fi
if [ -e "$TARGET_PATH/$targetfile" ]; then
unlink "$TARGET_PATH/$targetfile"
if [[ -e "${TARGET_PATH}/${targetfile}" ]]; then
unlink "${TARGET_PATH}/${targetfile}"
fi
done
done
@ -182,7 +187,8 @@ cmd_list() {
}
target_exists() {
[[ " ${!TARGETS[*]} " =~ " $1 " ]]
local -r target="$1"
[[ " ${!TARGETS[*]} " =~ " ${target} " ]]
}
cmd_install() {