darkcloud-nvimconfig/update

293 lines
8.9 KiB
Text
Raw Permalink Normal View History

#!/usr/bin/env bash
2015-01-21 01:51:48 -05:00
#
2024-03-01 16:28:35 -05:00
# Darkcloud Neovim Config Update Tool
# https://github.com/prurigro/darkcloud-nvimconfig
2015-01-21 01:51:48 -05:00
#
# Written by Kevin MacMartin (prurigro@gmail.com)
# Released under the MIT license
#
#
# VARIABLES
#
user_vim_config=local/user.vim
local_bundle_dir=local/bundle
error_log=update-errors.log
script_name="${0//*\/}"
script_home="${0%"$script_name"}"
if [[ -z "$script_home" ]]; then
script_home="$PWD"
else
2018-03-11 22:55:51 -04:00
pushd "$script_home" >/dev/null || exit
script_home="$PWD"
2018-03-11 22:55:51 -04:00
popd >/dev/null || exit
fi
2018-03-11 22:55:51 -04:00
if [[ -t 1 ]]; then
cfg_red=$'\e[1;31m'
cfg_green=$'\e[1;32m'
cfg_white=$'\e[1;37m'
cbg_red=$'\e[1;41m'
cbg_yellow=$'\e[1;43m'
cbg_blue=$'\e[1;44m'
cbg_black=$'\e[40m'
c_reset=$'\e[0m'
2018-03-11 22:55:51 -04:00
else
cbg_blue='#'
2018-03-11 22:55:51 -04:00
fi
2015-01-21 01:51:48 -05:00
#
# FUNCTIONS
#
2015-01-21 01:51:48 -05:00
# error: output and log error
function error {
printf '%s\n' "$cbg_blue $c_reset$cbg_red ! ERROR: $c_reset$cfg_red $2 " >&2
printf '%s\n' "$cbg_blue $c_reset$cbg_red ! COMMAND: $c_reset ${cfg_white}=> $1$c_reset" >&2
[[ -n "$3" ]] && printf '%s\n' "$cbg_blue $c_reset$cbg_red ! OUTPUT: $c_reset$cfg_white $3" >&2
printf '%s\n%s\n%s\n' "DATE: @ $(date)" "ERROR: $2" "COMMAND: $1" >> "$script_home/$error_log"
2018-03-11 22:55:51 -04:00
[[ -n "$3" ]] && printf '%s\n' "OUTPUT: $3" >> "$script_home/$error_log"
printf '\n' >> "$script_home/$error_log"
}
# show_version: displays version information
function show_version {
repo_version="$(printf "%s.r%s" "$(git show -s --format=%ci master | sed 's/\ .*//g;s/-//g')" "$(git rev-list --count HEAD)")"
2024-03-01 16:28:35 -05:00
printf '%s\n' "$script_name: darkcloud-nvimconfig update tool (version: $repo_version)"
}
# show_help: this function displays help output
function show_help {
printf '\n%s\n' 'USAGE'
printf ' %s\n\n' "./$script_name [OPTION]"
printf '%s\n' 'OPTIONS'
printf ' %s\t%s\n' '-v, --version' '| output version information and exit'
printf ' %s\t%s\n\n' '-h, --help' '| display this help and exit'
2024-03-01 16:28:35 -05:00
printf '%s\n' 'Run with no arguments to update darkcloud-nvimconfig'
}
#
# SETUP
#
if type -P timeout >/dev/null; then
timeout_command=timeout
elif type -P gtimeout >/dev/null; then
timeout_command=gtimeout
else
error 'type -P timeout' 'The timeout command could not be found (install coreutils)'
exit 1
fi
cd "$script_home" || exit
# delete old error log if it exists
2018-03-11 22:55:51 -04:00
[[ -f "$error_log" ]] && rm "$script_home/$error_log"
# parse for arguments (then handle them below)
2015-01-21 01:51:48 -05:00
[[ -n "$1" ]] && {
case "$1" in
-h|--help)
show_version
show_help
exit 0
;;
-v|--version)
show_version
exit 0
;;
*)
error "$ERROR" "$1 is not a valid option"
show_help
exit 1
;;
esac
}
2015-01-21 01:51:48 -05:00
# display script title
2024-03-01 16:28:35 -05:00
printf '\n%s\n' "$cbg_black ~~~ DarkCloud Neovim Config Update Tool ~~~ $c_reset"
2015-01-21 01:51:48 -05:00
# create the local bundle directory if it doesn't exist
[[ -d "$local_bundle_dir" ]] || {
printf '\n%s' "$cbg_blue >> Creating user plugin directory:$c_reset"
process_status="$(install -d "$local_bundle_dir" 2>&1)"
if (( ! $? )); then
printf '%s\n' "$cfg_green SUCCESS! $c_reset"
else
printf '%s\n' "$cfg_red FAIL! $c_reset"
error "install -d $local_bundle_dir" "User plugin directory couldn't be created" "$process_status"
fi
2015-01-21 01:51:48 -05:00
}
# create user.vim if it doesn't exist
[[ -e "$user_vim_config" ]] || {
printf '\n%s' "$cbg_blue >> Creating user config file:$c_reset"
process_status="$(touch "$user_vim_config" 2>&1)"
if (( ! $? )); then
{
printf '%s\n%s\n\n' '"Auto-start syntax checking: (1:start toggled on | *0:start toggled off)' '"let g:autostartchecker = 1'
printf '%s\n%s\n\n' '"Enable Treesitter: (1:treesitter enabled | *0:treesitter disabled)' '"let g:enabletreesitter = 1'
printf '%s\n%s\n\n' '"Enable Auto-completion: (1:autocompletion enabled | *0:autocompletion disabled)' '"let g:enablecompletion = 1'
printf '%s\n%s\n\n' '"Enable automatic tag generation: (1:enable tag generation | *0:disable automatic tag generation)' '"let g:enableautotags = 1'
2024-03-01 22:23:28 -05:00
printf '%s\n%s\n' '"Enable Powerline: (1:use powerline characters | *0:use regular characters)' '"let g:enablepowerline = 1'
} >> "$user_vim_config"
if [[ -e "$user_vim_config" ]]; then
printf '%s\n' "$cfg_green SUCCESS! $c_reset"
else
printf '%s\n' "$cfg_red FAIL! $c_reset"
fi
else
printf '%s\n' "$cfg_red FAIL! $c_reset"
error "touch $user_vim_config" "User config couldn't be created" "$process_status"
fi
2015-01-21 01:51:48 -05:00
}
#
# REPO UPDATE
#
2024-03-01 16:28:35 -05:00
printf '\n%s' "$cbg_blue >> Updating darkcloud-nvimconfig:$c_reset"
process_status="$(git pull origin master 2>&1)"
if (( ! $? )); then
printf '%s\n' "$cfg_green SUCCESS! $c_reset"
else
printf '%s\n' "$cfg_red FAIL! $c_reset"
error 'git pull origin master' 'Git failed to sync the repo' "$process_status"
exit 1
fi
#
# SUBMODULE UPDATE
#
printf '\n%s\n' "$cbg_blue >> Updating plugin submodules >> $c_reset"
printf '%s' "$cbg_blue $c_reset$cbg_yellow + Updating plugin URLs:$c_reset"
process_status="$(git submodule sync 2>&1)"
if (( ! $? )); then
printf '%s\n' "$cfg_green SUCCESS! $c_reset"
else
printf '%s\n' "$cfg_red FAIL! $c_reset"
error 'git submodule sync' 'Git failed to sync the submodules' "$process_status"
fi
# update each submodule to the new head and run 'git fetch --all'
printf '%s' "$cbg_blue $c_reset$cbg_yellow + Fetching updates:$c_reset"
git submodule foreach git reset --hard >/dev/null 2>&1
process_status="$(git submodule foreach git fetch --all 2>&1)"
if (( ! $? )); then
process_status=$(git submodule update --init --recursive 2>&1)
if (( ! $? )); then
printf '%s\n' "$cfg_green SUCCESS! $c_reset"
else
printf '%s\n' "$cfg_red FAIL! $c_reset"
error 'git submodule update --init --recursive' 'Git failed to update the submodules' "$process_status"
fi
else
printf '%s\n' "$cfg_red FAIL! $c_reset"
error 'git submodule foreach git fetch --all' "Git failed to fetch the submodules from their respective remotes" "$process_status"
fi
# clean plugin directories and remove plugins no longer in the repo
printf '\n%s\n' "$cbg_blue >> Clean plugin directories >> $c_reset"
printf '%s' "$cbg_blue $c_reset$cbg_yellow + Remove untracked files:$c_reset"
process_status="$(git submodule foreach git clean -dxf 2>&1)"
if (( ! $? )); then
printf '%s\n' "$cfg_green SUCCESS! $c_reset"
else
printf '%s\n' "$cfg_red FAIL! $c_reset"
error 'git submodule foreach git clean -dxf' 'Git failed to remove untracked files' "$process_status"
fi
2015-01-21 01:51:48 -05:00
[[ -f '.gitmodules' ]] && {
for plugin in vim/bundle/*; do
[[ -f "$plugin/.git" ]] && {
plugin_dirname="${plugin/*\/}"
grep 'path = ' .gitmodules | grep -oe '[^\/]*$' | grep -qe "^$plugin_dirname$" || {
[[ -z "$first_found" ]] && {
first_found=1
printf '%s\n' "$cbg_blue $c_reset$cbg_yellow + Removing old plugins: $c_reset"
}
process_status="$(rm -rf "${plugin:?}")"
if (( ! $? )); then
printf '%s\n' "$cbg_blue $c_reset$cbg_yellow = $cfg_white$plugin$c_reset"
else
error "rm -rf $plugin" "Folder couldn't be deleted" "$process_status"
exit 1
fi
2015-01-21 01:51:48 -05:00
}
}
done
printf '\n'
2015-01-21 01:51:48 -05:00
}
#
# USER PLUGIN UPDATE
#
[[ -d "$local_bundle_dir" ]] && find "$local_bundle_dir" -mindepth 3 -maxdepth 3 -name config | grep -q '.git/config' && {
printf '%s\n' "$cbg_blue >> Updating user plugins >> $c_reset"
pushd "$local_bundle_dir" >/dev/null || exit
2015-01-21 01:51:48 -05:00
for plugin in *; do
[[ -d "$plugin/.git" ]] && {
2018-03-11 22:55:51 -04:00
pushd "$plugin" >/dev/null || exit
printf '%s' "$cbg_blue $c_reset$cbg_yellow + Updating '$local_bundle_dir/$plugin':$c_reset"
process_status="$(git pull 2>&1)"
if (( ! $? )); then
if ! grep -q "Already up-to-date" <<< "$process_status"; then
printf '%s\n' "$cfg_green SUCCESS! $c_reset"
else
printf '%s\n' "$cfg_white UP TO DATE $c_reset"
fi
else
error 'git pull' "Failed pulling changes for $plugin" "$process_status"
fi
2018-03-11 22:55:51 -04:00
popd >/dev/null || exit
2015-01-21 01:51:48 -05:00
}
done
2018-03-11 22:55:51 -04:00
popd >/dev/null || exit
printf '\n'
2015-01-21 01:51:48 -05:00
}
#
# GENERATE PLUGIN HELP
#
type -P nvim >/dev/null && {
printf '%s' "$cbg_blue >> Generating plugin help:$c_reset"
2024-03-13 15:01:11 -04:00
$timeout_command --preserve-status --foreground 1m nvim -c 'helptags ALL|qa!'
if (( ! $? )); then
printf '%s\n' "$cfg_green SUCCESS! $c_reset"
else
reset -I
printf '%s\n' "$cfg_red FAIL! $c_reset"
2024-03-13 15:01:11 -04:00
error "nvim -c 'helptags ALL|qa!'" 'Generating helpdocs for the submodules failed'
fi
}
#
# FINISH
#
printf '\n%s\n\n' "$cbg_black ~~~ Update Complete ~~~ $c_reset"