fix(install): check reverse depends when unlink. feat(install): add target as dependensy for targets

This commit is contained in:
TheK4n 2023-10-17 22:30:45 +03:00
parent 16c317d2f5
commit fc9c3058d5

80
install
View File

@ -1,6 +1,7 @@
#!/bin/bash #!/bin/bash
set -ueo pipefail set -ueo pipefail
shopt -s nullglob
readonly TARGET_PATH="$HOME" readonly TARGET_PATH="$HOME"
@ -9,7 +10,7 @@ readonly SUB="$(pwd)/home/user"
declare -A TARGETS=( declare -A TARGETS=(
["bash"]=".subbash .bashrc .profile" ["bash"]=".subbash .bashrc .profile"
["zsh"]=".subbash .subzsh .zshrc .zprofile .zfunc .zlogout .inputrc" ["zsh"]="%bash .subzsh .zshrc .zprofile .zfunc .zlogout .inputrc"
["tmux"]=".tmux.conf" ["tmux"]=".tmux.conf"
["alacritty"]=".config/alacritty" ["alacritty"]=".config/alacritty"
["nvim"]=".config/nvim .local/bin/vim_askpass_helper .local/bin/vim_askpass_helper_python" ["nvim"]=".config/nvim .local/bin/vim_askpass_helper .local/bin/vim_askpass_helper_python"
@ -41,12 +42,18 @@ _die_if_installed() {
} }
_link_files_in_sandbox() { _link_files_in_sandbox() {
for target in "$@"; do local targetfile
echo "installing: $target" for targetfile in "$@"
if [ ! "$(dirname "$target")" = "." ]; then do
mkdir -p "$SANDBOX_PATH/$(dirname "$target")" 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")"
fi
ln -sT "$SUB/$targetfile" "$SANDBOX_PATH/$targetfile"
fi fi
ln -sT "$SUB/$target" "$SANDBOX_PATH/$target"
done done
} }
@ -74,6 +81,7 @@ __install_from_sandbox() {
echo "Merging to home..." echo "Merging to home..."
_merge_sandbox_to_home _merge_sandbox_to_home
echo "Successfully installed"
} }
cmd_bash() { cmd_bash() {
@ -192,11 +200,57 @@ cmd_docker() {
__install_from_sandbox __install_from_sandbox
} }
cmd_check() {
local not_fully_installed=0
local targetfile
for targetfile in ${TARGETS["$1"]}
do
if [ "${targetfile::1}" = "%" ]; then
cmd_check "${targetfile:1}" || not_fully_installed=1
else
if [ ! -e "$TARGET_PATH/$targetfile" ]; then
echo "$targetfile not linked"
not_fully_installed=1
fi
fi
done
if (( not_fully_installed != 0 )); then
echo "Target '$1' not fully installed"
echo
return 1
fi
return 0
}
find_targets_that_depend_on() {
local target
for target in "${!TARGETS[@]}"
do
if [[ " ${TARGETS["$target"]} " =~ " %$1 " ]]; then
echo "$target"
fi
done
}
cmd_unlink() { cmd_unlink() {
local target targetfile local target targetfile
for target in "$@"; do for target in "$@"
do
for reverse_dependecy in $(find_targets_that_depend_on "$target")
do
if cmd_check "$reverse_dependecy" >/dev/null; then
echo "'$reverse_dependecy' is depends on installed '$target'. Exiting..."
exit 1
fi
done
for targetfile in ${TARGETS["$target"]} for targetfile in ${TARGETS["$target"]}
do do
if [ "${targetfile::1}" = "%" ]; then
continue
fi
if [ -e "$TARGET_PATH/$targetfile" ]; then if [ -e "$TARGET_PATH/$targetfile" ]; then
unlink "$TARGET_PATH/$targetfile" unlink "$TARGET_PATH/$targetfile"
fi fi
@ -208,15 +262,6 @@ cmd_no_target() {
_die "TARGET not exists" 1 _die "TARGET not exists" 1
} }
cmd_check() {
local targetfile
for targetfile in ${TARGETS["$1"]}
do
if [ ! -e "$TARGET_PATH/$targetfile" ]; then
echo "$targetfile not linked"
fi
done
}
cmd_list() { cmd_list() {
echo "${!TARGETS[@]}" echo "${!TARGETS[@]}"
@ -224,7 +269,8 @@ cmd_list() {
cmd_install() { cmd_install() {
local target local target
for target in "$@"; do for target in "$@"
do
SANDBOX_PATH="$(mktemp -td "${USER:=user}.dotfiles_XXXXXXX")" SANDBOX_PATH="$(mktemp -td "${USER:=user}.dotfiles_XXXXXXX")"
case "$target" in case "$target" in
bash) shift; cmd_bash "$@" ;; bash) shift; cmd_bash "$@" ;;