]> git.armaanb.net Git - zpe.git/blob - zpe.zsh
Redesign clean command
[zpe.git] / zpe.zsh
1 #!/usr/bin/env zsh
2 # The ZPE Zsh plugin helper. Copyright 2020 Armaan Bhojwani, MIT License
3
4 # Set defaults
5 zpe_config_dir="${HOME}/.config/zpe/"
6 zpe_plugin_dir="${zpe_config_dir}/plugins/"
7 zpe_tmp_dir="/tmp/zpe/"
8
9 zpe-meta-pre() {
10   # Create necesary directories, check dependencies
11   [[ -d "${zpe_config_dir}" ]] || mkdir -p "${zpe_config_dir}"
12   [[ -d "${zpe_plugin_dir}" ]] || mkdir -p "${zpe_plugin_dir}"
13   [[ -d /tmp/zpe ]] || mkdir ${zpe_tmp_dir}
14   touch "${zpe_config_dir}"repositories
15   [[ -x git ]] && echo "please install git"
16 }
17
18 zpe-clone() {
19   # Clone all the repos in the config file recursively into the plugin directory
20   zpe-meta-pre
21   cat ${zpe_config_dir}repositories | xargs -P10 -I{} git -C ${zpe_plugin_dir} clone {}
22   echo "all plugins are downloaded"
23 }
24
25 zpe-pull() {
26   # Recursively pull
27   zpe-meta-pre
28   local find_dirs=$(find "${zpe_plugin_dir}" -name ".git" -type d)
29   echo $find_dirs | xargs -P10 -I{} git --git-dir={} config pull.ff only
30   echo $find_dirs | xargs  -P10 -I{} git --git-dir={} pull > /dev/null
31   echo "all plugins are up to date"
32 }
33
34 zpe-clean() {
35   # Remove cloned plugins not specified in the config file
36   zpe-meta-pre
37   echo -n "" > "${zpe_tmp_dir}"deletable
38   echo -n "" > "${zpe_tmp_dir}"diff
39
40   find ${zpe_plugin_dir} -maxdepth 1 -type d -exec git -C {} config --get remote.origin.url \; > "${zpe_tmp_dir}"installed-urls
41   comm -23 <(sort ${zpe_tmp_dir}installed-urls) <(sort "${zpe_config_dir}"repositories) >> "${zpe_tmp_dir}"/diff
42
43   while read line; do
44     grep -l $line "$zpe_plugin_dir"/*/.git/config >> "${zpe_tmp_dir}"deletable
45   done < ${zpe_tmp_dir}diff
46
47   sed -i -e 's/\/.git\/config//g' -e 's/\/\//\//g' "${zpe_tmp_dir}"deletable
48
49   if [[ $(wc -l "${zpe_tmp_dir}"deletable | cut -d ' ' -f 1) > 0 ]]; then
50     cat "${zpe_tmp_dir}"deletable
51     echo "Delete these directories? [y/N]: "
52     read -rs -k 1 ans
53     case "${ans}" in
54       y|Y)
55         while read line2; do
56           xargs rm -rf <<< $(echo $line2)
57         done < ${zpe_tmp_dir}deletable
58         echo "Cleaned!"
59         ;;
60       *|$'\n')
61         echo "Aborted"
62     esac
63   else
64     echo "Nothing to clean"
65   fi
66 }
67
68 zpe-source() {
69   source "${zpe_plugin_dir}"/$1
70 }