#compdef luarocks # {{{ helper: luarocks commands __luarocks_command(){ local -a commands=( build:'Build/compile a rock' config:'Query information about the LuaRocks configuration' doc:'Show documentation for an installed rock' download:'Download a specific rock file from a rocks server' help:'Help on commands' install:'Install a rock' lint:'Check syntax of a rockspec' list:'List currently installed rocks' make:'Compile package in current directory using a rockspec' new_version:'Auto-write a rockspec for a new version of a rock' pack:'Create a rock, packing sources or binaries' path:'Return the currently configured package path' purge:'Remove all installed rocks from a tree' remove:'Uninstall a rock' search:'Query the LuaRocks servers' show:'Show information about an installed rock' unpack:'Unpack the contents of a rock' upload:'Upload a rockspec to the public rocks repository' write_rockspec:'Write a template for a rockspec file' ) _describe -t commands 'command' commands "$@" } # }}} # {{{ helper: dependencies mode local option_deps_mode='--deps-mode=[How to handle dependencies]:MODE:__luarocks_deps_mode' __luarocks_deps_mode(){ local modes=( 'all:use all trees from the rocks_trees list for finding dependencies' 'one:use only the current tree (possibly set with --tree)' 'order:use trees based on order (use the current tree and all trees below it on the rocks_trees list)' 'none:ignore dependencies altogether' ) _describe 'dependencies mode' modes } # }}} # {{{ helper: versions of an external rock __luarocks_rock_version(){ for i in {2..${#build_command_options[@]}}; do if [[ ! -z "${words[$i]}" && ! "${words[$i]}" =~ '^-' && ! -f "${words[$i]}" ]]; then # TODO: complete with optional versions _message -e "version for rock ${words[$i]}" return fi done } # }}} # {{{ helper: list of libraries that C files need to link to __luarocks_c_libs(){ } # }}} # {{{ helper: lua versions # TODO __luarocks_lua_version(){ _values -s , } # }}} # {{{ helper: installed rocks cache policy ___luarocks_installed_rocks_cache_policy(){ # the number of seconds since 1970-01-01 the manifest file was modified local manifest_last_date_modified="$(date -r ~/.luarocks/lib/luarocks/rocks-5.3/manifest +%s 2>/dev/null)" # the number of seconds since 1970-01-01 the cache file was modified local cache_last_date_modified="$(date -r $1 +%s 2>/dev/null)" if [[ ! -z ${cache_last_date_modified} && ! -z ${manifest_last_date_modified} ]]; then # if the manifest file is newer then the cache: if [ ${manifest_last_date_modified} -ge ${cache_last_date_modified} ]; then (( 1 )) else (( 0 )) fi fi } # }}} # {{{ helper: installed rocks __luarocks_installed_rocks(){ local update_policy ret=1 zstyle -s ":completion:${curcontext}:" cache-policy update_policy if [[ -z "$update_policy" ]]; then zstyle ":completion:${curcontext}:" cache-policy ___luarocks_installed_rocks_cache_policy fi if _cache_invalid luarocks_installed_list; then rocks_list=($(luarocks list --porcelain)) _store_cache luarocks_installed_list rocks_list else _retrieve_cache luarocks_installed_list fi if _cache_invalid luarocks_installed_names; then rocks_names=() for i in {1.."${#rocks_list[@]}"..4}; do rocks_names+=("${rocks_list[$i]}") done _store_cache luarocks_installed_names rocks_names else _retrieve_cache luarocks_installed_names fi if _cache_invalid luarocks_installed_versions; then rocks_versions=() for i in {2.."${#rocks_list[@]}"..4}; do rocks_versions+=("${rocks_list[$i]}") done _store_cache luarocks_installed_versions rocks_versions else _retrieve_cache luarocks_installed_versions fi if _cache_invalid luarocks_installed_descriptions; then rocks_descriptions=() for i in {1.."${#rocks_names[@]}"}; do name_version_description="$(luarocks show ${rocks_names[$i]} | head -2 | tail -1)" total_length=${#name_version_description} garbage_length="$((${#rocks_names[$i]} + ${#rocks_versions[$i]} + 5))" description="${name_version_description[${garbage_length},${total_length}]}" rocks_descriptions+=("${description}") done _store_cache luarocks_installed_descriptions rocks_descriptions else _retrieve_cache luarocks_installed_descriptions fi if _cache_invalid luarocks_installed_names_and_descriptions; then rocks_names_and_descriptions=() for i in {1.."${#rocks_names[@]}"}; do name_and_description=${rocks_names[$i]}:${rocks_descriptions[$i]} rocks_names_and_descriptions+=(${name_and_description}) done else _store_cache luarocks_installed_names_and_descriptions rocks_names_and_descriptions fi _describe 'installed rocks' rocks_names_and_descriptions } # }}} # {{{ helper: rocks wrapper # Used to complete one or more of the followings: # - .rockspec file # - .src.rock file # - external rock __luarocks_rock(){ local -a alts=() for arg in "$@"; do case $arg in (rockspec) alts+=(':rock file:{_files -g "*.rockspec"}') ;; (rockpack) alts+=(':rock file:{_files -g "*.src.rock"}') ;; (external) alts+=(':external rock:') ;; (installed) alts+=(':local rock:__luarocks_installed_rocks') ;; esac done _alternative ${alts[@]} } # }}} # {{{ `build` command # arguments: # - must: .rockspec file / external rock # - optional: version (only when chossing external rock) local make_command_options=( '--pack-binary-rock[Produce a .rock file with the contents of compilation inside the current directory instead of installing it]' '--keep[Do not remove previously installed versions of the rock after building a new one]' '--branch=[Override the `source.branch` field in the loaded rockspec]:NAME:{_message "branch name"}' ) local build_command_options=( "${make_command_options[@]}" '--only-deps[Installs only the dependencies of the rock]' $option_deps_mode ) _luarocks_build(){ _arguments -A "-*" \ "${build_command_options[@]}" \ '1: :{__luarocks_rock "rockspec" "external"}' \ '2:: :__luarocks_rock_version' } # }}} # {{{ `config` command # arguments: # - must: VAR=VALUE local config_command_options=( '--lua-incdir[Path to Lua header files]' '--lua-libdir[Path to Lua library files]' '--lua-ver[Lua version (in major.minor format)]' '--system-config[Location of the system config file]' '--user-config[Location of the user config file]' '--rock-trees[Rocks trees in useFirst the user tree, then the system tree]' ) _luarocks_config(){ } # }}} # {{{ `doc` command # arguments: # - TODO local doc_command_options=( '--home[Open the home page of project]' '--list[List documentation files only]' ) _luarocks_doc(){ } # }}} # {{{ `download` command # arguments: # - must: external only rockspec local download_command_options=( '--all[Download all files if there are multiple matches]' '--source[Download .src.rock if available]' '--rockspec[Download .rockspec if available]' '--arch=[Download rock for a specific architecture]:ARCH:' ) _luarocks_download(){ _arguments -A "-*" \ "${download_command_options[@]}" \ '1: :{__luarocks_rock "external"}' \ '2:: :__luarocks_rock_version' } # }}} # {{{ `help` command # arguments: # must: luarocks sub command _luarocks_help(){ _arguments '1: :__luarocks_command' } # }}} # {{{ `install` command # arguments: # - must: .rockspec file, or external rock # - optional: version # NOTE: it receives the same argument as the build command and it accepts the same options as well _luarocks_install(){ _luarocks_build } # }}} # {{{ `lint` command # arguments: # must: rockspec file (first and last) _luarocks_lint(){ _arguments '1:ROCKSPEC_FILE:{__luarocks_rock "rockspec"}' } # }}} # {{{ `list` command # NOTE: receives only options local list_command_options=( '--outdated[List only rocks for which there is a higher version available in the rocks server]' '--porcelain[Produce machine-friendly output]' ) _luarocks_list(){ _arguments "${list_command_options[@]}" } # }}} # {{{ `make` command # arguments: # - optional: rockspec file # NOTE: it's options were already described above. _luarocks_make(){ _arguments '1:: :{__luarocks_rock "rockspec"}' } # }}} # {{{ `new_version` command # arguments: # - optional: .rockspec file / external rock # - optional: version (unless a --tag was given) # - optional: URL local new_version_command_options=( '--tag=[if no version is specified, this option'"'"'s argument is used instead]:TAG:__git_tag' ) _luarocks_new_version(){ } # }}} # {{{ `pack` command # arguments: # - must: .rockspec file / external rock # - optional: version _luarocks_pack(){ _luarocks_build } # }}} # {{{ `path` command # NOTE: receives only options local path_command_options=( '--bin[Adds the system path to the output]' '--append[Appends the paths to the existing paths]' '--lr-path[Exports the Lua path (not formatted as shell command)]' '--lr-cpath[Exports the Lua cpath (not formatted as shell command)]' '--lr-bin[Exports the system path (not formatted as shell command)]' ) _luarocks_path(){ _arguments "${path_command_options[@]}" } # }}} # {{{ `purge` command # NOTE: receives only options yet --tree is mandatory # NOTE: --force can be used only in conjunction with --old-versions local option_force='--force[Force removing old versions when]' local purge_command_options=( '--old-versions[Keep the highest-numbered version of each rock and remove the other ones]' $option_force ) _luarocks_purge(){ } # }}} # {{{ `remove` command # arguments: # - must: locally installed rock # - optional: version local option_force_fast='--force-fast[works like --force but doesn'"'"'t reports forced removals]' local remove_command_options=( $option_deps_mode $option_force $option_force_fast ) _luarocks_remove(){ _arguments -A "-*" \ "${remove_command_options[@]}" \ '1: :{__luarocks_rock "installed"}' \ '2:: :__luarocks_rock_version' } # }}} # {{{ `search` command # arguments: # - must: string as a search query local search_command_options=( '--source[Return only rockspecs and source rocks]' '--binary[Return only pure Lua and binary rocks (rocks that can be used with the "install" command without requiring a C toolchain)]' '--all[List all contents of the server that are suitable to this platform, do not filter by name]' ) _luarocks_search(){ _arguments \ "${search_command_options[@]}" \ '*:SEARCH QUERY:' } # }}} # {{{ `show` command # arguments: # - must: installed rock local show_command_options=( '--home[home page of project]' '--modules[all modules provided by this package as used by require()]' '--deps[packages this package depends on]' '--rockspec[the full path of the rockspec file]' '--mversion[the package version]' '--rock-tree[local tree where rock is installed]' '--rock-dir[data directory of the installed rock]' ) _luarocks_show(){ _arguments \ "${show_command_options[@]}" \ '1: :{__luarocks_rock "installed"}' } # }}} # {{{ `unpack` command # arguments: # - must: rock package file / an external rock # - optional: version (only when chossing external rock) local unpack_command_options=( '--force[Unpack files even if the output directory already exists]' ) _luarocks_unpack(){ _arguments \ "${unpack_command_options[@]}" \ '1: :{__luarocks_rock "rockpack" "external"}' } # }}} # {{{ `upload` command # arguments: # - must: rockspec file with .src.rock extension local upload_command_options=( '--skip-pack[Do not pack and send source rock]' '--api-key=[Give it an API key]:KEY:{_message "api key"}' '--force[Replace existing rockspec if the same revision of a module already exists]' ) _luarocks_upload(){ _arguments \ "${upload_command_options[@]}" \ '1: :{__luarocks_rock "rockspec"}' } # }}} # {{{ `write_rockspec` command # arguments: # - optional: name # - optional: version # - optional: URL / PATH # receives as an argument a name and a version with optionally a URL/PATH local write_rockspec_command_options=( '--output=[Write the rockspec with the given filename]:FILE:_files' '--license=[A license string, ]:LICENSE:{_message -e "write a license string such as "MIT/X11" or "GNU GPL v3"}' '--summary=[A short one-line description summary]:SUMMARY_TEXT:{_message -e "write a short summary of the rock"}' '--detailed=[A longer description string]:DETAILED_TEXT:{_message -e "write a detailed description of the rock"}' '--homepage=[Project homepage]:URL:_luarocks_write_rockspec_homepage' '--lua-version=[Supported Lua versions]:LUA_VER:__luarocks_version' '--rockspec-format=[Rockspec format version, such as "1.0" or "1.1"]:VER: ' '--tag=[Tag to use. Will attempt to extract version number from it]:TAG:__git_tag' '--lib=[A comma-separated list of libraries that C files need to link to]:__luarocks_c_libs' ) _luarocks_write_rockspec(){ } # }}} # The real thing _arguments -C \ '(--server --only-server)--server=[Fetch rocks/rockspecs from this server]:HOST:_hosts' \ '(--server --only-server)--only-server=[Fetch rocks/rockspecs from this server only]:HOST:_hosts' \ '--only-sources=[Restrict downloads to paths matching the given URL]:URL:_urls' \ '--tree=[Which tree to operate on]:TREE:{_files -/}' \ '--local[Use the tree in the user'"'"'s home directory]' \ '--verbose[Display verbose output of commands executed]' \ '--timeout=[Timeout on network operations]:SECONDS:{_message "timeout (seconds)"}' \ '1: :__luarocks_command' \ '*::arg:->args' case "$state" in (args) curcontext="${curcontext%:*:*}:luarocks_${words[1]}:" case $words[1] in (build) _luarocks_build ;; (config) _luarocks_config ;; (doc) _luarocks_doc ;; (download) _luarocks_download ;; (help) _luarocks_help ;; (install) _luarocks_install ;; (lint) _luarocks_lint ;; (list) _luarocks_list ;; (make) _luarocks_make ;; (new_version) _luarocks_new_version ;; (pack) _luarocks_pack ;; (path) _luarocks_path ;; (purge) _luarocks_purge ;; (remove) _luarocks_remove ;; (search) _luarocks_search ;; (show) _luarocks_show ;; (unpack) _luarocks_unpack ;; (upload) _luarocks_upload ;; (write_rockspec) _luarocks_write_rockspec ;; esac ;; esac