Skip to content

Commit

Permalink
chore(script/update-packages): Script cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TomJo2000 committed Feb 1, 2025
1 parent bd54335 commit 00f03e9
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions scripts/bin/update-packages
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ declare -A _LATEST_TAGS=()
declare -A _FAILED_UPDATES=()
declare -a _ALREADY_SEEN=() # Array of packages successfully updated or skipped.

# _fetch_and_cache_tags fetches all possible tags using termux_pkg_auto_update, but using Ninja build system.
# The key difference is that we make the process concurrent, allowing us to fetch tags simultaneously rather than one at a time.
# Once all tags are cached, the termux_pkg_auto_update function will operate much more quickly.
# _fetch_and_cache_tags fetches all possible tags using termux_pkg_auto_update, but using Ninja build system.
# The key difference is that we make the process concurrent, allowing us to fetch tags simultaneously rather than one at a time.
# Once all tags are cached, the termux_pkg_auto_update function will operate much more quickly.
# We avoid packages with overwritten termux_pkg_auto_update to prevent unexpected modifications to the package`s build.sh.
_fetch_and_cache_tags() {
if [ "$(uname -o)" = "Android" ] || [ -e "/system/bin/app_process" ]; then
Expand All @@ -137,7 +137,7 @@ _fetch_and_cache_tags() {
return 0
fi
fi

if ! command -v ninja &> /dev/null; then
echo "INFO: Fetching ninja build system"
. "${TERMUX_SCRIPTDIR}"/scripts/build/termux_download.sh
Expand All @@ -150,20 +150,26 @@ _fetch_and_cache_tags() {
# First invocation of termux_repology_api_get_latest_version fetches and caches repology metadata.
quiet termux_repology_api_get_latest_version ' '

local __PACKAGES=()
local __PACKAGES=()
for repo_dir in $(jq --raw-output 'del(.pkg_format) | keys | .[]' "${TERMUX_SCRIPTDIR}/repo.json"); do
for pkg_dir in "${repo_dir}"/*; do
! quiet _should_update "${pkg_dir}" && continue # Skip if not needed.
quiet _should_update "${pkg_dir}" || continue # Skip if not needed.
grep -q '^termux_pkg_auto_update' "${pkg_dir}/build.sh" && continue # Skip if package has custom auto-update
__PACKAGES+=("${pkg_dir}")
done
done

# This is called indirectly in the ninja file
# So silence shellcheck's unreachable code warning
# shellcheck disable=SC2317
__main__() {
cd ${TERMUX_SCRIPTDIR}
export TERMUX_PKG_NAME="${1##*/}" TERMUX_PKG_BUILDER_DIR=${1}
set +eu;
for i in scripts/updates/{**/,}*.sh "${1}/build.sh"; do source ${i}; done
for i in scripts/updates/{**/,}*.sh "${1}/build.sh"; do
# shellcheck disable=SC1090
source "${i}"
done
set -eu
termux_pkg_upgrade_version() {
[[ -n "$1" ]] && echo "PKG|$TERMUX_PKG_NAME|${1#*:}"
Expand All @@ -176,20 +182,22 @@ _fetch_and_cache_tags() {
termux_pkg_auto_update
}

# This function generates a ninja file for all packages in the ${__PACKAGES[@]}
__generate__() {
echo "rule update"
echo " command = bash -c \"\$\$F\" -- \$pkg_dir ||:"
echo " command = bash -c \"\$\$F\" -- \$pkg_dir || :"
sed 's/[^ ]\+/\nbuild &: update\n pkg_dir=&/g' <<< "${__PACKAGES[@]}"
echo "build run_all: phony ${__PACKAGES[@]}"
echo "build run_all: phony ${__PACKAGES[*]}"
echo "default run_all"
}

local LATEST_TAGS="$(\

local LATEST_TAGS
LATEST_TAGS="$(
F="$(declare -p TERMUX_SCRIPTDIR GITHUB_TOKEN TERMUX_REPOLOGY_DATA_FILE); $(declare -f __main__ | sed 's/__main__ ()//')" \
env --chdir=/tmp ninja -f /dev/stdin <<< "$(__generate__)" |& grep "^PKG|"
env --chdir=/tmp ninja -f /dev/stdin < <(__generate__ |& grep "^PKG|")
)"
unset -f __main__ __generate__

unset -f __main__ __generate__
while IFS='|' read -r _ pkg version; do
_LATEST_TAGS["${pkg:-_}"]="$version"
done <<< "$LATEST_TAGS"
Expand Down Expand Up @@ -303,7 +311,7 @@ _update_dependencies() {
termux_error_exit "ERROR: Obtaining update order failed for $(basename "${pkg_dir}")"
fi
_should_update "${dep_dir}" && ! _check_updated "${dep_dir}" && _run_update "${dep_dir}"
done <<<"$("${TERMUX_SCRIPTDIR}"/scripts/buildorder.py "${pkg_dir}" $TERMUX_PACKAGES_DIRECTORIES || echo "ERROR")"
done < <("${TERMUX_SCRIPTDIR}"/scripts/buildorder.py "${pkg_dir}" $TERMUX_PACKAGES_DIRECTORIES || echo "ERROR")
}

echo "INFO: Running update for: $*"
Expand Down Expand Up @@ -367,7 +375,7 @@ _gh_create_new_issue() {
fi

# Extract origin URL, commit hash and builder directory and put everything together
link="$(git config --get remote.origin.url |sed -E 's|\.git$||; s|git@([^:]+):(.+)|https://\1/\2|')/blob/$(git rev-parse HEAD)/$(echo */$1)"
link="$(git config --get remote.origin.url | sed -E 's|\.git$||; s|git@([^:]+):(.+)|https://\1/\2|')/blob/$(git rev-parse HEAD)/$(echo */"$1")"

body="$(
cat <<-EOF
Expand Down

0 comments on commit 00f03e9

Please sign in to comment.