From 36e361281521394a65947b7b6e136e6092d9c1d1 Mon Sep 17 00:00:00 2001 From: mattmc3 Date: Tue, 3 Oct 2023 20:04:28 -0400 Subject: [PATCH] WIP --- .editorconfig | 4 + bin/antibody | 268 +++++++++++++++++++++++++++++++++++++++++ tests/test_antibody.md | 118 ++++++++++++++++++ 3 files changed, 390 insertions(+) create mode 100755 bin/antibody create mode 100644 tests/test_antibody.md diff --git a/.editorconfig b/.editorconfig index 41e23c5..a3b2cf0 100644 --- a/.editorconfig +++ b/.editorconfig @@ -14,6 +14,10 @@ end_of_line = crlf [*.md] trim_trailing_whitespace = false +[**/bin/*] +indent_style = tab +indent_size = 4 + [makefile] indent_style = tab indent_size = 4 diff --git a/bin/antibody b/bin/antibody new file mode 100755 index 0000000..3ff1ad6 --- /dev/null +++ b/bin/antibody @@ -0,0 +1,268 @@ +#!/usr/bin/env zsh + +typeset -ga antibody_opts=( warn_create_global warn_nested_var extended_glob ) + +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}}) + (( $#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 { + emulate -L zsh; setopt local_options $antibody_opts + # TODO +} + +function __antibody_cmd_purge { + emulate -L zsh; setopt local_options $antibody_opts + # TODO +} + +function __antibody_cmd_list { + emulate -L zsh; setopt local_options $antibody_opts + # TODO +} + +function __antibody_cmd_path { + local bundle=$(__antibody_bundledir "$@") + if [[ ! -d $bundle ]]; then + print -ru2 -- "antibody: error: '$1' does not exist in cloned paths" + return 1 + fi + print -r -- $bundle +} + +function __antibody_cmd_init { + emulate -L zsh; setopt local_options $antibody_opts + 0=${(%):-%x} + local script=$(cat <<-EOS + #!/usr/bin/env zsh + antibody() { + case "\$1" in + bundle) + source <( ${0:A} \$@ ) || ${0:A} \$@ + ;; + *) + ${0:A} \$@ + ;; + esac + } + + _antibody() { + IFS=' ' read -A reply <<< "help bundle update home purge list init" + } + compctl -K _antibody antibody + EOS + ) + print -rl $script +} + +function __antibody_cmd_home { + emulate -L zsh; setopt local_options $antibody_opts + typeset -g REPLY= + local o_help + zparseopts -D -M -- h=o_help -help=h || return 1 + + if (( $#o_help )); then + __antibody_help + return + fi + + local result + if [[ -n "$ANTIBODY_HOME" ]]; then + result=$ANTIBODY_HOME + elif [[ "${OSTYPE}" == darwin* ]]; then + result=$HOME/Library/Caches/antibody + elif [[ "${OSTYPE}" == (cygwin|msys)* ]]; then + result=${LOCALAPPDATA:-$LocalAppData}/antibody + if type cygpath > /dev/null; then + result=$(cygpath "$result") + fi + else + result=${XDG_CACHE_HOME:-$HOME/.cache}/antibody + fi + + print -r -- $result +} + +function __antibody_help { + emulate -L zsh; setopt local_options $antibody_opts + 0=${(%):-%x} + local script=$(cat <<-EOS + usage: antibody [] [ ...] + + The fastest shell plugin manager + + Flags: + -h, --help Show context-sensitive help (also try --help-long and --help-man). + -v, --version Show application version. + + Commands: + help [...] + Show help. + + bundle [...] + downloads a bundle and prints its source line + + update + updates all previously bundled bundles + + home + prints where antibody is cloning the bundles + + purge + purges a bundle from your computer + + list + lists all currently installed bundles + + path + prints the path of a currently cloned bundle + + init + initializes the shell so Antibody can work as expected + EOS + ) + print -rl $script +} + +function __antibody_version { + emulate -L zsh; setopt local_options $antibody_opts + print -r "antibody version 1.9.2" +} + +function __antibody_bundledir { + emulate -L zsh; setopt local_options $antibody_opts + local bundle="$1" + if [[ "$bundle" != /* ]]; then + # sanitize URL for safe use as a dir name + local url=$bundle + if [[ $bundle != *://* && $bundle != git@*:*/* ]]; then + url=https://github.com/$bundle + fi + url=${url%.git} + url=${url:gs/\@/-AT-} + url=${url:gs/\:/-COLON-} + url=${url:gs/\//-SLASH-} + bundle=$(__antibody_cmd_home)/$url + fi + print -r -- $bundle +} + +function __antibody_parsebundles { + emulate -L zsh; setopt local_options $antibody_opts + + # handle bundles as newline delimited arg strings, + # or as ] [ ...] + +The fastest shell plugin manager + +Flags: + -h, --help Show context-sensitive help (also try --help-long and --help-man). + -v, --version Show application version. + +Commands: + help [...] + Show help. + + bundle [...] + downloads a bundle and prints its source line + + update + updates all previously bundled bundles + + home + prints where antibody is cloning the bundles + + purge + purges a bundle from your computer + + list + lists all currently installed bundles + + path + prints the path of a currently cloned bundle + + init + initializes the shell so Antibody can work as expected +% # short -h flag also supported +% ./bin/antibody -h #=> --lines 32 +% # running the bare command shows help too +% ./bin/antibody #=> --lines 32 +% +``` + +## Version + +```zsh +% ./bin/antibody -v +antibody version 1.9.2 +% ./bin/antibody --version +antibody version 1.9.2 +% +``` + +## Home + +```zsh +% [[ $OSTYPE == darwin* ]] && CACHEDIR=$HOME/Library/Caches || CACHEDIR=$HOME/.cache +% ./bin/antibody home | subenv CACHEDIR +$CACHEDIR/antibody +% +``` + +## Bundle + +```zsh +% ./bin/antibody bundle zsh-users/zsh-autosuggestions | subenv CACHEDIR +source $CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-zsh-users-SLASH-zsh-autosuggestions/zsh-autosuggestions.plugin.zsh +fpath+=( $CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-zsh-users-SLASH-zsh-autosuggestions ) +% +``` + +## Path + +```zsh +% ./bin/antibody path zsh-users/zsh-autosuggestions | subenv CACHEDIR +$CACHEDIR/antibody/https-COLON--SLASH--SLASH-github.com-SLASH-zsh-users-SLASH-zsh-autosuggestions +% +``` + +## Init + +```zsh +% ./bin/antibody init | subenv PWD +#!/usr/bin/env zsh +antibody() { + case "$1" in + bundle) + source <( $PWD/bin/antibody $@ ) || $PWD/bin/antibody $@ + ;; + *) + $PWD/bin/antibody $@ + ;; + esac +} + +_antibody() { + IFS=' ' read -A reply <<< "help bundle update home purge list init" +} +compctl -K _antibody antibody +% +``` + +## Teardown + +```zsh +% unfunction subenv +% +```