Commit 8110fb2f2b28e5af6b4ad0962e487e8647e73578
1 parent
f25c14816f
Add support for absolute paths as url arguments.
This is so that local repositories can be specified as bundles. This can be useful if you want to maintain a separate clone for your own plugin when you experiment and develop stuff and pull in to your environment when you feel confident.
Showing 1 changed file with 1 additions and 1 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://* && $url != /* ]]; 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 | -bundle-ensure-repo "$url" | 53 | -bundle-ensure-repo "$url" |
| 54 | 54 | ||
| 55 | -bundle-load "$url" "$loc" "$btype" | 55 | -bundle-load "$url" "$loc" "$btype" |
| 56 | 56 | ||
| 57 | } | 57 | } |
| 58 | 58 | ||
| 59 | bundle-update () { | 59 | bundle-update () { |
| 60 | # Update your bundles, i.e., `git pull` in all the plugin repos. | 60 | # Update your bundles, i.e., `git pull` in all the plugin repos. |
| 61 | -bundle-echo-record | awk '{print $1}' | sort -u | while read url; do | 61 | -bundle-echo-record | awk '{print $1}' | sort -u | while read url; do |
| 62 | -bundle-ensure-repo --update "$url" | 62 | -bundle-ensure-repo --update "$url" |
| 63 | done | 63 | done |
| 64 | } | 64 | } |
| 65 | 65 | ||
| 66 | -bundle-get-clone-dir () { | 66 | -bundle-get-clone-dir () { |
| 67 | # Takes a repo url and gives out the path that this url needs to be cloned | 67 | # Takes a repo url and gives out the path that this url needs to be cloned |
| 68 | # to. Doesn't actually clone anything. | 68 | # to. Doesn't actually clone anything. |
| 69 | # TODO: Memoize? | 69 | # TODO: Memoize? |
| 70 | echo -n $ADOTDIR/repos/ | 70 | echo -n $ADOTDIR/repos/ |
| 71 | echo "$1" | sed \ | 71 | echo "$1" | sed \ |
| 72 | -e 's/\.git$//' \ | 72 | -e 's/\.git$//' \ |
| 73 | -e 's./.-SLASH-.g' \ | 73 | -e 's./.-SLASH-.g' \ |
| 74 | -e 's.:.-COLON-.g' | 74 | -e 's.:.-COLON-.g' |
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | -bundle-get-clone-url () { | 77 | -bundle-get-clone-url () { |
| 78 | # Takes a repo's clone dir and gives out the repo's original url that was | 78 | # Takes a repo's clone dir and gives out the repo's original url that was |
| 79 | # used to create the given directory path. | 79 | # used to create the given directory path. |
| 80 | # TODO: Memoize? | 80 | # TODO: Memoize? |
| 81 | echo "$1" | sed \ | 81 | echo "$1" | sed \ |
| 82 | -e "s:^$ADOTDIR/repos/::" \ | 82 | -e "s:^$ADOTDIR/repos/::" \ |
| 83 | -e 's/$/.git/' \ | 83 | -e 's/$/.git/' \ |
| 84 | -e 's.-SLASH-./.g' \ | 84 | -e 's.-SLASH-./.g' \ |
| 85 | -e 's.-COLON-.:.g' | 85 | -e 's.-COLON-.:.g' |
| 86 | } | 86 | } |
| 87 | 87 | ||
| 88 | -bundle-ensure-repo () { | 88 | -bundle-ensure-repo () { |
| 89 | 89 | ||
| 90 | local update=false | 90 | local update=false |
| 91 | if [[ $1 == --update ]]; then | 91 | if [[ $1 == --update ]]; then |
| 92 | update=true | 92 | update=true |
| 93 | shift | 93 | shift |
| 94 | fi | 94 | fi |
| 95 | 95 | ||
| 96 | local url="$1" | 96 | local url="$1" |
| 97 | local clone_dir="$(-bundle-get-clone-dir $url)" | 97 | local clone_dir="$(-bundle-get-clone-dir $url)" |
| 98 | 98 | ||
| 99 | if [[ ! -d $clone_dir ]]; then | 99 | if [[ ! -d $clone_dir ]]; then |
| 100 | git clone "$url" "$clone_dir" | 100 | git clone "$url" "$clone_dir" |
| 101 | elif $update; then | 101 | elif $update; then |
| 102 | git --git-dir "$clone_dir/.git" pull | 102 | git --git-dir "$clone_dir/.git" pull |
| 103 | fi | 103 | fi |
| 104 | 104 | ||
| 105 | } | 105 | } |
| 106 | 106 | ||
| 107 | -bundle-load () { | 107 | -bundle-load () { |
| 108 | 108 | ||
| 109 | local url="$1" | 109 | local url="$1" |
| 110 | local location="$(-bundle-get-clone-dir "$url")/$2" | 110 | local location="$(-bundle-get-clone-dir "$url")/$2" |
| 111 | local btype="$3" | 111 | local btype="$3" |
| 112 | 112 | ||
| 113 | if [[ $btype == theme ]]; then | 113 | if [[ $btype == theme ]]; then |
| 114 | 114 | ||
| 115 | # Of course, if its a theme, the location would point to the script | 115 | # Of course, if its a theme, the location would point to the script |
| 116 | # file. | 116 | # file. |
| 117 | source "$location" | 117 | source "$location" |
| 118 | 118 | ||
| 119 | else | 119 | else |
| 120 | 120 | ||
| 121 | # Source the plugin script | 121 | # Source the plugin script |
| 122 | # FIXME: I don't know. Looks very very ugly. Needs a better | 122 | # FIXME: I don't know. Looks very very ugly. Needs a better |
| 123 | # implementation once tests are ready. | 123 | # implementation once tests are ready. |
| 124 | local script_loc="$(ls "$location" | grep -m1 '.plugin.zsh$')" | 124 | local script_loc="$(ls "$location" | grep -m1 '.plugin.zsh$')" |
| 125 | if [[ -f $script_loc ]]; then | 125 | if [[ -f $script_loc ]]; then |
| 126 | # If we have a `*.plugin.zsh`, source it. | 126 | # If we have a `*.plugin.zsh`, source it. |
| 127 | source "$script_loc" | 127 | source "$script_loc" |
| 128 | elif [[ ! -z "$(ls "$location" | grep -m1 '.zsh$')" ]]; then | 128 | elif [[ ! -z "$(ls "$location" | grep -m1 '.zsh$')" ]]; then |
| 129 | # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` | 129 | # If there is no `*.plugin.zsh` file, source *all* the `*.zsh` |
| 130 | # files. | 130 | # files. |
| 131 | for script ($location/*.zsh) source "$script" | 131 | for script ($location/*.zsh) source "$script" |
| 132 | fi | 132 | fi |
| 133 | 133 | ||
| 134 | # Add to $fpath, for completion(s) | 134 | # Add to $fpath, for completion(s) |
| 135 | fpath=($location $fpath) | 135 | fpath=($location $fpath) |
| 136 | 136 | ||
| 137 | fi | 137 | fi |
| 138 | 138 | ||
| 139 | } | 139 | } |
| 140 | 140 | ||
| 141 | bundle-cleanup () { | 141 | bundle-cleanup () { |
| 142 | 142 | ||
| 143 | if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then | 143 | if [[ ! -d "$ADOTDIR/repos" || -z "$(ls "$ADOTDIR/repos/")" ]]; then |
| 144 | echo "You don't have any bundles." | 144 | echo "You don't have any bundles." |
| 145 | return 0 | 145 | return 0 |
| 146 | fi | 146 | fi |
| 147 | 147 | ||
| 148 | # Find directores in ADOTDIR/repos, that are not in the bundles record. | 148 | # Find directores in ADOTDIR/repos, that are not in the bundles record. |
| 149 | local unused_clones="$(comm -13 \ | 149 | local unused_clones="$(comm -13 \ |
| 150 | <(-bundle-echo-record | awk '{print $1}' | sort -u) \ | 150 | <(-bundle-echo-record | awk '{print $1}' | sort -u) \ |
| 151 | <(ls "$ADOTDIR/repos" | while read line; do | 151 | <(ls "$ADOTDIR/repos" | while read line; do |
| 152 | -bundle-get-clone-url "$line" | 152 | -bundle-get-clone-url "$line" |
| 153 | done))" | 153 | done))" |
| 154 | 154 | ||
| 155 | if [[ -z $unused_clones ]]; then | 155 | if [[ -z $unused_clones ]]; then |
| 156 | echo "You don't have any unidentified bundles." | 156 | echo "You don't have any unidentified bundles." |
| 157 | return 0 | 157 | return 0 |
| 158 | fi | 158 | fi |
| 159 | 159 | ||
| 160 | echo 'You have clones for the following repos, but are not used.' | 160 | echo 'You have clones for the following repos, but are not used.' |
| 161 | echo "$unused_clones" | sed 's/^/ /' | 161 | echo "$unused_clones" | sed 's/^/ /' |
| 162 | 162 | ||
| 163 | echo -n '\nDelete them all? [y/N] ' | 163 | echo -n '\nDelete them all? [y/N] ' |
| 164 | if read -q; then | 164 | if read -q; then |
| 165 | echo | 165 | echo |
| 166 | echo | 166 | echo |
| 167 | echo "$unused_clones" | while read url; do | 167 | echo "$unused_clones" | while read url; do |
| 168 | echo -n "Deleting clone for $url..." | 168 | echo -n "Deleting clone for $url..." |
| 169 | rm -rf "$(-bundle-get-clone-dir $url)" | 169 | rm -rf "$(-bundle-get-clone-dir $url)" |
| 170 | echo ' done.' | 170 | echo ' done.' |
| 171 | done | 171 | done |
| 172 | else | 172 | else |
| 173 | echo | 173 | echo |
| 174 | echo Nothing deleted. | 174 | echo Nothing deleted. |
| 175 | fi | 175 | fi |
| 176 | } | 176 | } |
| 177 | 177 | ||
| 178 | bundle-lib () { | 178 | bundle-lib () { |
| 179 | bundle --loc=lib | 179 | bundle --loc=lib |
| 180 | } | 180 | } |
| 181 | 181 | ||
| 182 | bundle-theme () { | 182 | bundle-theme () { |
| 183 | local name="${1:-robbyrussell}" | 183 | local name="${1:-robbyrussell}" |
| 184 | bundle --loc=themes/$name.zsh-theme --btype=theme | 184 | bundle --loc=themes/$name.zsh-theme --btype=theme |
| 185 | } | 185 | } |
| 186 | 186 | ||
| 187 | bundle-apply () { | 187 | bundle-apply () { |
| 188 | # Initialize completion. | 188 | # Initialize completion. |
| 189 | # TODO: Only load completions if there are any changes to the bundle | 189 | # TODO: Only load completions if there are any changes to the bundle |
| 190 | # repositories. | 190 | # repositories. |
| 191 | compinit -i | 191 | compinit -i |
| 192 | } | 192 | } |
| 193 | 193 | ||
| 194 | bundle-list () { | 194 | bundle-list () { |
| 195 | # List all currently installed bundles | 195 | # List all currently installed bundles |
| 196 | if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then | 196 | if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then |
| 197 | echo "You don't have any bundles." >&2 | 197 | echo "You don't have any bundles." >&2 |
| 198 | return 1 | 198 | return 1 |
| 199 | else | 199 | else |
| 200 | -bundle-echo-record | 200 | -bundle-echo-record |
| 201 | fi | 201 | fi |
| 202 | } | 202 | } |
| 203 | 203 | ||
| 204 | # Echo the bundle specs as in the record. The first line is not echoed since it | 204 | # Echo the bundle specs as in the record. The first line is not echoed since it |
| 205 | # is a blank line. | 205 | # is a blank line. |
| 206 | -bundle-echo-record () { | 206 | -bundle-echo-record () { |
| 207 | echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p' | 207 | echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p' |
| 208 | } | 208 | } |
| 209 | 209 | ||
| 210 | -bundle-env-setup () { | 210 | -bundle-env-setup () { |
| 211 | # Pre-startup initializations | 211 | # Pre-startup initializations |
| 212 | -set-default ANTIGEN_DEFAULT_REPO_URL \ | 212 | -set-default ANTIGEN_DEFAULT_REPO_URL \ |
| 213 | https://github.com/robbyrussell/oh-my-zsh.git | 213 | https://github.com/robbyrussell/oh-my-zsh.git |
| 214 | -set-default ADOTDIR $HOME/.antigen | 214 | -set-default ADOTDIR $HOME/.antigen |
| 215 | 215 | ||
| 216 | # Load the compinit module | 216 | # Load the compinit module |
| 217 | autoload -U compinit | 217 | autoload -U compinit |
| 218 | 218 | ||
| 219 | # Without the following, `compdef` function is not defined. | 219 | # Without the following, `compdef` function is not defined. |
| 220 | compinit -i | 220 | compinit -i |
| 221 | } | 221 | } |
| 222 | 222 | ||
| 223 | # Same as `export $1=$2`, but will only happen if the name specified by `$1` is | 223 | # Same as `export $1=$2`, but will only happen if the name specified by `$1` is |
| 224 | # not already set. | 224 | # not already set. |
| 225 | -set-default () { | 225 | -set-default () { |
| 226 | local arg_name="$1" | 226 | local arg_name="$1" |
| 227 | local arg_value="$2" | 227 | local arg_value="$2" |
| 228 | eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" | 228 | eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'" |
| 229 | } | 229 | } |
| 230 | 230 | ||
| 231 | -bundle-env-setup | 231 | -bundle-env-setup |
| 232 | 232 |