From 4742b8d3d31bc717818d060a5f730bddd3c4dc99 Mon Sep 17 00:00:00 2001 From: Dict Xiong Date: Sat, 5 Nov 2022 19:12:55 +0800 Subject: [PATCH] preinstall_check --- install.sh | 42 ++++++++++++++++++++++++++++++++++++++---- tools/common.sh | 14 +++++++++++++- tools/msys2.sh | 2 +- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/install.sh b/install.sh index a315f49..57ccfbd 100755 --- a/install.sh +++ b/install.sh @@ -3,6 +3,7 @@ THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd ) source "$THIS_DIR/tools/common.sh" + if [[ ! "$DOTFILES" == "${HOME}"* ]]; then fmt_fatal "\"$DOTFILES\" is not under \"$HOME\". aborting ..." fi @@ -26,6 +27,30 @@ HOME_SYMLINKS_SRC[0]=".ssh/authorized_keys2" HOME_SYMLINKS_DST[0]=".ssh/authorized_keys2" +preinstall_check() +{ + mandatory_commands=( "git" "zsh" "curl" "ping" ) + optional_commands=( "python3" "vim" "tmux" ) + for i in "${mandatory_commands[@]}"; do + if [[ ! -x "$(command -v $i)" ]]; then + fmt_info "all this utils are required: ${mandatory_commands[@]}" + fmt_info "install them manually or check scripts in tools/" + fmt_fatal "\"$i\" not found. aborting ..." + fi + done + for i in "${optional_commands[@]}"; do + if [[ ! -x "$(command -v $i)" ]]; then + fmt_warning "\"$i\" not found" + ask_for_Yn "continue anyway?" + if [[ "$?" == "0" ]]; then + fmt_info "all this utils are suggested: ${optional_commands[@]}" + fmt_info "install them manually or check scripts in tools/" + fmt_fatal "aborting ..." + fi + fi + done +} + install_file_content() { for ((i=0; i<${#HOME_FILES_PATH[@]}; i++)); do @@ -101,13 +126,21 @@ uninstall_symlink() } install_crontab(){ - fmt_note "installing \"$CRON_JOB\" to crontab ..." - ( crontab -l | grep -vxF "${CRON_JOB}" | grep -v "no crontab for"; echo "$CRON_JOB" ) | crontab - + if [[ -x $(command -v crontab) ]]; then + fmt_note "installing \"$CRON_JOB\" to crontab ..." + ( crontab -l | grep -vxF "${CRON_JOB}" | grep -v "no crontab for"; echo "$CRON_JOB" ) | crontab - + else + fmt_warning "crontab does not exist. skipping ..." + fi } uninstall_crontab(){ - fmt_note "removing \"$CRON_JOB\" from crontab ..." - ( crontab -l | grep -vxF "$CRON_JOB" ) | crontab - + if [[ -x $(command -v crontab) ]]; then + fmt_note "removing \"$CRON_JOB\" from crontab ..." + ( crontab -l | grep -vxF "$CRON_JOB" ) | crontab - + else + fmt_note "crontab does not exist. skipping ..." + fi } install_tmux_tpm(){ @@ -152,6 +185,7 @@ uninstall_update(){ } install(){ + preinstall_check install_update install_crontab install_file_content diff --git a/tools/common.sh b/tools/common.sh index 53dfa89..4dd3c85 100755 --- a/tools/common.sh +++ b/tools/common.sh @@ -38,7 +38,7 @@ supports_truecolor() { fmt_fatal() { printf '%sfatal: %s%s\n' "${FMT_BOLD}${FMT_RED}" "$*" "${FMT_RESET}" >&2 - exit + exit 1 } fmt_error() { @@ -113,6 +113,18 @@ ask_for_yN() return 0 } +ask_for_Yn() +{ + while [[ -z "$DFS_QUIET" ]]; do + read -p "${FMT_YELLOW}$1${FMT_RESET} [Yn]: " yn + case $yn in + [Nn]* ) return 0;; + * ) return 1;; + esac + done + return 1 +} + post_log() { python3 "${DOTFILES}/tools/log.py" "[$1] $2: $3" diff --git a/tools/msys2.sh b/tools/msys2.sh index a857c89..a0efa8c 100755 --- a/tools/msys2.sh +++ b/tools/msys2.sh @@ -13,7 +13,7 @@ set_mirror() pacman_S() { pacman -Syu - pacman -S tmux git zsh curl vim wget base-devel mingw-w64-x86_64-toolchain make cmake gcc zip unzip + pacman -S tmux git zsh curl vim wget base-devel mingw-w64-x86_64-toolchain make cmake gcc zip unzip python3 python3-pip } router()