bash-completion-r1.eclass 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Copyright 1999-2014 Gentoo Foundation
  2. # Distributed under the terms of the GNU General Public License v2
  3. # @ECLASS: bash-completion-r1.eclass
  4. # @MAINTAINER:
  5. # mgorny@gentoo.org
  6. # @BLURB: A few quick functions to install bash-completion files
  7. # @EXAMPLE:
  8. #
  9. # @CODE
  10. # EAPI=5
  11. #
  12. # src_configure() {
  13. # econf \
  14. # --with-bash-completion-dir="$(get_bashcompdir)"
  15. # }
  16. #
  17. # src_install() {
  18. # default
  19. #
  20. # newbashcomp contrib/${PN}.bash-completion ${PN}
  21. # }
  22. # @CODE
  23. inherit toolchain-funcs
  24. case ${EAPI:-0} in
  25. 0|1|2|3|4|5|6) ;;
  26. *) die "EAPI ${EAPI} unsupported (yet)."
  27. esac
  28. # @FUNCTION: _bash-completion-r1_get_bashdir
  29. # @INTERNAL
  30. # @DESCRIPTION:
  31. # First argument is name of the string in bash-completion.pc
  32. # Second argument is the fallback directory if the string is not found
  33. # @EXAMPLE:
  34. # _bash-completion-r1_get_bashdir completionsdir /usr/share/bash-completion
  35. _bash-completion-r1_get_bashdir() {
  36. debug-print-function ${FUNCNAME} "${@}"
  37. if $(tc-getPKG_CONFIG) --exists bash-completion &>/dev/null; then
  38. local path
  39. path=$($(tc-getPKG_CONFIG) --variable="${1}" bash-completion) || die
  40. # we need to return unprefixed, so strip from what pkg-config returns
  41. # to us, bug #477692
  42. echo "${path#${EPREFIX}}"
  43. else
  44. echo "${2}"
  45. fi
  46. }
  47. # @FUNCTION: _bash-completion-r1_get_bashcompdir
  48. # @INTERNAL
  49. # @DESCRIPTION:
  50. # Get unprefixed bash-completion completions directory.
  51. _bash-completion-r1_get_bashcompdir() {
  52. debug-print-function ${FUNCNAME} "${@}"
  53. _bash-completion-r1_get_bashdir completionsdir /usr/share/bash-completion/completions
  54. }
  55. # @FUNCTION: _bash-completion-r1_get_helpersdir
  56. # @INTERNAL
  57. # @DESCRIPTION:
  58. # Get unprefixed bash-completion helpers directory.
  59. _bash-completion-r1_get_bashhelpersdir() {
  60. debug-print-function ${FUNCNAME} "${@}"
  61. _bash-completion-r1_get_bashdir helpersdir /usr/share/bash-completion/helpers
  62. }
  63. # @FUNCTION: get_bashcompdir
  64. # @DESCRIPTION:
  65. # Get the bash-completion completions directory.
  66. get_bashcompdir() {
  67. debug-print-function ${FUNCNAME} "${@}"
  68. echo "${EPREFIX}$(_bash-completion-r1_get_bashcompdir)"
  69. }
  70. # @FUNCTION: get_bashhelpersdir
  71. # @INTERNAL
  72. # @DESCRIPTION:
  73. # Get the bash-completion helpers directory.
  74. get_bashhelpersdir() {
  75. debug-print-function ${FUNCNAME} "${@}"
  76. echo "${EPREFIX}$(_bash-completion-r1_get_bashhelpersdir)"
  77. }
  78. # @FUNCTION: dobashcomp
  79. # @USAGE: file [...]
  80. # @DESCRIPTION:
  81. # Install bash-completion files passed as args. Has EAPI-dependant failure
  82. # behavior (like doins).
  83. dobashcomp() {
  84. debug-print-function ${FUNCNAME} "${@}"
  85. (
  86. insinto "$(_bash-completion-r1_get_bashcompdir)"
  87. doins "${@}"
  88. )
  89. }
  90. # @FUNCTION: newbashcomp
  91. # @USAGE: file newname
  92. # @DESCRIPTION:
  93. # Install bash-completion file under a new name. Has EAPI-dependant failure
  94. # behavior (like newins).
  95. newbashcomp() {
  96. debug-print-function ${FUNCNAME} "${@}"
  97. (
  98. insinto "$(_bash-completion-r1_get_bashcompdir)"
  99. newins "${@}"
  100. )
  101. }
  102. # @FUNCTION: bashcomp_alias
  103. # @USAGE: <basename> <alias>...
  104. # @DESCRIPTION:
  105. # Alias <basename> completion to one or more commands (<alias>es).
  106. bashcomp_alias() {
  107. debug-print-function ${FUNCNAME} "${@}"
  108. [[ ${#} -lt 2 ]] && die "Usage: ${FUNCNAME} <basename> <alias>..."
  109. local base=${1} f
  110. shift
  111. for f; do
  112. dosym "${base}" "$(_bash-completion-r1_get_bashcompdir)/${f}" \
  113. || return
  114. done
  115. }