Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmc3 committed Oct 27, 2023
1 parent 36e3612 commit f46931a
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 79 deletions.
2 changes: 2 additions & 0 deletions .bumpversion.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ current_version = 1.9.3
parse = v?(?P<major>\d+)\.(?P<minor>\d+)\.(?P<revision>\d+)
serialize = {major}.{minor}.{revision}

[bumpversion:file:bin/antibody]

[bumpversion:file:functions/__antidote_version]

[bumpversion:file:tests/README.md]
Expand Down
175 changes: 102 additions & 73 deletions bin/antibody
Original file line number Diff line number Diff line change
@@ -1,23 +1,101 @@
#!/usr/bin/env zsh

typeset -ga antibody_opts=( warn_create_global warn_nested_var extended_glob )
typeset -ga antibody_opts=( extended_glob no_monitor pipefail )
typeset -g antibody_version='1.9.2'
if [[ $ANTIBODY_TEST_MODE -eq 1 ]]; then
antibody_opts+=( warn_create_global warn_nested_var )
antibody_version=dev
fi

function __antibody_cmd_bundle {
emulate -L zsh; setopt local_options $antibody_opts

# parse the DSL for bundles
local bundles_tsv=$(__antibody_parsebundles $@)
bundles_tsv=(${(@f)${bundles}})
# handle bundles as newline delimited arg strings,
# or as <redirected or piped| input
local data bundles=()
if [[ $# -gt 0 ]]; then
bundles=("${(s.\n.)${@}}")
elif [[ ! -t 0 ]]; then
while IFS= read -r data || [[ -n "$data" ]]; do
bundles+=($data)
done
fi
(( $#bundles )) || return 1

local row
local -a bundles
local -A bundle=()
for row in $bundles_tsv; do
bundle=( ${(ps/\t/)row} )
typeset -p bundle
#print -r -- $bundlestr
local sed_cleanse_cmds=(
# Carriage returns to newlines.
-e 's/\r/\n/g'
# Tabs to spaces.
-e 's/\t/ /g'
# Collapse multiple spaces to one.
-e 's/ +/ /g'
# Trim leading spaces.
-e 's/^ +//g'
# Trim trailing spaces.
-e 's/ +$//g'
# Remove trailing comments.
-e 's/ +#.*$//g'
# Delete empty/comment lines.
-e '/^(#.+)?$/d'
)
local cleansed_bundles=$(print -rl -- $bundles | sed -E $sed_cleanse_cmds)

####
###local -a cleansed_bundles=("${(@f)$(sed -E $sed_cleanse_cmds $zsh_plugins)}")

local awk_giturls='
{ bundle=""; branch="" }
# /kind:defer/ { print "https://github.com/romkatv/zsh-defer" }
$1~/^[^\/\$~]+\/[^\/]+$/ { bundle=giturl"/"$1 }
$1~/^(https?:|(ssh|git)@)/ { bundle=$1 }
match($0, /branch:[^ ]+/) { branch=substr($0, RSTART+7, RLENGTH-7) }
branch!="" { printf "%s --branch %s\n",bundle,branch; next }
bundle!="" { print bundle }
'
local -a clone_bundles=("${(@f)$(
print -r -- $cleansed_bundles | awk -v giturl="${ANTIBODY_GITURL:-https://github.com}" $awk_giturls | sort | uniq
)}")

local clonestr giturl gitopts bundledir
local -a parts
for clonestr in $clone_bundles; do
parts=( ${(@s/ /)clonestr} )
giturl=$parts[1]
gitopts=(--quiet --depth 1 --recurse-submodules --shallow-submodules $parts[2,-1])
bundledir=$(__antibody_bundledir $giturl)
if [[ ! -d $bundledir ]]; then
git clone $gitopts $giturl $bundledir &
fi
done
wait

#printf 'clone %s\n' $clone_bundles
# awk_script='
# {
# printf "%s","__antibody_script "
# for (i=2; i<=NF; i++) {
# sub(/\:/, " ", $i)
# printf "--%s ",$i
# }
# printf "%s\n",$1
# }
# '

# sed -E $sed_cmds $zsh_plugins | awk $awk_script

# # parse the DSL for bundles
# local bundles_tsv=$(__antibody_parsebundles $@)
# bundles_tsv=(${(@f)${bundles}})
# (( $#bundles )) || return 1

# local row
# local -a bundles
# local -A bundle=()
# for row in $bundles_tsv; do
# bundle=( ${(ps/\t/)row} )
# typeset -p bundle
# #print -r -- $bundlestr
# done
}

function __antibody_cmd_update {
Expand All @@ -32,7 +110,16 @@ function __antibody_cmd_purge {

function __antibody_cmd_list {
emulate -L zsh; setopt local_options $antibody_opts
# TODO
local bundledir url
local output=()
local bundles=($(__antibody_cmd_home)/*/.git(/N))

for bundledir in $bundles; do
bundledir=${bundledir:h}
url=$(git -C "$bundledir" config remote.origin.url)
output+=("$(printf '%-64s %s\n' $url $bundledir)")
done
(( $#output )) && printf '%s\n' ${(o)output}
}

function __antibody_cmd_path {
Expand Down Expand Up @@ -106,8 +193,8 @@ function __antibody_help {
The fastest shell plugin manager
Flags:
-h, --help Show context-sensitive help (also try --help-long and --help-man).
-v, --version Show application version.
-h, --help Show context-sensitive help.
-v, --version Show application version.
Commands:
help [<command>...]
Expand Down Expand Up @@ -140,7 +227,7 @@ function __antibody_help {

function __antibody_version {
emulate -L zsh; setopt local_options $antibody_opts
print -r "antibody version 1.9.2"
print -r "antibody version ${antibody_version}"
}

function __antibody_bundledir {
Expand All @@ -161,64 +248,6 @@ function __antibody_bundledir {
print -r -- $bundle
}

function __antibody_parsebundles {
emulate -L zsh; setopt local_options $antibody_opts

# handle bundles as newline delimited arg strings,
# or as <redirected or piped| input
local data bundles=()
if [[ $# -gt 0 ]]; then
bundles=("${(s.\n.)${@}}")
elif [[ ! -t 0 ]]; then
while IFS= read -r data || [[ -n "$data" ]]; do
bundles+=($data)
done
fi
(( $#bundles )) || return 1

local bundlestr loc loctype
local -a bundle parts optstr annotations result

for bundlestr in $bundles; do
# normalize whitespace and remove comments
bundlestr=${bundlestr//[[:space:]]/ }
bundlestr=${bundlestr%%\#*}

# split on spaces into parts array and skip empty lines
parts=( ${(@s/ /)bundlestr} )
(( $#parts )) || continue

# the first element is the bundle location, and the remainder are a:b annotations
# split annotations into key/value pairs
loc=$parts[1]
case "$loc" in
(/|~|'$')*) loctype=path ;;
*://*) loctype=url ;;
*@*:*/*) loctype=sshurl ;;
*(:|@)*) loctype='?' ;;
*/*/*) loctype=path ;;
*/) loctype=path ;;
*/*) loctype=repo ;;
*) loctype=word ;;
esac

bundle=( loc $loc loctype $loctype )
annotations=( ${parts[@]:1} )
if (( $#annotations )); then
parts=( ${(@s/:/)annotations} )
(( ${#parts} % 2 == 0 )) || {
print -ru2 "antibody: error: bad annotation '$annotations'."
return 1
}
bundle+=( $parts )
fi

# output the parsed bundles as TSV
result+=(${(@pj/\t/)bundle})
done
print -rl -- $result
}

function __antibody_err_flag {
emulate -L zsh; setopt local_options $antibody_opts
print -r "antibody: error: unknown flag '$1', try --help"
Expand Down
68 changes: 62 additions & 6 deletions tests/test_antibody.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
```zsh
% # Pipe test output to subenv to substitute environment vars
% subenv() { sed "s|${(P)1}|\$$1|g"; }
% export ANTIBODY_TEST_MODE=1
% [[ $OSTYPE == darwin* ]] && CACHEDIR=$HOME/Library/Caches || CACHEDIR=$HOME/.cache
% # rm -rf -- $(./bin/antibody home)
%
```

Expand All @@ -17,8 +20,8 @@ usage: antibody [<flags>] <command> [<args> ...]
The fastest shell plugin manager

Flags:
-h, --help Show context-sensitive help (also try --help-long and --help-man).
-v, --version Show application version.
-h, --help Show context-sensitive help.
-v, --version Show application version.

Commands:
help [<command>...]
Expand Down Expand Up @@ -55,16 +58,15 @@ Commands:

```zsh
% ./bin/antibody -v
antibody version 1.9.2
antibody version dev
% ./bin/antibody --version
antibody version 1.9.2
antibody version dev
%
```

## Home

```zsh
% [[ $OSTYPE == darwin* ]] && CACHEDIR=$HOME/Library/Caches || CACHEDIR=$HOME/.cache
% ./bin/antibody home | subenv CACHEDIR
$CACHEDIR/antibody
%
Expand All @@ -79,6 +81,59 @@ fpath+=( $CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-zsh-users
%
```

```zsh
% bundles=('zsh-users/zsh-completions kind:fpath' 'zsh-users/zsh-history-substring-search kind:clone')
% print -rl -- $bundles | ./bin/antibody bundle | subenv CACHEDIR
fpath+=( $CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-zsh-users-SLASH-zsh-completions )

%
```

```zsh
% zsh_plugins=$PWD/tests/testdata/antibody/bundles.txt
% ./bin/antibody bundle <$zsh_plugins | subenv CACHEDIR
TODO
%
```

## List

```zsh
% ./bin/antibody list | cut -c 66- | subenv CACHEDIR
$CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-dracula-SLASH-zsh
$CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-mattmc3-SLASH-antidote
$CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-mattmc3-SLASH-zman
$CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-ohmyzsh-SLASH-ohmyzsh
$CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-peterhurford-SLASH-up.zsh
$CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-romkatv-SLASH-zsh-bench
$CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-rummik-SLASH-zsh-tailf
$CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-rupa-SLASH-z
$CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-sindresorhus-SLASH-pure
$CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-zdharma-continuum-SLASH-fast-syntax-highlighting
$CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-zsh-users-SLASH-antigen
$CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-zsh-users-SLASH-zsh-autosuggestions
$CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-zsh-users-SLASH-zsh-completions
$CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-zsh-users-SLASH-zsh-history-substring-search
$CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-zsh-users-SLASH-zsh-syntax-highlighting
% ./bin/antibody list | cut -c -65 | tr -d ' '
https://github.com/dracula/zsh
https://github.com/mattmc3/antidote
https://github.com/mattmc3/zman
https://github.com/ohmyzsh/ohmyzsh
https://github.com/peterhurford/up.zsh
https://github.com/romkatv/zsh-bench
https://github.com/rummik/zsh-tailf
https://github.com/rupa/z
https://github.com/sindresorhus/pure
https://github.com/zdharma-continuum/fast-syntax-highlighting
https://github.com/zsh-users/antigen
https://github.com/zsh-users/zsh-autosuggestions
https://github.com/zsh-users/zsh-completions
https://github.com/zsh-users/zsh-history-substring-search
https://github.com/zsh-users/zsh-syntax-highlighting
%
```

## Path

```zsh
Expand All @@ -90,7 +145,7 @@ $CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-zsh-users-SLASH-zs
## Init

```zsh
% ./bin/antibody init | subenv PWD
% ./bin/antibody init | subenv PWD | sed -e 's|\t| |g'
#!/usr/bin/env zsh
antibody() {
case "$1" in
Expand All @@ -114,5 +169,6 @@ compctl -K _antibody antibody

```zsh
% unfunction subenv
% zstyle -d ':antibody:tests' set-warn-options
%
```
Loading

0 comments on commit f46931a

Please sign in to comment.