2023-11-13 09:33:28 -05:00
|
|
|
#!/bin/sh
|
|
|
|
|
2024-07-08 05:41:45 -04:00
|
|
|
## SPDX-FileCopyrightText: 2023 - 2024 Benjamin Grande M. S. <ben.grande.b@gmail.com>
|
2023-11-13 09:33:28 -05:00
|
|
|
##
|
|
|
|
## SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
|
|
|
set -eu
|
|
|
|
|
|
|
|
usage(){
|
2024-08-06 12:15:24 -04:00
|
|
|
printf '%s\n' "Usage: ${0##*/} PROJECT [PROJECT ...]"
|
2023-11-13 09:33:28 -05:00
|
|
|
}
|
|
|
|
|
2024-06-14 13:22:43 -04:00
|
|
|
## Escape multiline strings for sed.
|
2024-06-21 11:00:06 -04:00
|
|
|
escape_key(){
|
|
|
|
key_type="${1}"
|
|
|
|
if test "${key_type}" = "scriptlet"; then
|
2024-08-06 12:15:24 -04:00
|
|
|
printf '%s\n' "${2}" | sed -e ':a;N;$!ba;s/\n/\\n /g' | \
|
|
|
|
sed -e 's/\$/\\$/'
|
2024-06-21 11:00:06 -04:00
|
|
|
elif test "${key_type}" = "text"; then
|
2024-08-06 12:15:24 -04:00
|
|
|
printf '%s\n' "${2}" | sed -e ':a;N;$!ba;s/\n/\\n/g' | sed -e 's/\$/\\$/'
|
2024-06-21 11:00:06 -04:00
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
2024-06-14 13:22:43 -04:00
|
|
|
}
|
|
|
|
|
2023-11-13 09:33:28 -05:00
|
|
|
# get_scriptlet scriptlet-action
|
|
|
|
# [pre|post]-[install|upgrade], [pre|post]un-[uninstall|upgrade]
|
|
|
|
## Get scriptlet command, else fail safe.
|
|
|
|
get_scriptlet(){
|
|
|
|
scriptlet="$1"
|
2024-07-09 11:42:07 -04:00
|
|
|
scriptlet_begin="-- pkg:begin:${scriptlet} --"
|
|
|
|
scriptlet_end="-- pkg:end:${scriptlet} --"
|
2024-08-06 11:04:16 -04:00
|
|
|
scriptlet="$(sed -n -e \
|
2024-07-09 11:42:07 -04:00
|
|
|
"/^<\!${scriptlet_begin}>$/,/^<\!${scriptlet_end}>$/p" \
|
2024-08-06 11:04:16 -04:00
|
|
|
-- "${readme}" | sed -e '/^```.*/d;/^\S*$/d;/^<\!-- pkg:/d;s/^sudo //')"
|
2024-06-14 13:22:43 -04:00
|
|
|
if test -z "${scriptlet}"; then
|
2024-08-06 12:15:24 -04:00
|
|
|
printf '%s\n' "true"
|
2024-06-14 13:22:43 -04:00
|
|
|
return 0
|
|
|
|
fi
|
2024-06-21 11:00:06 -04:00
|
|
|
escape_key scriptlet "${scriptlet}"
|
2023-11-13 09:33:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
get_spec(){
|
|
|
|
"${spec_get}" "${project}" "${1}"
|
|
|
|
}
|
|
|
|
|
|
|
|
gen_spec(){
|
2024-08-06 12:15:24 -04:00
|
|
|
project="$(printf '%s\n' "${1}" | sed -e "s|salt/||;s|/.*||")"
|
|
|
|
if printf '%s\n' "${projects_seen}" | grep -qF -e " ${project} "; then
|
2024-07-08 09:21:49 -04:00
|
|
|
return
|
|
|
|
fi
|
|
|
|
projects_seen="${projects_seen} ${project} "
|
2024-06-21 11:00:06 -04:00
|
|
|
|
2024-08-06 12:15:24 -04:00
|
|
|
if printf '%s\n' "${unwanted}" | grep -q -e "^${project}$"; then
|
|
|
|
printf '%s\n' "warn: skipping spec of untracked formula: ${project}" >&2
|
2024-06-21 11:00:06 -04:00
|
|
|
return 0
|
|
|
|
fi
|
2023-11-13 09:33:28 -05:00
|
|
|
|
|
|
|
## Test if a standard option works without error.
|
|
|
|
get_spec name >/dev/null
|
|
|
|
|
|
|
|
group="$(get_spec group)"
|
|
|
|
template="rpm_spec/template/template.spec"
|
|
|
|
target="rpm_spec/${group}-${project}.spec"
|
2024-06-21 11:00:06 -04:00
|
|
|
intended_target="${target}"
|
|
|
|
if test "${2-}" = "test"; then
|
|
|
|
tmpdir="$(mktemp -d)"
|
|
|
|
target="${tmpdir}/${group}-${project}.spec"
|
|
|
|
trap 'rm -rf -- "${tmpdir}"' EXIT INT HUP QUIT ABRT
|
|
|
|
fi
|
2023-11-13 09:33:28 -05:00
|
|
|
|
|
|
|
readme="$(get_spec readme)"
|
|
|
|
|
2024-06-21 11:00:06 -04:00
|
|
|
project_name="$(get_spec project)"
|
|
|
|
version="$(get_spec version)"
|
2024-06-24 02:24:48 -04:00
|
|
|
license_csv="$(get_spec license_csv)"
|
2024-07-08 09:21:49 -04:00
|
|
|
## Ideally we would query the license, but it is a heavy call.
|
2024-08-06 12:15:24 -04:00
|
|
|
license="$(printf '%s\n' "${license_csv}" | sed -e "s/\,/ AND /g")"
|
2024-06-21 11:00:06 -04:00
|
|
|
vendor="$(get_spec vendor)"
|
|
|
|
packager="$(get_spec packager)"
|
|
|
|
url="$(get_spec url)"
|
|
|
|
bug_url="$(get_spec bug_url)"
|
|
|
|
requires="$(get_spec requires)"
|
|
|
|
summary="$(get_spec summary)"
|
2024-07-10 08:36:05 -04:00
|
|
|
description="$(get_spec description)"
|
|
|
|
description="$(escape_key text "${description}")"
|
2024-06-21 11:00:06 -04:00
|
|
|
file_roots="$(get_spec file_roots)"
|
|
|
|
changelog="$(get_spec changelog)"
|
|
|
|
|
2024-06-14 13:22:43 -04:00
|
|
|
pre_install="$(get_scriptlet pre-install)"
|
|
|
|
pre_upgrade="$(get_scriptlet pre-upgrade)"
|
|
|
|
post_install="$(get_scriptlet post-install)"
|
|
|
|
post_upgrade="$(get_scriptlet post-upgrade)"
|
|
|
|
preun_uninstall="$(get_scriptlet preun-uninstall)"
|
|
|
|
preun_upgrade="$(get_scriptlet preun-upgrade)"
|
|
|
|
postun_uninstall="$(get_scriptlet postun-uninstall)"
|
|
|
|
postun_upgrade="$(get_scriptlet postun-upgrade)"
|
2023-11-13 09:33:28 -05:00
|
|
|
|
|
|
|
sed \
|
|
|
|
-e "s/@PRE_INSTALL@/${pre_install}/" \
|
|
|
|
-e "s/@PRE_UPGRADE@/${pre_upgrade}/" \
|
|
|
|
-e "s/@POST_INSTALL@/${post_install}/" \
|
|
|
|
-e "s/@POST_UPGRADE@/${post_upgrade}/" \
|
|
|
|
-e "s/@PREUN_UNINSTALL@/${preun_uninstall}/" \
|
|
|
|
-e "s/@PREUN_UPGRADE@/${preun_upgrade}/" \
|
|
|
|
-e "s/@POSTUN_UNINSTALL@/${postun_uninstall}/" \
|
|
|
|
-e "s/@POSTUN_UPGRADE@/${postun_upgrade}/" \
|
2024-06-21 11:00:06 -04:00
|
|
|
-e "s|@FILE_ROOTS@|${file_roots}|" \
|
2024-06-24 02:24:48 -04:00
|
|
|
-e "s/@NAME@/${project}/" \
|
2024-06-21 11:00:06 -04:00
|
|
|
-e "s/@PROJECT@/${project_name}/" \
|
2023-11-13 09:33:28 -05:00
|
|
|
-e "s/@VERSION@/${version}/" \
|
2024-06-21 11:00:06 -04:00
|
|
|
-e "s/@SUMMARY@/${summary}/" \
|
|
|
|
-e "s/@GROUP@/${group}/" \
|
|
|
|
-e "s/@PACKAGER@/${packager}/" \
|
|
|
|
-e "s/@VENDOR@/${vendor}/" \
|
|
|
|
-e "s/@LICENSE@/${license}/" \
|
2024-06-24 02:24:48 -04:00
|
|
|
-e "s/@LICENSE_CSV@/${license_csv}/" \
|
2024-06-21 11:00:06 -04:00
|
|
|
-e "s|@BUG_URL@|${bug_url}|" \
|
|
|
|
-e "s|@URL@|${url}|" \
|
|
|
|
-e "s|@DESCRIPTION@|${description}|" \
|
2023-11-13 09:33:28 -05:00
|
|
|
-e "/@CHANGELOG@/d" \
|
2024-08-06 11:04:16 -04:00
|
|
|
-- "${template}" | tee -- "${target}" >/dev/null
|
2023-11-13 09:33:28 -05:00
|
|
|
|
|
|
|
requires_key=""
|
2024-08-06 11:04:16 -04:00
|
|
|
for r in $(printf '%s' "${requires}" | tr " " "\n" | sort -u); do
|
2024-06-21 11:00:06 -04:00
|
|
|
requires_key="${requires_key:-}Requires: ${group}-${r}\n"
|
2023-11-13 09:33:28 -05:00
|
|
|
done
|
2024-08-06 11:04:16 -04:00
|
|
|
sed -i -e "s/@REQUIRES@/${requires_key}/" -- "${target}" >/dev/null
|
2024-08-06 12:15:24 -04:00
|
|
|
printf '%s\n' "${changelog}" | tee -a -- "${target}" >/dev/null
|
2024-06-21 11:00:06 -04:00
|
|
|
|
|
|
|
if test "${2-}" = "test"; then
|
2024-08-06 11:04:16 -04:00
|
|
|
if ! cmp -s -- "${target}" "${intended_target}"; then
|
2024-08-06 12:15:24 -04:00
|
|
|
printf '%s\n' "error: ${intended_target} is not up to date" >&2
|
2024-08-06 11:04:16 -04:00
|
|
|
diff --color=auto -- "${intended_target}" "${target}" || true
|
2024-07-08 09:21:49 -04:00
|
|
|
fail=1
|
|
|
|
else
|
2024-08-06 11:04:16 -04:00
|
|
|
unstaged_target="$(git diff --name-only -- "${intended_target}")" ||
|
|
|
|
true
|
2024-07-10 08:36:05 -04:00
|
|
|
if test -n "${unstaged_target}"; then
|
2024-08-06 12:15:24 -04:00
|
|
|
err_msg="warn: ${intended_target} is up to date but it is not staged"
|
|
|
|
printf '%s\n' "${err_msg}" >&2
|
2024-07-08 09:21:49 -04:00
|
|
|
fi
|
2024-06-21 11:00:06 -04:00
|
|
|
fi
|
|
|
|
fi
|
2023-11-13 09:33:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case "${1-}" in
|
2024-06-12 08:44:04 -04:00
|
|
|
-h|--?help) usage; exit 1;;
|
2024-07-10 08:36:05 -04:00
|
|
|
*) ;;
|
2023-11-13 09:33:28 -05:00
|
|
|
esac
|
|
|
|
|
2024-08-06 12:15:24 -04:00
|
|
|
command -v git >/dev/null ||
|
|
|
|
{ printf '%s\n' "Missing program: git" >&2; exit 1; }
|
2024-07-10 08:36:05 -04:00
|
|
|
repo_toplevel="$(git rev-parse --show-toplevel)"
|
|
|
|
test -d "${repo_toplevel}" || exit 1
|
2024-07-19 09:29:17 -04:00
|
|
|
cd "${repo_toplevel}"
|
2024-07-10 08:36:05 -04:00
|
|
|
unset repo_toplevel
|
2023-11-13 09:33:28 -05:00
|
|
|
|
|
|
|
spec_get="./scripts/spec-get.sh"
|
2024-06-24 11:11:46 -04:00
|
|
|
ignored="$(git ls-files --exclude-standard --others --ignored salt/)"
|
|
|
|
untracked="$(git ls-files --exclude-standard --others salt/)"
|
2024-08-06 11:04:16 -04:00
|
|
|
unwanted="$(printf '%s\n%s\n' "${ignored}" "${untracked}" \
|
|
|
|
| grep -e "^salt/\S\+/README.md" | cut -d "/" -f2 | sort -u)"
|
2024-06-24 11:11:46 -04:00
|
|
|
|
2024-07-08 09:21:49 -04:00
|
|
|
fail=""
|
|
|
|
gen_mode=""
|
|
|
|
|
|
|
|
if test "${1-}" = "test"; then
|
|
|
|
gen_mode="test"
|
|
|
|
shift
|
2024-06-21 11:00:06 -04:00
|
|
|
fi
|
|
|
|
|
2024-08-06 12:15:24 -04:00
|
|
|
if printf '%s\n' "${@}" | \
|
|
|
|
grep -qE -e "(^scripts/| scripts/|/template.spec)" ||
|
2024-07-09 11:42:07 -04:00
|
|
|
test -z "${1-}"
|
|
|
|
then
|
2024-07-10 08:36:05 -04:00
|
|
|
# shellcheck disable=SC2046,SC2312
|
2024-06-12 08:44:04 -04:00
|
|
|
set -- $(find salt/ -mindepth 1 -maxdepth 1 -type d -printf '%f\n' \
|
2024-07-08 11:26:34 -04:00
|
|
|
| sort -d | tr "\n" " ")
|
2024-06-12 08:44:04 -04:00
|
|
|
fi
|
2024-07-08 09:21:49 -04:00
|
|
|
|
|
|
|
projects_seen=""
|
2024-07-10 08:36:05 -04:00
|
|
|
for p in "${@}"; do
|
|
|
|
gen_spec "${p}" "${gen_mode}"
|
2023-11-13 09:33:28 -05:00
|
|
|
done
|
2024-07-08 09:21:49 -04:00
|
|
|
|
|
|
|
if test "${fail}" = "1" && test "${gen_mode}" = "test"; then
|
|
|
|
exit 1
|
|
|
|
fi
|