refactor: split steps into better-defined funcs

This commit is contained in:
Marc 2024-01-28 10:55:53 -05:00
parent 6e2e382388
commit 70742f2658
Signed by: marc
GPG key ID: 048E042F22B5DC79
3 changed files with 102 additions and 49 deletions

3
apt.txt Normal file
View file

@ -0,0 +1,3 @@
tmux
postgresql
postgresql-contrib

View file

@ -4,59 +4,23 @@
# #
# zsh is expected to be set up. # zsh is expected to be set up.
if [ -z "$(zshc --version 2> /dev/null)" ]; then if [ -z "$(zsh --version 2> /dev/null)" ]; then
echo "💥 Install zsh (https://www.zsh.org/) before installing environment." echo "💥 Install zsh (https://www.zsh.org/) before installing environment."
return return
fi fi
# 2. CONFIGURATION INJECTION # Ensuring steps are defined.
. ./steps.sh
# 2.1 Inject shell configuration. TOTAL_STEPS=4
#
# Adds a managed block to the shell configuration to
# source extra files on shell-start.
#
BLOCK_DELIMITER_PATTERN="mcataford/env" # Refer to steps.sh for step definition.
WORKING_PATH=$(git rev-parse --show-toplevel) bootstrap() {
SHELL_CONFIG_PATH="$HOME/.zshrc" ensure_apt_up_to_date
ensure_apt_dependencies
echo "Setting up shell configuration extras..." inject_shell_configuration
if [[ -z $(cat $SHELL_CONFIG_PATH | grep $BLOCK_DELIMITER_PATTERN) ]]; then inject_vim_configuration
echo "# $BLOCK_DELIMITER_PATTERN\:start }
source $WORKING_PATH/shell_extras
# $BLOCK_DELIMITER_PATTERN\:end" >> $SHELL_CONFIG_PATH
echo "✅ Added managed block to $SHELL_CONFIG_PATH"
else
echo "No changes to apply!"
fi
EDITOR_CONFIG=$HOME/.config/nvim
EDITOR_CONFIG_FILE=$EDITOR_CONFIG/init.vim
# 2.2 Inject nvim configuration.
#
# Similarly, sourcing for `extras.vim` is added to the vim configuration
# so plugins, bindings and whatnot are loaded on start.
#
if [[ -f $EDITOR_CONFIG_FILE ]]; then
echo "Setting up NVIM configuration extras..."
if [[ -z $(cat $EDITOR_CONFIG_FILE | grep $BLOCK_DELIMITER_PATTERN) ]]; then
echo "\" $BLOCK_DELIMITER_PATTERN\:start
source $WORKING_PATH/extras.vim
\" $BLOCK_DELIMITER_PATTERN\:end\n\n" >> $EDITOR_CONFIG_FILE.new
cat $EDITOR_CONFIG_FILE >> $EDITOR_CONFIG_FILE.new
mv $EDITOR_CONFIG_FILE $EDITOR_CONFIG_FILE.old
mv $EDITOR_CONFIG_FILE.new $EDITOR_CONFIG_FILE
echo "✅ Added managed block to $EDITOR_CONFIG_FILE"
else
echo "No changes to apply!"
fi
fi
echo "Setting up git configuration..."
source $WORKING_PATH/git_config
echo "✅ Set up git configuration, see $WORKING_PATH/git_config for details!"
bootstrap
return

86
steps.sh Normal file
View file

@ -0,0 +1,86 @@
#!/bin/bash
STEP_COUNT=0
# Helpers to pretty-print step titles and count.
pre_step() {
STEP_COUNT=$(($STEP_COUNT + 1))
echo -e "\e[1m[$STEP_COUNT/$TOTAL_STEPS] <====== $1 =======>\e[0m"
}
# Ensures that all existing Apt packages are up-to-date.
ensure_apt_up_to_date() {
pre_step "Ensuring apt packages are up-to-date"
if [ -z "$(apt --version 2> /dev/null)" ]; then
echo -e "\e[33mApt not installed, skipping updates.\e[0m"
else
echo -e "\e[1mEnsuring system packages are up-to-date...\e[0m"
sudo apt update && sudo apt upgrade -y --autoremove
echo -e "\e[1;32mSystem packages up-to-date.\e[0m"
fi
}
# Installs packages as specified in apt.txt
ensure_apt_dependencies() {
pre_step "Installing extra packages"
if [ -z "$(apt --version 2> /dev/null)" ]; then
echo -e "\e[33mApt not installed, skipping packages.\e[0m"
else
echo -e "\e[1mInstalling packages...\e[0m"
sudo apt install $(cat ./apt.txt) -y --autoremove
echo -e "\e[1;32mSystem packages installed.\e[0m"
fi
}
# Injects a managed block in the shell configuration.
inject_shell_configuration() {
pre_step "Injecting managed block in shell configuration"
BLOCK_DELIMITER_PATTERN="mcataford/env"
WORKING_PATH=$(git rev-parse --show-toplevel)
SHELL_CONFIG_PATH="$HOME/.zshrc"
echo "Setting up shell configuration extras..."
if [[ -z $(cat $SHELL_CONFIG_PATH | grep $BLOCK_DELIMITER_PATTERN) ]]; then
echo "# $BLOCK_DELIMITER_PATTERN\:start
source $WORKING_PATH/shell_extras
# $BLOCK_DELIMITER_PATTERN\:end" >> $SHELL_CONFIG_PATH
echo "✅ Added managed block to $SHELL_CONFIG_PATH"
else
echo "No changes to apply!"
fi
EDITOR_CONFIG=$HOME/.config/nvim
EDITOR_CONFIG_FILE=$EDITOR_CONFIG/init.vim
}
# Injects a managed block in the vim configuration
inject_vim_configuration() {
pre_step "Inject managed block in vim configuration"
if [[ -f $EDITOR_CONFIG_FILE ]]; then
echo "Setting up NVIM configuration extras..."
if [[ -z $(cat $EDITOR_CONFIG_FILE | grep $BLOCK_DELIMITER_PATTERN) ]]; then
echo "\" $BLOCK_DELIMITER_PATTERN\:start
source $WORKING_PATH/extras.vim
\" $BLOCK_DELIMITER_PATTERN\:end\n\n" >> $EDITOR_CONFIG_FILE.new
cat $EDITOR_CONFIG_FILE >> $EDITOR_CONFIG_FILE.new
mv $EDITOR_CONFIG_FILE $EDITOR_CONFIG_FILE.old
mv $EDITOR_CONFIG_FILE.new $EDITOR_CONFIG_FILE
echo "✅ Added managed block to $EDITOR_CONFIG_FILE"
else
echo "No changes to apply!"
fi
fi
echo "Setting up git configuration..."
source $WORKING_PATH/git_config
echo "✅ Set up git configuration, see $WORKING_PATH/git_config for details!"
}