chore(auto update): cleanup some codes

%ci:no-build

Above tag is for previous commit but CI checks head commit for
this, so applying here.

Signed-off-by: Aditya Alok <dev.aditya.alok@gmail.com>
This commit is contained in:
Aditya Alok 2022-04-20 23:38:21 +05:30
parent 3baa1a3a36
commit 85d52a4ab2
No known key found for this signature in database
GPG Key ID: 345AE134142077D8
5 changed files with 44 additions and 55 deletions

View File

@ -98,17 +98,9 @@ _update() {
# Only update if auto update is enabled.
if [[ "${TERMUX_PKG_AUTO_UPDATE}" == "true" ]]; then
echo "INFO: Updating ${TERMUX_PKG_NAME} [Current version: ${TERMUX_PKG_VERSION}]..."
pkg_dir=${pkg_dir} termux_pkg_auto_update
echo # Newline.
fi
}
_test_pkg_build_file() {
local pkg_dir="$1"
if [[ ! -f "${pkg_dir}/build.sh" ]]; then
# Fail if detected a non-package directory.
termux_error_exit "ERROR: directory '${pkg_dir}' is not a package."
echo "INFO: Updating ${TERMUX_PKG_NAME} [Current version: ${TERMUX_PKG_VERSION}]..."
termux_pkg_auto_update
fi
}
@ -116,7 +108,11 @@ declare -a _FAILED_UPDATES=()
_run_update() {
local pkg_dir="$1"
_test_pkg_build_file "${pkg_dir}"
# Check if this `pkg_dir` has a `build.sh` file.
if [[ ! -f "${pkg_dir}/build.sh" ]]; then
# Fail if detected a non-package directory.
termux_error_exit "ERROR: directory '${pkg_dir}' is not a package."
fi
# Run each package update in separate process since we include their environment variables.
(
set -euo pipefail
@ -132,22 +128,22 @@ main() {
echo "INFO: Running update for: $*"
if [[ "$1" == "@all" ]]; then
for repo_dir in $(jq --raw-output 'keys | .[]' ${TERMUX_SCRIPTDIR}/repo.json); do
for pkg_dir in $repo_dir/*; do
for repo_dir in $(jq --raw-output 'keys | .[]' "${TERMUX_SCRIPTDIR}/repo.json"); do
for pkg_dir in "${repo_dir}"/*; do
_run_update "${pkg_dir}"
done
done
else
for pkg in "$@"; do
if [ ! -d "${pkg}" ]; then
for repo_dir in $(jq --raw-output 'keys | .[]' ${TERMUX_SCRIPTDIR}/repo.json); do
if [ ! -d "${pkg}" ]; then # If only package name is given, try to find it's directory.
for repo_dir in $(jq --raw-output 'keys | .[]' "${TERMUX_SCRIPTDIR}/repo.json"); do
if [ -d "${repo_dir}/${pkg}" ]; then
pkg="${repo_dir}/${pkg}"
break
fi
done
fi
_run_update "${pkg}"
_run_update "${pkg}" # Here, `pkg` is a directory.
done
fi

View File

@ -1,4 +1,15 @@
# shellcheck shell=bash
# NOTE: Repology sometimes returns "1.0-1" as the latest version even if "1.0" is latest.
# This happens when any of the repositories tracked by repology has specified
# "1.0-1" as the latest.
#
# For example:
# latest lua:lpeg version (as of 2021-11-20T12:21:31) is "1.0.2" but MacPorts specifies as "1.0.2-1".
# Hence repology returns "1.0.2-1" as the latest.
#
# But hopefully, all this can be avoided if TERMUX_PKG_UPDATE_VERSION_REGEXP is set.
#
termux_repology_api_get_latest_version() {
if [[ -z "$1" ]]; then
termux_error_exit "Usage: ${FUNCNAME[0]} PKG_NAME"

View File

@ -6,8 +6,6 @@ termux_gitlab_auto_update() {
termux_gitlab_api_get_tag \
"${TERMUX_PKG_SRCURL}" "${TERMUX_PKG_UPDATE_TAG_TYPE}" "${TERMUX_GITLAB_API_HOST}"
)"
# No need to check for return code `2`, since gitlab api does not implement cache control.
if [[ -z "${latest_tag}" ]]; then
termux_error_exit "ERROR: Unable to get tag from ${TERMUX_PKG_SRCURL}"
fi

View File

@ -1,19 +1,6 @@
#!/bin/bash
#
# NOTE: This function returns true even when CURRENT_VERSION = "1.0" and LATEST_VERSION = "1.0-1".
# This is logically correct, but repology sometimes returns "1.0-1" as the latest version even
# if "1.0" is latest. This happens when any of the repositories tracked by repology has specified
# "1.0-1" as the latest.
#
# For example:
# latest lua:lpeg version (as of 2021-11-20T12:21:31) is "1.0.2" but MacPorts specifies as "1.0.2-1".
# Hence repology returns "1.0.2-1" as the latest.
#
# But hopefully, all this can be avoided if TERMUX_PKG_UPDATE_VERSION_REGEXP is set.
#
termux_pkg_is_update_needed() {
# USAGE: termux_pkg_is_update_needed <current-version> <latest-version> [regexp]
# USAGE: termux_pkg_is_update_needed <current-version> <latest-version>
if [[ -z "$1" ]] || [[ -z "$2" ]]; then
termux_error_exit "${BASH_SOURCE[0]}: at least 2 arguments expected"
fi
@ -40,22 +27,19 @@ termux_pkg_is_update_needed() {
return 1 # false. Update not needed.
}
show_help() {
echo "Usage: ${BASH_SOURCE[0]} [--help] <first-version> <second-version>] [version-regex]"
echo "--help - show this help message and exit"
echo " <first-version> - first version to compare"
echo " <second-version> - second version to compare"
echo " [version-regex] - optional regular expression to filter version numbers"
exit 0
}
# Make script sourceable as well as executable.
# Make it also usable as command line tool. `scripts/bin/apt-compare-versions` is symlinked to this file.
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
declare -f termux_error_exit >/dev/null ||
. "$(dirname "$(realpath "${BASH_SOURCE[0]}")")/termux_error_exit.sh" # realpath is used to resolve symlinks.
if [[ "${1}" == "--help" ]]; then
show_help
cat <<-EOF
Usage: $(basename "${BASH_SOURCE[0]}") [--help] <first-version> <second-version>] [version-regex]
--help - show this help message and exit
<first-version> - first version to compare
<second-version> - second version to compare
[version-regex] - optional regular expression to filter version numbers from given versions
EOF
fi
# Print in human readable format.
@ -69,9 +53,13 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
termux_error_exit "ERROR: Unable to parse version numbers using regexp '${version_regexp}'"
fi
fi
if termux_pkg_is_update_needed "${first_version}" "${second_version}" "${version_regexp}"; then
echo "${first_version} < ${second_version}"
if [[ "${first_version}" == "${second_version}" ]]; then
echo "${first_version} = ${second_version}"
else
echo "${first_version} >= ${second_version}"
if termux_pkg_is_update_needed "${first_version}" "${second_version}"; then
echo "${first_version} < ${second_version}"
else
echo "${first_version} > ${second_version}"
fi
fi
fi

View File

@ -1,7 +1,6 @@
# shellcheck shell=bash
termux_pkg_upgrade_version() {
if [[ "$#" -lt 1 ]]; then
# Show usage.
termux_error_exit <<-EndUsage
Usage: ${FUNCNAME[0]} LATEST_VERSION [--skip-version-check]
EndUsage
@ -9,9 +8,6 @@ termux_pkg_upgrade_version() {
local LATEST_VERSION="$1"
local SKIP_VERSION_CHECK="${2:-}"
local PKG_DIR
PKG_DIR="${pkg_dir}"
local EPOCH
EPOCH="${TERMUX_PKG_VERSION%%:*}" # If there is no epoch, this will be the full version.
# Check if it isn't the full version and add ':'.
@ -52,15 +48,15 @@ termux_pkg_upgrade_version() {
sed -i \
"s/^\(TERMUX_PKG_VERSION=\)\(.*\)\$/\1\"${EPOCH}${LATEST_VERSION}\"/g" \
"${PKG_DIR}/build.sh"
"${TERMUX_PKG_BUILDER_DIR}/build.sh"
sed -i \
"/TERMUX_PKG_REVISION=/d" \
"${PKG_DIR}/build.sh"
"${TERMUX_PKG_BUILDER_DIR}/build.sh"
# Update checksum
if [[ "${TERMUX_PKG_SHA256[*]}" != "SKIP_CHECKSUM" ]] && [[ "${TERMUX_PKG_SRCURL: -4}" != ".git" ]]; then
echo n | "${TERMUX_SCRIPTDIR}/scripts/bin/update-checksum" "${TERMUX_PKG_NAME}" || {
git checkout -- "${PKG_DIR}"
git checkout -- "${TERMUX_PKG_BUILDER_DIR}"
git pull --rebase
termux_error_exit "ERROR: failed to update checksum."
}
@ -71,7 +67,7 @@ termux_pkg_upgrade_version() {
if [[ "${GIT_COMMIT_PACKAGES}" == "true" ]]; then
echo "INFO: Committing package."
stderr="$(
git add "${PKG_DIR}" 2>&1 >/dev/null
git add "${TERMUX_PKG_BUILDER_DIR}" 2>&1 >/dev/null
git commit -m "${TERMUX_PKG_NAME}: update to ${LATEST_VERSION}" \
-m "This commit has been automatically submitted by Github Actions." 2>&1 >/dev/null
)" || {
@ -95,7 +91,7 @@ termux_pkg_upgrade_version() {
}
fi
else
git checkout -- "${PKG_DIR}"
git checkout -- "${TERMUX_PKG_BUILDER_DIR}"
termux_error_exit "ERROR: failed to build."
fi