Compare commits

..

2 commits

Author SHA1 Message Date
leafee98 3065562dab Refactor code, refactor build script
- Refactor log
- Use variable to specific which step to run, and stop after which step
- Add a lot of program arguments
2023-08-17 16:42:56 +08:00
leafee98 3500ce0058 typo: retrive, fix err use a second level log mark 2023-08-11 21:48:46 +08:00

236
makedeb
View file

@ -6,10 +6,30 @@ buildfile="${workspace}/DEBBUILD"
srcdir="${workspace}/src" srcdir="${workspace}/src"
pkgdir="${workspace}/pkg" pkgdir="${workspace}/pkg"
FAKE_PACKAGE=0 _IN_FAKEROOT=0
_BACKUP_STDOUT=3 # use file descriptor 3 to backup stdout
_BACKUP_STDERR=4 # use file descriptor 4 to backup stderr
_ARGLIST=("$@")
OVERRIDE_SOURCE=0 OVERRIDE_SOURCE=0
SHOW_TARGET_FILE=0
QUIET=0 QUIET=0
PACKAGELIST=0
FORCE=0
_STEP_CLEAN=1
_STEP_RETRIEVE_SOURCE=1
_STEP_EXTRACT_SOURCE=1
_STEP_UPDATE_PKGVER=1
_STEP_BUILD=1
_STEP_PACKAGE=1
_STEP_CREATE_ARCHIVE=1
_STOP_AFTER_CLEAN=0
_STOP_AFTER_RETRIEVE_SOURCE=0
_STOP_AFTER_EXTRACT_SOURCE=0
_STOP_AFTER_UPDATE_PKGVER=0
_STOP_AFTER_BUILD=0
_STOP_AFTER_PACKAGE=0
_STOP_AFTER_CREATE_ARCHIVE=0
set -o functrace set -o functrace
set -o nounset set -o nounset
@ -53,7 +73,7 @@ function msg2 {
function err { function err {
(( QUIET )) && return 0 (( 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
} }
function get_full_version { function get_full_version {
@ -86,11 +106,24 @@ function debian_control {
[[ -n "${priority:=optional}" ]] && echo "Priority: ${priority}" [[ -n "${priority:=optional}" ]] && echo "Priority: ${priority}"
echo "Description: ${pkgdesc}" echo "Description: ${pkgdesc}"
} }
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 generate_deb { function generate_deb {
msg "generating deb package..." 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"
@ -107,7 +140,12 @@ function generate_deb {
rm -rf "${tmpdir}" rm -rf "${tmpdir}"
msg2 "generated deb: %s" "$(get_deb_name)" msg2 "Generated deb: %s" "$(get_deb_name)"
}
function step_create_archive {
generate_control
generate_deb
} }
function url_type { function url_type {
@ -138,10 +176,12 @@ function create_soft_link {
if [[ ! -e "${src}" ]] ; then if [[ ! -e "${src}" ]] ; then
err "soft_link src %s not exist" "${src}" err "soft_link src %s not exist" "${src}"
return 1
fi fi
if [[ -e "${tgt}" ]] ; then if [[ -e "${tgt}" ]] ; then
err "soft_link tgt %s already exist" "${tgt}" err "soft_link tgt %s already exist" "${tgt}"
return 1
fi fi
ln --symbolic "${src}" "${tgt}" ln --symbolic "${src}" "${tgt}"
@ -158,17 +198,17 @@ function url_fragment {
# $1 is relative path to file # $1 is relative path to file
# $2 is url/(relative path to materials) # $2 is url/(relative path to materials)
function retrive_source_single { function retrieve_source_single {
local file_name="$1" local file_name="$1"
local url="$2" local url="$2"
case "$(url_type "${url}")" in case "$(url_type "${url}")" in
"git") "git")
if [[ -d "${file_name}" ]]; then if [[ -d "${file_name}" ]]; then
msg2 "updating ${file_name} from ${url} with git..." msg2 "Updating ${file_name} from ${url} with git..."
git --git-dir="${workspace}/${file_name}" fetch --all git --git-dir="${workspace}/${file_name}" fetch --all
else else
msg2 "cloning ${file_name} from ${url} with git..." msg2 "Cloning ${file_name} from ${url} with git..."
local git_source="${url##git+}" local git_source="${url##git+}"
git_source="${git_source%%#*}" git_source="${git_source%%#*}"
git clone --mirror "${git_source}" "${workspace}/${file_name}" git clone --mirror "${git_source}" "${workspace}/${file_name}"
@ -178,21 +218,21 @@ function retrive_source_single {
if (( ! OVERRIDE_SOURCE )) && [[ -f "${workspace}/${file_name}" ]] ; then if (( ! OVERRIDE_SOURCE )) && [[ -f "${workspace}/${file_name}" ]] ; then
msg2 "${file_name} already exists, skip download" msg2 "${file_name} already exists, skip download"
else else
msg2 "retriving ${file_name} from ${url} with curl..." msg2 "Retrieving ${file_name} from ${url} with curl..."
curl --location "${url}" --output "${workspace}/${file_name}" curl --location "${url}" --output "${workspace}/${file_name}"
fi fi
;; ;;
"file") "file")
if [[ -e "${workspace}/${url}" ]] ; then if [[ -e "${workspace}/${url}" ]] ; then
msg2 "found local file: ${url}" msg2 "Found local file: ${url}"
else else
err "local file not found: ${url}" err "Local file not found: ${url}"
err "Aborting..." err "Aborting..."
exit 1 exit 1
fi fi
;; ;;
*) *)
err "retriving url as type ${url_type} not supported" err "Retrieving url as type ${url_type} not supported"
err "Aborting..." err "Aborting..."
exit 1 exit 1
esac esac
@ -229,7 +269,7 @@ function extract_source_single {
esac esac
fi fi
msg2 "extracting git ${workspace}/${file_name} with reference ${ref}" msg2 "Extracting git ${workspace}/${file_name} with reference ${ref}"
git clone --shared "${workspace}/${file_name}" "${srcdir}/${file_name}" git clone --shared "${workspace}/${file_name}" "${srcdir}/${file_name}"
git -C "${srcdir}/${file_name}" switch --force-create makedeb --no-track "${ref}" git -C "${srcdir}/${file_name}" switch --force-create makedeb --no-track "${ref}"
;; ;;
@ -261,11 +301,11 @@ function decompress_source_file {
*.zip) *.zip)
cmd="unzip" ;; cmd="unzip" ;;
*) *)
msg2 "no need to decompress, skip %s" "${file}" msg2 "No need to decompress, skip %s" "${file}"
return return
esac esac
msg2 "decompressing %s with %s" "${file}" "${cmd}" msg2 "Decompressing %s with %s" "${file}" "${cmd}"
local res=0 local res=0
case "$cmd" in case "$cmd" in
"tar") "tar")
@ -323,24 +363,24 @@ function check_source_validation {
done done
} }
function clean_dir { function step_clean_dir {
msg "cleaning \$srcdir and \$pkgdir..." msg "Cleaning \$srcdir and \$pkgdir..."
rm -rf "${srcdir}" "${pkgdir}" rm -rf "${srcdir}" "${pkgdir}"
mkdir -p "${srcdir}" "${pkgdir}" mkdir -p "${srcdir}" "${pkgdir}"
} }
function retrieve_source { function step_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}" retrieve_source_single "${file_name}" "${url}"
done done
} }
function extract_source { function step_extract_source {
msg "extracting source..." msg "Extracting source..."
for s in "${source[@]}"; do for s in "${source[@]}"; do
file_name="${s%%::*}" file_name="${s%%::*}"
url="${s##*::}" url="${s##*::}"
@ -349,12 +389,12 @@ function extract_source {
done done
} }
function update_pkgver { function step_update_pkgver {
if ! is_function pkgver; then if ! is_function pkgver; then
return return
fi fi
msg "updating pkgver..." 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}"
@ -371,33 +411,34 @@ function update_pkgver {
source "${buildfile}" source "${buildfile}"
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 { function run_function {
if is_function "$1" ; then if is_function "$1" ; then
msg "run function: $1" msg "Run function: $1"
run_function_safe "$1" run_function_safe "$1"
fi fi
} }
function disable_stdout {
exec {_BACKUP_STDOUT}>&1
exec 1>/dev/null
}
function enable_stdout {
exec 1>&"$_BACKUP_STDOUT"
}
function show_help {
echo "$0 [OPTIONS]"
echo " -OS override source"
echo " -STF show the filename to be generated"
echo " -Q quiet, disable log"
}
## ##
## Here start the build logic ## Here start the build logic
@ -405,11 +446,36 @@ function run_function {
while (( "$#" >= 1 )); do while (( "$#" >= 1 )); do
case "$1" in case "$1" in
-F) FAKE_PACKAGE=1 ;; -F) _IN_FAKEROOT=1 ;;
-OS) OVERRIDE_SOURCE=1 ;; -OS|--override-source) OVERRIDE_SOURCE=1 ;;
-STF) SHOW_TARGET_FILE=1 ; QUIET=1 ;; -Q) QUIET=1 ;;
-Q) QUIET=1 ;; -f|--force) FORCE=1 ;;
*) err "Unkown option $1"; break ;; --noextract)
_STEP_CLEAN=0
_STEP_RETRIEVE_SOURCE=0
_STEP_EXTRACT_SOURCE=0
_STEP_UPDATE_PKGVER=0
;;
--nobuild)
_STEP_BUILD=0
_STEP_PACKAGE=0
_STEP_CREATE_ARCHIVE=0
_STOP_AFTER_UPDATE_PKGVER=1
;;
--noarchive)
_STEP_CREATE_ARCHIVE=0
_STOP_AFTER_BUILD=1
;;
--packagelist)
PACKAGELIST=1
;;
-h|--help) show_help ; exit 0;;
*) err "Unkown option $1"
err "Use $0 --help for help"
exit 1 ;;
esac esac
shift shift
done done
@ -417,39 +483,65 @@ done
source "${buildfile}" source "${buildfile}"
if (( ! FAKE_PACKAGE )) ; then if (( PACKAGELIST )) ; then
if (( SHOW_TARGET_FILE )) && ! is_function pkgver ; then echo "$(get_deb_name)"
echo "$(get_deb_name)" exit 0
exit 0 fi
fi
if (( QUIET )) ; then
disable_stdout
fi
if (( ! _IN_FAKEROOT )) ; then
check_source_validation check_source_validation
clean_dir if (( _STEP_CLEAN )) ; then
step_clean_dir
retrieve_source
extract_source
update_pkgver
if (( SHOW_TARGET_FILE )) && is_function pkgver ; then
echo "$(get_deb_name)"
exit 0
fi fi
run_function build if (( _STEP_RETRIEVE_SOURCE )) ; then
step_retrieve_source
fi
# recursive call self to run rest task in fakeroot if (( _STEP_EXTRACT_SOURCE )) ; then
msg "entering fakeroot environment..." step_extract_source
fakeroot -- bash -$- "${BASH_SOURCE[0]}" -F "${ARGLIST[@]}" || exit $? fi
msg "leaving fakeroot environment..."
msg "builds has done" if (( _STEP_UPDATE_PKGVER )) ; then
step_update_pkgver
(( _STOP_AFTER_UPDATE_PKGVER )) && msg "Sources are ready."
fi
# Check if the package already exists
if (( ! FORCE )) ; then
if [[ -f "$(get_deb_name)" ]] ; then
err "The package already exists. (use -f to force build)"
exit 13
fi
fi
if (( _STEP_BUILD )) ; then
run_function build
(( _STOP_AFTER_BUILD )) && msg "Package directory is ready."
fi
if (( _STEP_PACKAGE || _STEP_CREATE_ARCHIVE )) ; then
# 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..."
fi
else else
run_function package if (( _STEP_PACKAGE )) ; then
run_function package
fi
generate_control if (( _STEP_CREATE_ARCHIVE )) ; then
step_create_archive
generate_deb msg "Finish making: $(get_deb_name) $(date)"
fi
fi
if (( QUIET )) ; then
enable_stdout
fi fi