use re use str use path var asdf_dir = $E:HOME'/.asdf' if (and (has-env ASDF_DIR) (!=s $E:ASDF_DIR '')) { asdf_dir = $E:ASDF_DIR } else { set-env ASDF_DIR $asdf_dir } var asdf_data_dir = $asdf_dir if (and (has-env ASDF_DATA_DIR) (!=s $E:ASDF_DATA_DIR '')) { asdf_data_dir = $E:ASDF_DATA_DIR } # Add function wrapper so we can export variables fn asdf [command @args]{ if (==s $command 'shell') { # set environment variables parts = [($asdf_dir'/bin/asdf' export-shell-version elvish $@args)] if (==s $parts[0] 'set-env') { set-env $parts[1] $parts[2] } elif (==s $parts[0] 'unset-env') { unset-env $parts[1] } } else { # forward other commands to asdf script $asdf_dir'/bin/asdf' $command $@args } } fn match [argz @pats]{ var matched = $true; if (!= (count $argz) (count $pats)) { matched = $false } else { for i [(range (count $pats))] { pat = '^'$pats[$i]'$' arg = $argz[$i] if (not (re:match $pat $arg)) { matched = $false break } } } put $matched } fn ls-shims []{ ls $asdf_data_dir'/shims' } fn ls-executables []{ # Print all executable files and links in path try { find $@paths '(' -type f -o -type l ')' -print 2>/dev/null | each [p]{ try { if (test -x $p) { path:base $p } } except { # don't fail if permission denied } } } except { # silence default non-zero exit status } } fn ls-installed-versions [plugin_name]{ asdf list $plugin_name | each [version]{ put (re:replace '\s*(.*)\s*' '${1}' $version) } } fn ls-all-versions [plugin_name]{ asdf list-all $plugin_name | each [version]{ put (re:replace '\s*(.*)\s*' '${1}' $version) } } # Append ~/.asdf/bin and ~/.asdf/shims to PATH for path [ $asdf_dir'/bin' $asdf_data_dir'/shims' ] { if (not (has-value $paths $path)) { paths = [ $@paths $path ] } } # Setup argument completions fn arg-completer [@argz]{ argz = $argz[1:-1] # strip 'asdf' and trailing empty string var num = (count $argz) if (== $num 0) { # list all subcommands find $asdf_dir'/lib/commands' -name 'command-*' | each [cmd]{ put (re:replace '.*/command-(.*)\.bash' '${1}' $cmd) } put 'plugin' } else { if (match $argz 'current') { # asdf current asdf plugin-list } elif (match $argz 'env') { # asdf env ls-shims } elif (match $argz 'env' '.*') { # asdf env [util] ls-executables } elif (match $argz 'exec') { # asdf exec ls-shims } elif (match $argz 'global') { # asdf global asdf plugin-list } elif (match $argz 'global' '.*') { # asdf global ls-installed-versions $argz[-1] } elif (match $argz 'install') { # asdf install asdf plugin-list } elif (match $argz 'install' '.*') { # asdf install ls-all-versions $argz[-1] } elif (match $argz 'install' '.*' '.*') { # asdf install [--keep-download] put '--keep-download' } elif (match $argz 'latest') { # asdf latest asdf plugin-list } elif (match $argz 'latest' '.*') { # asdf latest [] ls-all-versions $argz[-1] } elif (match $argz 'list-all') { # asdf list all asdf plugin-list } elif (match $argz 'list-all' '.*') { # asdf list all [] ls-all-versions $argz[-1] } elif (match $argz 'list') { # asdf list asdf plugin-list } elif (match $argz 'list' '.*') { # asdf list [] ls-installed-versions $argz[-1] } elif (match $argz 'local') { # asdf local [-p|--parent] asdf plugin-list put '-p' put '--parent' } elif (match $argz 'local' '(-p|(--parent))') { # asdf local [-p|--parent] asdf plugin-list } elif (match $argz 'local' '.*') { # asdf local [-p|--parent] # asdf local ls-installed-versions $argz[-1] put '-p' put '--parent' } elif (match $argz 'local' '(-p|(--parent))' '.*') { # asdf local [-p|--parent] ls-installed-versions $argz[-1] } elif (match $argz 'local' '.*' '(-p|(--parent))') { # asdf local [-p|--parent] ls-installed-versions $argz[-2] } elif (match $argz 'local' '.*' '.*') { # asdf local [-p|--parent] put '-p' put '--parent' } elif (or (match $argz 'plugin-add') (match $argz 'plugin' 'add')) { # asdf plugin add asdf plugin-list-all | each [line]{ put (re:replace '([^\s]+)\s+.*' '${1}' $line) } } elif (or (match $argz 'plugin-list') (match $argz 'plugin' 'list')) { # asdf plugin list put '--urls' put '--refs' put 'all' } elif (or (match $argz 'plugin-push') (match $argz 'plugin' 'push')) { # asdf plugin push asdf plugin-list } elif (or (match $argz 'plugin-remove') (match $argz 'plugin' 'remove')) { # asdf plugin remove asdf plugin-list } elif (and (>= (count $argz) 3) (match $argz[:3] 'plugin-test' '.*' '.*')) { # asdf plugin-test [--asdf-tool-version ] [--asdf-plugin-gitref ] [test-command*] put '--asdf-plugin-gitref' put '--asdf-tool-version' ls-executables ls-shims } elif (and (>= (count $argz) 4) (match $argz[:4] 'plugin' 'test' '.*' '.*')) { # asdf plugin test [--asdf-tool-version ] [--asdf-plugin-gitref ] [test-command*] put '--asdf-plugin-gitref' put '--asdf-tool-version' ls-executables ls-shims } elif (or (match $argz 'plugin-update') (match $argz 'plugin' 'update')) { # asdf plugin update asdf plugin-list put '--all' } elif (match $argz 'plugin') { # list plugin-* subcommands find $asdf_dir'/lib/commands' -name 'command-plugin-*' | each [cmd]{ put (re:replace '.*/command-plugin-(.*)\.bash' '${1}' $cmd) } } elif (match $argz 'reshim') { # asdf reshim asdf plugin-list } elif (match $argz 'reshim' '.*') { # asdf reshim ls-installed-versions $argz[-1] } elif (match $argz 'shim-versions') { # asdf shim-versions ls-shims } elif (match $argz 'uninstall') { # asdf uninstall asdf plugin-list } elif (match $argz 'uninstall' '.*') { # asdf uninstall ls-installed-versions $argz[-1] } elif (match $argz 'update') { if (== $num 1) { # asdf update put '--head' } } elif (match $argz 'where') { # asdf where asdf plugin-list } elif (match $argz 'where' '.*') { # asdf where [] ls-installed-versions $argz[-1] } elif (match $argz 'which') { ls-shims } } }