From a5b44abdce6004b96419189aa9a3e1c490db96cb Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Mon, 29 May 2023 00:31:52 -0400 Subject: [PATCH] refactor: reduce sourcing (#4) * refactor: reduce sourcing * docs: readme update * docs: comments on managed blocks --- README.md | 27 +++++++++++++--------- setup | 11 ++++++--- shell_extras | 59 +++++++++++++++++++++++++++++++++++++++++++++++ source/aliases | 42 --------------------------------- source/autoupdate | 18 --------------- source/exports | 5 ---- 6 files changed, 83 insertions(+), 79 deletions(-) create mode 100644 shell_extras delete mode 100644 source/aliases delete mode 100644 source/autoupdate delete mode 100644 source/exports diff --git a/README.md b/README.md index 8d96d94..3fe2214 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,20 @@ -# Shell goodies +# 🥫Environment-in-a-can -A collection of shell enhancement for everyday happiness. +Environment tweaks for everyday happiness. ## Setup ```bash -git clone git@github.com:mcataford/env.git ~/.env_goodies | . ./setup +git clone git@github.com:mcataford/env.git | . ./setup ``` -## Updating existing setups +The setup script will look for pre-existing managed blocks and will not update the file if one is found. -Since `~/.env_goodies` is a cloned repository, pulling new changes should update everything. You may need to `source` -the shell configuration file again for new goodies to apply. +### Updating existing setups + +Since the managed blocks only `source` the files in this repository, pulling in updates from remote should bring in any +new tweaks you want to apply. If changes are made to the managed blocks, you will need to first remove them from where +they live and rerun the setup script. ## Structure @@ -20,10 +23,12 @@ The package is structured as such: ``` env/ setup # Adds bootstrap block to your shell's config file. - source/ - ... # Anything here is sourced on shell-start - extras.vim # Common config for NVIM + shell_extras # Functions, aliases and exports for the shell. + extras.vim # Common config for NVIM. ``` -Adding files to `source/` will add code that gets executed on shell-start. Files are discovered by globbing in the -bootstrap block added by `setup` and do not need to be registered anywhere. +Adding code to `shell_extras` will add code that gets executed on shell-start. + +## Contributing + +I'm not currently looking for contributions since this is mainly about standardizing my own setup across machines. diff --git a/setup b/setup index 878732e..5b67141 100644 --- a/setup +++ b/setup @@ -8,19 +8,22 @@ set -e # BLOCK_DELIMITER_PATTERN="mcataford/env-managed-block" -WORKING_PATH=$(realpath $(dirname $0)) +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 -for source_files in ~/.env_goodies/source/*; do source $source_files; done +source $WORKING_PATH/shell_extras # $BLOCK_DELIMITER_PATTERN\:end" >> $SHELL_CONFIG_PATH echo "✅ Added managed block to $SHELL_CONFIG_PATH" else @@ -30,11 +33,13 @@ 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 $HOME/.env_goodies/extras.vim +source $WORKING_PATH/extras.vim \" $BLOCK_DELIMITER_PATTERN\:end\n\n" >> $EDITOR_CONFIG_FILE.new cat $EDITOR_CONFIG_FILE >> $EDITOR_CONFIG_FILE.new diff --git a/shell_extras b/shell_extras new file mode 100644 index 0000000..5555546 --- /dev/null +++ b/shell_extras @@ -0,0 +1,59 @@ +#!/usr/bin/bash + +################ +# ALIASES # +################ + +# TMUX things. + +# Lists all sessions +alias ts="tmux ls" + +# Attaches to session with name $1 +alias ta="tmux attach -t $1" + +# Create new session with provided name. +alias tm="tmux new -s $1" + +# VIM/NVIM things. + +# All roads lead to nvim. +alias vim=nvim +alias v=nvim + +############## +# FUNCTIONS # +############## + +# Autoupdates the env setup. +function env_autoupdate { + SHELL_CONFIG_PATH="" + + if [[ -n $(echo $SHELL | grep zsh) ]]; then + SHELL_CONFIG_PATH="$HOME/.zshrc" + else + SHELL_CONFIG_PATH="$HOME/.bashrc" + fi + + + { + cd ~/.env_goodies + git pull + source $SHELL_CONFIG_PATH + cd - + } + +} + +# Inspects available aliases based on a grep query. +function which_alias { + alias | grep $1 +} + +############# +# EXPORTS # +############# + +# Ensures that passphrases can be entered when signing +# git commits. +export GPG_TTY=$(tty) diff --git a/source/aliases b/source/aliases deleted file mode 100644 index 0d75636..0000000 --- a/source/aliases +++ /dev/null @@ -1,42 +0,0 @@ -################ -# TMUX-related # -################ - -# Lists all sessions -alias ts="tmux ls" - -# Attaches to session with name $1 -alias ta="tmux attach -t $1" - -# Create new session with provided name. -alias tm="tmux new -s $1" - -############### -# VIM-related # -############### - -# All roads lead to nvim. -alias vim=nvim -alias v=nvim - -############### -# git-related # -############### - -unalias mgs 2>/dev/null -alias mgs="git status" - -unalias mgf 2>/dev/null -alias mgf="git fetch" - -unalias mgc 2>/dev/null -alias mgc="git commit" - -unalias mgp 2>/dev/null -alias mgp="git push" - -unalias mgcb 2>/dev/null -alias mgcb="git branch --show-current" - -unalias mgl 2>/dev/null -alias mgl="git log" diff --git a/source/autoupdate b/source/autoupdate deleted file mode 100644 index d890afb..0000000 --- a/source/autoupdate +++ /dev/null @@ -1,18 +0,0 @@ -function env_autoupdate { - SHELL_CONFIG_PATH="" - - if [[ -n $(echo $SHELL | grep zsh) ]]; then - SHELL_CONFIG_PATH="$HOME/.zshrc" - else - SHELL_CONFIG_PATH="$HOME/.bashrc" - fi - - - { - cd ~/.env_goodies - git pull - source $SHELL_CONFIG_PATH - cd - - } - -} diff --git a/source/exports b/source/exports deleted file mode 100644 index 31b41f4..0000000 --- a/source/exports +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash - -# Ensures that passphrases can be entered when signing -# git commits. -export GPG_TTY=$(tty)