From 668f1d56238d00c3577966ef801eeb6c11dec7b6 Mon Sep 17 00:00:00 2001 From: Pocco81 Date: Mon, 2 Aug 2021 21:23:20 -0500 Subject: [PATCH 1/7] skeleton for new install script --- install.sh | 249 +++++++++++++++++++---------------------------------- 1 file changed, 88 insertions(+), 161 deletions(-) diff --git a/install.sh b/install.sh index 31d0264..3ad1c00 100755 --- a/install.sh +++ b/install.sh @@ -1,171 +1,98 @@ #!/bin/sh -_usage() { - printf "%s" \ - "Usage: sh ${0##*/} [ options ] - -h | --help => Show this help. +RED='\033[0;31m' +GREEN='\033[0;32m' +ORANGE='\033[0;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color - -i | --install => Install the config. - - -u | --update => Update the existing config without removing existing stuff. - - -b | --backup 'path' => Custom config backup path. - - -nb | --no-backup => Don't take existing config backup. Use with caution. -" - exit 0 -} - -_check_install_dependencies() { - _error_dependencies() { - printf "%s\n" "Error: Install ${1} before proceeding." - exit 1 - } - command -v git 1>/dev/null || _error_dependencies git - _SED="$(command -v sed)" || _error_dependencies sed - return 0 -} - -_check_nvim_version() { - version_string="$(nvim --version | grep -Eo 'NVIM.*v[0-9.]+' -m 1 | grep -Eo '[0-9.]+')" || return 1 - printf "%s\n" "${version_string}" | while IFS=. read -r num1 num2 num3; do - { [ "${num1}" -ge 0 ] && [ "${num2}" -ge 5 ] && [ "${num3}" -ge 0 ]; } || return 1 - done -} - -_copy_config() { - printf "%s\n" "Linking config" - printf "%s\n" "Old nvim config will be changed to nvim.bak if exists! :0" - - # copying config - if [ -d "${_CONFIG_PATH}" ]; then - printf "%s\n" "Nvim Directory exists" - if [ "${_NO_BACKUP}" = "true" ]; then - printf "%s\n" "Skipping backup as --no-backup flag was passed.." - [ "${_UPDATE}" = "false" ] && rm -rf "${_CONFIG_PATH}" - else - printf "%s\n" "Taking backup of existing config.." - mv "${_CONFIG_PATH}" "${_BACKUP_PATH}" || { - printf "\n%s\n" "Error: Cannot move ${_CONFIG_PATH} to ${_BACKUP_PATH}" - printf "\n%s\n" "Take backup manually or use -b/--backup to provide non-empty path or use -nb/--no-backup to skip backup." - exit 1 - } - fi - else - printf "%s\n" "Nvim config doesn't exist, creating it now" - fi - - mkdir -p "${_CONFIG_PATH}" || { - printf "%s\n" "Error: Cannot create folder ${_CONFIG_PATH}" - exit 1 - } - - { cp -r init.lua lua "${_CONFIG_PATH}"/; } || { - printf "Error: Couldn't copy nvim config\n" - exit 1 - } - return 0 -} - -_setup_terminal_shell() { - _mappings_file="${_CONFIG_PATH}/lua/mappings.lua" - # only ask for shellname if running in terminal - if [ -t 1 ]; then - if chsh -l 2>/dev/null 1>&2; then - printf "\nAvailable Shells:\n" - chsh -l | nl - printf "\n%s\n" "Which shell do you want to use? (Eg. 2)" - printf "\t%s\n" "[ Enter nothing for current shell ( $_CURRENT_SHELL ) ]" - read -r shellnum - [ "${shellnum}" -gt 0 ] 2>/dev/null && _SHELLPATH="$(chsh -l | sed -n "$shellnum p")" - fi - fi - - # don't try to do any changes user wants their default shell in nvim - if [ -n "$_SHELLPATH" ]; then - # Reference: https://stackoverflow.com/a/4247319 - # \( & \) will use regex brackets (for later reference with \1) - # ( & ) will match text brackets - if "${_SED}" --posix -i'.bak' -e "s=^\(map(.* \+*terminal\) \(.*)\)=\1$_SHELLPATH \2=g" "${_mappings_file}"; then - printf "%s\n" "=> Neovim shell changed to $_SHELLPATH successfully!" - else - printf "%s\n" "Cannot edit with sed, edit ${_mappings_file} manually to replace bash with $_SHELLPATH." - fi - rm -f "${_mappings_file}".bak # delete backup file created by sed - fi - printf "%s\n" "=> Neovim shell will be ${_SHELLPATH:-${_CURRENT_SHELL}}" - return 0 -} -_setup_arguments() { - # default variables to be used - _CONFIG_PATH="${XDG_CONFIG_HOME:-${HOME}/.config}/nvim" - _UPDATE="" - _BACKUP_PATH="${_CONFIG_PATH}.bak" - _NO_BACKUP="false" - _CURRENT_SHELL="${SHELL##*/}" - - _check_longoptions() { - [ -z "${2}" ] && - printf '%s: %s: option requires an argument\nTry '"%s -h/--help"' for more information.\n' "${0##*/}" "${1}" "${0##*/}" && - exit 1 - return 0 - } - - while [ $# -gt 0 ]; do - case "${1}" in - -h | --help) _usage ;; - -i | --install) _UPDATE="false" ;; - -u | --update) _UPDATE="true" ;; - -b | --backup) - _check_longoptions "${1}" "${2}" - _BACKUP_PATH="${2}" && shift - ;; - -nb | --nobackup) _NO_BACKUP="true" ;; +# https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux +prompt() { + case ${1} in + "-s" | "--success") + printf "${GREEN}%s${NC}\n" "${2}" + ;; + "-e" | "--error") + printf "${RED}%s${NC}\n" "${2}" + ;; + "-w" | "--warning") + printf "${ORANGE}%s${NC}\n" "${2}" + ;; + "-i" | "--info") + printf "${BLUE}%s${NC}\n" "${2}" + ;; + *) + printf "${GREEN}%s${NC}\n" "${2}" + ;; esac - shift - done - - [ -z "${_UPDATE}" ] && { - printf "%s\n" "Error: Either -i/--install or -u/--update flag must be used." - printf "%s\n" "See -h/--help for more info." - exit 1 - } - - printf "%s\n" \ - "CONFIG PATH: ${_CONFIG_PATH} -$(if [ "${_NO_BACKUP}" = "true" ]; then - printf "SKIPPING CONFIG BACKUP !\n" - else - printf "%s\n" "CONFIG BACKUP PATH: ${_BACKUP_PATH}" - fi) -CURRENT SHELL: ${_CURRENT_SHELL} -" - - return 0 } +_usage() { + printf "%s" \ + "Usage: sh ${0##*/} [ options ] + -h | --help => Show this help. + -i | --install => Install the config. + -r | --remove => Remove the config. + -u | --update => Update the existing config without removing existing stuff. +" + exit 0 +} + +_eval_exit() { + status=$1 + suc_msg=$2 + err_msg=$3 + + case $status in + 0) + prompt -s "${suc_msg}" + ;; + *) + prompt -e "${err_msg}" + ;; + esac + +} + +# _install() {} +_remove() { + prompt -i "-> Cleaning '$HOME/.config/nvim/'" + # rm -rf "$HOME/.config/nvim/" + + prompt -i "-> Cleaning '$HOME/.local/share/nvim/'" + # rm -rf "$HOME./local/share/nvim/" +} +# _update() {} + main() { - [ $# = 0 ] && _usage + while :; do + case $1 in + -h | -\? | --help) + _usage + exit 0 + ;; + -i | --install) + _install + ;; + -r | --remove) + _remove + _eval_exit $? "Successfully removeed NvChad!" "Failed to remove NvChad" + ;; + *) # Default case: No more options, so break out of the loop. + break + ;; + esac + shift + done - _check_install_dependencies - - _setup_arguments "${@}" - _copy_config - [ "${_UPDATE}" = "false" ] && _setup_terminal_shell - - # install all plugins + compile them - if _NVIM="$(command -v nvim)"; then - if _check_nvim_version; then - printf "\n%s\n" "=> Neovim will now open." && sleep 1 - "${_NVIM}" +"autocmd User PackerComplete ++once quitall" \ - +":lua require 'pluginList' vim.cmd('PackerSync')" - "${_NVIM}" - else - printf "Error: Neovim is installed, but version is lower than 0.5.x, install Neovim >= 5.x and then run nvim & do :PackerSync\n." - fi - else - printf "Error: Neovim is not installed, install Neovim >= 5.x and then run neovim & do :PackerSync.\n" - fi } -main "${@}" +init() { + if [ $# -eq 0 ]; then + prompt -e "ERROR: This script needs at least one argument" + else + main "${@}" + fi +} + +init "${@}" From bbbd73fc5642ab7ef07385501afb05456b35baa6 Mon Sep 17 00:00:00 2001 From: Pocco81 Date: Tue, 3 Aug 2021 22:38:03 -0500 Subject: [PATCH 2/7] basic parser --- install.sh | 173 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 151 insertions(+), 22 deletions(-) diff --git a/install.sh b/install.sh index 3ad1c00..2b34089 100755 --- a/install.sh +++ b/install.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash RED='\033[0;31m' GREEN='\033[0;32m' @@ -35,11 +35,10 @@ _usage() { -r | --remove => Remove the config. -u | --update => Update the existing config without removing existing stuff. " - exit 0 } _eval_exit() { - status=$1 + status=$1 suc_msg=$2 err_msg=$3 @@ -54,43 +53,173 @@ _eval_exit() { } -# _install() {} +_check_dependencies() { + _error_dependencies() { + prompt -r "Error: Install ${1} before proceeding." + exit 1 + } + command -v git 1>/dev/null || _error_dependencies git + return 0 +} + +_install() { + _check_dependencies +} + _remove() { - prompt -i "-> Cleaning '$HOME/.config/nvim/'" + prompt -i "-> Cleaning config ($HOME/.config/nvim/)" # rm -rf "$HOME/.config/nvim/" - prompt -i "-> Cleaning '$HOME/.local/share/nvim/'" - # rm -rf "$HOME./local/share/nvim/" + prompt -i "-> Cleaning miscellaneous ($HOME/.local/share/nvim/)" + # rm -rf "$HOME/.local/share/nvim/" + + prompt -i "-> Cleaning cache ($HOME/.cache/nvim/)" + # rm -rf "$HOME/.cache/nvim/" } # _update() {} -main() { - while :; do - case $1 in - -h | -\? | --help) - _usage - exit 0 +_init_settings() { + no_backup="false" + install="false" + uninstall="false" +} + +_clean_arg() { + arg=$1 + if [[ "$arg" == "--"* ]]; then + echo "${arg:2}" + elif [[ "$arg" == "-"* ]]; then + echo "${arg:1}" + fi +} + + +_parse_args() { + local args_func=$1 + local argv=("$@") + local argc=${#argv[@]} + local skip=0 + + _skip_ahead() { + amount=$1 + skip=$((skip + amount)) + } + + _clean_arg() { + arg=$1 + if [[ "$arg" == "--"* ]]; then + echo "${arg:2}" + elif [[ "$arg" == "-"* ]]; then + echo "${arg:1}" + fi + } + + for j in "${!argv[@]}"; do + if [[ ${skip} -gt 0 ]]; then + left=$((argc - j)) + while [[ ${skip} > ${left} ]]; do ((skip--)); done + skip=$((skip - 1)) + continue + fi + + case ${argv[j]} in + --*) # End of all options. + case ${argv[j]} in + --) # End of all options. + break + ;; + *) + eval "${eval_args}" "$(_clean_arg "${argv[j]}")" "$j" + ;; + esac ;; - -i | --install) - _install + -*) + tangled_args=$(_clean_arg "${argv[j]}") + for ((k = 0; k < ${#tangled_args}; k++)); do + eval "${eval_args}" "${tangled_args:$k:1}" "$j" + done ;; - -r | --remove) - _remove - _eval_exit $? "Successfully removeed NvChad!" "Failed to remove NvChad" - ;; - *) # Default case: No more options, so break out of the loop. - break + *) + prompt -w "Warning: flag ''${argv[j]}' not recognized" ;; esac - shift done +} +main() { + local argv=("$@") + local argc=${#argv[@]} + local skip=0 + + assert_arg() { + var=$1 + index=$2 + case ${var} in + h | help) + _usage + ;; + i | install) + prompt -i "installing..." + ;; + r | remove) + prompt -i "removing..." + ;; + # p | --path + *) + prompt -w "Warning: flag '${var}' not recognized" + ;; + esac + } + + _skip_ahead() { + amount=$1 + skip=$((skip + amount)) + } + + # works for: + # 1. normal flags (e.g. -h,--help) + # 2. nested flags (e.g. -ivh) + # 3. space sperated flags and args (e.g. --option argument) + # 4. equal separated flags and args (e.g. --option=argument) + + + for j in "${!argv[@]}"; do + if [[ ${skip} -gt 0 ]]; then + left=$((argc - j)) + while [[ ${skip} > ${left} ]]; do ((skip--)); done + skip=$((skip - 1)) + continue + fi + + case ${argv[j]} in + --*) # End of all options. + case ${argv[j]} in + --) # End of all options. + break + ;; + *) + assert_arg "$(_clean_arg "${argv[j]}")" "$j" + ;; + esac + ;; + -*) + tangled_args=$(_clean_arg "${argv[j]}") + for ((k = 0; k < ${#tangled_args}; k++)); do + assert_arg "${tangled_args:$k:1}" "$j" + done + ;; + *) + prompt -w "Warning: flag ''${argv[j]}' not recognized" + ;; + esac + done } init() { if [ $# -eq 0 ]; then prompt -e "ERROR: This script needs at least one argument" else + _init_settings main "${@}" fi } From acceccda87c60c76c06bef56a3c5780588473158 Mon Sep 17 00:00:00 2001 From: Pocco81 Date: Tue, 3 Aug 2021 23:09:18 -0500 Subject: [PATCH 3/7] accept both args and flags --- install.sh | 173 ++++++++++++++--------------------------------------- 1 file changed, 46 insertions(+), 127 deletions(-) diff --git a/install.sh b/install.sh index 2b34089..7ed3898 100755 --- a/install.sh +++ b/install.sh @@ -30,75 +30,31 @@ prompt() { _usage() { printf "%s" \ "Usage: sh ${0##*/} [ options ] - -h | --help => Show this help. - -i | --install => Install the config. - -r | --remove => Remove the config. - -u | --update => Update the existing config without removing existing stuff. + -h, --help -> Show this help. + -i, --install -> Install the config. + -r, --remove -> Remove the config. + -u, --update -> Update the existing config without removing existing stuff. " } -_eval_exit() { - status=$1 - suc_msg=$2 - err_msg=$3 - - case $status in - 0) - prompt -s "${suc_msg}" - ;; - *) - prompt -e "${err_msg}" - ;; - esac +skip=0 +_skip_ahead() { + amount=$1 + skip=$((skip + amount)) } -_check_dependencies() { - _error_dependencies() { - prompt -r "Error: Install ${1} before proceeding." - exit 1 - } - command -v git 1>/dev/null || _error_dependencies git - return 0 -} - -_install() { - _check_dependencies -} - -_remove() { - prompt -i "-> Cleaning config ($HOME/.config/nvim/)" - # rm -rf "$HOME/.config/nvim/" - - prompt -i "-> Cleaning miscellaneous ($HOME/.local/share/nvim/)" - # rm -rf "$HOME/.local/share/nvim/" - - prompt -i "-> Cleaning cache ($HOME/.cache/nvim/)" - # rm -rf "$HOME/.cache/nvim/" -} -# _update() {} - -_init_settings() { - no_backup="false" - install="false" - uninstall="false" -} - -_clean_arg() { - arg=$1 - if [[ "$arg" == "--"* ]]; then - echo "${arg:2}" - elif [[ "$arg" == "-"* ]]; then - echo "${arg:1}" - fi -} - - _parse_args() { - local args_func=$1 + local func_args=$1 local argv=("$@") + # local skip=0 + + unset 'argv[0]' # becuase arg1 is $func_arg + for i in "${!argv[@]}"; do new_array+=( "${argv[i]}" ); done + argv=("${new_array[@]}") + unset new_array + local argc=${#argv[@]} - local skip=0 _skip_ahead() { amount=$1 @@ -129,99 +85,62 @@ _parse_args() { break ;; *) - eval "${eval_args}" "$(_clean_arg "${argv[j]}")" "$j" + # eval "${func_args}" "$(_clean_arg "${argv[j]}")" "$j" + eval "${func_args}" "${argv[j]}" "$j" ;; esac ;; -*) - tangled_args=$(_clean_arg "${argv[j]}") - for ((k = 0; k < ${#tangled_args}; k++)); do - eval "${eval_args}" "${tangled_args:$k:1}" "$j" - done + if [[ ${#argv[j]} -le 2 ]]; then + eval "${func_args}" "${argv[j]}" "$j" + else + tangled_args=$(_clean_arg "${argv[j]}") + for ((k = 0; k < ${#tangled_args}; k++)); do + eval "${func_args}" "-${tangled_args:$k:1}" "$j" + done + fi ;; *) - prompt -w "Warning: flag ''${argv[j]}' not recognized" + eval "${func_args}" "${argv[j]}" "$j" ;; esac done } main() { - local argv=("$@") - local argc=${#argv[@]} - local skip=0 + local argvs=("$@") + local argc=${#argvs[@]} assert_arg() { - var=$1 - index=$2 + var=$1 # flag + index=$2 # flag's index case ${var} in - h | help) + -h | --help) _usage ;; - i | install) + -i | --install) prompt -i "installing..." ;; - r | remove) + -r | --remove) prompt -i "removing..." ;; - # p | --path + -a | --action) + action=${argvs[index+1]} + prompt -i "Action to perform -> ${action}" + _skip_ahead 1 + ;; + -p=* | --path=*) + path="${var#*=}" + prompt -i "Path was set to -> ${path}" + ;; *) - prompt -w "Warning: flag '${var}' not recognized" + prompt -w "Warning: unknown command '${var}'" ;; esac } - _skip_ahead() { - amount=$1 - skip=$((skip + amount)) - } - - # works for: - # 1. normal flags (e.g. -h,--help) - # 2. nested flags (e.g. -ivh) - # 3. space sperated flags and args (e.g. --option argument) - # 4. equal separated flags and args (e.g. --option=argument) - - - for j in "${!argv[@]}"; do - if [[ ${skip} -gt 0 ]]; then - left=$((argc - j)) - while [[ ${skip} > ${left} ]]; do ((skip--)); done - skip=$((skip - 1)) - continue - fi - - case ${argv[j]} in - --*) # End of all options. - case ${argv[j]} in - --) # End of all options. - break - ;; - *) - assert_arg "$(_clean_arg "${argv[j]}")" "$j" - ;; - esac - ;; - -*) - tangled_args=$(_clean_arg "${argv[j]}") - for ((k = 0; k < ${#tangled_args}; k++)); do - assert_arg "${tangled_args:$k:1}" "$j" - done - ;; - *) - prompt -w "Warning: flag ''${argv[j]}' not recognized" - ;; - esac - done + _parse_args "assert_arg" "${argvs[@]}" } -init() { - if [ $# -eq 0 ]; then - prompt -e "ERROR: This script needs at least one argument" - else - _init_settings - main "${@}" - fi -} -init "${@}" +main "${@}" From a49c54fc41a4e2063f0de42633a9600f3ef4d5f3 Mon Sep 17 00:00:00 2001 From: Pocco81 Date: Tue, 3 Aug 2021 23:17:40 -0500 Subject: [PATCH 4/7] refactor --- install.sh | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/install.sh b/install.sh index 7ed3898..4863cf3 100755 --- a/install.sh +++ b/install.sh @@ -6,6 +6,8 @@ ORANGE='\033[0;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color +skip=0 + # https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux prompt() { case ${1} in @@ -37,7 +39,6 @@ _usage() { " } -skip=0 _skip_ahead() { amount=$1 @@ -142,5 +143,12 @@ main() { _parse_args "assert_arg" "${argvs[@]}" } +init() { + if [ $# -eq 0 ]; then + prompt -e "ERROR: This script needs at least one argument" + else + main "${@}" + fi +} -main "${@}" +init "${@}" From af1aa860da91d56b3abf3ba835b616ba7a69cfe0 Mon Sep 17 00:00:00 2001 From: Pocco81 Date: Wed, 4 Aug 2021 00:11:15 -0500 Subject: [PATCH 5/7] remove and install cmds --- install.sh | 92 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 77 insertions(+), 15 deletions(-) diff --git a/install.sh b/install.sh index 4863cf3..8f20647 100755 --- a/install.sh +++ b/install.sh @@ -6,7 +6,17 @@ ORANGE='\033[0;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color +REPO="https://github.com/siduck76/NvChad.git" + skip=0 +nvchad_path="$HOME/.config/nvim/" +dependencies=( + "git" +) +preserved_files=( + "lua/mappings.lua" + "lua/user_config.lua" +) # https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux prompt() { @@ -31,7 +41,7 @@ prompt() { _usage() { printf "%s" \ - "Usage: sh ${0##*/} [ options ] + "Usage: ./${0##*/} [ options ] -h, --help -> Show this help. -i, --install -> Install the config. -r, --remove -> Remove the config. @@ -39,6 +49,51 @@ _usage() { " } +_remove() { + prompt -w "Removing config -> ($HOME/.config/nvim/)" + # rm -rf "$HOME/.config/nvim/" + prompt -w "Removing miscellaneous -> ($HOME/.local/share/nvim/)" + # rm -rf "$HOME/.local/share/nvim/" + prompt -w "Removing cache -> ($HOME/.cache/nvim/)" + # rm -rf "$HOME/.cache/nvim/" +} + +_check_dependencies() { + local err + + for i in "${dependencies[@]}"; do + + if ! command -v "${i}" &> /dev/null + then + prompt -e "Error: You need to install the dependency '${i}'" + err="true" + fi + done + + if [[ "${err}" == "true" ]]; then + exit 1 + fi +} + +_fetch() { + printf " + %s\n" "$(prompt -i "Fetching repo...")" + git clone -n ${REPO} --depth 1 "${nvchad_path}" + cd "${nvchad_path}" || return + + printf " + %s\n" "$(prompt -i "Checking out core...")" + git checkout HEAD lua/ + printf " + %s\n" "$(prompt -i "Checking out init file...")" + git checkout HEAD init.lua +} + +_install() { + prompt -w "-> Checking dependencies..." + _check_dependencies + prompt -w "-> Cloning..." + _fetch +} + +# _update() {} _skip_ahead() { amount=$1 @@ -48,7 +103,6 @@ _skip_ahead() { _parse_args() { local func_args=$1 local argv=("$@") - # local skip=0 unset 'argv[0]' # becuase arg1 is $func_arg for i in "${!argv[@]}"; do new_array+=( "${argv[i]}" ); done @@ -112,7 +166,17 @@ main() { local argvs=("$@") local argc=${#argvs[@]} - assert_arg() { + assert_aditional_args() { + var=$1 # flag + index=$2 # flag's index + case ${var} in + -p=* | --path=*) + nvchad_path="${var#*=}" + ;; + esac + } + + assert_args() { var=$1 # flag index=$2 # flag's index case ${var} in @@ -120,27 +184,25 @@ main() { _usage ;; -i | --install) - prompt -i "installing..." + prompt -i "Installing NvChad..." + _install ;; -r | --remove) - prompt -i "removing..." + prompt -i "Removing NvChad..." + _remove ;; - -a | --action) - action=${argvs[index+1]} - prompt -i "Action to perform -> ${action}" - _skip_ahead 1 - ;; - -p=* | --path=*) - path="${var#*=}" - prompt -i "Path was set to -> ${path}" - ;; + -u | --update) + prompt -i "Updating NvChad..." + ;; + -p=* | --path=*) ;; *) prompt -w "Warning: unknown command '${var}'" ;; esac } - _parse_args "assert_arg" "${argvs[@]}" + _parse_args "assert_aditional_args" "${argvs[@]}" + _parse_args "assert_args" "${argvs[@]}" } init() { From c5fd109a2d6e311fd252e95507028e5405460c18 Mon Sep 17 00:00:00 2001 From: Pocco81 Date: Wed, 4 Aug 2021 14:49:33 -0500 Subject: [PATCH 6/7] done --- install.sh | 293 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 220 insertions(+), 73 deletions(-) diff --git a/install.sh b/install.sh index 8f20647..52a1a87 100755 --- a/install.sh +++ b/install.sh @@ -7,15 +7,19 @@ BLUE='\033[0;34m' NC='\033[0m' # No Color REPO="https://github.com/siduck76/NvChad.git" +BACKUP_PATH="/tmp/nvchad/" +TMP_NVCHAD="/tmp/nvchad_updates/" skip=0 -nvchad_path="$HOME/.config/nvim/" +# nvchad_path="$HOME/.config/nvim/" +nvchad_path="/tmp/test_chad/" +do_startup="true" dependencies=( - "git" + "git" ) preserved_files=( - "lua/mappings.lua" - "lua/user_config.lua" + "lua/mappings.lua" + "lua/user_config.lua" ) # https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux @@ -41,73 +45,192 @@ prompt() { _usage() { printf "%s" \ - "Usage: ./${0##*/} [ options ] - -h, --help -> Show this help. - -i, --install -> Install the config. - -r, --remove -> Remove the config. - -u, --update -> Update the existing config without removing existing stuff. + "Usage: ./${0##*/} [ options ] + -h, --help -> Show this help. + -i, --install -> Install the config. + -r, --remove -> Remove the config. + -u, --update -> Update the existing config without removing existing stuff. + -s, --skip-startup -> Skip starting up nvim after installing the config. + -c, --clean-backups -> Remove backups (if any) from your system. + -p=*, --path=* -> Path to NvChad. Relevant for installing and updating. " } _remove() { - prompt -w "Removing config -> ($HOME/.config/nvim/)" - # rm -rf "$HOME/.config/nvim/" - prompt -w "Removing miscellaneous -> ($HOME/.local/share/nvim/)" - # rm -rf "$HOME/.local/share/nvim/" - prompt -w "Removing cache -> ($HOME/.cache/nvim/)" - # rm -rf "$HOME/.cache/nvim/" + + read -p "Are you sure you want to uninstall NvChad? This will 'flush' dirs such as $HOME/.config/nvim/. (y/n): " u_reply + + if [[ "${u_reply}" == "y" ]]; then + printf " + %s\n" "$(prompt -w "Removing config -> ($HOME/.config/nvim/)")" + rm -rf "$HOME/.config/nvim/" + + printf " + %s\n" "$(prompt -w "Removing miscellaneous -> ($HOME/.local/share/nvim/)")" + rm -rf "$HOME/.local/share/nvim/" + + printf " + %s\n" "$(prompt -w "Removing cache -> ($HOME/.cache/nvim/)")" + rm -rf "$HOME/.cache/nvim/" + elif [[ "${u_reply}" == "n" ]]; then + prompt -i "Ok, enjoy NvChad :)" + else + prompt -e "Error: you must type either 'n' for no or 'y' for yes." + fi + } _check_dependencies() { - local err + local err for i in "${dependencies[@]}"; do - - if ! command -v "${i}" &> /dev/null - then - prompt -e "Error: You need to install the dependency '${i}'" - err="true" - fi - done - if [[ "${err}" == "true" ]]; then - exit 1 - fi + if ! command -v "${i}" &>/dev/null; then + prompt -e "Error: You need to install the dependency '${i}'" + err="true" + fi + done + + if [[ "${err}" == "true" ]]; then + exit 1 + fi } _fetch() { - printf " + %s\n" "$(prompt -i "Fetching repo...")" - git clone -n ${REPO} --depth 1 "${nvchad_path}" - cd "${nvchad_path}" || return + printf " + %s\n" "$(prompt -i "Cleaning workspace")" + rm -rf ${nvchad_path} + printf " + %s\n" "$(prompt -i "Fetching repo")" + git clone -n ${REPO} --depth 1 "${nvchad_path}" + cd "${nvchad_path}" || return - printf " + %s\n" "$(prompt -i "Checking out core...")" - git checkout HEAD lua/ - printf " + %s\n" "$(prompt -i "Checking out init file...")" - git checkout HEAD init.lua + printf " + %s\n" "$(prompt -i "Checking out core")" + git checkout HEAD lua/ + printf " + %s\n" "$(prompt -i "Checking out init file")" + git checkout HEAD init.lua +} + +_check_prev_conf() { + if [[ -d "${nvchad_path}" ]]; then + mkdir -p "${BACKUP_PATH}" + amount_files=$(ls "${BACKUP_PATH}" | wc -l) + printf " + %s\n" "$(prompt -i "Previous config found, backing it up (/tmp/nvchad/backup_${amount_files})")" + cp -r "${nvchad_path}" "${BACKUP_PATH}backup_${amount_files}" + fi +} + +_check_nvim_version() { + version_string="$(nvim --version | grep -Eo 'NVIM.*v[0-9.]+' -m 1 | grep -Eo '[0-9.]+')" || return 1 + printf "%s\n" "${version_string}" | while IFS=. read -r num1 num2 num3; do + { [ "${num1}" -ge 0 ] && [ "${num2}" -ge 5 ] && [ "${num3}" -ge 0 ]; } || return 1 + done +} + +_startup() { + + printf " + %s\n" "$(prompt -i "Checking nvim version")" + if _NVIM="$(command -v nvim)"; then + if _check_nvim_version; then + printf " + %s\n" "$(prompt -i "NeoVim will open now")" + sleep 1 + "${_NVIM}" +'autocmd User PackerComplete ++once lua print "Waiting for PackerCompile.." vim.cmd "PackerCompile"' \ + +'autocmd User PackerCompileDone ++once quitall' \ + +'lua print "Wait for PackerUpdate and PackerCompile to complete.." require "pluginList" vim.cmd "PackerUpdate"' + "${_NVIM}" + else + prompt -e "Error: Neovim is installed, but version is lower than 0.5.x, install Neovim >= 5.x and then run nvim & do :PackerSync" + fi + else + prompt -e "Error: Neovim is not installed, install Neovim >= 5.x and then run neovim & do :PackerSync" + fi } _install() { - prompt -w "-> Checking dependencies..." - _check_dependencies - prompt -w "-> Cloning..." - _fetch + prompt -w "-> Checking dependencies..." + _check_dependencies + prompt -w "-> Checking for previous confs..." + _check_prev_conf + prompt -w "-> Cloning..." + _fetch + + if [[ "$do_startup" == "true" ]]; then + prompt -w "-> Loading nvim..." + _startup + fi } -# _update() {} +_clean_backups() { + prompt -w "-> Looking for backups..." + if [[ -d "${BACKUP_PATH}" ]]; then + if [[ -z "$(ls -A ${BACKUP_PATH})" ]]; then + printf " + %s\n" "$(prompt -w "No backups were found")" + else + printf " + %s\n" "$(prompt -i "Backups were found, removing them...")" + rm -rf "${BACKUP_PATH}" + fi + else + printf " + %s\n" "$(prompt -w "No backups were found")" + fi +} + +_update() { + + prompt -w "-> Checking if conf is eligible for being updated..." + + if [[ -z "$(ls -A ${nvchad_path})" ]]; then + printf " + %s\n" "$(prompt -e "Error: there is nothing to update")" + else + printf " + %s\n" "$(prompt -i "Updating config")" + mkdir -p ${TMP_NVCHAD} + for to_preservef in "${!preserved_files[@]}"; do + file="${nvchad_path}${preserved_files[to_preservef]}" + printf " + %s\n" "$(prompt -i "saving file: ${preserved_files[to_preservef]}")" + if [[ -e "${file}" ]]; then + mv "${file}" "${TMP_NVCHAD}" + fi + done + + do_startup="false" + _install + + for to_preservef in "${!preserved_files[@]}"; do + destination_file="${nvchad_path}${preserved_files[to_preservef]}" + file_name=$(basename "${preserved_files[to_preservef]}") + location_path=$(dirname "${destination_file}") + stored_file="${TMP_NVCHAD}${file_name}" + printf " + %s\n" "$(prompt -i "restoring file: ${preserved_files[to_preservef]}")" + + if [[ -e "${stored_file}" ]]; then + rm -rf "${destination_file}" + mkdir -p "${location_path}" + mv "${stored_file}" "${destination_file}" + fi + done + rm -rf "${TMP_NVCHAD}" + fi +} _skip_ahead() { - amount=$1 - skip=$((skip + amount)) + amount=$1 + skip=$((skip + amount)) +} + +_reorder_array() { + local arr=("$@") + + for i in "${!arr[@]}"; do + new_array+=("${arr[i]}") + done + arr=("${new_array[@]}") + unset new_array + + echo "${arr[@]}" } _parse_args() { - local func_args=$1 - local argv=("$@") + local func_args=$1 + local argv=("$@") - unset 'argv[0]' # becuase arg1 is $func_arg - for i in "${!argv[@]}"; do new_array+=( "${argv[i]}" ); done - argv=("${new_array[@]}") - unset new_array + unset 'argv[0]' # becuase arg1 is $func_arg + for i in "${!argv[@]}"; do new_array+=("${argv[i]}"); done + argv=("${new_array[@]}") + unset new_array local argc=${#argv[@]} @@ -116,14 +239,14 @@ _parse_args() { skip=$((skip + amount)) } - _clean_arg() { - arg=$1 - if [[ "$arg" == "--"* ]]; then - echo "${arg:2}" - elif [[ "$arg" == "-"* ]]; then - echo "${arg:1}" - fi - } + _clean_arg() { + arg=$1 + if [[ "$arg" == "--"* ]]; then + echo "${arg:2}" + elif [[ "$arg" == "-"* ]]; then + echo "${arg:1}" + fi + } for j in "${!argv[@]}"; do if [[ ${skip} -gt 0 ]]; then @@ -146,14 +269,14 @@ _parse_args() { esac ;; -*) - if [[ ${#argv[j]} -le 2 ]]; then - eval "${func_args}" "${argv[j]}" "$j" - else - tangled_args=$(_clean_arg "${argv[j]}") - for ((k = 0; k < ${#tangled_args}; k++)); do - eval "${func_args}" "-${tangled_args:$k:1}" "$j" - done - fi + if [[ ${#argv[j]} -le 2 ]]; then + eval "${func_args}" "${argv[j]}" "$j" + else + tangled_args=$(_clean_arg "${argv[j]}") + for ((k = 0; k < ${#tangled_args}; k++)); do + eval "${func_args}" "-${tangled_args:$k:1}" "$j" + done + fi ;; *) eval "${func_args}" "${argv[j]}" "$j" @@ -165,19 +288,37 @@ _parse_args() { main() { local argvs=("$@") local argc=${#argvs[@]} + local counter=0 + + _set_params() { + argc=${#argvs[@]} + counter=0 + } + + _free_arg() { + local index=$1 + unset 'argvs[index-counter]' + argvs=($(_reorder_array "${argvs[@]}")) + ((counter++)) + } assert_aditional_args() { - var=$1 # flag + var=$1 # flag index=$2 # flag's index case ${var} in - -p=* | --path=*) - nvchad_path="${var#*=}" - ;; + -p=* | --path=*) + nvchad_path="${var#*=}" + _free_arg "${index}" + ;; + -s | --skip-startup) + do_startup="false" + _free_arg "${index}" + ;; esac } assert_args() { - var=$1 # flag + var=$1 # flag index=$2 # flag's index case ${var} in -h | --help) @@ -185,24 +326,30 @@ main() { ;; -i | --install) prompt -i "Installing NvChad..." - _install + _install ;; -r | --remove) prompt -i "Removing NvChad..." - _remove + _remove ;; -u | --update) prompt -i "Updating NvChad..." + _update + ;; + -c | --clean-backups) + prompt -i "Cleaning NvChad backups..." + _clean_backups ;; - -p=* | --path=*) ;; *) - prompt -w "Warning: unknown command '${var}'" + prompt -w "Warning: --unknown command '${var}'" ;; esac } - _parse_args "assert_aditional_args" "${argvs[@]}" - _parse_args "assert_args" "${argvs[@]}" + _set_params + _parse_args "assert_aditional_args" "${argvs[@]}" + _set_params + _parse_args "assert_args" "${argvs[@]}" } init() { From 9e7bec1198ab331ec2e8023b1f6f0c97294f1b75 Mon Sep 17 00:00:00 2001 From: Pocco81 Date: Wed, 4 Aug 2021 17:19:38 -0500 Subject: [PATCH 7/7] changed nvchad_path --- install.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/install.sh b/install.sh index 3979030..4fdd3fd 100755 --- a/install.sh +++ b/install.sh @@ -11,8 +11,7 @@ BACKUP_PATH="/tmp/nvchad/" TMP_NVCHAD="/tmp/nvchad_updates/" skip=0 -# nvchad_path="$HOME/.config/nvim/" -nvchad_path="/tmp/test_chad/" +nvchad_path="$HOME/.config/nvim/" do_startup="true" dependencies=( "git" @@ -360,4 +359,4 @@ init() { fi } -init "${@}" \ No newline at end of file +init "${@}"