|
| 1 | +#!/bin/zsh |
| 2 | + |
| 3 | +### Parse antidote's bundle DSL to an associative array. |
| 4 | +# Example: |
| 5 | +# __antidote_parser 'foo/bar path:plugins/baz kind:fpath pre:myprecmd # comment' |
| 6 | +# typeset -A bundle=( [kind]=fpath [path]=plugins/baz [pre]=myprecmd [name]=foo/bar ) |
| 7 | +# |
| 8 | +# Notes: |
| 9 | +# bundle_str : antidote DSL syntax |
| 10 | +# bundle : assoc array representation |
| 11 | +# bundle_repr : Zsh serialization of the bundle assoc arrary |
| 12 | +# |
| 13 | +# Metadata: |
| 14 | +# _repodir : The clone destination dir |
| 15 | +# _type : The type of bundle (url, repo, path, ?) |
| 16 | +# _repo : The user/repo short form of the URL |
| 17 | +# _url : The git repo URL |
| 18 | +# |
| 19 | +#function __antidote_parser { |
| 20 | + emulate -L zsh; setopt local_options $_adote_funcopts |
| 21 | + local MATCH MBEGIN MEND; local -a match mbegin mend # appease 'warn_create_global' |
| 22 | + local bundle_str bundle_var bundle_repr gitsite str pair key value |
| 23 | + local -a kvpairs parts |
| 24 | + local -A bundle |
| 25 | + |
| 26 | + bundle_str="$1" |
| 27 | + bundle_var="${2:-bundle}" |
| 28 | + |
| 29 | + # Allow the user to override the default git site if they really want to |
| 30 | + zstyle -s ':antidote:gitremote' url 'gitsite' \ |
| 31 | + || gitsite='https://github.com' |
| 32 | + gitsite="${gitsite%/}" |
| 33 | + |
| 34 | + # Remove anything after the first '#' |
| 35 | + bundle_str=${bundle_str%%\#*} |
| 36 | + # Trim spaces |
| 37 | + bundle_str=${${bundle_str/#[[:space:]]#}/%[[:space:]]#} |
| 38 | + # Skip empty bundle strings |
| 39 | + [[ -z "$bundle_str" ]] && return 0 |
| 40 | + # 1st field gets a 'name:' prefix so we can treat everything as key:val pairs |
| 41 | + bundle_str="name:${bundle_str}" |
| 42 | + |
| 43 | + # Split line into key-value pairs with quoting |
| 44 | + kvpairs=(${(Q)${(z)bundle_str}}) |
| 45 | + for pair in "${kvpairs[@]}"; do |
| 46 | + key=${pair%%:*} # Extract key (before first ':') |
| 47 | + if [[ "$pair" == *:* ]]; then |
| 48 | + value=${pair#*:} # Extract value (after first ':') |
| 49 | + else |
| 50 | + value= |
| 51 | + fi |
| 52 | + bundle[$key]=$value |
| 53 | + done |
| 54 | + |
| 55 | + # Enhance the bundle with metadata fields. Metadata fields begin with an underscore |
| 56 | + # since those will never be part of the DSL. Let's start with _type, which tells us |
| 57 | + # whether the bundle is a URL, a user/repo, or a path |
| 58 | + if [[ "$bundle[name]" == *://*/*/* || "$bundle[name]" == (ssh|git)@*:*/* ]]; then |
| 59 | + if [[ "$bundle[name]" == *://*/*/*/* || "$bundle[name]" == *@*:*/*/* ]]; then |
| 60 | + bundle[_type]="?" |
| 61 | + else |
| 62 | + bundle[_type]="url" |
| 63 | + fi |
| 64 | + elif [[ "$bundle[name]" == *('@'|':')* ]] ; then |
| 65 | + bundle[_type]="?" # bad URLs |
| 66 | + elif [[ "$bundle[name]" == ('~'|'$'|'.')* ]]; then |
| 67 | + bundle[_type]="path" |
| 68 | + elif [[ "$bundle[name]" == */* && "$bundle[name]" != */*/* ]]; then |
| 69 | + bundle[_type]="repo" |
| 70 | + elif [[ "$bundle[name]" == */* ]]; then |
| 71 | + bundle[_type]="path" |
| 72 | + else |
| 73 | + bundle[_type]="?" |
| 74 | + fi |
| 75 | + |
| 76 | + # For git repos, we add a metadata field for the URL |
| 77 | + if [[ "$bundle[_type]" == url ]]; then |
| 78 | + str="$bundle[name]" |
| 79 | + str=${str%.git} |
| 80 | + str=${str:gs/\:/\/} |
| 81 | + parts=( ${(ps./.)str} ) |
| 82 | + if [[ $#parts -gt 1 ]]; then |
| 83 | + bundle[_repo]="${parts[-2]}/${parts[-1]}" |
| 84 | + else |
| 85 | + bundle[_repo]="$str" |
| 86 | + fi |
| 87 | + bundle[_url]="$bundle[name]" |
| 88 | + elif [[ "$bundle[_type]" == repo ]]; then |
| 89 | + bundle[_repo]="${bundle[name]}" |
| 90 | + bundle[_url]="${gitsite}/${bundle[name]}" |
| 91 | + fi |
| 92 | + |
| 93 | + # If there's a git URL, we also need to set the _repodir |
| 94 | + if [[ -v bundle[_url] ]]; then |
| 95 | + # TODO: Remove for antidote 2.0 |
| 96 | + if zstyle -t ':antidote:compatibility-mode' 'antibody' || ! zstyle -t ':antidote:bundle' use-friendly-names; then |
| 97 | + # sanitize URL for safe use as a dir name |
| 98 | + # ex: $ANTIDOTE_HOME/https-COLON--SLASH--SLASH-github.com-SLASH-zsh-users-SLASH-zsh-autosuggestions |
| 99 | + str="$bundle[_url]" |
| 100 | + str=${str%.git} |
| 101 | + str=${str:gs/\@/-AT-} |
| 102 | + str=${str:gs/\:/-COLON-} |
| 103 | + str=${str:gs/\//-SLASH-} |
| 104 | + bundle[_repodir]="$str" |
| 105 | + else |
| 106 | + bundle[_repodir]="$bundle[_repo]" |
| 107 | + fi |
| 108 | + fi |
| 109 | + |
| 110 | + # Print the parsed bundle assoc arr using whatever bundle_var the user wants |
| 111 | + bundle_repr="$(declare -p bundle)" |
| 112 | + bundle_repr="typeset -A ${bundle_var}=${bundle_repr#*=}" |
| 113 | + |
| 114 | + # Sanity check that I probably don't need. |
| 115 | + if [[ ! "$bundle_repr" =~ "^typeset\ -A\ ${bundle_var}=" ]]; then |
| 116 | + print -ru2 -- "antidote: Unable to parse bundle string: '$bundle_str'." |
| 117 | + return 1 |
| 118 | + fi |
| 119 | + |
| 120 | + # Return/print the result. |
| 121 | + typeset -g REPLY="$bundle_repr" |
| 122 | + print -r -- "$REPLY" |
| 123 | +#} |
0 commit comments