#!/bin/sh # Rock Zsh Plugin Manager - inspired by zcomet & zap # shellcheck disable=SC1090 export ROCK_DIR="$HOME/.local/share/rock" export ROCK_PLUGIN_DIR="$ROCK_DIR/plugins" export ROCK_ACTIVE_PLUGINS="$ROCK_DIR/installed_plugins" export ROCK_ZSHRC="$ZDOTDIR/.zshrc" export ZSH_CACHE_DIR="$ROCK_DIR/cache/" plugin_name="" if [ -z "$ZDOTDIR" ]; then export ROCK_ZSHRC="$HOME/.zshrc" # ~/.zshrc fi fpath=("$ROCK_DIR/completion" $fpath) autoload -Uz add-zsh-hook \ _rock_{help,version,update,clean,remove} rm -rf "$ROCK_ACTIVE_PLUGINS" _try_source() { [[ -f "$1" ]] && source "$1" } _get_plugin_name() { plugin_name=$(echo "$1" | awk -F / '{print $NF}') plugin_name="${plugin_name/.git/}" } _try_pull() { echo "ā™Ŗ $1" git pull > /dev/null 2>&1 if [ $? -ne 0 ]; then echo "Failed to update $1" && return 1; fi echo -e "\e[1A\e[KšŸŽ¶ $1" } _rock_help() { cat "$ROCK_DIR/help" } _rock_version() { echo "šŸŽø Rock Version $(cat "$ROCK_DIR/.git/packed-refs" | grep tags | tail -1 | awk -F / '{print $NF}')" } _rock_update() { local plugins="" if [[ -f "$ROCK_ACTIVE_PLUGINS" ]];then plugins=$(cat "$ROCK_ACTIVE_PLUGINS" | awk 'BEGIN { FS = "\n" } { print " " int((NR)) echo " ā™Ŗ " $1 }') fi echo "šŸŽ¼ Please select an option to update \n" echo -e " 0 šŸŽø Rock" echo "$plugins \n" echo -n "šŸŽ¼ Plugin Number(s) | (a) All Plugins | (0) šŸŽø update Rock: " read plugin pwd=$(pwd) echo "" if [[ $plugin == "a" ]]; then cd "$ROCK_PLUGIN_DIR" for plug in *; do cd $plug _try_pull $plug cd "$ROCK_PLUGIN_DIR" done cd $pwd > /dev/null 2>&1 elif [[ $plugin == "0" ]]; then cd "$ROCK_DIR" _try_pull 'rock' cd $pwd else selected_number=($(echo $plugin | awk -F '[[:space:]]' '{print $0}')) sorted=($(printf '%s\n' "${selected_number[@]}"|sort -u)) for item in $sorted;do for plug in $plugins; do selected=$(echo $plug | grep -E "^ ${item##*( )}" | awk -F / '{print $NF}') selected="${selected/.git/}" if [[ -n $selected ]]; then if [[ -d "$ROCK_PLUGIN_DIR/$selected" ]];then cd "$ROCK_PLUGIN_DIR/$selected" _try_pull $selected cd - > /dev/null 2>&1 fi else echo "šŸ“£ ${item##*( )} is not a valid option!" fi done done fi if [[ $ROCK_CLEAN_ON_UPDATE == true ]]; then _rock_clean fi } _rock_clean() { unused_plugins=() for i in "$ROCK_PLUGIN_DIR"/*; do local plugin_name=$(basename "$i") if ! grep -q "$plugin_name" "$ROCK_ACTIVE_PLUGINS"; then unused_plugins+=("$plugin_name") fi done if [ ${#unused_plugins[@]} -eq 0 ]; then echo "šŸ“£ Nothing to clean" else for p in ${unused_plugins[@]}; do echo -n "Remove: $p? (n/y): " read answer if [[ $answer == "y" || $answer == "Y" ]]; then rm -rf "$ROCK_PLUGIN_DIR/$p" echo "removed: $p" fi done fi } _rock_remove() { echo -n "šŸ”‡ Removing Rock, are you sure? (n/y):" read answer if [[ $answer == "y" || $answer == "Y" ]];then cd "$HOME" rm -rf "$ROCK_DIR" if [[ -f $ROCK_ZSHRC ]];then sed -i '/source "$HOME\/.local\/share\/rock\/rock.zsh"/d' $ROCK_ZSHRC sed -i '/^plug /d' $ROCK_ZSHRC source $ROCK_ZSHRC fi echo "šŸ‘‹ Bye Bye" else echo "Great choice! šŸŽµšŸŽ§šŸŽ­ šŸŽ¶" fi } plug() { local plugin="$1" if [[ -f "$plugin" ]];then _try_source $plugin else local full_plugin_name="$1" _get_plugin_name $plugin local plugin_nick=$(echo $* | awk -F "[[:space:]]" '/.zsh$/{print $NF}') local plugin_dir="$ROCK_PLUGIN_DIR/$plugin_name" if [[ ! -d "$plugin_dir" ]];then echo "ā™Ŗ $plugin_name" git clone $plugin --depth 1 "$plugin_dir" > /dev/null 2>&1 if [ $? -ne 0 ]; then git clone "https://github.com/$plugin.git" --depth 1 "$plugin_dir" > /dev/null 2>&1 if [ $? -ne 0 ]; then echo "Failed to clone $plugin_name" && return 1; fi fi if [ -n "$git_ref" ]; then git -C "$plugin_dir" checkout "$git_ref" > /dev/null 2>&1 if [ $? -ne 0 ]; then echo "Failed to checkout $git_ref" && return 1; fi fi echo -e "\e[1A\e[KšŸŽ¶ $plugin_name" fi _try_source "$plugin_dir/$plugin_name.plugin.zsh" _try_source "$plugin_dir/$plugin_name.zsh" _try_source "$plugin_dir/$plugin_name.zsh-theme" if [[ -n "$plugin_nick" ]];then _try_source "$plugin_dir/$plugin_nick"; fi fi if [[ -n $full_plugin_name ]]; then echo "$full_plugin_name" >> "$ROCK_DIR/installed_plugins" fi } typeset -A opts opts=( -h "_rock_help" --help "_rock_help" -u "_rock_update" --update "_rock_update" -c "_rock_clean" --clean "_rock_clean" -v "_rock_version" --version "_rock_version" -r "_rock_remove" --remove "_rock_remove" ) rock() { emulate -L zsh if [[ -z "$opts[$1]" ]]; then _rock_help return 1 fi opt="${opts[$1]}" $opt } # vim: ft=bash sw=4 ts=4 et