# Copyright 2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# Autogenerated by pycargoebuild 0.16.0

EAPI=8

RUST_MIN_VER="1.94.0"

CRATES=""

inherit cargo shell-completion

DESCRIPTION="Git hook manager and project linting tool"
HOMEPAGE="https://hk.jdx.dev https://github.com/jdx/hk"
# The published crate carries the generated Pkl modules (pkl/Builtins.pkl and
# pkl/builtins_meta.json) that the GitHub tag archive omits. Without them hk is
# built without its embedded modules and rejects hk:/Config.pkl imports.
SRC_URI="https://static.crates.io/crates/${PN}/${P}.crate"
SRC_URI+=" https://github.com/0x6d6e647a/mndz-overlay-assets/releases/download/hk-${PV}/hk-${PV}-crates.tar.xz"

LICENSE="MIT"
# Dependent crate licenses
LICENSE+="
	Apache-2.0 Apache-2.0-with-LLVM-exceptions BSD CDLA-Permissive-2.0
	ISC MIT MPL-2.0 Unicode-3.0 ZLIB
"
SLOT="0"
KEYWORDS="~amd64 ~arm ~arm64 ~loong ~mips ~ppc ~ppc64 ~riscv ~s390 ~sparc ~x86"
IUSE="bash-completion fish-completion test zsh-completion"
RESTRICT="!test? ( test )"

DEPEND="sys-libs/zlib"
RDEPEND="
	${DEPEND}
	dev-vcs/git
	bash-completion? ( dev-util/usage )
	fish-completion? ( dev-util/usage )
	zsh-completion? ( dev-util/usage )
"
BDEPEND="
	dev-build/cmake
	virtual/pkgconfig
	test? ( dev-vcs/git )
	bash-completion? ( dev-util/usage )
	fish-completion? ( dev-util/usage )
	zsh-completion? ( dev-util/usage )
"

# rust does not use *FLAGS from make.conf, silence portage warning
QA_FLAGS_IGNORED="usr/bin/hk"

src_configure() {
	# aws-lc-sys jitterentropy #errors when Portage -O2 follows the crate -O0.
	export AWS_LC_SYS_NO_JITTER_ENTROPY=1

	# Fail the build instead of silently omitting the embedded Pkl modules.
	export HK_REQUIRE_EMBEDDED_PKL=1

	# Skip the generate-docs helper binary
	cargo_src_configure --bin hk
}

src_test() {
	# Upstream unit tests open the current directory as a git repository, and
	# the crate has no .git.
	git init -q || die
	git add -- . ':!target' || die
	git -c user.name="Portage" -c user.email="portage@localhost" \
		commit -q -m "Track test fixtures" || die

	cargo_src_test

	# Check the just-built executable, not an installed hk, with empty caches
	# and no network. Both public module entry points must resolve.
	local hk="${S}/$(cargo_target_dir)/hk"
	[[ -x ${hk} ]] || die "built executable not found: ${hk}"

	local fixture="${T}/embedded-pkl"
	mkdir -p "${fixture}/project" "${fixture}/home" || die
	cat > "${fixture}/project/hk.pkl" <<-'EOF' || die
	amends "hk:/Config.pkl"
	import "hk:/Builtins.pkl"

	hooks {
	  ["check"] {
	    steps {
	      ["fmt"] = Builtins.prettier
	    }
	  }
	}
	EOF

	pushd "${fixture}/project" >/dev/null || die
	git init -q . || die
	env -i \
		PATH="${PATH}" \
		HOME="${fixture}/home" \
		HK_CACHE_DIR="${fixture}/cache" \
		HK_PKL_CACHE_DIR="${fixture}/pkl-cache" \
		HK_STATE_DIR="${fixture}/state" \
		HK_CONFIG_DIR="${fixture}/config" \
		HK_PKL_OFFLINE=1 \
		"${hk}" validate || die "offline hk:/ module validation failed"
	popd >/dev/null || die
}

src_install() {
	cargo_src_install
	dodoc README.md CHANGELOG.md

	# Completions are generated via the external usage CLI (jdx/usage).
	# Put the just-installed hk on PATH for --usage-cmd.
	local -x PATH="${ED}/usr/bin:${PATH}"

	if use bash-completion; then
		usage g completion bash hk --usage-cmd "hk usage" > hk.bash ||
			die 'Unable to generate bash completions'
		newbashcomp hk.bash hk
	fi
	if use fish-completion; then
		usage g completion fish hk --usage-cmd "hk usage" > hk.fish ||
			die 'Unable to generate fish completions'
		newfishcomp hk.fish hk.fish
	fi
	if use zsh-completion; then
		usage g completion zsh hk --usage-cmd "hk usage" > hk.zsh ||
			die 'Unable to generate zsh completions'
		newzshcomp hk.zsh _hk
	fi
}