summaryrefslogtreecommitdiff
path: root/devel/hs-optparse-applicative/application.mk
blob: e072a77e25035b24db2accc85b0cf3777bccb5b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# $NetBSD: application.mk,v 1.2 2022/09/03 16:27:56 pho Exp $
#
# Generate shell completion scripts automatically.
#
# This mk fragment should be included in packages that install executables
# which parse command-line arguments using optparse-applicative. If the
# package installs a library too, and the library depends on
# optparse-applicative, the package should include both "application.mk"
# and "buildlink3.mk" so that pkgtools/createbuildlink works correctly.
#
# Package-settable variables:
#
# OPTPARSE_APPLICATIVE_EXECUTABLES
#	A list of names of executables supporting automatic completor
#	generation. These executables are assumed to be installed in
#	${PREFIX}/bin.
#
#	Default value: ${PKGBASE}
#
# Keywords: haskell optparse-applicative
#

OPTPARSE_APPLICATIVE_EXECUTABLES?=	${PKGBASE}

.include "../../devel/hs-optparse-applicative/buildlink3.mk"

# Declarations for ../../mk/misc/show.mk
_VARGROUPS+=					optparse-applicative
_PKG_VARS.optparse-applicative=			\
	OPTPARSE_APPLICATIVE_EXECUTABLES
_DEF_VARS.optparse-applicative=			\
	PRINT_PLIST_AWK				\
	GENERATE_PLIST				\
	OA.shell-types				\
	OA.generate-cmds
_USE_VARS.optparse-applicative=			\
	DESTDIR					\
	PKGBASE					\
	PREFIX					\
	WRKSRC
_IGN_VARS.optparse-applicative=			\
	OA.script.bash				\
	OA.script.fish				\
	OA.script.zsh
_LISTED_VARS.optparse-applicative=		\
	PRINT_PLIST_AWK				\
	GENERATE_PLIST				\
	OA.generate-cmds
_SORTED_VARS.optparse-applicative=		\
	OPTPARSE_APPLICATIVE_EXECUTABLES	\
	OA.shell-types

# A list of shell types currently supported by optparse-applicative.
OA.shell-types=	bash fish zsh

# The paths to completion script for each shell. "@exec@" will be
# substituted later.
OA.script.bash=	share/bash-completion/completions/@exec@
OA.script.fish=	share/fish/completions/@exec@.fish
OA.script.zsh=	share/zsh/site-functions/_@exec@

# Completion scripts should be excluded from PLIST files because the exact
# set of installed scripts can vary depending on the version of
# optparse-applicative. Note that we can't loop on
# ${OPTPARSE_APPLICATIVE_EXECUTABLES} with ".for" because ${PKGBASE} is not
# yet defined.
PRINT_PLIST_AWK+=					\
	${OPTPARSE_APPLICATIVE_EXECUTABLES:@.exec.@	\
		${OA.shell-types:@.shell.@		\
			/^${OA.script.${.shell.}:S/@exec@/${.exec.}/:S,/,\\/,g}$$/ \
				{ next }		\
		@}					\
	@}

# Automatically insert completion scripts to PLIST.
GENERATE_PLIST+=	\
	${OPTPARSE_APPLICATIVE_EXECUTABLES:@.exec.@	\
		${OA.shell-types:@.shell.@		\
			${ECHO} ${OA.script.${.shell.}:S/@exec@/${.exec.}/:Q}; \
		@}					\
	@}

# The post-install hook to generate completion scripts. We need to run the
# executables we just built in order to generate scripts. However, unlike
# cabal-install, the plain Cabal doesn't provide a convenient way to do
# it. So we have to resort to something fragile.
OA.generate-cmds=	\
	${OPTPARSE_APPLICATIVE_EXECUTABLES:@.exec.@	\
		${STEP_MSG} "Generating shell completion scripts for ${.exec.}"; \
		${OA.shell-types:@.shell.@		\
			${INSTALL_DATA_DIR} ${DESTDIR:Q}${PREFIX:Q}/${OA.script.${.shell.}:S/@exec@/${.exec.}/:H:Q}; \
			${PKGSRC_SETENV} LD_LIBRARY_PATH=${WRKSRC:Q}/dist/build \
				${WRKSRC:Q}/dist/build/${.exec.:Q}/${.exec.:Q} \
				--${.shell.:Q}-completion-script \
				${PREFIX:Q}/bin/${.exec.:Q} \
				> ${DESTDIR:Q}${PREFIX:Q}/${OA.script.${.shell.}:S/@exec@/${.exec.}/:Q}; \
		@}					\
	@}
.PHONY: generate-optparse-applicative-shell-completion-scripts
post-install: generate-optparse-applicative-shell-completion-scripts
generate-optparse-applicative-shell-completion-scripts:
	${RUN}${OA.generate-cmds}