From e0c397925f3be21e50a90f6e1fa2053f6f2c74aa Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Fri, 23 Feb 2024 22:28:08 -0500 Subject: [PATCH 1/8] refactor: condense steps and bootstrap into a single script --- bootstrap.sh | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++- steps.sh | 145 ---------------------------------------- 2 files changed, 181 insertions(+), 147 deletions(-) delete mode 100644 steps.sh diff --git a/bootstrap.sh b/bootstrap.sh index 6c01403..6455408 100644 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -1,7 +1,186 @@ #!/bin/bash -# Ensuring steps are defined. -. ./steps.sh +########################################################### +# +# Marc's environment bootstrap script. +# +# Table of contents +# +# 1. Helpers and utilities +# These functions are used by individual bootstrap +# steps and are reused all over. +# 2. Steps +# Functions implementing a specific action in the +# bootstrap process. +# 3. Bootstrap sequence and callsite +# Where the magic happens. +# This is where the ordered sequence of steps +# is defined and run. +# +########################################################### + +########################################################### +# +# 1. Helpers and utilities +# +########################################################### + +STEP_COUNT=0 +SUBSTEP_COUNT=0 + +# Pretty-print step headers. +pre_step() { + STEP_COUNT=$(($STEP_COUNT + 1)) + SUBSTEP_COUNT=0 + echo -e "\e[1m[$STEP_COUNT/$TOTAL_STEPS] <====== $1 =======>\e[0m" +} + +# Pretty-prints substep headers. +pre_substep() { + SUBSTEP_COUNT=$(($SUBSTEP_COUNT + 1)) + echo -e "\e[1m[$STEP_COUNT.$SUBSTEP_COUNT] <====== $1 =======>\e[0m" +} + +########################################################## +# +# 2. Steps +# +########################################################## + +# 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 +} + +# Installs the rustup toolchain if not installed. +# If installed, the toolchain is updated. +install_rust() { + pre_step "Install and configure Rust toolchain" + + if [ -z "$(rustup --version 2> /dev/null)" ]; then + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh + else + rustup update + fi +} + +ensure_omz() { + pre_step "Installs Oh My ZSH" + curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh | bash +} + +# Installs the Starship prompt and adds an initialization +# command to the shell configuration file. +# +# Requires: install_rust +install_and_configure_starship() { + pre_step "Install and configure Starship" + + pre_substep "Install Starship via cargo" + cargo install starship --locked + + pre_substep "Add initialization command to shell configuration" + STARSHIP_INIT_COMMAND='eval "$(starship init zsh)"' + if [ -z "$(rg "starship init zsh" ~/.zshrc)" ]; then + echo "#Initialize Starship prompt\n$STARSHIP_INIT_COMMAND" >> ~/.zshrc + fi + + pre_substep "Copy configuration in configuration directory" + STARSHIP_CONFIGURATION_PATH=~/.config/starship.toml + if [ -f $STARSHIP_CONFIGURATION_PATH ]; then + cp $STARSHIP_CONFIGURATION_PATH $STARSHIP_CONFIGURATION_PATH.old + fi + + cp ./files/starship.toml $STARSHIP_CONFIGURATION_PATH +} + +ensure_nvm() { + pre_step "Installing nvm to manage node version" + curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash + + # Ensuring that nvm is usable right away without restarting the shell + export NVM_DIR="$HOME/.nvm" + [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" + [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" +} + +# 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!" +} + +########################################################### +# +# 3. Bootstrap sequence +# +########################################################### TOTAL_STEPS=8 diff --git a/steps.sh b/steps.sh deleted file mode 100644 index e94c47a..0000000 --- a/steps.sh +++ /dev/null @@ -1,145 +0,0 @@ -#!/bin/bash - -STEP_COUNT=0 -SUBSTEP_COUNT=0 - -# Helpers to pretty-print step titles and count. -pre_step() { - STEP_COUNT=$(($STEP_COUNT + 1)) - SUBSTEP_COUNT=0 - echo -e "\e[1m[$STEP_COUNT/$TOTAL_STEPS] <====== $1 =======>\e[0m" -} - -pre_substep() { - SUBSTEP_COUNT=$(($SUBSTEP_COUNT + 1)) - echo -e "\e[1m[$STEP_COUNT.$SUBSTEP_COUNT] <====== $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 -} - -# Installs the rustup toolchain if not installed. -# If installed, the toolchain is updated. -install_rust() { - pre_step "Install and configure Rust toolchain" - - if [ -z "$(rustup --version 2> /dev/null)" ]; then - curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh - else - rustup update - fi -} - -ensure_omz() { - pre_step "Installs Oh My ZSH" - curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh | bash -} - -# Installs the Starship prompt and adds an initialization -# command to the shell configuration file. -# -# Requires: install_rust -install_and_configure_starship() { - pre_step "Install and configure Starship" - - pre_substep "Install Starship via cargo" - cargo install starship --locked - - pre_substep "Add initialization command to shell configuration" - STARSHIP_INIT_COMMAND='eval "$(starship init zsh)"' - if [ -z "$(rg "starship init zsh" ~/.zshrc)" ]; then - echo "#Initialize Starship prompt\n$STARSHIP_INIT_COMMAND" >> ~/.zshrc - fi - - pre_substep "Copy configuration in configuration directory" - STARSHIP_CONFIGURATION_PATH=~/.config/starship.toml - if [ -f $STARSHIP_CONFIGURATION_PATH ]; then - cp $STARSHIP_CONFIGURATION_PATH $STARSHIP_CONFIGURATION_PATH.old - fi - - cp ./files/starship.toml $STARSHIP_CONFIGURATION_PATH -} - -ensure_nvm() { - pre_step "Installing nvm to manage node version" - curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash - - # Ensuring that nvm is usable right away without restarting the shell - export NVM_DIR="$HOME/.nvm" - [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" - [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" -} - -# 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!" -} From 6d9ad6c09e111675fe6e45679ccfb49c8738c60b Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Fri, 23 Feb 2024 22:29:23 -0500 Subject: [PATCH 2/8] refactor: move apt package list to files dir --- bootstrap.sh | 4 ++-- apt.txt => files/apt.txt | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename apt.txt => files/apt.txt (100%) diff --git a/bootstrap.sh b/bootstrap.sh index 6455408..080d30d 100644 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -50,7 +50,7 @@ pre_substep() { # 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 @@ -71,7 +71,7 @@ ensure_apt_dependencies() { else echo -e "\e[1mInstalling packages...\e[0m" - sudo apt install $(cat ./apt.txt) -y --autoremove + sudo apt install $(cat ./files/apt.txt) -y --autoremove echo -e "\e[1;32mSystem packages installed.\e[0m" fi diff --git a/apt.txt b/files/apt.txt similarity index 100% rename from apt.txt rename to files/apt.txt From 7f27eecbf78dae52e79381882235ccc3fe2a5db9 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Fri, 23 Feb 2024 22:36:41 -0500 Subject: [PATCH 3/8] feat: add missing pyenv install --- bootstrap.sh | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/bootstrap.sh b/bootstrap.sh index 080d30d..acae3a1 100644 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -121,14 +121,23 @@ install_and_configure_starship() { ensure_nvm() { pre_step "Installing nvm to manage node version" + + pre_substep "Installing NVM from remote" curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash + pre_substep "Ensuring that NVM can be run right away" # Ensuring that nvm is usable right away without restarting the shell export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" } +install_pyenv() { + pre_step "Install and configure pyenv" + + curl https://pyenv.run | bash +} + # Injects a managed block in the shell configuration. inject_shell_configuration() { pre_step "Injecting managed block in shell configuration" @@ -182,13 +191,17 @@ inject_vim_configuration() { # ########################################################### -TOTAL_STEPS=8 +TOTAL_STEPS=9 # Refer to steps.sh for step definition. bootstrap() { + # System updates ensure_apt_up_to_date ensure_apt_dependencies + + # Development tooling and SDKs install_rust + install_pyenv ensure_nvm # Shell & prompt setup From aa5dbd536b625625a4e4515608c8149fe6815649 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Fri, 23 Feb 2024 22:37:18 -0500 Subject: [PATCH 4/8] refactor: ensure_nvm -> install_nvm --- bootstrap.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bootstrap.sh b/bootstrap.sh index acae3a1..2f82ae8 100644 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -119,7 +119,7 @@ install_and_configure_starship() { cp ./files/starship.toml $STARSHIP_CONFIGURATION_PATH } -ensure_nvm() { +install_nvm() { pre_step "Installing nvm to manage node version" pre_substep "Installing NVM from remote" @@ -202,7 +202,7 @@ bootstrap() { # Development tooling and SDKs install_rust install_pyenv - ensure_nvm + install_nvm # Shell & prompt setup ensure_omz From f86f0aef008ee3b7c079206a111bd81ea946b1fa Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Fri, 23 Feb 2024 22:37:46 -0500 Subject: [PATCH 5/8] refactor: ensure_omz -> install_omz --- bootstrap.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bootstrap.sh b/bootstrap.sh index 2f82ae8..89e9fe7 100644 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -89,7 +89,7 @@ install_rust() { fi } -ensure_omz() { +install_omz() { pre_step "Installs Oh My ZSH" curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh | bash } @@ -205,7 +205,7 @@ bootstrap() { install_nvm # Shell & prompt setup - ensure_omz + install_omz install_and_configure_starship inject_shell_configuration From e5229323927662996f6e757c3b3e48a9d1423b89 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Fri, 23 Feb 2024 22:41:42 -0500 Subject: [PATCH 6/8] refactor: move extras to files --- bootstrap.sh | 10 ++++++---- extras.vim => files/extras.vim | 0 git_config => files/git_config | 0 shell_extras => files/shell_extras | 0 4 files changed, 6 insertions(+), 4 deletions(-) rename extras.vim => files/extras.vim (100%) rename git_config => files/git_config (100%) rename shell_extras => files/shell_extras (100%) diff --git a/bootstrap.sh b/bootstrap.sh index 89e9fe7..ea98235 100644 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -149,7 +149,7 @@ inject_shell_configuration() { 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 + source $WORKING_PATH/files/shell_extras # $BLOCK_DELIMITER_PATTERN\:end" >> $SHELL_CONFIG_PATH echo "✅ Added managed block to $SHELL_CONFIG_PATH" else @@ -164,11 +164,13 @@ inject_shell_configuration() { inject_vim_configuration() { pre_step "Inject managed block in vim configuration" + WORKING_PATH=$(git rev-parse --show-toplevel) + 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 + source $WORKING_PATH/files/extras.vim \" $BLOCK_DELIMITER_PATTERN\:end\n\n" >> $EDITOR_CONFIG_FILE.new cat $EDITOR_CONFIG_FILE >> $EDITOR_CONFIG_FILE.new @@ -181,8 +183,8 @@ inject_vim_configuration() { fi echo "Setting up git configuration..." - source $WORKING_PATH/git_config - echo "✅ Set up git configuration, see $WORKING_PATH/git_config for details!" + source $WORKING_PATH/files/git_config + echo "✅ Set up git configuration, see $WORKING_PATH/files/git_config for details!" } ########################################################### diff --git a/extras.vim b/files/extras.vim similarity index 100% rename from extras.vim rename to files/extras.vim diff --git a/git_config b/files/git_config similarity index 100% rename from git_config rename to files/git_config diff --git a/shell_extras b/files/shell_extras similarity index 100% rename from shell_extras rename to files/shell_extras From e3d43d8411f192cb0742421e0344fbb276b78c48 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Fri, 23 Feb 2024 22:43:23 -0500 Subject: [PATCH 7/8] docs: update README --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3d1b64a..dbc5f2d 100644 --- a/README.md +++ b/README.md @@ -22,10 +22,12 @@ The package is structured as such: ``` env/ - bootstrap.sh # Adds bootstrap block to your shell's config file. - steps.sh # Definitions for bootstrapping steps - shell_extras # Functions, aliases and exports for the shell. - extras.vim # Common config for NVIM. + bootstrap.sh # Adds bootstrap block to your shell's config file. + files/ + shell_extras # Functions, aliases and exports for the shell. + extras.vim # Common config for neovim. + git_config # Configuration commands for git + starship.toml # Starship prompt configuration ``` Adding code to `shell_extras` will add code that gets executed on shell-start. From 6220fac3e13953cfe3c7468a1a4d5df90f40fe45 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Fri, 23 Feb 2024 22:47:27 -0500 Subject: [PATCH 8/8] feat: add gh plugins --- bootstrap.sh | 14 ++++++++++++-- files/apt.txt | 1 + 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/bootstrap.sh b/bootstrap.sh index ea98235..7018f62 100644 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -132,6 +132,15 @@ install_nvm() { [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" } +install_gh_plugins() { + pre_step "Installing gh cli plugins" + + gh extension install nektos/gh-act + gh extension install dlvhdr/gh-dash + + gh extension upgrade --all +} + install_pyenv() { pre_step "Install and configure pyenv" @@ -193,7 +202,7 @@ inject_vim_configuration() { # ########################################################### -TOTAL_STEPS=9 +TOTAL_STEPS=10 # Refer to steps.sh for step definition. bootstrap() { @@ -205,7 +214,8 @@ bootstrap() { install_rust install_pyenv install_nvm - + install_gh_plugins + # Shell & prompt setup install_omz install_and_configure_starship diff --git a/files/apt.txt b/files/apt.txt index bf7b028..17dbd6e 100644 --- a/files/apt.txt +++ b/files/apt.txt @@ -2,3 +2,4 @@ zsh tmux postgresql postgresql-contrib +gh