void-packages/common/hooks/pre-pkg/99-pkglint-subpkgs.sh
Đoàn Trần Công Danh 3996821f07 99-pkglint-subpkgs: correct for multiline subpackages
As discussing in [1], on template with "subpackages" as multilines will
report false positive on some packages will never be built.

There're multiple problems here:

- expanded "subpackages" will have an empty line if it has a newline
  inside template
- "sed" expression couldn't work with multilines "subpackages"

Let's not quote "$subpkgs" and "$subpackages" in "printf" to let the
shell do expansion and trim the empty lines for us. And rewrite the
"sed" expression to work with multilines "subpackages"

[1]: https://github.com/void-linux/void-packages/pull/26939#issuecomment-739098547
2020-12-14 09:35:02 +07:00

50 lines
1.5 KiB
Bash

# vim: set ts=4 sw=4 et:
#
# This hook executes the following tasks:
# - Warns if the main package is in subpackages=
# - Warns if a subpackage is unreachable (never appears in subpackages=)
hook() {
local subpkgs matches
# Run this only against the main package
if [ "$pkgname" != "$sourcepkg" ]; then
return 0
fi
if [ -z "$subpackages" ]; then
return 0
fi
subpkgs=$(get_subpkgs)
# Sort the strings so they can be compare for equality
subpkgs="$(printf '%s\n' $subpkgs | sort)"
subpackages="$(printf '%s\n' $subpackages | sort)"
if [ "$subpackages" = "$subpkgs" ]; then
return 0
fi
# sed supports comment but let's put them here
# 1: print everything between pairs of <""> in subpackages[+]?="..."
# 2: multiline subpackages="...\n..."
# 2.1: For any line in the middle, i.e. no <"> exists, print it
# 2.2: For the first line, print everything after <">
# 2.3: For last line, print everything before <">
matches="$(sed -n -e 's/subpackages.*"\(.*\)"[^"]*$/\1/p' \
-e '/subpackages[^"]*"[^"]*$/,/"/{
/"/!p
/subpackages/s/.*"//p
s/".*//p
}' $XBPS_SRCPKGDIR/$pkgname/template |
tr ' ' '\n' | sort)"
for s in $subpkgs; do
grep -q "^$s$" <<< "$matches" ||
msg_warn "${s}_package() defined but will never be built.\n"
done
grep -q "^$pkgname$" <<< "$matches" &&
msg_warn "$pkgname is sourcepkg but is in subpackages=.\n" || :
}