Created
May 4, 2022 10:49
-
-
Save marek-saji/338d1ee0ae1a2890ad762ba801c96acb to your computer and use it in GitHub Desktop.
Script for linking dotfiles using stow(8)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -eu | |
print_help () | |
{ | |
printf "Link dot files\n" | |
printf "Usage: link.sh [--dry-run]\n" | |
} | |
if echo " $* " | grep -E ' (-h|--help) ' | |
then | |
print_help | |
exit 0 | |
fi | |
DRY_RUN= | |
if echo " $* " | grep -E ' (-n|--dry-run) ' | |
then | |
DRY_RUN=1 | |
fi | |
MKDIR='mkdir -vp' | |
STOW='stow -v' | |
if [ -n "$DRY_RUN" ] | |
then | |
MKDIR="echo $MKDIR" | |
STOW="$STOW --no -v" | |
fi | |
if ! command -v stow >/dev/null 2>&1 | |
then | |
echo 'ERROR: You need to install stow(8).' | |
exit 69 | |
fi | |
cd "$( dirname "$0" )" | |
echo "Linking from $PWD to $HOME" | |
# We want to make sure that we'll link some directories as a whole, | |
# instead of their contents to make sure that if we add files there, | |
# they will be synced up | |
for dir in .vim .config/git .bash local/bin local/etc | |
do | |
if [ -d "$HOME/$dir" ] && ! [ -L "$HOME/$dir" ] | |
then | |
echo "$dir should not exist" | |
exit 1 | |
fi | |
done | |
# Similarly, we _do not_ want that to some files -- files created | |
# locally should not be synced, so we ensure that directories exist. | |
# Mark those files by putting a LEAF_DIR empty file in them | |
# shellcheck disable=SC2156 | |
find . -name LEAF_DIR -exec sh -c "$MKDIR \$( dirname \"$HOME/{}\" )" \; | |
# A more elegant way might be to have multiple packages in this | |
# repository and only stow things that you need on a given machine, but | |
# that's too much effort, so we'll just keep everything in one bag. | |
package="$( basename "$PWD" )" | |
cd .. | |
$STOW "$package" --target="$HOME" \ | |
--ignore="LEAF_DIR" \ | |
--ignore="\.sync" \ | |
--ignore="\.gitignore" \ | |
--ignore="\.DS_Store" \ | |
--ignore="\.swp" \ | |
--ignore="pack.sh" \ | |
--ignore="link.sh" | |
# Running stow will fail if some of the files already exist. Usually the | |
# solution is to remove them from the machine before calling this | |
# script, but for some (currently bash config files) we want to keep | |
# main files not synced, so we instead link *.common file and source | |
# them in local ones. | |
# But we want to keep number of those files to minimum. | |
ensure_line () | |
{ | |
target="$1" | |
line="$2" | |
if ! [ -e "$target" ] || ! grep -q "$line" "$target" | |
then | |
if [ -n "$DRY_RUN" ] | |
then | |
echo echo "$line" \>\> "$target" | |
else | |
echo "$line" >> "$target" | |
fi | |
fi | |
} | |
ensure_line "$HOME/.bashrc" 'source ~/.bashrc.common' | |
ensure_line "$HOME/.bash_profile" 'source ~/.bash_profile.common' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment