Skip to content

Commit

Permalink
Add vim setup and general style improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
VersusFacit committed Aug 3, 2021
1 parent 6ebece5 commit cbd2c9a
Show file tree
Hide file tree
Showing 13 changed files with 414 additions and 46 deletions.
6 changes: 3 additions & 3 deletions lib/exit-handling.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ err_routine () {

if [ "${exit_status}" -ne '0' ]; then
1>&2 echo "${script}: Error in ${func_name} on line ${line_number}"\
"with exit status ${exit_status}"
"with exit status ${exit_status}"
fi

exit ${exit_status}
exit "${exit_status}"
}

exit_routine() {
Expand All @@ -20,7 +20,7 @@ exit_routine() {
rm -rf "${CLONE_DIR}"
fi

exit ${exit_status}
exit "${exit_status}"
}

trap 'err_routine $? ${LINENO} ${FUNCNAME[0]:-${0}}' ERR
Expand Down
23 changes: 13 additions & 10 deletions lib/logging-helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
#
# Basic logging
#
timestamp() {
echo "[$(date +"%y-%m-%d %T")]"
}

log () {
local -r term_width="$(tput cols)"
printf "${@}\n" | fold -sw "${term_width}"
printf "%s %s\n" "$(timestamp)" "${@}" | fold -sw "${term_width}"
}

log_stderr() {
>&2 log "$@"
1>&2 log "$@"
}

#
Expand All @@ -18,27 +21,27 @@ log_stderr() {

run_cmd_with_verbosity() {
local -r volume_limit="${1}"
if [[ ${VERBOSITY:-0} -ge "${volume_limit}" ]]; then
if [ "${VERBOSITY:-0}" -ge "${volume_limit}" ]; then
eval "${@:2}"
else
>/dev/null eval "${@:2}"
fi
}

quiet() {
run_quiet() {
run_cmd_with_verbosity '1' "${@}"
}

very_quiet() {
run_very_quiet() {
run_cmd_with_verbosity '2' "${@}"
}

log_quiet() {
run_cmd_with_verbosity '1' "echo '${@}'"
run_cmd_with_verbosity '1' "echo '${*}'"
}

log_very_quiet() {
run_cmd_with_verbosity '2' "echo '${@}'"
run_cmd_with_verbosity '2' "echo '${*}'"
}

#
Expand All @@ -51,17 +54,17 @@ color_reset() {

log_info() {
local -r color="$(tput setaf 2)"
printf "${color}${@}\n$(color_reset)"
printf "%s %s\n%s" "$(timestamp)" "${color}${*}" "$(color_reset)"
tput sgr0
}

log_warn() {
local -r color="$(tput setaf 3)"
>&2 printf "${color}${@}\n$(color_reset)"
>&2 printf "%s %s\n%s" "$(timestamp)" "${color}${*}" "$(color_reset)"
}

log_error() {
local -r color="$(tput setaf 1)"
>&2 printf "${color}${@}\n$(color_reset)"
>&2 printf "%s %s\n%s" "$(timestamp)" "${color}${*}" "$(color_reset)"
}

79 changes: 68 additions & 11 deletions lib/pkg-helpers.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@

is_installed() {
local -r pkg_name="$@"
dpkg -l "${pkg_name}"
}
#
# Apt
#

ensure_package_installed() {
local -r pkg_name="${@}"
local -r pkg_name="${*}"

if ! very_quiet is_installed "${pkg_name}"; then
log_quiet "installing ${pkg_name}..."
sudo apt-get install "${pkg_name}"
else
# can't use pipe with --quiet or hit SIGPIPE
if grep --quiet "${pkg_name}" <(dpkg -l); then
log_quiet "${pkg_name} is already installed, skipping..."
else
sudo apt-get install "${pkg_name}"
fi
}

ensure_install_all() {
for package_name in "${@}"; do
log_info "Installing package ${package_name}"
ensure_package_installed "${package_name}"
done
}

#
# Git
#

ensure_clean_git_tmp_dir_exists() {
if [ -d "${CLONE_DIR}" ]; then
rm -rf "${CLONE_DIR}"
Expand All @@ -29,11 +39,58 @@ clone_github_repo() {
local -r url="https://github.com/${repo_author}/${repo_name}.git"

local git_option='--quiet'
if [[ "${VERBOSITY:-0}" == 1 ]]; then
if [ "${VERBOSITY:-0}" = '1' ]; then
git_option='--'
elif [[ "${VERBOSITY:-0}" == 2 ]]; then
elif [ "${VERBOSITY:-0}" = '2' ]; then
git_option='--verbose'
fi

git clone "${git_option}" "$url" "$clone_destination"
}

# See http://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
repo_is_out_of_date() {
local -r local="$( git rev-parse @ )"
local -r remote="$( git rev-parse "@{u}" )"
local -r base="$( git merge-base @ "@{u}" )"

if [ "$local" = "$remote" ]; then
return 1
elif [ "$local" = "$base" ]; then
return 0
else
log_warn "Warning: Git repo has diverged. Skipping..."
fi
}

fast_forward_repo_to_remote() {
if ! grep --quiet "^Already up-to-date.$" <(2>&1 git pull origin master ); then
echo 'Repo now up to date'
fi
}

sync_git_repo() {
local author="${1}"
local name="${2}"
local path="${3:-.}"

if run_quiet 1>&2 pushd "${path}/${name}"; then
run_quiet git fetch
if repo_is_out_of_date; then
fast_forward_repo_to_remote
fi
log_info "${name} up to date"
run_quiet popd
else
clone_github_repo "${name}" "${author}" "${name}"
log_info "${name} installed"
fi
}

#
# Pip
#

install_flake8() {
run_quiet python3 -m pip install flake8
}
31 changes: 29 additions & 2 deletions lib/system-helpers.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@

change-mode-for-execution() {
chmod +x "$EXECUTABLE"
#
# Interface
#

bell() {
local -r BELL_FILEPATH='/usr/share/sounds/freedesktop/stereo/complete.oga'
2>&1 paplay --volume=41000 "${BELL_FILEPATH}"
}

wait_for_enter() {
read -n 1 -s -r -p "Press any key to continue"
printf "\r"
}

wait_for_user_intervention() {
local -r msg="${*}"
notify-send "$0" "${msg}"
run_quiet bell
wait_for_enter
}

#
# OS management
#

ensure_directory_exists() {
if [ ! -d "${1}" ]; then
mkdir -p "${1}"
fi
}

66 changes: 55 additions & 11 deletions main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,74 @@
#
# 0. Setup
#
LIB_DIR='./lib'
CLONE_DIR='./git-tmp'
declare -r CLONE_DIR='./git-tmp'
declare -r LIB_DIR='./lib'
declare -r PYTHON_DIR='./python_setup'
declare -r RUNCOMS_DIR='./runcoms'
declare -r SOLARIZED_DIR='./solarized_setup'
declare -r VIM_DIR='./vim_setup'

# Enter root of script repository to ensure relative path correctness
cd "$(dirname "$0")"
cd "$(dirname "$0")" # move to location of this script during runtime

source "${LIB_DIR}/setup.sh"
source "${LIB_DIR}/exit-handling.sh"
source "${LIB_DIR}/logging-helpers.sh"
source "${LIB_DIR}/pkg-helpers.sh"
source "${LIB_DIR}/system-helpers.sh"

ensure_clean_git_tmp_dir_exists

ensure_install_all 'shellcheck' 'vim-gtk3' 'dconf-cli' 'curl' 'vlc' \
'pulseaudio'

#
# 1. Solarized terminal setup
#
declare -r SOLARIZED_DIR='solarized_setup/'

log_info 'Installing package dconf-cli'
ensure_package_installed 'dconf-cli'

log_info 'Loading solarized module'
source "${LIB_DIR}/solarized_functions.sh"
log_info 'Loading solarized installation module'
source "${SOLARIZED_DIR}/solarized_functions.sh"

log_info 'Installing solarized colors for gnome terminal'
install_solarized_terminal_colors

#
# 2. Vim setup
#

log_info 'Loading vim installation module'
source "${VIM_DIR}/vim_functions.sh"

log_info 'Installing package vim'
ensure_package_installed 'vim'
ensure_vim_directories_exist

log_info 'Installing pathogen'
ensure_pathogen_installed

#TODO: make robust checksum copy-paste setup to avoid losing edits to .vimrc and make it an idempotent installation at best
log_info 'Copying .vimrc'
cp "${RUNCOMS_DIR}/.vimrc" "${HOME}/.vimrc"

log_info 'Copying general vim plugins'
install_manifest_plugins 'vim-plugin-manifest.txt'

log_info 'Copying Powerline fonts for terminal'
install_powerline_fonts

#
# 2a. Vim Python setup
#

log_info 'Loading python installation module'
source "${PYTHON_DIR}/python_setup_functions.sh"

log_info 'Installing flake8 for syntastic'
install_flake8

log_info 'Copying Python vim plugins.'
install_manifest_plugins 'python-plugin-manifest.txt'

log_info 'Copying python virtual env autocomplete script.'
ensure_python_vim_directories_exist
copy_python_virtual_env_script

ping_for_jedi_vim_setup
1 change: 1 addition & 0 deletions manifests/python-plugin-manifest.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
davidhalter jedi-vim
12 changes: 12 additions & 0 deletions manifests/vim-plugin-manifest.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
powerline fonts
morhetz gruvbox
flazz vim-colorschemes
tpope vim-commentary
tpope vim-dispatch
tpope vim-fugitive
takac vim-hardtime
vim-syntastic syntastic
scrooloose nerdtree
vim-airline vim-airline
vim-airline vim-airline
jeffkreeftmeijer vim-numbertoggle
7 changes: 7 additions & 0 deletions python_setup/enable_virtual_env_autocomplete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os

if 'VIRTUAL_ENV' in os.environ:
project_base_dir = os.environ['VIRTUAL_ENV']
activate_this = os.path.join(project_base_dir, 'bin/activate_this.py')

exec(open(activate_this).read(), dict(__file__=activate_this))
16 changes: 16 additions & 0 deletions python_setup/python_setup_functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

ensure_python_vim_directories_exist() {
ensure_directory_exists "${VIM_CONFIG_DIR}/python"
}

copy_python_virtual_env_script() {
cp "${PYTHON_DIR}/enable_virtual_env_autocomplete.py" \
"${VIM_CONFIG_DIR}/python"
}

ping_for_jedi_vim_setup() {
local -r msg=$(echo 'User action needed: Run "git submodule' \
'update --init --recursive".')
wait_for_user_intervention "${msg}"
}

Loading

0 comments on commit cbd2c9a

Please sign in to comment.