Commit 0b32bec279b0a78a01729cb848e7bd2921f4bb13
1 parent
575a537db9
bundle-cleanup command now works.
The bundle-cleanup, now cleans up unused clones of repositories, to which no plugin is referring to.
Showing 1 changed file with 48 additions and 36 deletions Inline Diff
antigen.zsh
1 | #!/bin/zsh | 1 | #!/bin/zsh |
2 | 2 | ||
3 | # Each line in this string has the following entries separated by a space | 3 | # Each line in this string has the following entries separated by a space |
4 | # character. | 4 | # character. |
5 | # <repo-url>, <plugin-location>, <bundle-type> | 5 | # <repo-url>, <plugin-location>, <bundle-type> |
6 | # FIXME: Is not kept local by zsh! | 6 | # FIXME: Is not kept local by zsh! |
7 | local _ANTIGEN_BUNDLE_RECORD="" | 7 | local _ANTIGEN_BUNDLE_RECORD="" |
8 | 8 | ||
9 | # Syntaxes | 9 | # Syntaxes |
10 | # bundle <url> [<loc>=/] | 10 | # bundle <url> [<loc>=/] |
11 | bundle () { | 11 | bundle () { |
12 | 12 | ||
13 | # Bundle spec arguments' default values. | 13 | # Bundle spec arguments' default values. |
14 | local url="$ANTIGEN_DEFAULT_REPO_URL" | 14 | local url="$ANTIGEN_DEFAULT_REPO_URL" |
15 | local loc=/ | 15 | local loc=/ |
16 | local btype=plugin | 16 | local btype=plugin |
17 | 17 | ||
18 | # Set spec values based on the positional arguments. | 18 | # Set spec values based on the positional arguments. |
19 | local position_args='url loc' | 19 | local position_args='url loc' |
20 | local i=1 | 20 | local i=1 |
21 | while ! [[ -z $1 || $1 == --*=* ]]; do | 21 | while ! [[ -z $1 || $1 == --*=* ]]; do |
22 | local arg_name="$(echo "$position_args" | cut -d\ -f$i)" | 22 | local arg_name="$(echo "$position_args" | cut -d\ -f$i)" |
23 | local arg_value="$1" | 23 | local arg_value="$1" |
24 | eval "local $arg_name='$arg_value'" | 24 | eval "local $arg_name='$arg_value'" |
25 | shift | 25 | shift |
26 | i=$(($i + 1)) | 26 | i=$(($i + 1)) |
27 | done | 27 | done |
28 | 28 | ||
29 | # Check if url is just the plugin name. Super short syntax. | 29 | # Check if url is just the plugin name. Super short syntax. |
30 | if [[ "$url" != */* ]]; then | 30 | if [[ "$url" != */* ]]; then |
31 | loc="plugins/$url" | 31 | loc="plugins/$url" |
32 | url="$ANTIGEN_DEFAULT_REPO_URL" | 32 | url="$ANTIGEN_DEFAULT_REPO_URL" |
33 | fi | 33 | fi |
34 | 34 | ||
35 | # Set spec values from keyword arguments, if any. The remaining arguments | 35 | # Set spec values from keyword arguments, if any. The remaining arguments |
36 | # are all assumed to be keyword arguments. | 36 | # are all assumed to be keyword arguments. |
37 | while [[ $1 == --*=* ]]; do | 37 | while [[ $1 == --*=* ]]; do |
38 | local arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')" | 38 | local arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')" |
39 | local arg_value="$(echo "$1" | cut -d= -f2)" | 39 | local arg_value="$(echo "$1" | cut -d= -f2)" |
40 | eval "local $arg_name='$arg_value'" | 40 | eval "local $arg_name='$arg_value'" |
41 | shift | 41 | shift |
42 | done | 42 | done |
43 | 43 | ||
44 | # Resolve the url. | 44 | # Resolve the url. |
45 | if [[ $url != git://* && $url != https://* ]]; then | 45 | if [[ $url != git://* && $url != https://* ]]; then |
46 | url="${url%.git}" | 46 | url="${url%.git}" |
47 | url="https://github.com/$url.git" | 47 | url="https://github.com/$url.git" |
48 | fi | 48 | fi |
49 | 49 | ||
50 | # Add it to the record. | 50 | # Add it to the record. |
51 | _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype" | 51 | _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD\n$url $loc $btype" |
52 | 52 | ||
53 | -antigen-ensure-repo "$url" | 53 | -antigen-ensure-repo "$url" |
54 | 54 | ||
55 | bundle-load "$url" "$loc" "$btype" | 55 | bundle-load "$url" "$loc" "$btype" |
56 | 56 | ||
57 | } | 57 | } |
58 | 58 | ||
59 | -antigen-get-clone-dir () { | 59 | -antigen-get-clone-dir () { |
60 | # Takes a repo url and gives out the path that this url needs to be cloned | 60 | # Takes a repo url and gives out the path that this url needs to be cloned |
61 | # to. Doesn't actually clone anything. | 61 | # to. Doesn't actually clone anything. |
62 | # TODO: Memoize? | 62 | # TODO: Memoize? |
63 | echo -n $ADOTDIR/repos/ | 63 | echo -n $ADOTDIR/repos/ |
64 | echo "$1" | sed \ | 64 | echo "$1" | sed \ |
65 | -e 's/\.git$//' \ | 65 | -e 's/\.git$//' \ |
66 | -e 's./.-SLASH-.g' \ | 66 | -e 's./.-SLASH-.g' \ |
67 | -e 's.:.-COLON-.g' | 67 | -e 's.:.-COLON-.g' |
68 | } | 68 | } |
69 | 69 | ||
70 | -antigen-get-clone-url () { | ||
71 | # Takes a repo's clone dir and gives out the repo's original url that was | ||
72 | # used to create the given directory path. | ||
73 | # TODO: Memoize? | ||
74 | echo "$1" | sed \ | ||
75 | -e "s:^$ADOTDIR/repos/::" \ | ||
76 | -e 's/$/.git/' \ | ||
77 | -e 's.-SLASH-./.g' \ | ||
78 | -e 's.-COLON-.:.g' | ||
79 | } | ||
80 | |||
70 | -antigen-ensure-repo () { | 81 | -antigen-ensure-repo () { |
71 | 82 | ||
72 | local update=false | 83 | local update=false |
73 | if [[ $1 == --update ]]; then | 84 | if [[ $1 == --update ]]; then |
74 | update=true | 85 | update=true |
75 | shift | 86 | shift |
76 | fi | 87 | fi |
77 | 88 | ||
78 | local handled_repos="" | 89 | local handled_repos="" |
79 | local install_bundles="" | 90 | local install_bundles="" |
80 | 91 | ||
81 | local url="$1" | 92 | local url="$1" |
82 | local clone_dir="$(-antigen-get-clone-dir $url)" | 93 | local clone_dir="$(-antigen-get-clone-dir $url)" |
83 | 94 | ||
84 | if ! echo "$handled_repos" | grep -Fqm1 "$url"; then | 95 | if ! echo "$handled_repos" | grep -Fqm1 "$url"; then |
85 | if [[ ! -d $clone_dir ]]; then | 96 | if [[ ! -d $clone_dir ]]; then |
86 | git clone "$url" "$clone_dir" | 97 | git clone "$url" "$clone_dir" |
87 | elif $update; then | 98 | elif $update; then |
88 | git --git-dir "$clone_dir/.git" pull | 99 | git --git-dir "$clone_dir/.git" pull |
89 | fi | 100 | fi |
90 | 101 | ||
91 | handled_repos="$handled_repos\n$url" | 102 | handled_repos="$handled_repos\n$url" |
92 | fi | 103 | fi |
93 | 104 | ||
94 | } | 105 | } |
95 | 106 | ||
96 | bundle-update () { | 107 | bundle-update () { |
97 | # Update your bundles, i.e., `git pull` in all the plugin repos. | 108 | # Update your bundles, i.e., `git pull` in all the plugin repos. |
98 | -bundle-echo-record | awk '{print $1}' | sort -u | while read url; do | 109 | -bundle-echo-record | awk '{print $1}' | sort -u | while read url; do |
99 | -antigen-ensure-repo --update "$url" | 110 | -antigen-ensure-repo --update "$url" |
100 | done | 111 | done |
101 | } | 112 | } |
102 | 113 | ||
103 | bundle-cleanup () { | ||
104 | |||
105 | if [[ ! -d "$ADOTDIR/bundles" || \ | ||
106 | "$(ls "$ADOTDIR/bundles/" | wc -l)" == 0 ]]; then | ||
107 | echo "You don't have any bundles." | ||
108 | return 0 | ||
109 | fi | ||
110 | |||
111 | # Find directores in ADOTDIR/bundles, that are not in the bundles record. | ||
112 | local unidentified_bundles="$(comm -13 \ | ||
113 | <(-bundle-echo-record | awk '{print $1}' | sort) \ | ||
114 | <(ls -1 "$ADOTDIR/bundles"))" | ||
115 | |||
116 | if [[ -z $unidentified_bundles ]]; then | ||
117 | echo "You don't have any unidentified bundles." | ||
118 | return 0 | ||
119 | fi | ||
120 | |||
121 | echo The following bundles are not recorded: | ||
122 | echo "$unidentified_bundles" | sed 's/^/ /' | ||
123 | |||
124 | echo -n '\nDelete them all? [y/N] ' | ||
125 | if read -q; then | ||
126 | echo | ||
127 | echo | ||
128 | echo "$unidentified_bundles" | while read name; do | ||
129 | echo -n Deleting $name... | ||
130 | rm -rf "$ADOTDIR/bundles/$name" | ||
131 | echo ' done.' | ||
132 | done | ||
133 | else | ||
134 | echo | ||
135 | echo Nothing deleted. | ||
136 | fi | ||
137 | } | ||
138 | |||
139 | bundle-load () { | 114 | bundle-load () { |
140 | 115 | ||
141 | local url="$1" | 116 | local url="$1" |
142 | local location="$(-antigen-get-clone-dir "$url")/$2" | 117 | local location="$(-antigen-get-clone-dir "$url")/$2" |
143 | local btype="$3" | 118 | local btype="$3" |
144 | 119 | ||
145 | if [[ $btype == theme ]]; then | 120 | if [[ $btype == theme ]]; then |
146 | 121 | ||
147 | # Of course, if its a theme, the location would point to the script | 122 | # Of course, if its a theme, the location would point to the script |
148 | # file. | 123 | # file. |
149 | source "$location" | 124 | source "$location" |
150 | 125 | ||
151 | else | 126 | else |
152 | 127 | ||
153 | # Source the plugin script | 128 | # Source the plugin script |
154 | # FIXME: I don't know. Looks very very ugly. Needs a better | 129 | # FIXME: I don't know. Looks very very ugly. Needs a better |
155 | # implementation once tests are ready. | 130 | # implementation once tests are ready. |
156 | local script_loc="$(ls "$location" | grep -m1 '.plugin.zsh$')" | 131 | local script_loc="$(ls "$location" | grep -m1 '.plugin.zsh$')" |
157 | if [[ -f $script_loc ]]; then | 132 | if [[ -f $script_loc ]]; then |
158 | # If we have a `*.plugin.zsh`, source it. | 133 | # If we have a `*.plugin.zsh`, source it. |
159 | source "$script_loc" | 134 | source "$script_loc" |
160 | elif [[ ! -z "$(ls "$location" | grep -m1 '.zsh$')" ]]; then | 135 | elif [[ ! -z "$(ls "$location" | grep -m1 '.zsh$')" ]]; then |
161 | # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` | 136 | # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` |
162 | # files. | 137 | # files. |
163 | for script ($location/*.zsh) source "$script" | 138 | for script ($location/*.zsh) source "$script" |
164 | fi | 139 | fi |
165 | 140 | ||
166 | # Add to $fpath, for completion(s) | 141 | # Add to $fpath, for completion(s) |
167 | fpath=($location $fpath) | 142 | fpath=($location $fpath) |
168 | 143 | ||
169 | fi | 144 | fi |
170 | 145 | ||
171 | } | 146 | } |
172 | 147 | ||
148 | bundle-cleanup () { | ||
149 | |||
150 | if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then | ||
151 | echo "You don't have any bundles." | ||
152 | return 0 | ||
153 | fi | ||
154 | |||
155 | # Find directores in ADOTDIR/repos, that are not in the bundles record. | ||
156 | local unused_clones="$(comm -13 \ | ||
157 | <(-bundle-echo-record | awk '{print $1}' | sort -u) \ | ||
158 | <(ls "$ADOTDIR/repos" | while read line; do | ||
159 | -antigen-get-clone-url "$line" | ||
160 | done))" | ||
161 | |||
162 | if [[ -z $unused_clones ]]; then | ||
163 | echo "You don't have any unidentified bundles." | ||
164 | return 0 | ||
165 | fi | ||
166 | |||
167 | echo 'You have clones for the following repos, but are not used.' | ||
168 | echo "$unused_clones" | sed 's/^/ /' | ||
169 | |||
170 | echo -n '\nDelete them all? [y/N] ' | ||
171 | if read -q; then | ||
172 | echo | ||
173 | echo | ||
174 | echo "$unused_clones" | while read url; do | ||
175 | echo -n "Deleting clone for $url..." | ||
176 | rm -rf "$(-antigen-get-clone-dir $url)" | ||
177 | echo ' done.' | ||
178 | done | ||
179 | else | ||
180 | echo | ||
181 | echo Nothing deleted. | ||
182 | fi | ||
183 | } | ||
184 | |||
173 | bundle-lib () { | 185 | bundle-lib () { |
174 | bundle --loc=lib | 186 | bundle --loc=lib |
175 | } | 187 | } |
176 | 188 | ||
177 | bundle-theme () { | 189 | bundle-theme () { |
178 | local url="$ANTIGEN_DEFAULT_REPO_URL" | 190 | local url="$ANTIGEN_DEFAULT_REPO_URL" |
179 | local name="${1:-robbyrussell}" | 191 | local name="${1:-robbyrussell}" |
180 | bundle --loc=themes/$name.zsh-theme --btype=theme | 192 | bundle --loc=themes/$name.zsh-theme --btype=theme |
181 | } | 193 | } |
182 | 194 | ||
183 | bundle-apply () { | 195 | bundle-apply () { |
184 | # Initialize completion. | 196 | # Initialize completion. |
185 | # TODO: Doesn't look like this is really necessary. Need to investigate. | 197 | # TODO: Doesn't look like this is really necessary. Need to investigate. |
186 | compinit -i | 198 | compinit -i |
187 | } | 199 | } |
188 | 200 | ||
189 | bundle-list () { | 201 | bundle-list () { |
190 | # List all currently installed bundles | 202 | # List all currently installed bundles |
191 | if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then | 203 | if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then |