public inbox for gentoo-guru@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Michał Górny" <mgorny@gentoo.org>
To: "Anna (cybertailor) Vyalkova" <cyber+gentoo@sysrq.in>,
	 gentoo-guru@lists.gentoo.org
Subject: Re: [gentoo-guru] [PATCH 1/2] click-app.eclass: new eclass
Date: Sun, 24 Nov 2024 14:21:31 +0100	[thread overview]
Message-ID: <c48c6e6bdbc7c8abde5e2b443f2a98636d7c3144.camel@gentoo.org> (raw)
In-Reply-To: <20241123073302.24038-2-cyber+gentoo@sysrq.in>

[-- Attachment #1: Type: text/plain, Size: 6217 bytes --]

On Sat, 2024-11-23 at 12:27 +0500, Anna (cybertailor) Vyalkova 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} )"
> +
> +	_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

That sounds like a lot of complexity for something that could be more
cleanly achieved by a single function called from python_install()
(and detecting that it's been called already by existing completions).

-- 
Best regards,
Michał Górny


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 512 bytes --]

  parent 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
2024-11-24 14:30     ` Anna (cybertailor) Vyalkova
2024-11-24 13:21   ` Michał Górny [this message]
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=c48c6e6bdbc7c8abde5e2b443f2a98636d7c3144.camel@gentoo.org \
    --to=mgorny@gentoo.org \
    --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