From 28ca844234a1ce25ae20e13719ca1168c3e4508f Mon Sep 17 00:00:00 2001 From: agnostic-apollo Date: Sun, 22 May 2022 23:00:24 +0500 Subject: [PATCH] changed(scripts|main/termux-tools): Use TERMUX_APP_PACKAGE_MANAGER instead of TERMUX_MAIN_PACKAGE_FORMAT Make changes as per new design implemented in termux/termux-app@b950efec and termux/termux-app#2740 The package build and termux-tools scripts use current package manager for custom logic. The `termux-tools/termux-setup-package-manager` script has been added that will now be used to provide backward compatibility for termux-app `< 0.119.0` (when its released) and validate the package manager. It will also ensure the variable in not unset to prevent `unbound variable` errors if `set -u` is being used by calling scripts. Closes #10782 --- packages/termux-tools/build.sh | 3 ++- packages/termux-tools/login | 10 ++++++-- packages/termux-tools/pkg | 15 +++++++----- packages/termux-tools/termux-info | 23 +++++++++++-------- .../termux-tools/termux-setup-package-manager | 20 ++++++++++++++++ 5 files changed, 52 insertions(+), 19 deletions(-) create mode 100755 packages/termux-tools/termux-setup-package-manager diff --git a/packages/termux-tools/build.sh b/packages/termux-tools/build.sh index abea569..70deb7c 100644 --- a/packages/termux-tools/build.sh +++ b/packages/termux-tools/build.sh @@ -40,7 +40,7 @@ termux_step_make_install() { for script in chsh dalvikvm login pkg su termux-fix-shebang termux-backup \ termux-info termux-open termux-open-url termux-reload-settings \ termux-reset termux-restore termux-setup-storage termux-wake-lock \ - termux-wake-unlock termux-change-repo; do + termux-wake-unlock termux-change-repo termux-setup-package-manager; do install -Dm700 $TERMUX_PKG_BUILDER_DIR/$script $TERMUX_PREFIX/bin/$script sed -i -e "s%\@TERMUX_APP_PACKAGE\@%${TERMUX_APP_PACKAGE}%g" \ -e "s%\@TERMUX_BASE_DIR\@%${TERMUX_BASE_DIR}%g" \ @@ -49,6 +49,7 @@ termux_step_make_install() { -e "s%\@TERMUX_PREFIX\@%${TERMUX_PREFIX}%g" \ -e "s%\@PACKAGE_VERSION\@%${TERMUX_PKG_VERSION}%g" \ -e "s%\@TERMUX_PACKAGE_FORMAT\@%${TERMUX_PACKAGE_FORMAT}%g" \ + -e "s%\@TERMUX_PACKAGE_MANAGER\@%${TERMUX_PACKAGE_MANAGER}%g" \ $TERMUX_PREFIX/bin/$script done diff --git a/packages/termux-tools/login b/packages/termux-tools/login index bf7a7b3..8a338ba 100755 --- a/packages/termux-tools/login +++ b/packages/termux-tools/login @@ -23,8 +23,14 @@ else done fi -# for the correct operation of scripts that work with the package manager -export TERMUX_MAIN_PACKAGE_FORMAT="@TERMUX_PACKAGE_FORMAT@" +# TERMUX_APP_PACKAGE_MANAGER should be exported by termux-app v0.119.0+ itself +if [ -z "${TERMUX_APP_PACKAGE_MANAGER-}" ]; then + if { [ -n "$(command -v dpkg)" ] && dpkg --compare-versions "$TERMUX_VERSION" lt 0.119.0; } || # apt + { [ -n "$(command -v vercmp)" ] && [ "$(vercmp "$TERMUX_VERSION" 0.119.0)" = "-1" ]; }; then # pacman + # For the correct operation of scripts that work with the package manager + export TERMUX_MAIN_PACKAGE_FORMAT="@TERMUX_PACKAGE_FORMAT@" + fi +fi if [ -f @TERMUX_PREFIX@/lib/libtermux-exec.so ]; then export LD_PRELOAD=@TERMUX_PREFIX@/lib/libtermux-exec.so diff --git a/packages/termux-tools/pkg b/packages/termux-tools/pkg index dbeca72..eb4c7fb 100755 --- a/packages/termux-tools/pkg +++ b/packages/termux-tools/pkg @@ -2,16 +2,19 @@ set -eu declare -A commands_pkg=( -["debian"]="dpkg -L|apt show|select_mirror; update_apt_cache; apt install|apt autoclean|apt clean|apt list|apt list --installed|apt install --reinstall|select_mirror; update_apt_cache; apt search|apt remove|select_mirror; apt update; apt full-upgrade" +["apt"]="dpkg -L|apt show|select_mirror; update_apt_cache; apt install|apt autoclean|apt clean|apt list|apt list --installed|apt install --reinstall|select_mirror; update_apt_cache; apt search|apt remove|select_mirror; apt update; apt full-upgrade" ["pacman"]="pacman -Ql|pacman -Qi|pacman -Sy --needed|pacman -Sc|pacman -Scc|pacman -Sl|pacman -Q|pacman -S|pacman -Sys|pacman -Rcns|pacman -Syu" ) +# Setup TERMUX_APP_PACKAGE_MANAGER +source "@TERMUX_PREFIX@/bin/termux-setup-package-manager" || exit 1 + show_help() { local cache_size local cache_dir="" - if [ "$TERMUX_MAIN_PACKAGE_FORMAT" = "debian" ]; then + if [ "$TERMUX_APP_PACKAGE_MANAGER" = "apt" ]; then cache_dir="@TERMUX_CACHE_DIR@/apt/archives" - elif [ "$TERMUX_MAIN_PACKAGE_FORMAT" = "pacman" ]; then + elif [ "$TERMUX_APP_PACKAGE_MANAGER" = "pacman" ]; then cache_dir="@TERMUX_PREFIX@/var/cache/pacman/pkg" fi cache_size=$(du -sh "$cache_dir" 2>/dev/null | cut -f1) @@ -194,9 +197,9 @@ if [ $# = 0 ]; then show_help fi -case "$TERMUX_MAIN_PACKAGE_FORMAT" in - debian|pacman) IFS="|" pkg_cmd=(${commands_pkg["$TERMUX_MAIN_PACKAGE_FORMAT"]});; - *) echo "Error: pkg is not supported with '$TERMUX_MAIN_PACKAGE_FORMAT' package manager format"; exit 1;; +case "${TERMUX_APP_PACKAGE_MANAGER-}" in + apt|pacman) IFS="|" pkg_cmd=(${commands_pkg["$TERMUX_APP_PACKAGE_MANAGER"]});; + *) echo "Error: pkg is not supported with '${TERMUX_APP_PACKAGE_MANAGER-}' package manager"; exit 1;; esac CMD="$1" diff --git a/packages/termux-tools/termux-info b/packages/termux-tools/termux-info index 039dc4e..4847515 100755 --- a/packages/termux-tools/termux-info +++ b/packages/termux-tools/termux-info @@ -12,12 +12,12 @@ updates() { if [ "$(id -u)" = "0" ]; then echo "Running as root. Cannot check package updates." else - if [ "$TERMUX_MAIN_PACKAGE_FORMAT" = "pacman" ]; then - pacman -Sy >/dev/null 2>&1 - updatable=$(pacman -Qu) - else + if [ "$TERMUX_APP_PACKAGE_MANAGER" = "apt" ]; then apt update >/dev/null 2>&1 updatable=$(apt list --upgradable 2>/dev/null | tail -n +2) + elif [ "$TERMUX_APP_PACKAGE_MANAGER" = "pacman" ]; then + pacman -Sy >/dev/null 2>&1 + updatable=$(pacman -Qu) fi if [ -z "$updatable" ];then @@ -69,6 +69,9 @@ repo_subscriptions_pacman() { fi } +# Setup TERMUX_APP_PACKAGE_MANAGER +source "@TERMUX_PREFIX@/bin/termux-setup-package-manager" || exit 1 + output="" if [ -n "$TERMUX_VERSION" ]; then @@ -84,18 +87,18 @@ fi output+="Packages CPU architecture: $( - if [ "$TERMUX_MAIN_PACKAGE_FORMAT" = "pacman" ]; then - pacman-conf | grep Architecture | sed 's/Architecture = //g' - else + if [ "$TERMUX_APP_PACKAGE_MANAGER" = "apt" ]; then dpkg --print-architecture + elif [ "$TERMUX_APP_PACKAGE_MANAGER" = "pacman" ]; then + pacman-conf | grep Architecture | sed 's/Architecture = //g' fi ) Subscribed repositories: $( - if [ "$TERMUX_MAIN_PACKAGE_FORMAT" = "pacman" ]; then - repo_subscriptions_pacman - else + if [ "$TERMUX_APP_PACKAGE_MANAGER" = "apt" ]; then repo_subscriptions_apt + elif [ "$TERMUX_APP_PACKAGE_MANAGER" = "pacman" ]; then + repo_subscriptions_pacman fi ) Updatable packages: diff --git a/packages/termux-tools/termux-setup-package-manager b/packages/termux-tools/termux-setup-package-manager new file mode 100755 index 0000000..49b33f2 --- /dev/null +++ b/packages/termux-tools/termux-setup-package-manager @@ -0,0 +1,20 @@ +#!/bin/bash + +# TERMUX_APP_PACKAGE_MANAGER should be exported by termux-app v0.119.0+ +# itself and should contain "apt" or "pacman". +# TERMUX_MAIN_PACKAGE_FORMAT should be exported by login script in +# termux-tools v0.161+ if termux-app version is less than 0.119.0 and +# should contain "debian" or "pacman". +if [ -z "${TERMUX_APP_PACKAGE_MANAGER-}" ]; then + if [ -n "${TERMUX_MAIN_PACKAGE_FORMAT-}" ]; then + TERMUX_APP_PACKAGE_MANAGER="$([ "${TERMUX_MAIN_PACKAGE_FORMAT-}" = "debian" ] && echo "apt" || echo "${TERMUX_MAIN_PACKAGE_FORMAT-}")" + else + TERMUX_APP_PACKAGE_MANAGER="@TERMUX_PACKAGE_MANAGER@" + fi +fi + +case "${TERMUX_APP_PACKAGE_MANAGER-}" in + apt|pacman) :;; + *) echo "Unsupported package manager \"${TERMUX_APP_PACKAGE_MANAGER-}\". Only 'apt' and 'pacman' managers are supported" 1>&2; exit 1;; +esac +export TERMUX_APP_PACKAGE_MANAGER