Skip to content

Commit

Permalink
changed(scripts|main/termux-tools): Use TERMUX_APP_PACKAGE_MANAGER in…
Browse files Browse the repository at this point in the history
…stead 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
  • Loading branch information
agnostic-apollo committed May 22, 2022
1 parent 82f2127 commit 28ca844
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 19 deletions.
3 changes: 2 additions & 1 deletion packages/termux-tools/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand All @@ -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

Expand Down
10 changes: 8 additions & 2 deletions packages/termux-tools/login
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 9 additions & 6 deletions packages/termux-tools/pkg
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand Down
23 changes: 13 additions & 10 deletions packages/termux-tools/termux-info
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
20 changes: 20 additions & 0 deletions packages/termux-tools/termux-setup-package-manager
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 28ca844

Please sign in to comment.