From b46a0711d4d5f8b70721f96869f38bc8a8e4f229 Mon Sep 17 00:00:00 2001 From: "Anna (cybertailor) Vyalkova" Date: Sat, 23 Nov 2024 12:21:47 +0500 Subject: [PATCH] click-app.eclass: new eclass Signed-off-by: Anna (cybertailor) Vyalkova --- eclass/click-app.eclass | 129 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 eclass/click-app.eclass diff --git a/eclass/click-app.eclass b/eclass/click-app.eclass new file mode 100644 index 0000000000..d429e8128c --- /dev/null +++ b/eclass/click-app.eclass @@ -0,0 +1,129 @@ +# Copyright 2024 Gentoo Authors +# Distributed under the terms of the GNU General Public License v2 + +# @ECLASS: click-app.eclass +# @MAINTAINER: +# Anna +# @AUTHOR: +# Anna +# @SUPPORTED_EAPIS: 8 +# @BLURB: eclass for Click-based Python applications +# @DESCRIPTION: +# This eclass provides a streamlined way to generate and install shell +# completions for Python applications based on the Click library +# (dev-python/click package). + +case ${EAPI} in + 8) ;; + *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;; +esac + +if [[ ! ${_CLICK_APP_ECLASS} ]]; then +_CLICK_APP_ECLASS=1 + +inherit distutils-r1 shell-completion + +# @FUNCTION: click-app_enable_completions +# @USAGE: +# @DESCRIPTION: +# Set up IUSE, BDEPEND and python_install() to generate and install shell +# completions for the given scripts. +# +# This function does not overwrite python_install() if it is already defined. +# If you need to extend python_install(), you can call the original +# implementation as click-app_python_install. +# +# This function must be called in global scope. +# +# See also: https://click.palletsprojects.com/en/stable/shell-completion/ +click-app_enable_completions() { + debug-print-function "${FUNCNAME}" "${@}" + (( $# >= 1 )) || + die "${FUNCNAME} takes at least one argument" + + IUSE+=" bash-completion" + BDEPEND+=" bash-completion? ( ${RDEPEND} )" + + readonly -a _CLICK_SHELLCOMP_SCRIPTS=( "${@}" ) + + click-app_python_install() { + debug-print-function "${FUNCNAME}" "${@}" + use bash-completion || return 0 + + local script_name + for script_name in "${_CLICK_SHELLCOMP_SCRIPTS[@]?}"; do + click_install_completions "${script_name:?}" + done + } + + if ! declare -f python_install; then + python_install() { + click-app_python_install + distutils-r1_python_install + } + fi + + # we need to ensure successful return in case we're called last, + # otherwise Portage may wrongly assume sourcing failed + return 0 +} + +# @FUNCTION: click-app_python_install +# @USAGE: +# @DESCRIPTION: +# Generate and install shell completions for the given scripts. +# +# Note that this function checks if USE="bash-completion" is enabled, and if +# not automatically exits. Therefore, there is no need to wrap this function +# in an "if" statement. +# +# This function needs to be called before distutils-r1_python_install. + +# @FUNCTION: click_install_completions +# @USAGE: