public inbox for gentoo-guru@lists.gentoo.org
 help / color / mirror / Atom feed
From: Takuya Wakazono <pastalian46@gmail.com>
To: "Anna (cybertailor) Vyalkova" <cyber+gentoo@sysrq.in>
Cc: gentoo-guru@lists.gentoo.org
Subject: Re: [gentoo-guru] [PATCH 1/2] click-app.eclass: new eclass
Date: Sun, 24 Nov 2024 22:00:33 +0900	[thread overview]
Message-ID: <CAAaAyq_1O9twBoq9hWaA8BOm6B_jJUQ8zqVsL=48T89mCBLuqQ@mail.gmail.com> (raw)
In-Reply-To: <20241123073302.24038-2-cyber+gentoo@sysrq.in>

On Sat, Nov 23, 2024 at 4:33 PM Anna (cybertailor) Vyalkova
<cyber+gentoo@sysrq.in> wrote:
>
> Signed-off-by: Anna (cybertailor) Vyalkova <cyber+gentoo@sysrq.in>
> ---
>  eclass/click-app.eclass | 162 ++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 162 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 000000000..0a75b9ea5
> --- /dev/null
> +++ b/eclass/click-app.eclass
> @@ -0,0 +1,162 @@
> +# Copyright 2024 Gentoo Authors
> +# Distributed under the terms of the GNU General Public License v2
> +
> +# @ECLASS: click-app.eclass
> +# @MAINTAINER:
> +# Anna <cyber+gentoo@sysrq.in>
> +# @AUTHOR:
> +# Anna <cyber+gentoo@sysrq.in>
> +# @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
> +
> +readonly _CLICK_COMPLETIONS_BUILD_DIR="${WORKDIR}/${P}_click-shell-completions"
> +
> +# @FUNCTION: click-app_enable_completions
> +# @USAGE: <script...>
> +# @DESCRIPTION:
> +# Set up IUSE, BDEPEND, python_compile_all() and python_install_all() to
> +# generate and install shell completions for the given scripts.
> +#
> +# This function does not overwrite python_compile_all() or python_install_all()
> +# if they are already defined (e.g. by distutils_enable_sphinx).  You should call
> +# click-app_python_compile_all or click-app_python_install_all in the
> +# corresponding common phase functions in such cases.
> +#
> +# 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} )"
How about 'shell-completion'? 'bash-completion' controlling fish and zsh
completion files feels unintuitive.

> +
> +       _CLICK_SCRIPTS=()
> +       for script in "${@}"; do
> +               _CLICK_SCRIPTS+=( "${script}" )
> +       done
> +       readonly -a _CLICK_SCRIPTS
> +
> +       if ! declare -f python_compile_all; then
> +               python_compile_all() { click-app_python_compile_all; }
> +       fi
> +
> +       if ! declare -f python_install_all; then
> +               python_install_all() {
> +                       click-app_python_install_all
> +                       distutils-r1_python_install_all
> +               }
> +       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_pkg_setup
> +# @DESCRIPTION:
> +# Ensure that only enabled Python implementations are used in python_..._all
> +# pseudo-phases when shell completions are to be built.
> +click-app_pkg_setup() {
> +       debug-print-function "${FUNCNAME}" "${@}"
> +       use bash-completion || return 0
> +
> +       # If the package isn't built for a Python implementation that runs
> +       # python_compile_all(), we won't be able to call Python scripts from
> +       # its install tree.
> +       DISTUTILS_ALL_SUBPHASE_IMPLS=()
> +       for impl in "${PYTHON_COMPAT[@]}"; do
> +               use "python_targets_${impl}" &&
> +                       DISTUTILS_ALL_SUBPHASE_IMPLS+=( "${impl}" )
> +       done
> +}
> +
> +# @FUNCTION: click-app_python_compile_all
> +# @DESCRIPTION:
> +# Build shell completions for all scripts for which shell completions
> +# were requested with click-app_enable_completions.
> +click-app_python_compile_all() {
> +       debug-print-function "${FUNCNAME}" "${@}"
> +       use bash-completion || return 0
> +
> +       for script in "${_CLICK_SCRIPTS[@]}"; do
> +               build_click_completions "${script}"
> +       done
> +}
> +
> +# @FUNCTION: click-app_python_install_all
> +# @DESCRIPTION:
> +# Install generated shell completions for all scripts for which shell
> +# completions were requested with click-app_enable_completions.
> +click-app_python_install_all() {
> +       debug-print-function "${FUNCNAME}" "${@}"
> +       use bash-completion || return 0
> +
> +       for script in "${_CLICK_SCRIPTS[@]}"; do
> +               install_click_completions "${script}"
> +       done
> +}
> +
> +# @FUNCTION: build_click_completions
> +# @USAGE: <script>
> +# @DESCRIPTION:
> +# Build shell completions for a script.
> +build_click_completions() {
> +       debug-print-function "${FUNCNAME}" "${@}"
> +       (( $# == 1 )) ||
> +               die "${FUNCNAME} takes exactly one argument"
> +
> +       local env_var_name out_path script_path t
> +
> +       script_path="${BUILD_DIR}/install${EPREFIX}/usr/bin/${1}"
> +       [[ -f "${script_path}" ]] ||
> +               die "${script_path} not found, build_click_completions call wrong"
> +
> +       # convert to screaming snake case
> +       t=${1^^}
> +       t=${t//-/_}
> +       env_var_name="_${t}_COMPLETE"
> +
> +       mkdir -p "${_CLICK_COMPLETIONS_BUILD_DIR}" || die
> +       for shell in bash fish zsh; do
> +               out_path="${_CLICK_COMPLETIONS_BUILD_DIR}/${1}.${shell}"
> +
> +               echo "${env_var_name}=${shell}_source ${script_path} > ${out_path}" >&2
> +               local -x "${env_var_name}"="${shell}_source" || die
> +               "${script_path}" > "${out_path}" || die
> +       done
> +}
> +
> +# @FUNCTION: install_click_completions
> +# @USAGE: <script>
> +# @DESCRIPTION:
> +# Install generated shell completions for a script.
> +install_click_completions() {
> +       debug-print-function "${FUNCNAME}" "${@}"
> +       (( $# == 1 )) ||
> +               die "${FUNCNAME} takes exactly one argument"
> +
> +       newbashcomp "${_CLICK_COMPLETIONS_BUILD_DIR}/${1}.bash" "${1}"
> +       newfishcomp "${_CLICK_COMPLETIONS_BUILD_DIR}/${1}.fish" "${1}.fish"
> +       newzshcomp "${_CLICK_COMPLETIONS_BUILD_DIR}/${1}.zsh" "_${1}"
> +}
> +
> +fi
> +
> +EXPORT_FUNCTIONS pkg_setup
> --
> 2.47.0
>
>


  reply	other threads:[~2024-11-24 22:47 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-23  7:27 [gentoo-guru] [0/2] new eclass: click-app; generate and install shell completions Anna (cybertailor) Vyalkova
2024-11-23  7:27 ` [gentoo-guru] [PATCH 1/2] click-app.eclass: new eclass Anna (cybertailor) Vyalkova
2024-11-24 13:00   ` Takuya Wakazono [this message]
2024-11-24 14:30     ` Anna (cybertailor) Vyalkova
2024-11-24 13:21   ` Michał Górny
2024-11-24 14:46     ` Anna (cybertailor) Vyalkova
2024-11-24 15:26       ` Michał Górny
2024-11-23  7:27 ` [gentoo-guru] [PATCH 2/2] dev-util/bump-my-version: add completions support Anna (cybertailor) Vyalkova

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAAaAyq_1O9twBoq9hWaA8BOm6B_jJUQ8zqVsL=48T89mCBLuqQ@mail.gmail.com' \
    --to=pastalian46@gmail.com \
    --cc=cyber+gentoo@sysrq.in \
    --cc=gentoo-guru@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox