From 01067990cce27b7af94506f583de025d28e31c1b Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Sun, 5 Sep 2021 12:06:19 -0500 Subject: Change bootstrapping method Now uses a script, ./bootstrap.sh, and a file, ./bootstrap.manifest. --- Makefile | 10 --- bash/bash_logout | 6 ++ bash/bash_profile | 4 + bash/bashrc | 41 +++++++++++ bootstrap.manifest | 19 +++++ bootstrap.sh | 195 +++++++++++++++++++++++++++++++++++++++++++++++++ bootstrap/bash_logout | 6 -- bootstrap/bash_profile | 4 - bootstrap/bashrc | 41 ----------- bootstrap/profile | 17 ----- profile/profile | 17 +++++ 11 files changed, 282 insertions(+), 78 deletions(-) delete mode 100644 Makefile create mode 100644 bash/bash_logout create mode 100644 bash/bash_profile create mode 100644 bash/bashrc create mode 100644 bootstrap.manifest create mode 100755 bootstrap.sh delete mode 100644 bootstrap/bash_logout delete mode 100644 bootstrap/bash_profile delete mode 100644 bootstrap/bashrc delete mode 100644 bootstrap/profile create mode 100644 profile/profile diff --git a/Makefile b/Makefile deleted file mode 100644 index 712877b..0000000 --- a/Makefile +++ /dev/null @@ -1,10 +0,0 @@ -# ~/etc/ Makefile - -BOOTSTRAPS:=$(wildcard bootstrap/*) -BOOTSTRAPS_OUT:=$(foreach b,$(BOOTSTRAPS),$(patsubst bootstrap/%,~/.%,$(b))) - -.PHONY: bootstrap -bootstrap: $(BOOTSTRAPS_OUT) - -~/.%: bootstrap/% - ln -sf etc/$< $@ diff --git a/bash/bash_logout b/bash/bash_logout new file mode 100644 index 0000000..d6c9b7a --- /dev/null +++ b/bash/bash_logout @@ -0,0 +1,6 @@ +# bash_logout +# vim:ft=sh + +if [[ -r "$XDG_CONFIG_HOME"/bash/logout ]]; then + source "$XDG_CONFIG_HOME"/bash/logout +fi diff --git a/bash/bash_profile b/bash/bash_profile new file mode 100644 index 0000000..4439ca7 --- /dev/null +++ b/bash/bash_profile @@ -0,0 +1,4 @@ +# bash_profile + +[[ -f ~/.profile ]] && source ~/.profile +[[ -f ~/.bashrc ]] && source ~/.bashrc diff --git a/bash/bashrc b/bash/bashrc new file mode 100644 index 0000000..4966e75 --- /dev/null +++ b/bash/bashrc @@ -0,0 +1,41 @@ +# bashrc + +# If not running interactively, don't do anything +[[ $- != *i* ]] && return + +BASH_SOURCE_FIRST=( + aliases + functions +) + +BASH_SOURCE_LAST=( + blesh +) + +for f in "${BASH_SOURCE_FIRST[@]}"; do + file="${XDG_CONFIG_HOME:-$HOME/.config}/bash/$f.bash" + [[ -r "$file" ]] && source "$file" # || echo >&2 "no '$file' found" +done + +for file in "$XDG_CONFIG_HOME"/bash/*.bash; do + file_base="${file##*/}" + memq "${file_base%.bash}" "${BASH_SOURCE_FIRST[@]}" && { + # echo >&2 "'$file' in BASH_SOURCE_FIRST, skipping" + continue + } + memq "${file_base%.bash}" "${BASH_SOURCE_LAST[@]}" && { + # echo >&2 "'$file' in BASH_SOURCE_LAST, skipping" + continue + } + [[ -r "$file" ]] && { + # echo >&2 "Sourcing '$file'" + source "$file" + } + unset file_base +done + +for f in "${BASH_SOURCE_LAST[@]}"; do + file="${XDG_CONFIG_HOME:-$HOME/.config}/bash/$f.bash" + [[ -r "$file" ]] && source "$file" # || echo >&2 "no '$file' found" + true +done diff --git a/bootstrap.manifest b/bootstrap.manifest new file mode 100644 index 0000000..7c83a5c --- /dev/null +++ b/bootstrap.manifest @@ -0,0 +1,19 @@ +### bootstrap.manifest -*- conf -*- +# TAB-separated SOURCEs and DESTINATIONs for configuration-file links to ~. +# see bootstrap.sh for details. + +# Custom XDG Directories (just in case they're hardcoded somewhere) +~/etc ~/.config +~/usr ~/.local + +# Bash +bash/bashrc ~/.bashrc +bash/bash_profile ~/.bash_profile +bash/bash_logout ~/.bash_logout + +# Profile +profile/profile ~/.profile + +# Local Variables: +# indent-tabs-mode: t +# End: diff --git a/bootstrap.sh b/bootstrap.sh new file mode 100755 index 0000000..d37ef6a --- /dev/null +++ b/bootstrap.sh @@ -0,0 +1,195 @@ +#!/bin/sh +# Bootstrap file for XDG_CONFIG_HOME +# by Case Duckworth + +### License: + +# Everyone is permitted to do whatever with this software, without +# limitation. This software comes without any warranty whatsoever, +# but with two pieces of advice: +# - Don't hurt yourself. +# - Make good choices. + +### Commentary: + +# For some time now, there has been a standard place to put configuration files +# for various programs: $XDG_CONFIG_HOME. Slowly but surely, programs have +# been adapting to that standard for their configuration files, but there have +# been some stubborn holdouts. Examples include shells such as sh, bash, and +# zsh. + +# Luckily, the filesystem can help us. This script takes input from a file in +# its $PWD, bootstrap.manifest, and reads linking directions from tab-delimited +# lines in that file. Then, it creates links from within the cozy confines of +# $XDG_CONFIG_HOME to the wild west of ~, so that everyone is happy: you have +# your zen garden of configuration, and misbehaving programs have their bazaar. + +### Code: + +### Main entry point + +main() { + ## Sanity checking + # Since bootstrap.sh does some naive path-mangling, let's show an error + # if it's not run correctly. Yes, there are other ways to run a + # script. But this script should ideally be run, like, one time. Also + # you can obviously comment this out or change it if you know what + # you're doing! + + case "$0" in + ./*) ;; # this is the way bootstrap.sh /should/ be run. + *) + printf >&2 'Weird invocation! %s\n' "$*" + printf >&2 'Try: cd ; ./bootstrap.sh\n' + exit 127 + ;; + esac + + ## Variables + + # option: -d/--dry-run + : "${BOOTSTRAP_ACTION:=link}" + # option: -v/--verbose + : "${BOOTSTRAP_DEBUG:=false}" + # option: -k/--keep-going + : "${BOOTSTRAP_QUIT_ON_ERROR:=true}" + # option: -m/--manifest FILE + : "${BOOTSTRAP_MANIFEST_FILE:=bootstrap.manifest}" + # option: -- (rest are passed to ln) + : "${BOOTSTRAP_LN_ARGS:=-s}" + + ## Handle command-line flags + # Basically an easier way of setting the above variables. + while [ -n "$1" ]; do + case "$1" in + -h|--help) + cat >&2 <&2 "Bad argument: '$2'" + exit 129 + ;; + esac + BOOTSTRAP_MANIFEST_FILE="$2" + shift 2 + ;; + -f|--force) + BOOTSTRAP_LN_ARGS="$BOOTSTRAP_LN_ARGS -f" + shift 1 + ;; + --) + BOOTSTRAP_LN_ARGS="$@" + break + ;; + esac + done + + ## Main loop + while IFS=' ' read -r source destination; do + # Ignore lines beginning with '#' + case "$source" in + '#'*) + if "$BOOTSTRAP_DEBUG"; then + printf >&2 'Skipping comment: %s %s\n' \ + "$source" "$destination" + fi + continue + ;; + esac + + # Ignore empty lines, or lines with only SOURCE or DESTINATION + if [ -z "$source" ] || [ -z "$destination" ]; then + if "$BOOTSTRAP_DEBUG"; then + printf >&2 'Skipping line: %s\t%s\n' \ + "$source" "$destination" + fi + continue + fi + + # Do the thing + if ! dispatch "$source" "$destination"; then + printf >&2 'ERROR: %s -> %s\n' \ + "$source" "$destination" + if "$BOOTSTRAP_QUIT_ON_ERROR"; then + exit "$dispatch_error" + fi + fi + done < "$BOOTSTRAP_MANIFEST_FILE" +} + + +### Functions + +dispatch() { # dispatch SOURCE DESTINATION + # Depending on environment variables, do the linking or displaying or + # whatever of a source and a destination. + + ## Variables + + src="$1" + dest="$2" + dispatch_error=0 # success + + ## Sanitize pathnames + + # If the SOURCE starts with ~, /, or $, keep it as-is; otherwise, + # prepend "$PWD/". + case "$src" in + '/'* | '~'* | '$'* ) ;; + *) src="$PWD/$src" ;; + esac + + # Convert ~ to $HOME in SOURCE and DESTINATION, to get around shell + # quoting rules. + src="$(printf '%s\n' "$src" | sed "s#^~#$HOME#")" + dest="$(printf '%s\n' "$dest" | sed "s#^~#$HOME#")" + + ## Do the thing + + # /Always/ tell the user what we're doing. + printf >&2 "ln %s %s %s\n" "$BOOTSTRAP_LN_ARGS" "$src" "$dest" + + case "$BOOTSTRAP_ACTION" in + link) # actually ... do the links + ln $BOOTSTRAP_LN_ARGS "$src" "$dest" || + dispatch_error="$?" + ;; + print) ;; # already printed. + esac + + return "$dispatch_error" +} + +### Do the thing + +main "$@" diff --git a/bootstrap/bash_logout b/bootstrap/bash_logout deleted file mode 100644 index d6c9b7a..0000000 --- a/bootstrap/bash_logout +++ /dev/null @@ -1,6 +0,0 @@ -# bash_logout -# vim:ft=sh - -if [[ -r "$XDG_CONFIG_HOME"/bash/logout ]]; then - source "$XDG_CONFIG_HOME"/bash/logout -fi diff --git a/bootstrap/bash_profile b/bootstrap/bash_profile deleted file mode 100644 index 4439ca7..0000000 --- a/bootstrap/bash_profile +++ /dev/null @@ -1,4 +0,0 @@ -# bash_profile - -[[ -f ~/.profile ]] && source ~/.profile -[[ -f ~/.bashrc ]] && source ~/.bashrc diff --git a/bootstrap/bashrc b/bootstrap/bashrc deleted file mode 100644 index 4966e75..0000000 --- a/bootstrap/bashrc +++ /dev/null @@ -1,41 +0,0 @@ -# bashrc - -# If not running interactively, don't do anything -[[ $- != *i* ]] && return - -BASH_SOURCE_FIRST=( - aliases - functions -) - -BASH_SOURCE_LAST=( - blesh -) - -for f in "${BASH_SOURCE_FIRST[@]}"; do - file="${XDG_CONFIG_HOME:-$HOME/.config}/bash/$f.bash" - [[ -r "$file" ]] && source "$file" # || echo >&2 "no '$file' found" -done - -for file in "$XDG_CONFIG_HOME"/bash/*.bash; do - file_base="${file##*/}" - memq "${file_base%.bash}" "${BASH_SOURCE_FIRST[@]}" && { - # echo >&2 "'$file' in BASH_SOURCE_FIRST, skipping" - continue - } - memq "${file_base%.bash}" "${BASH_SOURCE_LAST[@]}" && { - # echo >&2 "'$file' in BASH_SOURCE_LAST, skipping" - continue - } - [[ -r "$file" ]] && { - # echo >&2 "Sourcing '$file'" - source "$file" - } - unset file_base -done - -for f in "${BASH_SOURCE_LAST[@]}"; do - file="${XDG_CONFIG_HOME:-$HOME/.config}/bash/$f.bash" - [[ -r "$file" ]] && source "$file" # || echo >&2 "no '$file' found" - true -done diff --git a/bootstrap/profile b/bootstrap/profile deleted file mode 100644 index 4cb459d..0000000 --- a/bootstrap/profile +++ /dev/null @@ -1,17 +0,0 @@ -# profile -# vim:ft=sh - -# XDG directories -export XDG_CONFIG_HOME="$HOME/etc" -export XDG_CACHE_HOME="$HOME/var/cache" -export XDG_DATA_HOME="$HOME/usr/share" - -export XDG_DATA_DIRS="${XDG_DATA_DIRS:-/usr/local/share:/usr/share}" -export XDG_CONFIG_DIRS="${XDG_CONFIG_DIRS:-/etc/xdg}" - -# source files in $XDG_CONFIG_HOME/profile -if [ -d "$XDG_CONFIG_HOME/profile" ]; then - for file in "$XDG_CONFIG_HOME"/profile/*.sh; do - [ -r "$file" ] && . "$file" - done -fi diff --git a/profile/profile b/profile/profile new file mode 100644 index 0000000..7a86c76 --- /dev/null +++ b/profile/profile @@ -0,0 +1,17 @@ +# ~/.profile -*- sh -*- +# vim: ft=sh + +# XDG directories +export XDG_CONFIG_HOME="$HOME/etc" +export XDG_CACHE_HOME="$HOME/var/cache" +export XDG_DATA_HOME="$HOME/usr/share" + +export XDG_DATA_DIRS="${XDG_DATA_DIRS:-/usr/local/share:/usr/share}" +export XDG_CONFIG_DIRS="${XDG_CONFIG_DIRS:-/etc/xdg}" + +# source files in $XDG_CONFIG_HOME/profile +if [ -d "$XDG_CONFIG_HOME/profile" ]; then + for file in "$XDG_CONFIG_HOME"/profile/*.sh; do + [ -r "$file" ] && . "$file" + done +fi -- cgit 1.4.1-21-gabe81