debian-packages/script/build.sh

194 lines
6 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
function msg_info {
(( QUIET )) && return
local mesg="$1" ; shift
printf "INFO: $mesg\n" "$@"
}
function msg_warn {
(( QUIET )) && return
local mesg="$1" ; shift
printf "WARN: $mesg\n" "$@"
}
function usage {
echo "$_PROGRAM_NAME:"
echo " --package <dir-name> add a dir to build, can use multi times"
echo " --package-dir <dir> all dir in this directory is a package,"
echo " can use multi times"
echo " --makedeb-path <path> specify the path of makedeb"
echo " --upload-to-webdav https://webdav_host/path/dir"
echo " upload built file to a webdav server"
echo " --webdav-user <username> username of webdav server (if required)"
echo " --webdav-pass <password> password of webdav server (if required)"
echo ' --package-exists-hook "/path/to/hook --args {}"'
echo " this program will run the hook to check"
echo " if specific file already built, the {} will"
echo " be replaced with the final package name,"
echo " exit with 0 means package exists"
}
function upload_to_webdav {
local file="$1"
local webdav_path="$2"
local webdav_user="$3"
local webdav_pass="$4"
if [[ -z "$webdav_user" || -z "$webdav_pass" ]] ; then
curl --show-error --silent \
--upload-file "$file" "${webdav_path}/$file"
else
curl --show-error --silent --user "${webdav_user}:${webdav_pass}" \
--upload-file "$file" "${webdav_path}/$file"
fi
}
function run_hook {
local exe_str="$1"
local place_holder="$2"
local package_name="$3"
local log_exe_str="${4-}"
local final_exe_str="${exe_str/$place_holder/$package_name}"
if [[ -n "${log_exe_str}" ]] ; then
msg_info "Running: $final_exe_str"
fi
$final_exe_str
}
shopt -s nullglob
set -o functrace
set -o nounset
set -o errexit
set -o errtrace
_DELIMITER="================================================================"
_PROGRAM_NAME="$0"
_PACKGES_TO_BUILD=()
MAKEDEB_PATH=""
UPLOAD_TO_WEBDAV=""
WEBDAV_USER=""
WEBDAV_PASS=""
PACKAGE_DIR=()
PACKAGE_EXISTS_HOOK=""
QUIET=0
while (( "$#" >= 1 )); do
case "$1" in
--package) _PACKGES_TO_BUILD+=("$2") ; shift ;;
--package-dir) PACKAGE_DIR+=("$2") ; shift ;;
--makedeb-path) MAKEDEB_PATH="$2" ; shift ;;
--upload-to-webdav) UPLOAD_TO_WEBDAV="$2" ; shift ;;
--webdav-user) WEBDAV_USER="$2" ; shift ;;
--webdav-pass) WEBDAV_PASS="$2" ; shift ;;
--package-exists-hook)
PACKAGE_EXISTS_HOOK="$2"
# strip white space, see https://stackoverflow.com/a/3352015
PACKAGE_EXISTS_HOOK="${PACKAGE_EXISTS_HOOK#"${PACKAGE_EXISTS_HOOK%%[![:space:]]*}"}"
PACKAGE_EXISTS_HOOK="${PACKAGE_EXISTS_HOOK%"${PACKAGE_EXISTS_HOOK##*[![:space:]]}"}"
shift ;;
--)
shift
MAKEDEB_ARGS=("$@")
while (( "$#" > 1 )) ; do # leave one arg for shift command at the end of while loop
shift
done
;;
-h|--help) usage ; exit 0 ;;
*) echo "Unkown option $1"
echo "Use $0 --help for help"
exit 1 ;;
esac
shift
done
if [[ -z "${MAKEDEB_PATH}" ]] ; then
MAKEDEB_PATH="${MAKEDEB_PATH:-"$(which makedeb)"}"
else
MAKEDEB_PATH="$(realpath "$MAKEDEB_PATH")"
fi
if [[ -n "${PACKAGE_DIR-}" && "${#PACKAGE_DIR}" -gt 0 ]] ; then
for p in "${PACKAGE_DIR[@]}" ; do
for f in "$p"/* ; do
_PACKGES_TO_BUILD+=("$f")
done
done
fi
for package in "${_PACKGES_TO_BUILD[@]}" ; do
msg_info "Package to be built: $package"
done
if [[ "${#_PACKGES_TO_BUILD[@]}" -eq 0 ]] ; then
msg_info "No package to build, exiting..."
fi
if [[ -n "$PACKAGE_EXISTS_HOOK" ]] ; then
msg_info "hook for checking if package exists: $PACKAGE_EXISTS_HOOK"
fi
for package in "${_PACKGES_TO_BUILD[@]}" ; do
(
msg_info "$_DELIMITER"
msg_info "Start for $package"
cd "$package"
function retrieve_source {
"$MAKEDEB_PATH" --nobuild "${MAKEDEB_ARGS[@]}" && ret=$? || ret=$?
if [[ $ret -ne 0 ]] ; then
msg_warn "Error occurred when running makedeb, skip this package"
exit 4
fi
}
function is_dynamic_pkgver {
"$MAKEDEB_PATH" --is-dynamic-pkgver "${MAKEDEB_ARGS[@]}"
}
package_name=""
source_ready=0
if [[ -n "${PACKAGE_EXISTS_HOOK}" ]] ; then
if is_dynamic_pkgver ; then
retrieve_source
source_ready=1
fi
package_name="$("$MAKEDEB_PATH" --packagelist "${MAKEDEB_ARGS[@]}")"
msg_info "Checking if package $package_name exists..."
run_hook "$PACKAGE_EXISTS_HOOK" "{}" "$package_name" log_exe_str && ret="$?" || ret="$?"
if [[ $ret -eq 0 ]] ; then
msg_info "Package ${package_name} already built, skip"
exit 0
fi
fi
if (( ! source_ready )) ; then
retrieve_source
fi
if [[ -z "$package_name" ]] ; then
package_name="$("$MAKEDEB_PATH" --packagelist "${MAKEDEB_ARGS[@]}")"
fi
msg_info "Start building..."
"$MAKEDEB_PATH" --noextract "${MAKEDEB_ARGS[@]}" && ret=$? || ret=$?
if [[ $ret -ne 0 ]] ; then
msg_warn "Error occurred when running makedeb, skip this package"
exit 4
fi
if [[ -n "${UPLOAD_TO_WEBDAV}" ]] ; then
msg_info "Uploading $package_name to $UPLOAD_TO_WEBDAV"
upload_to_webdav "$package_name" "$UPLOAD_TO_WEBDAV" "$WEBDAV_USER" "$WEBDAV_PASS"
fi
) || true
done