From 85d52a4ab22967146804538b471249dde689d6c0 Mon Sep 17 00:00:00 2001 From: Aditya Alok Date: Wed, 20 Apr 2022 23:38:21 +0530 Subject: [PATCH] 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 --- scripts/bin/update-packages | 28 +++++------- .../termux_repology_api_get_latest_version.sh | 11 +++++ .../internal/termux_gitlab_auto_update.sh | 2 - .../utils/termux_pkg_is_update_needed.sh | 44 +++++++------------ .../utils/termux_pkg_upgrade_version.sh | 14 +++--- 5 files changed, 44 insertions(+), 55 deletions(-) diff --git a/scripts/bin/update-packages b/scripts/bin/update-packages index a057874e1..b3e1f85c1 100755 --- a/scripts/bin/update-packages +++ b/scripts/bin/update-packages @@ -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 diff --git a/scripts/updates/api/termux_repology_api_get_latest_version.sh b/scripts/updates/api/termux_repology_api_get_latest_version.sh index 46ac698fc..fd841baa5 100644 --- a/scripts/updates/api/termux_repology_api_get_latest_version.sh +++ b/scripts/updates/api/termux_repology_api_get_latest_version.sh @@ -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" diff --git a/scripts/updates/internal/termux_gitlab_auto_update.sh b/scripts/updates/internal/termux_gitlab_auto_update.sh index 52f8f1486..0534a0a0a 100644 --- a/scripts/updates/internal/termux_gitlab_auto_update.sh +++ b/scripts/updates/internal/termux_gitlab_auto_update.sh @@ -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 diff --git a/scripts/updates/utils/termux_pkg_is_update_needed.sh b/scripts/updates/utils/termux_pkg_is_update_needed.sh index 32f7c590a..a70415b1b 100755 --- a/scripts/updates/utils/termux_pkg_is_update_needed.sh +++ b/scripts/updates/utils/termux_pkg_is_update_needed.sh @@ -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 [regexp] - + # USAGE: termux_pkg_is_update_needed 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] ] [version-regex]" - echo "--help - show this help message and exit" - echo " - first version to compare" - echo " - 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] ] [version-regex] + --help - show this help message and exit + - first version to compare + - 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 diff --git a/scripts/updates/utils/termux_pkg_upgrade_version.sh b/scripts/updates/utils/termux_pkg_upgrade_version.sh index 22302b7f7..c78a04a71 100755 --- a/scripts/updates/utils/termux_pkg_upgrade_version.sh +++ b/scripts/updates/utils/termux_pkg_upgrade_version.sh @@ -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