Skip to content

Commit 84103cb

Browse files
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 termux#10782
1 parent 92985f3 commit 84103cb

20 files changed

+100
-53
lines changed

build-package.sh

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,13 @@ source "$TERMUX_SCRIPTDIR/scripts/build/termux_step_finish_build.sh"
313313
. "$TERMUX_SCRIPTDIR/scripts/properties.sh"
314314

315315
if [ "$TERMUX_ON_DEVICE_BUILD" = "true" ]; then
316+
# Setup TERMUX_APP_PACKAGE_MANAGER
317+
source "$TERMUX_PREFIX/bin/termux-setup-package-manager"
318+
316319
# For on device builds cross compiling is not supported.
317320
# Target architecture must be same as for environment used currently.
318-
case "$TERMUX_MAIN_PACKAGE_FORMAT" in
319-
"debian") TERMUX_ARCH=$(dpkg --print-architecture);;
321+
case "$TERMUX_APP_PACKAGE_MANAGER" in
322+
"apt") TERMUX_ARCH=$(dpkg --print-architecture);;
320323
"pacman") TERMUX_ARCH=$(pacman-conf | grep Architecture | sed 's/Architecture = //g');;
321324
esac
322325
export TERMUX_ARCH
@@ -360,11 +363,7 @@ while (($# >= 1)); do
360363
if [ -z "$1" ]; then
361364
termux_error_exit "./build-package.sh: argument to '--format' should not be empty"
362365
fi
363-
364-
case "$1" in
365-
debian|pacman) TERMUX_PACKAGE_FORMAT="$1";;
366-
*) termux_error_exit "./build-package.sh: only 'debian' and 'pacman' formats are supported";;
367-
esac
366+
TERMUX_PACKAGE_FORMAT="$1"
368367
else
369368
termux_error_exit "./build-package.sh: option '--format' requires an argument"
370369
fi
@@ -416,6 +415,13 @@ while (($# >= 1)); do
416415
done
417416
unset -f _show_usage
418417

418+
if [ -n "${TERMUX_PACKAGE_FORMAT-}" ]; then
419+
case "${TERMUX_PACKAGE_FORMAT-}" in
420+
debian|pacman) :;;
421+
*) termux_error_exit "Unsupported package format \"${TERMUX_PACKAGE_FORMAT-}\". Only 'debian' and 'pacman' formats are supported";;
422+
esac
423+
fi
424+
419425
if [ "${TERMUX_INSTALL_DEPS-false}" = "true" ]; then
420426
# Setup PGP keys for verifying integrity of dependencies.
421427
# Keys are obtained from our keyring package.

packages/termux-tools/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ termux_step_make_install() {
4040
for script in chsh dalvikvm login pkg su termux-fix-shebang termux-backup \
4141
termux-info termux-open termux-open-url termux-reload-settings \
4242
termux-reset termux-restore termux-setup-storage termux-wake-lock \
43-
termux-wake-unlock termux-change-repo; do
43+
termux-wake-unlock termux-change-repo termux-setup-package-manager; do
4444
install -Dm700 $TERMUX_PKG_BUILDER_DIR/$script $TERMUX_PREFIX/bin/$script
4545
sed -i -e "s%\@TERMUX_APP_PACKAGE\@%${TERMUX_APP_PACKAGE}%g" \
4646
-e "s%\@TERMUX_BASE_DIR\@%${TERMUX_BASE_DIR}%g" \

packages/termux-tools/login

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ else
2323
done
2424
fi
2525

26-
# for the correct operation of scripts that work with the package manager
27-
export TERMUX_MAIN_PACKAGE_FORMAT="@TERMUX_PACKAGE_FORMAT@"
26+
# TERMUX_APP_PACKAGE_MANAGER should be exported by termux-app v0.119.0+ itself
27+
if [ -z "${TERMUX_APP_PACKAGE_MANAGER-}" ] && dpkg --compare-versions "$TERMUX_VERSION" lt 0.119.0; then
28+
# For the correct operation of scripts that work with the package manager
29+
export TERMUX_MAIN_PACKAGE_FORMAT="@TERMUX_PACKAGE_FORMAT@"
30+
fi
2831

2932
if [ -f @TERMUX_PREFIX@/lib/libtermux-exec.so ]; then
3033
export LD_PRELOAD=@TERMUX_PREFIX@/lib/libtermux-exec.so

packages/termux-tools/pkg

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
set -eu
33

44
declare -A commands_pkg=(
5-
["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"
5+
["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"
66
["pacman"]="pacman -Ql|pacman -Qi|pacman -Sy --needed|pacman -Sc|pacman -Scc|pacman -Sl|pacman -Q|pacman -S|pacman -Sys|pacman -Rcns|pacman -Syu"
77
)
88

9+
# Setup TERMUX_APP_PACKAGE_MANAGER
10+
source "@TERMUX_PREFIX@/bin/termux-setup-package-manager" || exit 1
11+
912
show_help() {
1013
local cache_size
1114
local cache_dir=""
12-
if [ "$TERMUX_MAIN_PACKAGE_FORMAT" = "debian" ]; then
15+
if [ "$TERMUX_APP_PACKAGE_MANAGER" = "apt" ]; then
1316
cache_dir="@TERMUX_CACHE_DIR@/apt/archives"
14-
elif [ "$TERMUX_MAIN_PACKAGE_FORMAT" = "pacman" ]; then
17+
elif [ "$TERMUX_APP_PACKAGE_MANAGER" = "pacman" ]; then
1518
cache_dir="@TERMUX_PREFIX@/var/cache/pacman/pkg"
1619
fi
1720
cache_size=$(du -sh "$cache_dir" 2>/dev/null | cut -f1)
@@ -194,9 +197,9 @@ if [ $# = 0 ]; then
194197
show_help
195198
fi
196199

197-
case "$TERMUX_MAIN_PACKAGE_FORMAT" in
198-
debian|pacman) IFS="|" pkg_cmd=(${commands_pkg["$TERMUX_MAIN_PACKAGE_FORMAT"]});;
199-
*) echo "Error: pkg is not supported with '$TERMUX_MAIN_PACKAGE_FORMAT' package manager format"; exit 1;;
200+
case "${TERMUX_APP_PACKAGE_MANAGER-}" in
201+
apt|pacman) IFS="|" pkg_cmd=(${commands_pkg["$TERMUX_APP_PACKAGE_MANAGER"]});;
202+
*) echo "Error: pkg is not supported with '${TERMUX_APP_PACKAGE_MANAGER-}' package manager"; exit 1;;
200203
esac
201204

202205
CMD="$1"

packages/termux-tools/termux-info

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ updates() {
1212
if [ "$(id -u)" = "0" ]; then
1313
echo "Running as root. Cannot check package updates."
1414
else
15-
if [ "$TERMUX_MAIN_PACKAGE_FORMAT" = "pacman" ]; then
16-
pacman -Sy >/dev/null 2>&1
17-
updatable=$(pacman -Qu)
18-
else
15+
if [ "$TERMUX_APP_PACKAGE_MANAGER" = "apt" ]; then
1916
apt update >/dev/null 2>&1
2017
updatable=$(apt list --upgradable 2>/dev/null | tail -n +2)
18+
elif [ "$TERMUX_APP_PACKAGE_MANAGER" = "pacman" ]; then
19+
pacman -Sy >/dev/null 2>&1
20+
updatable=$(pacman -Qu)
2121
fi
2222

2323
if [ -z "$updatable" ];then
@@ -69,6 +69,9 @@ repo_subscriptions_pacman() {
6969
fi
7070
}
7171

72+
# Setup TERMUX_APP_PACKAGE_MANAGER
73+
source "@TERMUX_PREFIX@/bin/termux-setup-package-manager" || exit 1
74+
7275
output=""
7376

7477
if [ -n "$TERMUX_VERSION" ]; then
@@ -84,18 +87,18 @@ fi
8487
8588
output+="Packages CPU architecture:
8689
$(
87-
if [ "$TERMUX_MAIN_PACKAGE_FORMAT" = "pacman" ]; then
88-
pacman-conf | grep Architecture | sed 's/Architecture = //g'
89-
else
90+
if [ "$TERMUX_APP_PACKAGE_MANAGER" = "apt" ]; then
9091
dpkg --print-architecture
92+
elif [ "$TERMUX_APP_PACKAGE_MANAGER" = "pacman" ]; then
93+
pacman-conf | grep Architecture | sed 's/Architecture = //g'
9194
fi
9295
)
9396
Subscribed repositories:
9497
$(
95-
if [ "$TERMUX_MAIN_PACKAGE_FORMAT" = "pacman" ]; then
96-
repo_subscriptions_pacman
97-
else
98+
if [ "$TERMUX_APP_PACKAGE_MANAGER" = "apt" ]; then
9899
repo_subscriptions_apt
100+
elif [ "$TERMUX_APP_PACKAGE_MANAGER" = "pacman" ]; then
101+
repo_subscriptions_pacman
99102
fi
100103
)
101104
Updatable packages:
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
# TERMUX_APP_PACKAGE_MANAGER should be exported by termux-app v0.119.0+
4+
# itself and should contain "apt" or "pacman".
5+
# TERMUX_MAIN_PACKAGE_FORMAT should be exported by login script in
6+
# termux-tools v0.161+ if termux-app version is less than 0.119.0 and
7+
# should contain "debian" or "pacman".
8+
if [ -z "${TERMUX_APP_PACKAGE_MANAGER-}" ]; then
9+
if [ -n "${TERMUX_MAIN_PACKAGE_FORMAT-}" ]; then
10+
TERMUX_APP_PACKAGE_MANAGER="$([ "${TERMUX_MAIN_PACKAGE_FORMAT-}" = "debian" ] && echo "apt" || echo "${TERMUX_MAIN_PACKAGE_FORMAT-}")"
11+
else
12+
TERMUX_APP_PACKAGE_MANAGER="apt"
13+
fi
14+
fi
15+
16+
case "${TERMUX_APP_PACKAGE_MANAGER-}" in
17+
apt|pacman) :;;
18+
*) echo "Unsupported package manager \"${TERMUX_APP_PACKAGE_MANAGER-}\". Only 'apt' and 'pacman' managers are supported" 1>&2; exit 1;;
19+
esac
20+
export TERMUX_APP_PACKAGE_MANAGER

scripts/build/setup/termux_setup_cabal.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ termux_setup_cabal() {
2727
cabal update
2828

2929
else
30-
if [[ "${TERMUX_MAIN_PACKAGE_FORMAT}" == "debian" ]] && "$(dpkg-query -W -f '${db:Status-Status}\n' cabal-install 2>/dev/null)" != "installed" ||
31-
[[ "${TERMUX_MAIN_PACKAGE_FORMAT}" == "pacman" ]] && ! "$(pacman -Q cabal-install 2>/dev/null)"; then
30+
if [[ "${TERMUX_APP_PACKAGE_MANAGER}" == "apt" ]] && "$(dpkg-query -W -f '${db:Status-Status}\n' cabal-install 2>/dev/null)" != "installed" ||
31+
[[ "${TERMUX_APP_PACKAGE_MANAGER}" == "pacman" ]] && ! "$(pacman -Q cabal-install 2>/dev/null)"; then
3232
echo "Package 'cabal-install' is not installed."
3333
exit 1
3434
fi

scripts/build/setup/termux_setup_cmake.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ termux_setup_cmake() {
2525

2626
export PATH=$TERMUX_CMAKE_FOLDER/bin:$PATH
2727
else
28-
if [[ "$TERMUX_MAIN_PACKAGE_FORMAT" = "debian" && "$(dpkg-query -W -f '${db:Status-Status}\n' cmake 2>/dev/null)" != "installed" ]] ||
29-
[[ "$TERMUX_MAIN_PACKAGE_FORMAT" = "pacman" && ! "$(pacman -Q cmake 2>/dev/null)" ]]; then
28+
if [[ "$TERMUX_APP_PACKAGE_MANAGER" = "apt" && "$(dpkg-query -W -f '${db:Status-Status}\n' cmake 2>/dev/null)" != "installed" ]] ||
29+
[[ "$TERMUX_APP_PACKAGE_MANAGER" = "pacman" && ! "$(pacman -Q cmake 2>/dev/null)" ]]; then
3030
echo "Package 'cmake' is not installed."
3131
echo "You can install it with"
3232
echo

scripts/build/setup/termux_setup_ghc.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ termux_setup_ghc() {
5858
)
5959
rm -Rf "$TERMUX_GHC_TEMP_FOLDER" "$TERMUX_GHC_TAR"
6060
else
61-
if [[ "$TERMUX_MAIN_PACKAGE_FORMAT" = "debian" && "$(dpkg-query -W -f '${db:Status-Status}\n' ghc 2>/dev/null)" != "installed" ]] ||
62-
[[ "$TERMUX_MAIN_PACKAGE_FORMAT" = "pacman" && ! "$(pacman -Q ghc 2>/dev/null)" ]]; then
61+
if [[ "$TERMUX_APP_PACKAGE_MANAGER" = "apt" && "$(dpkg-query -W -f '${db:Status-Status}\n' ghc 2>/dev/null)" != "installed" ]] ||
62+
[[ "$TERMUX_APP_PACKAGE_MANAGER" = "pacman" && ! "$(pacman -Q ghc 2>/dev/null)" ]]; then
6363
echo "Package 'ghc' is not installed."
6464
exit 1
6565
fi

scripts/build/setup/termux_setup_ghc_cross_compiler.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ termux_setup_ghc_cross_compiler() {
124124
rm "${TERMUX_GHC_TAR}"
125125
else
126126

127-
if [[ "${TERMUX_MAIN_PACKAGE_FORMAT}" == "debian" ]] && "$(dpkg-query -W -f '${db:Status-Status}\n' ghc 2>/dev/null)" != "installed" ||
128-
[[ "${TERMUX_MAIN_PACKAGE_FORMAT}" == "pacman" ]] && ! "$(pacman -Q ghc 2>/dev/null)"; then
127+
if [[ "${TERMUX_APP_PACKAGE_MANAGER}" == "apt" ]] && "$(dpkg-query -W -f '${db:Status-Status}\n' ghc 2>/dev/null)" != "installed" ||
128+
[[ "${TERMUX_APP_PACKAGE_MANAGER}" == "pacman" ]] && ! "$(pacman -Q ghc 2>/dev/null)"; then
129129
echo "Package 'ghc' is not installed."
130130
exit 1
131131
else

0 commit comments

Comments
 (0)