2021-05-08 14:42:53 +00:00
|
|
|
#!/bin/bash
|
2020-03-23 00:42:02 +00:00
|
|
|
|
2024-02-24 03:28:08 +00:00
|
|
|
###########################################################
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
###########################################################
|
|
|
|
|
2024-02-29 05:50:03 +00:00
|
|
|
CURRENT_TIME=$(date +%T)
|
|
|
|
|
2024-02-24 03:28:08 +00:00
|
|
|
###########################################################
|
|
|
|
#
|
|
|
|
# 1. Helpers and utilities
|
|
|
|
#
|
|
|
|
###########################################################
|
|
|
|
|
|
|
|
STEP_COUNT=0
|
|
|
|
SUBSTEP_COUNT=0
|
|
|
|
|
|
|
|
# Pretty-print step headers.
|
|
|
|
pre_step() {
|
2024-02-29 05:29:31 +00:00
|
|
|
STEP_COUNT=$((STEP_COUNT + 1))
|
2024-02-24 03:28:08 +00:00
|
|
|
SUBSTEP_COUNT=0
|
|
|
|
echo -e "\e[1m[$STEP_COUNT/$TOTAL_STEPS] <====== $1 =======>\e[0m"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Pretty-prints substep headers.
|
|
|
|
pre_substep() {
|
2024-02-29 05:29:31 +00:00
|
|
|
SUBSTEP_COUNT=$((SUBSTEP_COUNT + 1))
|
2024-02-24 03:28:08 +00:00
|
|
|
echo -e "\e[1m[$STEP_COUNT.$SUBSTEP_COUNT] <====== $1 =======>\e[0m"
|
|
|
|
}
|
|
|
|
|
2024-02-26 03:26:24 +00:00
|
|
|
# Copies a file from $1 to $2.
|
|
|
|
#
|
|
|
|
# If $2 exists, checks if $1 == $2 and asks what to do.
|
|
|
|
# From there, the user providing "O" will overwrite (proceed with copy),
|
|
|
|
# "S" will skip the file and leave the destination as-is, and anyting else
|
|
|
|
# will skip the copy altogether (i.e. "S").
|
|
|
|
copy_file() {
|
|
|
|
source_path=$1
|
|
|
|
dest_path=$2
|
|
|
|
|
|
|
|
if [ ! -e "$dest_path" ]; then
|
2024-02-29 05:29:31 +00:00
|
|
|
cp "$source_path" "$dest_path"
|
2024-02-26 03:26:24 +00:00
|
|
|
echo "Copied $source_path > $dest_path"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2024-02-29 05:29:31 +00:00
|
|
|
difference=$(diff "$source_path" "$dest_path")
|
2024-02-26 03:26:24 +00:00
|
|
|
|
|
|
|
if [ -z "$difference" ]; then
|
|
|
|
echo "$dest_path already exists, but is the same as $source_path - skipping."
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Differences detected between $source_path and $dest_path:"
|
2024-02-29 05:29:31 +00:00
|
|
|
diff --color "$source_path" "$dest_path"
|
|
|
|
read -p "$dest_path already exists. What should be done? [O:overwrite,S:skip,B:save backup and copy] " -r action
|
2024-02-26 03:26:24 +00:00
|
|
|
|
|
|
|
if [[ $action == "O" ]]; then
|
2024-02-29 05:29:31 +00:00
|
|
|
cp "$source_path" "$dest_path"
|
2024-02-26 03:26:24 +00:00
|
|
|
echo "Overwrote $source_path > $dest_path"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ $action == "S" ]]; then
|
|
|
|
echo "Skipped and left $dest_path as is."
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2024-02-26 03:33:54 +00:00
|
|
|
if [[ $action == "B" ]]; then
|
2024-02-29 05:29:31 +00:00
|
|
|
cp "$dest_path" "$dest_path".old
|
2024-02-26 03:33:54 +00:00
|
|
|
echo "Backed up $dest_path up to $dest_path.old"
|
2024-02-29 05:29:31 +00:00
|
|
|
cp "$source_path" "$dest_path"
|
2024-02-26 03:33:54 +00:00
|
|
|
echo "Overwrote $source_path > $dest_path"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2024-02-26 03:26:24 +00:00
|
|
|
echo "Unrecognized action, doing nothing."
|
|
|
|
}
|
|
|
|
|
2024-02-24 03:28:08 +00:00
|
|
|
##########################################################
|
|
|
|
#
|
|
|
|
# 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"
|
2024-02-24 03:29:23 +00:00
|
|
|
|
2024-02-24 03:28:08 +00:00
|
|
|
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"
|
|
|
|
|
2024-02-29 05:29:31 +00:00
|
|
|
sudo apt install "$(cat ./files/apt.txt)" -y --autoremove
|
2024-02-24 03:28:08 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-02-24 03:37:46 +00:00
|
|
|
install_omz() {
|
2024-02-24 03:28:08 +00:00
|
|
|
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"
|
|
|
|
if [ -z "$(rg "starship init zsh" ~/.zshrc)" ]; then
|
2024-02-29 05:29:31 +00:00
|
|
|
printf "#Initialize Starship prompt\neval \"\$(starship init zsh)\"" >> ~/.zshrc
|
2024-02-24 03:28:08 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
pre_substep "Copy configuration in configuration directory"
|
|
|
|
|
2024-02-26 03:33:54 +00:00
|
|
|
copy_file ./files/starship.toml ~/.config/starship.toml
|
2024-02-24 03:28:08 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 03:37:18 +00:00
|
|
|
install_nvm() {
|
2024-02-24 03:28:08 +00:00
|
|
|
pre_step "Installing nvm to manage node version"
|
2024-02-24 03:36:41 +00:00
|
|
|
|
|
|
|
pre_substep "Installing NVM from remote"
|
2024-02-24 03:28:08 +00:00
|
|
|
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
|
|
|
|
|
2024-02-24 03:36:41 +00:00
|
|
|
pre_substep "Ensuring that NVM can be run right away"
|
2024-02-24 03:28:08 +00:00
|
|
|
# Ensuring that nvm is usable right away without restarting the shell
|
|
|
|
export NVM_DIR="$HOME/.nvm"
|
2024-02-29 05:29:31 +00:00
|
|
|
# shellcheck disable=SC1091
|
2024-02-24 03:28:08 +00:00
|
|
|
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
|
2024-02-29 05:29:31 +00:00
|
|
|
# shellcheck disable=SC1091
|
2024-02-24 03:28:08 +00:00
|
|
|
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
|
|
|
|
}
|
|
|
|
|
2024-02-24 03:47:27 +00:00
|
|
|
install_gh_plugins() {
|
|
|
|
pre_step "Installing gh cli plugins"
|
|
|
|
|
|
|
|
gh extension install nektos/gh-act
|
2024-02-26 03:54:09 +00:00
|
|
|
|
2024-02-24 03:47:27 +00:00
|
|
|
gh extension install dlvhdr/gh-dash
|
2024-02-29 05:29:31 +00:00
|
|
|
copy_file ./files/gh-dash_config."$ENV_PROFILE".yml ~/.config/gh-dash/config.yml
|
2024-02-24 03:47:27 +00:00
|
|
|
|
|
|
|
gh extension upgrade --all
|
|
|
|
}
|
|
|
|
|
2024-02-24 03:36:41 +00:00
|
|
|
install_pyenv() {
|
|
|
|
pre_step "Install and configure pyenv"
|
|
|
|
|
|
|
|
curl https://pyenv.run | bash
|
|
|
|
}
|
|
|
|
|
2024-02-24 03:28:08 +00:00
|
|
|
# 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..."
|
2024-02-29 05:29:31 +00:00
|
|
|
if ! grep -q "$BLOCK_DELIMITER_PATTERN" < "$SHELL_CONFIG_PATH"; then
|
2024-02-29 05:50:03 +00:00
|
|
|
TEMPORARY_PATH=/tmp/init_vim_$CURRENT_TIME
|
|
|
|
copy_file "$SHELL_CONFIG_PATH" "$TEMPORARY_PATH"
|
2024-02-29 05:29:31 +00:00
|
|
|
printf "# %s:start\nsource %s/files/shell_extras\n# %s:end" \
|
|
|
|
"$BLOCK_DELIMITER_PATTERN" "$WORKING_PATH" "$BLOCK_DELIMITER_PATTERN" \
|
2024-02-29 05:50:03 +00:00
|
|
|
>> "$TEMPORARY_PATH"
|
|
|
|
copy_file "$TEMPORARY_PATH" "$SHELL_CONFIG_PATH"
|
2024-02-24 03:28:08 +00:00
|
|
|
echo "✅ Added managed block to $SHELL_CONFIG_PATH"
|
2024-02-29 05:50:03 +00:00
|
|
|
rm "$TEMPORARY_PATH"
|
2024-02-24 03:28:08 +00:00
|
|
|
else
|
|
|
|
echo "No changes to apply!"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Injects a managed block in the vim configuration
|
|
|
|
inject_vim_configuration() {
|
|
|
|
pre_step "Inject managed block in vim configuration"
|
|
|
|
|
2024-02-29 05:50:03 +00:00
|
|
|
EDITOR_CONFIG=$HOME/.config/nvim
|
|
|
|
EDITOR_CONFIG_FILE=$EDITOR_CONFIG/init.vim
|
2024-02-24 03:41:42 +00:00
|
|
|
WORKING_PATH=$(git rev-parse --show-toplevel)
|
2024-02-29 05:50:03 +00:00
|
|
|
|
2024-02-24 03:28:08 +00:00
|
|
|
if [[ -f $EDITOR_CONFIG_FILE ]]; then
|
|
|
|
echo "Setting up NVIM configuration extras..."
|
2024-02-29 05:29:31 +00:00
|
|
|
if ! grep -q "$BLOCK_DELIMITER_PATTERN" < "$EDITOR_CONFIG_FILE"; then
|
2024-02-29 05:50:03 +00:00
|
|
|
TEMPORARY_PATH=/tmp/init_vim_$CURRENT_TIME
|
2024-02-29 05:29:31 +00:00
|
|
|
printf "\" %s:start\nsource %s/files/extras.vim\n\" %s:end\n\n" \
|
2024-02-29 05:50:03 +00:00
|
|
|
"$BLOCK_DELIMITER_PATTERN" "$WORKING_PATH" "$BLOCK_DELIMITER_PATTERN" \
|
|
|
|
>> "$TEMPORARY_PATH"
|
|
|
|
cat "$EDITOR_CONFIG_FILE" >> "$TEMPORARY_PATH"
|
2024-02-29 05:29:31 +00:00
|
|
|
|
2024-02-29 05:50:03 +00:00
|
|
|
copy_file "$EDITOR_CONFIG_FILE" "$EDITOR_CONFIG_FILE".old
|
|
|
|
copy_file "$TEMPORARY_PATH" "$EDITOR_CONFIG_FILE"
|
2024-02-24 03:28:08 +00:00
|
|
|
echo "✅ Added managed block to $EDITOR_CONFIG_FILE"
|
2024-02-29 05:50:03 +00:00
|
|
|
rm "$TEMPORARY_PATH"
|
2024-02-24 03:28:08 +00:00
|
|
|
else
|
|
|
|
echo "No changes to apply!"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Setting up git configuration..."
|
2024-02-29 05:29:31 +00:00
|
|
|
source "$WORKING_PATH"/files/git_config
|
2024-02-24 03:41:42 +00:00
|
|
|
echo "✅ Set up git configuration, see $WORKING_PATH/files/git_config for details!"
|
2024-02-24 03:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
###########################################################
|
|
|
|
#
|
|
|
|
# 3. Bootstrap sequence
|
|
|
|
#
|
|
|
|
###########################################################
|
2023-12-16 05:43:31 +00:00
|
|
|
|
2024-02-26 05:48:03 +00:00
|
|
|
bootstrap_home() {
|
|
|
|
TOTAL_STEPS=10
|
2022-11-12 00:44:21 +00:00
|
|
|
|
2024-02-24 03:36:41 +00:00
|
|
|
# System updates
|
2024-01-28 15:55:53 +00:00
|
|
|
ensure_apt_up_to_date
|
|
|
|
ensure_apt_dependencies
|
2024-02-24 03:36:41 +00:00
|
|
|
|
|
|
|
# Development tooling and SDKs
|
2024-02-16 06:18:37 +00:00
|
|
|
install_rust
|
2024-02-24 03:36:41 +00:00
|
|
|
install_pyenv
|
2024-02-24 03:37:18 +00:00
|
|
|
install_nvm
|
2024-02-24 03:47:27 +00:00
|
|
|
install_gh_plugins
|
|
|
|
|
2024-02-16 06:18:37 +00:00
|
|
|
# Shell & prompt setup
|
2024-02-24 03:37:46 +00:00
|
|
|
install_omz
|
2024-02-16 06:18:37 +00:00
|
|
|
install_and_configure_starship
|
|
|
|
|
2024-01-28 15:55:53 +00:00
|
|
|
inject_shell_configuration
|
|
|
|
inject_vim_configuration
|
|
|
|
}
|
2022-11-12 00:44:21 +00:00
|
|
|
|
2024-02-26 05:48:03 +00:00
|
|
|
bootstrap_work() {
|
|
|
|
TOTAL_STEPS=4
|
|
|
|
|
|
|
|
install_gh_plugins
|
|
|
|
|
|
|
|
install_omz
|
|
|
|
install_and_configure_starship
|
|
|
|
|
|
|
|
inject_shell_configuration
|
|
|
|
inject_vim_configuration
|
|
|
|
}
|
|
|
|
|
|
|
|
if [[ $ENV_PROFILE == "home" ]]; then
|
|
|
|
bootstrap_home
|
|
|
|
elif [[ $ENV_PROFILE == "work" ]]; then
|
|
|
|
bootstrap_work
|
|
|
|
else
|
|
|
|
echo "Unknown \$ENV_PROFILE. :shrug:"
|
|
|
|
fi
|