dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles
Log | Files | Refs | README | LICENSE Back to index

commit 936b80cff9f92c8f143283e675512a6c67085e83
parent 59fc89f577226f8f663aa16be5cb72c9ed3dc9de
Author: Anders Damsgaard <anders@adamsgaard.dk>
Date:   Thu, 14 Mar 2019 14:57:24 +0100

Add fast and simple shell plugin manager `fffs`

Diffstat:
Alinks/bin/fffs | 149+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 149 insertions(+), 0 deletions(-)

diff --git a/links/bin/fffs b/links/bin/fffs @@ -0,0 +1,149 @@ +#!/usr/bin/env bash + +set -e +#set -v + +version='0.1.0' + +confsubdir=".config/fffs" +confdir="$HOME/$confsubdir" +plugindir="$confdir/lib" +pluginfile="$confdir/plugins" + +function show_help { + echo "${0##*/} is a fast and simple manager for shell plugins" + echo "usage: ${0##*/} [OPTIONS] SHELL OPERATION" + echo "where SHELL can be 'bash' or 'zsh'." + echo "Valid COMMANDS are:" + echo " init clone repositories specified in pluginfile" + echo " clean remove all local plugin content" + echo " update update all local plugin content" + echo "OPTIONS are one or more of the following:" + echo " -h, --help show this message" + echo " -v, --version show version and license information" + echo "Set the plugin sources in $pluginfile-zsh or $pluginfile-bash" + echo + echo "Add the generated sources file in the shell-rc file. For ~/.zshrc:" + echo " if [ -f \"\$HOME/.config/fffs/lib/zsh/sources\" ]; then" + echo " . \"\$HOME/.config/fffs/lib/zsh/sources\"" + echo " else" + echo " fffs zsh init" + echo " . \"\$HOME/.config/fffs/lib/zsh/sources\"" + echo " fi" +} + +function show_version { + echo "${0##*/} version $version" + echo "Licensed under the GNU Public License, v3+" + echo "written by Anders Damsgaard, anders@adamsgaard.dk" + echo "https://gitlab.com/admesg/fffs" +} + +function die { + printf '%s\n' "$1" >&2 + exit 1 +} + +function clone_repositories { + if [[ ! -f "$pluginfile-$shell" || + $(wc -l <"$pluginfile-$shell") -lt 1 ]]; then + (>&2 echo "No plugins specified in $pluginfile-$shell" ) + fi + + # loop over all repositories in $configfile, and clone them if they do not + # exist in $plugindir + while read -r line; do + target="$plugindir/$shell/${line##*/}" + if [[ -d "$target" ]]; then + echo "$target already exists, skipping." + else + echo "$line > $target" + git clone "$line" "$target" + fi + done < "$pluginfile-$shell" +} + +function update_repositories { + [[ ! -d "$plugindir/$shell" ]] && return + + for dir in "$plugindir/$shell"/*/; do + echo "Updating $dir..." + (cd "$dir" && git pull) + done + init_sources +} + +function check_if_source_in_shellrc { + if ! grep -r "$confsubdir/lib/$shell/sources" "$HOME/.${shell}rc" \ + >/dev/null 2>&1; then + echo + echo "Make sure to source the following line in your ~/.${shell}rc:" + echo " $plugindir/$shell/sources" + fi +} + +function init_sources { + echo "Generating source file $plugindir/$shell/sources" + echo "#!/usr/bin/env $shell" > "$plugindir/$shell/sources" + find "$plugindir/$shell/" -maxdepth 2 -type f -iname '*.zsh' \ + | sed 's/^/. /' >> "$plugindir/$shell/sources" + + check_if_source_in_shellrc +} + +function init { + clone_repositories + init_sources +} + +function clean { + echo rm -rf "$plugindir" + rm -rf "$plugindir" +} + + +## Parse command-line arguments + +[[ $# -lt 1 ]] && (show_help && exit 1) + +shell="" +while :; do + case "$1" in + zsh) + shell='zsh' + ;; + bash) + shell='bash' + ;; + init) + [[ "$shell" == "" ]] && die 'Error: No SHELL specified' + (init && exit 0) + ;; + update) + [[ "$shell" == "" ]] && die 'Error: No SHELL specified' + (update_repositories && exit 0) + ;; + upgrade) + die 'Did you mean "update"?' + ;; + clean) + [[ "$shell" == "" ]] && die 'Error: No SHELL specified' + (clean && exit 0) + ;; + clear) + die 'Did you mean "clean"?' + ;; + -h|-\?|--help) + (show_help && exit 0) + ;; + -v|--version) + (show_version && exit 0) + ;; + -?*) + die 'Error: Unknown option specified' + ;; + *) # No more options + break + esac + shift +done