Skip to content

Commit

Permalink
feat: plugins will be cleaned up after update, removed activate, deac…
Browse files Browse the repository at this point in the history
…tivate, remove (#61)
  • Loading branch information
ChristianChiarulli authored Nov 13, 2022
1 parent ddb7cd3 commit 95c9f79
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 71 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
plugins/
installed_plugins
4 changes: 1 addition & 3 deletions doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ OPTIONS:
-h, --help help
-v, --version version
-u, --update update plugin(s)
-a, --activate activate plugin(s)
-d, --deactivate deactivate plugin(s)
-r, --remove remove plugin(s)
-c, --clean clean plugin(s)
97 changes: 29 additions & 68 deletions zap.zsh
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/bin/sh
# shellcheck disable=SC1090

rm -rf "$HOME/.local/share/zap/installed_plugins"
export ZAP_DIR="$HOME/.local/share/zap"
export ZAP_PLUGIN_DIR="$ZAP_DIR/plugins"
if [ -z "$ZDOTDIR" ]; then
export ZAP_ZSHRC=$HOME/.zshrc # ~/.zshrc
export ZAP_ZSHRC="$HOME/.zshrc" # ~/.zshrc
else
export ZAP_ZSHRC=$ZDOTDIR/.zshrc
export ZAP_ZSHRC="$ZDOTDIR/.zshrc"
fi

_try_source() {
Expand Down Expand Up @@ -38,6 +39,9 @@ plug() {
_try_source "$plugin_dir/$plugin_name.zsh"
_try_source "$plugin_dir/$plugin_name.zsh-theme"
fi
if [[ -n $full_plugin_name ]]; then
echo "$full_plugin_name" >> "$HOME/.local/share/zap/installed_plugins"
fi
}

_pull() {
Expand All @@ -47,8 +51,26 @@ _pull() {
echo -e "\e[1A\e[K⚡$1"
}

_zap_clean() {
unused_plugins=()
for i in "$HOME"/.local/share/zap/plugins/*; do
local plugin_name=$(basename "$i")
if ! grep -q "$plugin_name" "$HOME/.local/share/zap/installed_plugins"; then
unused_plugins+=("$plugin_name")
fi
done
for p in ${unused_plugins[@]}; do
echo -n "Remove: $p? (y/n): "
read answer
if [[ $answer == "y" ]]; then
rm -rf "$HOME/.local/share/zap/plugins/$p"
echo "removed: $p"
fi
done
}

_zap_update() {
plugins=$(awk 'BEGIN { FS = "[ plug]" } { print }' $ZAP_ZSHRC | grep -E '^plug "[Aa0-Zz9]' | awk 'BEGIN { FS = "[ \"]" } { print " " int((NR)) echo " 🔌 " $3 }')
plugins=$(cat "$HOME/.local/share/zap/installed_plugins" | awk 'BEGIN { FS = "\n" } { print " " int((NR)) echo " 🔌 " $1 }')
echo -e " 0 ⚡ Zap"
echo "$plugins \n"
echo -n "🔌 Plugin Number | (a) All Plugins | (0) ⚡ Zap Itself: "
Expand All @@ -75,65 +97,8 @@ _zap_update() {
cd - > /dev/null 2>&1
done
fi
}

_zap_remove() {
plugins=$(awk 'BEGIN { FS = "[ plug]" } { print }' $ZAP_ZSHRC | grep -E '^plug "[Aa0-Zz9]' | awk 'BEGIN { FS = "[ \"]" } { print " " int((NR)) echo " 🔌 " $3 }')
if [ -z $plugins ]; then
echo "There are no plugins"
return 0
fi
echo "$plugins \n"
echo -n "🔌 Plugin Number: "
read plugin
pwd=$(pwd)
for plug in $plugins; do
usr=$(echo $plug | grep -E "^ $plugin" | awk 'BEGIN { FS = "[ /]" } { print $5 }')
plg=$(echo $plug | grep -E "^ $plugin" | awk 'BEGIN { FS = "[ /]" } { print $6 }')
sed -i'.backup' --follow-symlinks "/$usr\/$plg/s/^/#\ /g" $ZAP_ZSHRC
rm -rf $ZAP_PLUGIN_DIR/$plg && echo "Removed $usr's $plg plugin" || echo "Failed to Remove $usr's $plg plugin \n"
done
}

_zap_deactivate() {
plugins=$(awk 'BEGIN { FS = "[ plug]" } { print }' $ZAP_ZSHRC | grep -E '^plug "[Aa0-Zz9]' | awk 'BEGIN { FS = "[ \"]" } { print " " int((NR)) echo " 🔌 " $3 }')
if [ -z $plugins ]; then
echo "There is no plugin to deactivate"
return 0
fi
echo "$plugins \n"
echo -n "🔌 Plugin Number | (a) All Plugins: "
read plugin
echo ""
if [[ $plugin == "a" ]]; then
sed -i'.backup' --follow-symlinks '/^plug/s/^/#\ /g' $ZAP_ZSHRC && echo "Deactivated all active plugins" || echo "Failed to Deactivate all active plugins \n"
else
for plug in $plugins; do
usr=$(echo $plug | grep -E "^ $plugin" | awk 'BEGIN { FS = "[ /]" } { print $5 }')
plg=$(echo $plug | grep -E "^ $plugin" | awk 'BEGIN { FS = "[ /]" } { print $6 }')
sed -i'.backup' --follow-symlinks "/$usr\/$plg/s/^/#\ /g" $ZAP_ZSHRC && echo "Deactivated $usr's $plg plugin" || echo "Failed to Deactivate $usr's $plg plugin \n"
done
fi
}

_zap_activate() {
plugins=$(awk 'BEGIN { FS = "[ plug]" } { print }' $ZAP_ZSHRC | grep -E '^# plug "[Aa0-Zz9]' | awk 'BEGIN { FS = "[ \"]" } { print " " int((NR)) echo " 🔌 " $4 }')
if [ -z $plugins ]; then
echo "All plugins are active"
return 0
fi
echo "$plugins \n"
echo -n "🔌 Plugin Number | (a) Plug All: "
read plugin
echo ""
if [[ $plugin == "a" ]]; then
sed -i'.backup' --follow-symlinks '/^#\ plug/s/^#\ //g' $ZAP_ZSHRC && echo "Activated all inactive plugins" || echo "Failed to Activate all inactive plugins \n"
else
for plug in $plugins; do
usr=$(echo $plug | grep -E "^ $plugin" | awk 'BEGIN { FS = "[ /]" } { print $5 }')
plg=$(echo $plug | grep -E "^ $plugin" | awk 'BEGIN { FS = "[ /]" } { print $6 }')
sed -i'.backup' --follow-symlinks "/$usr\/$plg/s/^#\ //g" $ZAP_ZSHRC && echo "Activated $usr's $plg plugin" || echo "Failed to Activate $usr's $plg plugin \n"
done
if [[ $ZAP_CLEAN_ON_UPDATE == true ]]; then
_zap_clean
fi
}

Expand All @@ -150,16 +115,12 @@ _zap_version() {

typeset -A opts
opts=(
-a "_zap_activate"
--activate "_zap_activate"
-d "_zap_deactivate"
--deactivate "_zap_deactivate"
-h "_zap_help"
--help "_zap_help"
-r "_zap_remove"
--remove "_zap_remove"
-u "_zap_update"
--update "_zap_update"
-c "_zap_clean"
--clean "_zap_clean"
-v "_zap_version"
--version "_zap_version"
)
Expand Down

0 comments on commit 95c9f79

Please sign in to comment.