Newer
Older
dub_jkp / scripts / zsh-completion / _dub
@Geod24 Geod24 on 4 Jan 2020 11 KB Add zsh completion script
  1. #compdef dub
  2.  
  3. # Useful help:
  4. # https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org
  5. # http://zsh.sourceforge.net/Doc/Release/Completion-System.html
  6. # http://zdharma.org/Zsh-100-Commits-Club/Zsh-Native-Scripting-Handbook.html
  7. #
  8. # Completions installed on your system, e.g. for MacOSX + Homebrew users:
  9. # /usr/local/Cellar/zsh/$VERSION/share/zsh/functions
  10. # The GIT completion is quite amazing (and equally complex)
  11. # The CVS completion is much easier to grok (e.g. for function dispatch)
  12.  
  13.  
  14. # Entry point
  15. _dub() {
  16. # TODO:
  17. # - Handle registry URLs
  18. # - Handle multiple dub (e.g. ./bin/dub add [TAB])
  19. # - Interactively query configuration (for -c and --override-config)
  20. # => Dub does not currently support this
  21. # - Add ability to provide version, e.g. vibe-d@0.8.6 (see dub add)
  22. # - Get registry packages if it doesn't make us lag
  23. # - Query compilers
  24.  
  25. # Note that global arguments won't show up when completing commands
  26. # This is on purpose, to reduce the amount of options being shown during completion,
  27. # as users are much more likely to be looking for command-specific options.
  28. _arguments -S -C \
  29. '(* : -)'{-h,--help}'[Display general or command specific help and exit]' \
  30. '(* : -)--version[Print version information and exit]' \
  31. \
  32. '--root=[Run as if dub was started in given path]: :_directories' \
  33. '--skip-registry=[Skips searching packages on certain repositories]:mode:(none standard configured all)' \
  34. '--registry=[Search the given registry URL first when resolving dependencies]:registry URL:_urls' \
  35. '--bare[Read only packages contained in the current directory]' \
  36. '--cache=[Puts any fetched packages in the specified location]:cache location:(local|system|user)' \
  37. '--annotate[Do not perform any action, just print what would be done]' \
  38. \
  39. + '(verbosity)' \
  40. '--vquiet[Print no messages]' \
  41. '--verror[Only print errors]' \
  42. {-q,--quiet}'[Only print warnings and errors]' \
  43. {-v,--verbose}'[Print diagnostic output]' \
  44. '--vverbose[Print debug output]' \
  45. \
  46. '--[End of dub arguments, the following will be sent to the program]' \
  47. '*::dub command:_command_dispatch'
  48. }
  49.  
  50.  
  51. # Command dispatch function
  52. _command_dispatch() {
  53. declare -a commands=(
  54. init:'Initialize a new dub package'
  55. run:'Build and run a dub package (default action)'
  56. build:'Build a dub package (by name, or in the working directory by default)'
  57. test:'Execute the tests of a dub package'
  58. generate:'Generates project files using the specified generator'
  59. describe:'Prints a JSON description of the project and its dependencies'
  60. clean:'Removes intermediate build files and cached build results'
  61. dustmite:'Create reduced test cases for build errors'
  62.  
  63. fetch:'Manually retrieves and caches a package'
  64. add:'Adds dependencies to the package file'
  65. remove:'Removes a cached package'
  66. upgrade:'Forces an upgrade of the dependencies'
  67. add-path:'Adds a default package search path'
  68. remove-path:'Removes a package search path'
  69. add-local:'Adds a local package directory (e.g. a git repository)'
  70. remove-local:'Removes a local package directory'
  71. list:'Prints a list of all local packages dub is aware of'
  72. search:'Search for available packages'
  73. add-override:'Adds a new package override'
  74. remove-override:'Removes an existing package override'
  75. list-overrides:'Prints a list of all local package overrides'
  76. clean-caches:'Removes cached metadata'
  77. convert:'Converts the file format of the package recipe'
  78. )
  79. if (( CURRENT == 1 )); then
  80. _alternative \
  81. 'files:filename:_files -g "*.d"' \
  82. "commands:dub command: _describe -t commands command commands"
  83. else
  84. integer ret=0
  85. local cmd=${${(k)commands}[(r)$words[1]:*]%%:*}
  86. if [ ! -z "$cmd" ]; then
  87. _call_function ret _dub_$cmd
  88. else
  89. # Assume single file, it takes program arguments
  90. _message "Arguments for single file package $words[1]"
  91. fi
  92. return 0
  93. fi
  94. }
  95.  
  96. (( $+functions[_dub_add] )) ||
  97. _dub_add() {
  98. # TODO: Make dub list more machine-readable
  99. local -a dubList=("${(@f)$(dub list)}")
  100. # First element is 'Packages present in the system...'
  101. # Last element is an empty line
  102. dubList=(${dubList[2,$#dubList-1]})
  103. local -A pkgs
  104. # Collect versions and names
  105. for ((i = 1; i <= ${#dubList}; i++)); do
  106. pkg_name=${${=${dubList[i]}}[1]}
  107. pkg_version=${${${=${dubList[i]}}[2]}%:}
  108. # Subpackages are currently not supported by 'dub add' (see dlang/dub#1846)
  109. if [ ! -z "${pkg_name:#*:*}" ]; then
  110. pkgs[${pkg_name}]+="${pkg_version}, "
  111. fi
  112. done
  113. # Merge versions
  114. local -a packages
  115. for name ver in ${(kv)pkgs}; do
  116. packages+=${name}:"${ver%, }"
  117. done
  118.  
  119. # Package list includes ':' which is used as description
  120. #_values 'local packages' ${pkgs//:/\\:}
  121.  
  122. # Use the unique property to get rid of subpkgs
  123. _describe -t packages package packages
  124. }
  125.  
  126. (( $+functions[_dub_init] )) ||
  127. _dub_init() {
  128. _arguments -S -C \
  129. ':package directory:_directories' \
  130. '*:package dependency:_dub_add' \
  131. '(-t --type)'{-t,--type}'[Set the type of project to generate]:project type:((minimal\:"simple hello world project (default)" vibe.d\:"minimal HTTP server based on vibe.d" deimos\:"skeleton for C header bindings" custom\:"custom project provided by dub package"))' \
  132. '(-f --format)'{-f,--format}'[Sets the format to use for the manifest file]:format:(json sdl)' \
  133. '(-n --non-iteractive)'{-n,--non-iteractive}'[Do not prompt for values and use only defaults]' \
  134. '(* : -)'{-h,--help}'[Display general or command specific help and exit]'
  135. }
  136.  
  137. (( $+functions[_dub_list] )) ||
  138. _dub_list() {
  139. _arguments -S -C \
  140. '(* : -)'{-h,--help}'[Display general or command specific help and exit]'
  141. }
  142.  
  143. # dub generate, dub build, dub run...
  144. (( $+functions[_dub_generate_generic] )) ||
  145. _dub_generate_generic() {
  146. _arguments -S -C \
  147. $@ \
  148. '::package:_dub_add' \
  149. '(* : -)'{-h,--help}'[Display general or command specific help and exit]' \
  150. '(-b --build)'{-b,--build=}'[Specifies the type of build to perform]:build type:("debug (default)" plain release release-debug release-nobounds unittest profile profile-gc docs ddox cov unittest-cov syntax)' \
  151. '(-c --config)'{-c,--config=}'[Builds the specified configuration]:package configuration: ' \
  152. '*--override-config=[ Uses the specified configuration for a certain dependency]:dependency/config: ' \
  153. '--compiler=[Specifies the compiler binary to use (can be a path)]:compiler:(dmd gdc ldc gdmd ldmd)' \
  154. '(-a --arch)'{-a,--arch=}'[Force a different architecture (e.g. x86 or x86_64)]:architecture: ' \
  155. '(-d --debug)*'{-d,--debug=}'[Define the specified debug version identifier when building]:Debug version: ' \
  156. '--nodeps[Do not resolve missing dependencies before building]' \
  157. '--build-mode=[Specifies the way the compiler and linker are invoked]:build mode:("separate (default)" allAtOnce singleFile)' \
  158. '--single[Treats the package name as a filename. The file must contain a package recipe comment]:file:_files -g "*.d"' \
  159. '--filter-versions[Experimental: Filter version identifiers and debug version identifiers to improve build cache efficiency]' \
  160. '--combined[Tries to build the whole project in a single compiler run]' \
  161. '--print-builds[Prints the list of available build types]' \
  162. '--print-configs[Prints the list of available configurations]' \
  163. '--print-platform[Prints the identifiers for the current build platform as used for the manifests build field]' \
  164. '--parallel[Runs multiple compiler instances in parallel, if possible]'
  165. }
  166.  
  167. (( $+functions[_dub_generate] )) ||
  168. _dub_generate() {
  169. local curcontext="$curcontext"
  170. declare -a generators=(
  171. visuald:'VisualD project files',
  172. sublimetext:'SublimeText project file'
  173. cmake:'CMake build scripts'
  174. build:'Builds the package directly (use "dub build" instead)'
  175. )
  176. local localArgs=(
  177. ':generator: _describe -t generators generator generators'
  178. )
  179.  
  180. integer ret=0
  181. _call_function ret _dub_generate_generic ${(@)localArgs}
  182. return ret
  183. }
  184.  
  185. (( $+functions[_dub_build] )) ||
  186. _dub_build() {
  187. local localArgs=(
  188. $@
  189. '--rdmd[Use rdmd instead of directly invoking the compiler]'
  190. '(-f --force)'{-f,--force}'[Forces a recompilation even if the target is up to date]'
  191. '(-y--yes)'{-y,--yes}'[Assume "yes" as answer to all interactive prompts]'
  192. '(-n--non-interactive)'{-n,--non-interactive}'[Do not enter interactive mode]'
  193. )
  194.  
  195. integer ret=0
  196. _call_function ret _dub_generate_generic ${(@)localArgs}
  197. return ret
  198. }
  199.  
  200. (( $+functions[_dub_run] )) ||
  201. _dub_run() {
  202. local localArgs=(
  203. '--[End of dub arguments, the following will be sent to the program]'
  204. '--temp-build[Builds the project in the temp folder if possible]'
  205. )
  206.  
  207. integer ret=0
  208. _call_function ret _dub_build ${(@)localArgs}
  209. return ret
  210. }
  211.  
  212. (( $+functions[_dub_test] )) ||
  213. _dub_test() {
  214. local localArgs=(
  215. '--main-file=[Specifies a custom file containing the main() function to use for running the tests]:main file:_files -g "*.d"'
  216. )
  217.  
  218. integer ret=0
  219. _call_function ret _dub_build ${(@)localArgs}
  220. return ret
  221. }
  222.  
  223. (( $+functions[_dub_describe] )) ||
  224. _dub_describe() {
  225. local localArgs=(
  226. '--import-paths[Shortcut for --data=import-paths --data-list]'
  227. '--string-import-paths[Shortcut for --data=string-import-paths --data-list]'
  228. '--data=[List the values of a particular build setting]:listing options: _values -s , argument main-source-file dflags lflags libs linker-files source-files versions debug-versions import-paths string-import-paths import-files options'
  229. '--data-list[Output --data information in list format (line-by-line)]'
  230. '--data-0[Output --data information using null-delimiters, rather than spaces or newlines]'
  231. )
  232.  
  233. integer ret=0
  234. _call_function ret _dub_build ${(@)localArgs}
  235. return ret
  236. }
  237.  
  238. (( $+functions[_dub_clean] )) ||
  239. _dub_clean() {
  240. _arguments -S -C \
  241. '(* : -)'{-h,--help}'[Display general or command specific help and exit]' \
  242. + '(toclean)' \
  243. '--all-packages[Cleans up *all* known packages (dub list)]' \
  244. ':package:_dub_add'
  245. }
  246.  
  247. (( $+functions[_dub_dustmite] )) ||
  248. _dub_dustmite() {
  249. local localArgs=(
  250. ':target directory:_directories'
  251. '--compiler-status=[The expected status code of the compiler run]:status code: '
  252. '--compiler-regex=[A regular expression used to match against the compiler output]:regex: '
  253. '--linker-status=[The expected status code of the linker run]:status code: '
  254. '--linker-regex=[A regular expression used to match against the linker output]:regex: '
  255. '--program-status=[The expected status code of the built executable]:status code: '
  256. '--program-regex=[A regular expression used to match against the program output]:regex: '
  257. '--[End of dub arguments, the following will be sent to the program]'
  258. )
  259.  
  260. integer ret=0
  261. _call_function ret _dub_generate_generic ${(@)localArgs}
  262. return ret
  263. }
  264.  
  265. _dub