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