From 027e525fa76254ef1e2e0e47b2dc34167ab903e2 Mon Sep 17 00:00:00 2001 From: Lucas F Date: Mon, 21 Nov 2022 09:57:03 -0300 Subject: [PATCH] initial --- .gitignore | 5 ++ README.md | 66 ++++++++++++++++- completion/_rock | 8 ++ help | 8 ++ install.sh | 27 +++++++ rock.zsh | 189 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 301 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 completion/_rock create mode 100644 help create mode 100644 install.sh create mode 100644 rock.zsh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d9c8e0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +cache/ +log/ + +plugins/ +installed_plugins diff --git a/README.md b/README.md index 49a3038..abea039 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,65 @@ -# rock +# šŸŽø Rock šŸ¤˜ + +A minimal zsh plugin manager. + +Inspired by: +zcomet - https://github.com/agkozak/zcomet +zap - https://github.com/zap-zsh/zap + + +## Install + +```sh +sh <(curl -s https://git.lucasf.dev/public/rock/raw/branch/master/install.sh) +``` + +## Basic plugins + +Add the following to your `.zshrc` + +```sh +# Basic Options for zsh configuration. +plug "https://git.lucasf.dev/public/zsh-basics.git" + +plug "zsh-users/zsh-autosuggestions" +plug "zsh-users/zsh-syntax-highlighting" + +# A simple prompt theme +plug "https://git.lucasf.dev/public/zsh-prompt.git" +``` + +## Commands + +- To update plugins or Rock itself: + +```sh +rock --update +``` + +- To cleanup plugins you are no longer using: + +```sh +rock --clean +``` + +- To uninstall Rock: + +```sh +rock --remove +``` + +## Notes + +By default the manager will try to source plugins that the file name is the same as the repository, with the following extensions: + + - `.plugin.zsh` + - `.zsh` + - `.zsh-theme` + + +To add some plugin that are named different from the repo name, you need to pass the file name as a parameter as in the example below: + +```sh +plug "spaceship-prompt/spaceship-prompt" spaceship.zsh +``` -A minimal zsh plugin manager. \ No newline at end of file diff --git a/completion/_rock b/completion/_rock new file mode 100644 index 0000000..a35b1ee --- /dev/null +++ b/completion/_rock @@ -0,0 +1,8 @@ +#compdef rock + +_arguments -C -s \ + "(-)"{-h,--help}"[Show help information]" \ + "(-)"{-v,--version}"[Show version information]" \ + "(-)"{-u,--update}"[Update plugins]" \ + "(-)"{-c,--clean}"[Remove unused plugins]" \ + "(-)"{-r,--remove}"[Uninstall rock]" \ diff --git a/help b/help new file mode 100644 index 0000000..57018d8 --- /dev/null +++ b/help @@ -0,0 +1,8 @@ +šŸŽø rock [options] + +OPTIONS: + -h, --help help + -v, --version version + -u, --update update plugin(s) + -c, --clean clean plugin(s) + -r, --remove uninstall rock diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..babae26 --- /dev/null +++ b/install.sh @@ -0,0 +1,27 @@ +#!/bin/sh + +main() { + local ROCKDIR="$HOME/.local/share/rock" + local ZSHRC="$HOME/.zshrc" + local CMD='[[ -f "$HOME/.local/share/rock/rock.zsh" ]] && source "$HOME/.local/share/rock/rock.zsh"' + + git clone https://git.lucasf.dev/public/rock "$ROCKDIR" > /dev/null 2>&1 + if [[ ! -d "$ROCKDIR/plugins" ]]; then + mkdir -p "$ROCKDIR/plugins" + fi + if [[ -n "$ZDOTDIR" ]] && [[ -f "$ZDOTDIR/.zshrc" ]]; then + ZSHRC="$ZDOTDIR/.zshrc" + fi + if [[ ! -f $ZSHRC || $(cat $ZSHRC | wc -l) = 0 ]];then + echo $CMD > $ZSHRC + else + if ! grep -q '[[ -f "$HOME/.local/share/rock/rock.zsh" ]] && source "$HOME/.local/share/rock/rock.zsh"' $ZSHRC; then + sed -i '1 i[[ -f "$HOME/.local/share/rock/rock.zsh" ]] && source "$HOME/.local/share/rock/rock.zsh"' $ZSHRC + fi + fi +} + +main +if [[ $? -eq 0 ]];then echo "šŸŽø You Rock Yeah!! šŸ¤˜"; fi + +# vim: ft=zsh:ts=4:sts=4:sw=4 diff --git a/rock.zsh b/rock.zsh new file mode 100644 index 0000000..00dcc14 --- /dev/null +++ b/rock.zsh @@ -0,0 +1,189 @@ +#!/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