Marc Cataford
a5b44abdce
* refactor: reduce sourcing * docs: readme update * docs: comments on managed blocks
57 lines
1.9 KiB
Bash
57 lines
1.9 KiB
Bash
#!/bin/bash
|
|
|
|
set -e
|
|
|
|
#
|
|
# Detects the current shell (bash, zsh, ...) and adds, if necessary, a managed
|
|
# block to its configuration to source extra files on shell-start.
|
|
#
|
|
|
|
BLOCK_DELIMITER_PATTERN="mcataford/env-managed-block"
|
|
WORKING_PATH=$(git rev-parse --show-toplevel)
|
|
SHELL_CONFIG_PATH=""
|
|
|
|
# First, we need to know if we're dealing with ZSH or Bash.
|
|
if [[ -n $(echo $SHELL | grep zsh) ]]; then
|
|
SHELL_CONFIG_PATH="$HOME/.zshrc"
|
|
else
|
|
SHELL_CONFIG_PATH="$HOME/.bashrc"
|
|
fi
|
|
|
|
# A managed block is added to source the `shell_extras` file
|
|
# in the shell configuration (either `.zshrc` or `.bashrc`).
|
|
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
|
|
|
|
# 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!"
|
|
|