Feature: "-STF" to show target deb file, refactor code

This commit is contained in:
leafee98 2023-03-28 21:41:14 +08:00
parent 5da0362dca
commit f53a2e1de8

199
makedeb
View file

@ -8,7 +8,8 @@ pkgdir="${workspace}/pkg"
FAKE_PACKAGE=0 FAKE_PACKAGE=0
OVERRIDE_SOURCE=0 OVERRIDE_SOURCE=0
SHOW_TARGET_FILE=0
QUIET=0
set -o functrace set -o functrace
set -o nounset set -o nounset
@ -40,14 +41,17 @@ fi
readonly ALL_OFF BOLD BLUE GREEN RED YELLOW readonly ALL_OFF BOLD BLUE GREEN RED YELLOW
function msg { function msg {
(( QUIET )) && return 0
local mesg=$1; shift local mesg=$1; shift
printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" printf "${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@"
} }
function msg2 { function msg2 {
(( QUIET )) && return 0
local mesg=$1; shift local mesg=$1; shift
printf "${BLUE} ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@" printf "${BLUE} ->${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n" "$@"
} }
function err { function err {
(( QUIET )) && return 0
local mesg=$1; shift local mesg=$1; shift
printf "${RED} ->${ALL_OFF}${BOLD}${RED} ${mesg}${ALL_OFF}\n" "$@" >&2 printf "${RED} ->${ALL_OFF}${BOLD}${RED} ${mesg}${ALL_OFF}\n" "$@" >&2
} }
@ -82,6 +86,8 @@ EOF
} }
function generate_deb { function generate_deb {
msg "generating deb package..."
local tmpdir="$(mktemp --directory)" local tmpdir="$(mktemp --directory)"
local data_tgz="${tmpdir}/data.tar.gz" local data_tgz="${tmpdir}/data.tar.gz"
local control_tgz="${tmpdir}/control.tar.gz" local control_tgz="${tmpdir}/control.tar.gz"
@ -304,79 +310,48 @@ function is_function {
declare -F "$1" > /dev/null declare -F "$1" > /dev/null
} }
function check_source_validation {
# check if all source is valid
for s in "${source[@]}"; do
if ! grep "::" <<< "$s" > /dev/null; then
err "source must contain \"::\" to specify file name"
exit 1
fi
done
}
## function clean_dir {
## Here start the build logic msg "cleaning \$srcdir and \$pkgdir..."
## rm -rf "${srcdir}" "${pkgdir}"
mkdir -p "${srcdir}" "${pkgdir}"
}
while (( "$#" >= 1 )); do function retrieve_source {
case "$1" in msg "retrieving source..."
-F) FAKE_PACKAGE=1 ;; for s in "${source[@]}"; do
-OS) OVERRIDE_SOURCE=1 ;; file_name="${s%%::*}"
*) err "Unkown option $1"; break ;; url="${s##*::}"
esac
shift
done
source "${buildfile}" retrive_source_single "${file_name}" "${url}"
done
}
# Run package and generate deb and exit function extract_source {
if (( FAKE_PACKAGE )); then msg "extracting source..."
for s in "${source[@]}"; do
file_name="${s%%::*}"
url="${s##*::}"
if is_function package; then extract_source_single "${file_name}" "${url}"
msg "run function: custom package..." done
run_function_safe package }
function update_pkgver {
if ! is_function pkgver; then
return
fi fi
msg "updating pkgver..."
msg "generating control info..."
mkdir -p "${pkgdir}/DEBIAN"
echo 2 > "${pkgdir}/DEBIAN/compat"
debian_control > "${pkgdir}/DEBIAN/control"
function debian_hooks_warpper { is_function "$1" && "$1" > "$2" && chmod +x "$2" || true; }
debian_hooks_warpper debian_preinst ${pkgdir}/DEBIAN/preinst
debian_hooks_warpper debian_postinst ${pkgdir}/DEBIAN/postinst
debian_hooks_warpper debian_prerm ${pkgdir}/DEBIAN/prerm
debian_hooks_warpper debian_postrm ${pkgdir}/DEBIAN/postrm
msg "generating deb package..."
generate_deb
exit $?
fi
# check if all source is valid
for s in "${source[@]}"; do
if ! grep "::" <<< "$s" > /dev/null; then
err "source must contain \"::\" to specify file name"
exit 1
fi
done
msg "cleaning \$srcdir and \$pkgdir..."
rm -rf "${srcdir}" "${pkgdir}"
mkdir -p "${srcdir}" "${pkgdir}"
msg "retrieving source..."
for s in "${source[@]}"; do
file_name="${s%%::*}"
url="${s##*::}"
retrive_source_single "${file_name}" "${url}"
done
msg "extracting source..."
for s in "${source[@]}"; do
file_name="${s%%::*}"
url="${s##*::}"
extract_source_single "${file_name}" "${url}"
done
if is_function pkgver; then
msg "run function: pkgver..."
newpkgver="$(run_function_safe pkgver)" newpkgver="$(run_function_safe pkgver)"
if [[ "${newpkgver}" != "${pkgver:-}" ]] ; then if [[ "${newpkgver}" != "${pkgver:-}" ]] ; then
mapfile -t bfcontent < "${buildfile}" mapfile -t bfcontent < "${buildfile}"
@ -395,17 +370,83 @@ if is_function pkgver; then
msg2 "updated version: ${newpkgver}" msg2 "updated version: ${newpkgver}"
fi fi
pkgver="${newpkgver}" pkgver="${newpkgver}"
}
function generate_control {
msg "generating control info..."
mkdir -p "${pkgdir}/DEBIAN"
echo 2 > "${pkgdir}/DEBIAN/compat"
debian_control > "${pkgdir}/DEBIAN/control"
function debian_hooks_warpper { is_function "$1" && "$1" > "$2" && chmod +x "$2" || true; }
debian_hooks_warpper debian_preinst ${pkgdir}/DEBIAN/preinst
debian_hooks_warpper debian_postinst ${pkgdir}/DEBIAN/postinst
debian_hooks_warpper debian_prerm ${pkgdir}/DEBIAN/prerm
debian_hooks_warpper debian_postrm ${pkgdir}/DEBIAN/postrm
}
function run_function {
if is_function "$1" ; then
msg "run function: $1"
run_function_safe "$1"
fi
}
##
## Here start the build logic
##
while (( "$#" >= 1 )); do
case "$1" in
-F) FAKE_PACKAGE=1 ;;
-OS) OVERRIDE_SOURCE=1 ;;
-STF) SHOW_TARGET_FILE=1 ; QUIET=1 ;;
-Q) QUIET=1 ;;
*) err "Unkown option $1"; break ;;
esac
shift
done
source "${buildfile}"
if (( ! FAKE_PACKAGE )) ; then
if (( SHOW_TARGET_FILE )) && ! is_function pkgver ; then
echo "$(get_deb_name)"
exit 0
fi
check_source_validation
clean_dir
retrieve_source
extract_source
update_pkgver
if (( SHOW_TARGET_FILE )) && is_function pkgver ; then
echo "$(get_deb_name)"
exit 0
fi
run_function build
# recursive call self to run rest task in fakeroot
msg "entering fakeroot environment..."
fakeroot -- bash -$- "${BASH_SOURCE[0]}" -F "${ARGLIST[@]}" || exit $?
msg "leaving fakeroot environment..."
msg "builds has done"
else
run_function package
generate_control
generate_deb
fi fi
if is_function build; then
msg "run function: custom build..."
run_function_safe build
fi
# recursive call self to run rest task in fakeroot
msg "entering fakeroot environment..."
fakeroot -- bash -$- "${BASH_SOURCE[0]}" -F "${ARGLIST[@]}" || exit $?
msg "leaving fakeroot environment..."
msg "builds has done"