From: Armaan Bhojwani Date: Mon, 8 Feb 2021 14:39:23 +0000 (-0500) Subject: Cleanup code X-Git-Url: https://git.armaanb.net/?p=zpe.git;a=commitdiff_plain;h=7a14b6808c5a9fbf7fa4eb1de0b1230d71e3ef56 Cleanup code --- diff --git a/README.md b/README.md index 8f7378d..277acaf 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ A utility to ease the management of Zsh plugins. This is NOT a complete Zsh plug 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. + 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.sh` script. ### Extra commands * `zpe-pull` updates the repositories diff --git a/zpe.sh b/zpe.sh new file mode 100755 index 0000000..dc8cd00 --- /dev/null +++ b/zpe.sh @@ -0,0 +1,79 @@ +#!/usr/bin/env bash +# ZPE Zsh plugin helper +# Copyright 2021 Armaan Bhojwani, MIT License + +# Set defaults +zpe_config_dir="${HOME}/.config/zpe/" +zpe_plugin_dir="${zpe_config_dir}/plugins/" +zpe_tmp_dir="/tmp/zpe/" +zpe_log_dir="${zpe_tmp_dir}/logs" + +zpe-meta-pre() { + # Create necesary directories, check dependencies + [[ -d "${zpe_config_dir}" ]] || mkdir -p "${zpe_config_dir}" + [[ -d "${zpe_plugin_dir}" ]] || mkdir -p "${zpe_plugin_dir}" + [[ -d "${zpe_tmp_dir}" ]] || mkdir ${zpe_tmp_dir} + [[ -d "${zpe_log_dir}" ]] || mkdir ${zpe_log_dir} + touch "${zpe_config_dir}"repositories + [[ -x git ]] && echo "Please install git" +} + +zpe-clone() { + # Clone all the repos in the config file recursively into the plugin directory + zpe-meta-pre + cat ${zpe_config_dir}repositories | xargs -P10 -I{} git -C ${zpe_plugin_dir} \ + clone {} -q --depth 1 &> "${zpe_log_dir}"/$(date -Iseconds) + echo "All plugins are downloaded" +} + +zpe-pull() { + # Recursively pull + zpe-meta-pre + 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={} --work-tree={} pull &> \ + "${zpe_log_dir}"/$(date -Iseconds) + echo "All plugins are up to date" +} + +zpe-clean() { + # Remove cloned plugins not specified in the config file + zpe-meta-pre + 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 + + echo -n "" > "${zpe_tmp_dir}"deletable + 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!" + ;; + *) + echo "Aborted" + esac + else + echo "Nothing to clean" + fi +} + +zpe-source() { + source "${zpe_plugin_dir}"/$1 +} + +zpe() { + ${EDITOR} ${zpe_config_dir}repositories +} diff --git a/zpe.zsh b/zpe.zsh deleted file mode 100755 index a3a25a3..0000000 --- a/zpe.zsh +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/env zsh -# The ZPE Zsh plugin helper. Copyright 2020 Armaan Bhojwani, MIT License - -# Set defaults -zpe_config_dir="${HOME}/.config/zpe/" -zpe_plugin_dir="${zpe_config_dir}/plugins/" -zpe_tmp_dir="/tmp/zpe/" -zpe_log_dir="/tmp/zpe/logs" - -zpe-meta-pre() { - # Create necesary directories, check dependencies - [[ -d "${zpe_config_dir}" ]] || mkdir -p "${zpe_config_dir}" - [[ -d "${zpe_plugin_dir}" ]] || mkdir -p "${zpe_plugin_dir}" - [[ -d "${zpe_tmp_dir}" ]] || mkdir ${zpe_tmp_dir} - [[ -d "${zpe_log_dir}" ]] || mkdir ${zpe_log_dir} - touch "${zpe_config_dir}"repositories - [[ -x git ]] && echo "Please install git" -} - -zpe-clone() { - # Clone all the repos in the config file recursively into the plugin directory - zpe-meta-pre - cat ${zpe_config_dir}repositories | xargs -P10 -I{} git -C ${zpe_plugin_dir} clone {} -q --depth 1 &> "${zpe_log_dir}"/$(date -Iseconds) - echo "All plugins are downloaded" -} - -zpe-pull() { - # Recursively pull - zpe-meta-pre - 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={} --work-tree={} pull &> "${zpe_log_dir}"/$(date -Iseconds) - echo "All plugins are up to date" -} - -zpe-clean() { - # Remove cloned plugins not specified in the config file - zpe-meta-pre - 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 - - echo -n "" > "${zpe_tmp_dir}"deletable - 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 "${zpe_plugin_dir}"/$1 -} - -zpe() { - ${EDITOR} ${zpe_config_dir}repositories -}