From 6e2e382388877657f60deb34d53d201a55489cc2 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sun, 28 Jan 2024 10:07:17 -0500 Subject: [PATCH 1/6] refactor: rename for clarity, bootstrap v. install --- README.md | 4 ++-- install.sh => bootstrap.sh | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename install.sh => bootstrap.sh (100%) diff --git a/README.md b/README.md index 22b9d64..72fbb97 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Environment tweaks for everyday happiness. ## Setup ```bash -git clone git@github.com:mcataford/env.git | . ./install.sh +git clone git@github.com:mcataford/env.git | . ./bootstrap.sh ``` The setup script will look for pre-existing managed blocks and will not update the file if one is found. @@ -22,7 +22,7 @@ The package is structured as such: ``` env/ - install.sh # Adds bootstrap block to your shell's config file. + bootstrap.sh # Adds bootstrap block to your shell's config file. shell_extras # Functions, aliases and exports for the shell. extras.vim # Common config for NVIM. ``` diff --git a/install.sh b/bootstrap.sh similarity index 100% rename from install.sh rename to bootstrap.sh From 70742f265862f2bba9bd0e03f2fbdc4357438272 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sun, 28 Jan 2024 10:55:53 -0500 Subject: [PATCH 2/6] refactor: split steps into better-defined funcs --- apt.txt | 3 ++ bootstrap.sh | 62 ++++++++----------------------------- steps.sh | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 49 deletions(-) create mode 100644 apt.txt create mode 100644 steps.sh diff --git a/apt.txt b/apt.txt new file mode 100644 index 0000000..a165697 --- /dev/null +++ b/apt.txt @@ -0,0 +1,3 @@ +tmux +postgresql +postgresql-contrib diff --git a/bootstrap.sh b/bootstrap.sh index 402b85c..b72e644 100644 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -4,59 +4,23 @@ # # 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." return fi -# 2. CONFIGURATION INJECTION +# Ensuring steps are defined. +. ./steps.sh -# 2.1 Inject shell configuration. -# -# Adds a managed block to the shell configuration to -# source extra files on shell-start. -# +TOTAL_STEPS=4 -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 - -# 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!" +# Refer to steps.sh for step definition. +bootstrap() { + ensure_apt_up_to_date + ensure_apt_dependencies + inject_shell_configuration + inject_vim_configuration +} +bootstrap +return diff --git a/steps.sh b/steps.sh new file mode 100644 index 0000000..e9f3b5d --- /dev/null +++ b/steps.sh @@ -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!" +} From 1abade9e53d582925ded2cadffcdca74507cbb6d Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sun, 28 Jan 2024 10:59:54 -0500 Subject: [PATCH 3/6] feat: add nvm install step --- bootstrap.sh | 3 ++- steps.sh | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/bootstrap.sh b/bootstrap.sh index b72e644..9c1dc14 100644 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -12,12 +12,13 @@ fi # Ensuring steps are defined. . ./steps.sh -TOTAL_STEPS=4 +TOTAL_STEPS=5 # Refer to steps.sh for step definition. bootstrap() { ensure_apt_up_to_date ensure_apt_dependencies + ensure_nvm inject_shell_configuration inject_vim_configuration } diff --git a/steps.sh b/steps.sh index e9f3b5d..d7c5fec 100644 --- a/steps.sh +++ b/steps.sh @@ -38,6 +38,16 @@ ensure_apt_dependencies() { fi } +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" From eff28920e83342a03a35ebd45bf0dc16d7db9825 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sun, 28 Jan 2024 11:01:36 -0500 Subject: [PATCH 4/6] docs: update notes on steps --- README.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 72fbb97..3d1b64a 100644 --- a/README.md +++ b/README.md @@ -22,13 +22,20 @@ The package is structured as such: ``` env/ - bootstrap.sh # Adds bootstrap block to your shell's config file. - 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. + steps.sh # Definitions for bootstrapping steps + shell_extras # Functions, aliases and exports for the shell. + extras.vim # Common config for NVIM. ``` Adding code to `shell_extras` will add code that gets executed on shell-start. +### Adding steps + +Each step is defined as a function in `steps.sh` and should start with a call to `pre_step` to ensure that a header gets +echo'ed out. Additionally, the function should be called from `bootstrap` in `bootstrap.sh` and the `TOTAL_STEPS` should +be updated to reflect the number of steps. + ## Contributing I'm not currently looking for contributions since this is mainly about standardizing my own setup across machines. From 79782375fb3b3ad7b2343af26813f0524a3e06dd Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sun, 28 Jan 2024 11:08:21 -0500 Subject: [PATCH 5/6] feat: install zsh instead of expecting it --- apt.txt | 1 + bootstrap.sh | 9 --------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/apt.txt b/apt.txt index a165697..bf7b028 100644 --- a/apt.txt +++ b/apt.txt @@ -1,3 +1,4 @@ +zsh tmux postgresql postgresql-contrib diff --git a/bootstrap.sh b/bootstrap.sh index 9c1dc14..0e93f51 100644 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -1,14 +1,5 @@ #!/bin/bash -# 1. PRECONDITIONS -# -# zsh is expected to be set up. - -if [ -z "$(zsh --version 2> /dev/null)" ]; then - echo "💥 Install zsh (https://www.zsh.org/) before installing environment." - return -fi - # Ensuring steps are defined. . ./steps.sh From 07d5aeba6977ad5b69924ecc6e2db2864326a314 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Sun, 28 Jan 2024 11:04:06 -0500 Subject: [PATCH 6/6] ci: run bootstrap in CI to validate --- .github/workflows/ci.yaml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .github/workflows/ci.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..9fdfc1d --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,14 @@ +name: CI + +on: + pull_request: + push: + branches: [main] + +jobs: + validate: + name: Validate bootstrap + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: . ./bootstrap.sh