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

181
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
## Here start the build logic for s in "${source[@]}"; do
##
while (( "$#" >= 1 )); do
case "$1" in
-F) FAKE_PACKAGE=1 ;;
-OS) OVERRIDE_SOURCE=1 ;;
*) err "Unkown option $1"; break ;;
esac
shift
done
source "${buildfile}"
# Run package and generate deb and exit
if (( FAKE_PACKAGE )); then
if is_function package; then
msg "run function: custom package..."
run_function_safe package
fi
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 if ! grep "::" <<< "$s" > /dev/null; then
err "source must contain \"::\" to specify file name" err "source must contain \"::\" to specify file name"
exit 1 exit 1
fi fi
done done
}
msg "cleaning \$srcdir and \$pkgdir..." function clean_dir {
rm -rf "${srcdir}" "${pkgdir}" msg "cleaning \$srcdir and \$pkgdir..."
rm -rf "${srcdir}" "${pkgdir}"
mkdir -p "${srcdir}" "${pkgdir}"
}
mkdir -p "${srcdir}" "${pkgdir}" function retrieve_source {
msg "retrieving source..."
msg "retrieving source..." for s in "${source[@]}"; do
for s in "${source[@]}"; do
file_name="${s%%::*}" file_name="${s%%::*}"
url="${s##*::}" url="${s##*::}"
retrive_source_single "${file_name}" "${url}" retrive_source_single "${file_name}" "${url}"
done done
}
msg "extracting source..." function extract_source {
for s in "${source[@]}"; do msg "extracting source..."
for s in "${source[@]}"; do
file_name="${s%%::*}" file_name="${s%%::*}"
url="${s##*::}" url="${s##*::}"
extract_source_single "${file_name}" "${url}" extract_source_single "${file_name}" "${url}"
done done
}
if is_function pkgver; then function update_pkgver {
msg "run function: pkgver..." if ! is_function pkgver; then
return
fi
msg "updating 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"