From 49d4296eaf353e79a144eec03bef0a2631c541d0 Mon Sep 17 00:00:00 2001 From: Armaan Bhojwani <3fb650a9-b47e-4604-a282-1dd91953b2ee@anonaddy.me> Date: Fri, 13 Nov 2020 15:18:56 -0500 Subject: [PATCH] Redesign clean command refactor lots of code, and make the zpe-clean command much more user-friendly --- README.md | 15 +++++++++-- zpe.zsh | 74 +++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 60 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index b783580..7eebd1c 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,19 @@ # zpe -A utility to ease the management of zsh plugins. This is NOT a complete zsh plugin manager, I reccomend [znap](https://github.com/marlonrichert/zsh-snap/) if you are looking for one. This script just takes the edge off of manually managing them, if you don't want to deal with one of the full-fledged plugin managers. +A utility to ease the management of Zsh plugins. This is NOT a complete Zsh plugin manager, I reccomend [znap](https://github.com/marlonrichert/zsh-snap/) if you are looking for one. This script just takes the edge off of manually managing them, if you don't want to deal with one of the full-fledged plugin managers. ## Usage -Just put a list of git repos (full url, either ssh or http) in `~/.config/zpe/repositories`, and then put `source /path/to/zpe.zsh` in your zshrc. Install plugins using `zpe-clone`, update them using `zpe-pull` and remove unused ones using `zpe-clean`. You must manually source the plugins in your zshrc, however this is made easier using the `zpe-source` command. For each plugin, locate the source-able file (usually contains the word init, or has the .plugin.zsh extension) and add it to your zshrc using the following syntax: `zpe-source /`. Make sure that this goes after sourcing the `zpe.zsh` script. + 1. Source the script in your `zshrc` + 2. Add a list of the plugin repositories in `~/.config/zpe/repositories` + * Use the full URL, either SSH or HTTP + * Comments can be added with a # + 3. Run `zpe-clone` to pull the repositories + 4. Source the plugin in your zshrc using the `zpe-source` command. + * For each plugin, locate the source-able file (usually contains the word init, or has the .plugin.zsh extension) + * Add it to your zshrc using the following syntax: `zpe-source /`. + * Make sure that this goes after sourcing the `zpe.zsh` script. + * Extra commads + * `zpe-pull` updates the repositories + * `zpe-clean` removes any cloned repositories that are no longer in `repositories` file ## Motivation All of the other available plugin managers try to get way to fancy. I just need something to automatically clone git repositories, and make my system nice and portable. I set myself a code limit of 100 sloc to try and stop feature creep. The script is intentionally very extensible and feature-minimal. diff --git a/zpe.zsh b/zpe.zsh index b6c9cad..eb31f0e 100755 --- a/zpe.zsh +++ b/zpe.zsh @@ -1,50 +1,70 @@ #!/usr/bin/env zsh -# The ZPE plugin helper. Copyright 2020 Armaan Bhojwani, MIT License +# The ZPE Zsh plugin helper. Copyright 2020 Armaan Bhojwani, MIT License -config_dir=$HOME/.config/zpe/ -plugin_dir=$HOME/.config/zpe/plugins/ +# Set defaults +zpe_config_dir="${HOME}/.config/zpe/" +zpe_plugin_dir="${zpe_config_dir}/plugins/" +zpe_tmp_dir="/tmp/zpe/" zpe-meta-pre() { - [[ -d "$config_dir" ]] || mkdir -p "$config_dir" - touch "$config_dir"repositories - [[ -d "$plugin_dir" ]] || mkdir -p "$plugin_dir" + # Create necesary directories, check dependencies + [[ -d "${zpe_config_dir}" ]] || mkdir -p "${zpe_config_dir}" + [[ -d "${zpe_plugin_dir}" ]] || mkdir -p "${zpe_plugin_dir}" + [[ -d /tmp/zpe ]] || mkdir ${zpe_tmp_dir} + touch "${zpe_config_dir}"repositories [[ -x git ]] && echo "please install git" - [[ -x find ]] && echo "please install find" - [[ -x diff ]] && echo "please install diff" } zpe-clone() { + # Clone all the repos in the config file recursively into the plugin directory zpe-meta-pre - while read line; do - git -C "$plugin_dir" clone $line --depth=1 2> /dev/null - done < "${config_dir}repositories" + cat ${zpe_config_dir}repositories | xargs -P10 -I{} git -C ${zpe_plugin_dir} clone {} echo "all plugins are downloaded" } zpe-pull() { + # Recursively pull zpe-meta-pre - local find_dirs=$(find "$plugin_dir" -name ".git" -type d) + local find_dirs=$(find "${zpe_plugin_dir}" -name ".git" -type d) echo $find_dirs | xargs -P10 -I{} git --git-dir={} config pull.ff only - echo $find_dirs | xargs -P10 -I{} git --git-dir={} pull > /dev/null + echo $find_dirs | xargs -P10 -I{} git --git-dir={} pull > /dev/null echo "all plugins are up to date" } zpe-clean() { - [[ -d /tmp/zpe ]] || mkdir /tmp/zpe/ - find $plugin_dir -maxdepth 1 -type d -exec git -C {} config --get remote.origin.url \; > /tmp/zpe/installed-urls - comm -23 <(sort /tmp/zpe/installed-urls) <(sort "$config_dir"repositories) >> /tmp/zpe/diff - - for d in "$plugin_dir"*; do - while read line; do - grep -l $line "$d"/.git/config >> /tmp/zpe/deletable - done < /tmp/zpe/diff - done - - while read line2; do - xargs rm -rf <<< $(echo $line2 | sed 's/\/.git\/config//g') - done < /tmp/zpe/deletable + # Remove cloned plugins not specified in the config file + zpe-meta-pre + echo -n "" > "${zpe_tmp_dir}"deletable + echo -n "" > "${zpe_tmp_dir}"diff + + find ${zpe_plugin_dir} -maxdepth 1 -type d -exec git -C {} config --get remote.origin.url \; > "${zpe_tmp_dir}"installed-urls + comm -23 <(sort ${zpe_tmp_dir}installed-urls) <(sort "${zpe_config_dir}"repositories) >> "${zpe_tmp_dir}"/diff + + while read line; do + grep -l $line "$zpe_plugin_dir"/*/.git/config >> "${zpe_tmp_dir}"deletable + done < ${zpe_tmp_dir}diff + + sed -i -e 's/\/.git\/config//g' -e 's/\/\//\//g' "${zpe_tmp_dir}"deletable + + if [[ $(wc -l "${zpe_tmp_dir}"deletable | cut -d ' ' -f 1) > 0 ]]; then + cat "${zpe_tmp_dir}"deletable + echo "Delete these directories? [y/N]: " + read -rs -k 1 ans + case "${ans}" in + y|Y) + while read line2; do + xargs rm -rf <<< $(echo $line2) + done < ${zpe_tmp_dir}deletable + echo "Cleaned!" + ;; + *|$'\n') + echo "Aborted" + esac + else + echo "Nothing to clean" + fi } zpe-source() { - source "$plugin_dir"/$1 + source "${zpe_plugin_dir}"/$1 } -- 2.39.2