install: simplifying refactor

This commit is contained in:
Fernando Schauenburg 2021-08-16 10:40:07 +02:00
parent 257f62d3df
commit d20e3d2403

View file

@ -37,10 +37,6 @@ ok() { printf '%s ' "${green}OK:${rst}" "$@"; printf '\n'; }
warn() { printf '%s ' "${yellow}$@${rst}"; printf '\n'; } warn() { printf '%s ' "${yellow}$@${rst}"; printf '\n'; }
error() { printf '%s ' "${red}$@${rst}"; printf '\n'; } error() { printf '%s ' "${red}$@${rst}"; printf '\n'; }
is_installed() {
command -v nvim >/dev/null 2>&1
}
greeting() { greeting() {
dry_run && info "Dry run: no changes will be made to filesytem (use -f to override)." dry_run && info "Dry run: no changes will be made to filesytem (use -f to override)."
info "Deploying with git user $yellow${GIT_USER:-$DEFAULT_GIT_USER} <${GIT_EMAIL:-$DEFAULT_GIT_EMAIL}>$rst" info "Deploying with git user $yellow${GIT_USER:-$DEFAULT_GIT_USER} <${GIT_EMAIL:-$DEFAULT_GIT_EMAIL}>$rst"
@ -52,32 +48,23 @@ greeting() {
# Make sure directory $1 exists. # Make sure directory $1 exists.
ensure_directory() { ensure_directory() {
if [ -d "$1" ]; then [ -d "$1" ] && { ok "$1/"; return; }
ok "$1/" warn "creating $1/"
else dry_run || mkdir -p "$1/"
warn "creating $1/"
dry_run || mkdir -p "$1/"
fi
} }
# Make sure file $1 does not exist. # Make sure file $1 does not exist.
remove_file() { remove_file() {
if [ ! -f "$1" ]; then [ ! -f "$1" ] && { ok "$1"; return; }
ok "$1" warn "removing $1"
else dry_run || rm "$1"
warn "removing $1"
dry_run || rm "$1"
fi
} }
# Make sure file $1 exists (content is irrelevant). # Make sure file $1 exists (content is irrelevant).
touch_file() { touch_file() {
if [ -f "$1" ]; then [ -f "$1" ] && { ok "$1"; return; }
ok "$1" warn "creating $1"
else dry_run || touch "$1"
warn "creating $1"
dry_run || touch "$1"
fi
} }
# Remove $1 if it's a broken link to a dotfile script. # Remove $1 if it's a broken link to a dotfile script.
@ -98,23 +85,17 @@ prune_cmd() {
# Make sure $2 is a link to $1. # Make sure $2 is a link to $1.
link() { link() {
target="$(realpath -s "$1")" target="$(realpath -s "$1")"
if [ "$(readlink "$2")" = "$target" ]; then [ "$(readlink "$2")" = "$target" ] && { ok "$2"; return; }
ok "$2" warn "linking $2 -> $target"
else dry_run || ln -sf "$target" "$2"
warn "linking $2 -> $target"
dry_run || ln -sf "$target" "$2"
fi
} }
# Ensure $1 and $2 contents are equal, updating $1 if needed. # Ensure $1 and $2 contents are equal, updating $1 if needed.
equal_content() { equal_content() {
if diff "$1" "$2" >/dev/null 2>&1; then diff "$1" "$2" >/dev/null 2>&1 && { ok "$1"; return; }
ok "$1" warn "overwriting $1 with $2:"
else cat "$2"
warn "overwriting $1 with $2:" dry_run || cp -f "$2" "$1"
cat "$2"
dry_run || cp -f "$2" "$1"
fi
} }
############################################################################### ###############################################################################
@ -197,7 +178,7 @@ deploy_nvim() {
link "$DOTFILES/nvim/plug.vim" "$XDG_CONFIG_HOME/nvim/autoload/plug.vim" link "$DOTFILES/nvim/plug.vim" "$XDG_CONFIG_HOME/nvim/autoload/plug.vim"
link "$DOTFILES/nvim/init.vim" "$XDG_CONFIG_HOME/nvim/init.vim" link "$DOTFILES/nvim/init.vim" "$XDG_CONFIG_HOME/nvim/init.vim"
if is_installed nvim; then if command -v nvim >/dev/null 2>&1; then
warn "installing neovim plugins..." warn "installing neovim plugins..."
dry_run || nvim -nes -u "$XDG_CONFIG_HOME/nvim/init.vim" -c 'PlugInstall | qall!' dry_run || nvim -nes -u "$XDG_CONFIG_HOME/nvim/init.vim" -c 'PlugInstall | qall!'
else else