Skip to content

Commit a04f45d

Browse files
committed
Implementation of working alternatives on pacman
For testing
1 parent b982d53 commit a04f45d

10 files changed

+215
-121
lines changed

build-package.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,8 @@ source "$TERMUX_SCRIPTDIR/scripts/build/termux_step_create_pacman_package.sh"
396396

397397
# Process 'update-alternatives' entries from `.alternatives` files.
398398
# Not to be overridden by package scripts.
399-
# shellcheck source=scripts/build/termux_step_update_alternatives.sh
400-
source "$TERMUX_SCRIPTDIR/scripts/build/termux_step_update_alternatives.sh"
399+
# shellcheck source=scripts/build/termux_step_setup_alternatives.sh
400+
source "$TERMUX_SCRIPTDIR/scripts/build/termux_step_setup_alternatives.sh"
401401

402402
# Finish the build. Not to be overridden by package scripts.
403403
# shellcheck source=scripts/build/termux_step_finish_build.sh

packages/pacman/build.sh

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ TERMUX_PKG_DESCRIPTION="A library-based package manager with dependency support"
33
TERMUX_PKG_LICENSE="GPL-2.0"
44
TERMUX_PKG_MAINTAINER="@Maxython <[email protected]>"
55
TERMUX_PKG_VERSION=7.0.0
6-
TERMUX_PKG_REVISION=4
7-
TERMUX_PKG_SRCURL=https://gitlab.archlinux.org/pacman/pacman/-/releases/v${TERMUX_PKG_VERSION}/downloads/pacman-${TERMUX_PKG_VERSION}.tar.xz
8-
TERMUX_PKG_SHA256=61cbd445d1381b4b184bc7c4e2791f07a79f0f2807b7c600399d0d08e8cd28cf
6+
TERMUX_PKG_REVISION=5
7+
TERMUX_PKG_SRCURL=(https://gitlab.archlinux.org/pacman/pacman/-/releases/v${TERMUX_PKG_VERSION}/downloads/pacman-${TERMUX_PKG_VERSION}.tar.xz
8+
https://github.com/termux-pacman/pacman-switch/archive/refs/heads/main.zip)
9+
TERMUX_PKG_SHA256=(61cbd445d1381b4b184bc7c4e2791f07a79f0f2807b7c600399d0d08e8cd28cf
10+
707669cd9700916254890ce0bf0bd2a0c93e53610bf3540b06dd0e99110178f9)
911
TERMUX_PKG_DEPENDS="bash, curl, gpgme, libandroid-glob, libarchive, libcurl, openssl, termux-licenses, termux-keyring"
1012
TERMUX_PKG_BUILD_DEPENDS="doxygen, asciidoc, nettle"
1113
TERMUX_PKG_GROUPS="base-devel"
@@ -25,6 +27,7 @@ termux_step_pre_configure() {
2527
rm -f ./scripts/libmakepkg/executable/fakeroot.sh.in
2628

2729
sed -i "s/@TERMUX_ARCH@/${TERMUX_ARCH}/" ./etc/{pacman,makepkg}.conf.in
30+
sed -i "s|_ps_prefix=.*|_ps_prefix=\"${TERMUX_PREFIX}\"|" ./pacman-switch-main/pacman-switch.sh
2831
}
2932

3033
termux_step_post_configure() {
@@ -34,12 +37,13 @@ termux_step_post_configure() {
3437
termux_step_post_make_install() {
3538
mkdir -p $TERMUX_PREFIX/etc/pacman.d
3639
install -m644 $TERMUX_PKG_BUILDER_DIR/serverlist $TERMUX_PREFIX/etc/pacman.d/serverlist
40+
install -m755 $TERMUX_PKG_SRCDIR/pacman-switch-main/pacman-switch.sh $TERMUX_PREFIX/bin/pacman-switch
3741
}
3842

39-
termux_step_create_debscripts() {
40-
echo "#!$TERMUX_PREFIX/bin/bash" > postinst
41-
echo "mkdir -p $TERMUX_PREFIX/var/lib/pacman/sync" >> postinst
42-
echo "mkdir -p $TERMUX_PREFIX/var/lib/pacman/local" >> postinst
43-
echo "mkdir -p $TERMUX_PREFIX/var/cache/pacman/pkg" >> postinst
44-
chmod 755 postinst
43+
termux_step_post_massage() {
44+
mkdir -p ./var/lib/pacman/sync
45+
mkdir -p ./var/lib/pacman/local
46+
mkdir -p ./var/cache/pacman/pkg
47+
mkdir -p ./share/pacman-switch
48+
mkdir -p ./var/lib/pacman/switch
4549
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[Trigger]
2+
Type = Path
3+
Operation = Remove
4+
Target = @TERMUX_PREFIX_TARGET@/share/pacman-switch/*.sw
5+
6+
[Action]
7+
Description = Automatic disabling switchers...
8+
When = PreTransaction
9+
Exec = @TERMUX_PREFIX@/share/libalpm/scripts/run-pacman-switch disable
10+
NeedsTargets
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[Trigger]
2+
Type = Path
3+
Operation = Install
4+
Target = @TERMUX_PREFIX_TARGET@/share/pacman-switch/*.sw
5+
6+
[Action]
7+
Description = Automatic enabling switchers...
8+
When = PostTransaction
9+
Exec = @TERMUX_PREFIX@/share/libalpm/scripts/run-pacman-switch enable
10+
NeedsTargets
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[Trigger]
2+
Type = Path
3+
Operation = Upgrade
4+
Target = @TERMUX_PREFIX_TARGET@/share/pacman-switch/*.sw
5+
6+
[Action]
7+
Description = Automatic updating switchers...
8+
When = PostTransaction
9+
Exec = @TERMUX_PREFIX@/share/libalpm/scripts/run-pacman-switch update
10+
NeedsTargets
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!@TERMUX_PREFIX@/bin/bash
2+
3+
set -e
4+
5+
sws=()
6+
export _PS_RUN_IN_ALPM_HOOKS=true
7+
8+
while read -r i; do
9+
sws+=("$(basename "${i//.sw/}")")
10+
done
11+
12+
if [[ "${1}" = "update" || "${1}" = "disable" ]]; then
13+
sws=($(pacman-switch -Qs ${sws[@]}))
14+
if [ -z "${sws}" ]; then
15+
exit 0
16+
fi
17+
fi
18+
19+
case "${1}" in
20+
"enable") pacman-switch -Ea ${sws[@]};;
21+
"update") pacman-switch -Su ${sws[@]};;
22+
"disable") pacman-switch -Da ${sws[@]};;
23+
esac

scripts/build/termux_step_create_debian_package.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ termux_step_create_debian_package() {
5353
termux_step_create_debscripts
5454
# Process `update-alternatives` entries from `.alternatives` files
5555
# These need to be merged into the `.postinst` and `.prerm` files, so after those are created.
56-
termux_step_update_alternatives
56+
termux_step_setup_alternatives
5757

5858
# Create control.tar.xz
5959
tar --sort=name \

scripts/build/termux_step_create_pacman_package.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ termux_step_create_pacman_package() {
124124
termux_step_create_debscripts
125125
# Process `update-alternatives` entries from `.alternatives` files
126126
# These need to be merged into the `.postinst` and `.prerm` files, so after those are created.
127-
termux_step_update_alternatives
127+
termux_step_setup_alternatives
128128
termux_step_create_pacman_install_hook
129129

130130
# ensure all elements of the package have the same mtime
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# shellcheck shell=bash
2+
3+
# The Debian `.alternatives` format is a simple ad-hoc plain text format
4+
# to declaratively define groups for `update-alternatives`.
5+
# This function parses files of this format and returns their contents
6+
# in the associative arrays: ${LINK[@]} ${[ALTERNATIVE@]} ${DEPENDENTS[@]} ${PRIORITY[@]}
7+
termux_parse_alternatives() {
8+
local line key value
9+
local dependents=0
10+
while IFS=$'\n' read -r line; do
11+
12+
key="${line%%:*}" # Part before the first ':'
13+
value="${line#*:[[:blank:]]*}" # Part after the first `:`, leading whitespace stripped
14+
15+
case "$key" in
16+
'Name') NAME+=("$value") dependents=0 ;;
17+
'Link') LINK[${NAME[-1]}]="$value" dependents=0 ;;
18+
'Alternative') ALTERNATIVE[${NAME[-1]}]="$value" dependents=0 ;;
19+
'Priority') PRIORITY[${NAME[-1]}]="$value" dependents=0 ;;
20+
'Dependents') dependents=1; continue;;
21+
esac
22+
23+
if (( dependents )); then
24+
read -r dep_link dep_name dep_alternative <<< "$line"
25+
if [ "${TERMUX_PACKAGE_FORMAT}" = "pacman" ]; then
26+
# Data format for pacman-switch
27+
DEPENDENTS[${NAME[-1]}]+=" ${dep_link}:${dep_alternative}"
28+
else
29+
# Data format for update-alternatives
30+
DEPENDENTS[${NAME[-1]}]+=" --slave \"${TERMUX_PREFIX}/${dep_link}\" \"${dep_name}\" \"${TERMUX_PREFIX}/${dep_alternative}\""$' \\\n'
31+
fi
32+
fi
33+
34+
done < <(sed -e 's|\s*#.*$||g' "$1") # Strip out any comments
35+
}
36+
37+
termux_step_setup_alternatives() {
38+
if [ "${TERMUX_PACKAGE_FORMAT}" = "pacman" ]; then
39+
printf '%s\n' "INFO: Creating switcher files for 'pacman-switch':" 1>&2
40+
else
41+
printf '%s\n' "INFO: Processing 'update-alternatives' entries:" 1>&2
42+
fi
43+
for alternatives_file in "${TERMUX_PKG_BUILDER_DIR}"/*.alternatives; do
44+
[[ -f "$alternatives_file" ]] || continue
45+
local -a NAME=()
46+
local -A DEPENDENTS=() LINK=() ALTERNATIVE=() PRIORITY=()
47+
termux_parse_alternatives "$alternatives_file"
48+
49+
local name
50+
for name in "${NAME[@]}"; do
51+
# Not every entry will have dependents in its group
52+
# but we need to initialize the keys regardless
53+
: "${DEPENDENTS[$name]:=}"
54+
done
55+
56+
if [ "${TERMUX_PACKAGE_FORMAT}" = "pacman" ]; then
57+
local name_alternatives="$(basename "${alternatives_file//.alternatives/}")"
58+
(
59+
# Storage location of switcher files
60+
cd "$TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX_CLASSICAL"
61+
mkdir -p ./share/pacman-switch
62+
# Log message to indicate the switcher name
63+
printf 'INFO: %s\n' "${name_alternatives}:" 1>&2
64+
{
65+
for name in "${NAME[@]}"; do
66+
# The second part of the log messenger to
67+
# indicate the switcher group and its points
68+
printf 'INFO: %s\n' "${name}:${PRIORITY[$name]}:" 1>&2
69+
local point
70+
for point in ${LINK[$name]}:${ALTERNATIVE[$name]} ${DEPENDENTS[$name]}; do
71+
printf 'INFO: %s\n' "${point//:/ -> }" 1>&2
72+
done
73+
# Filling the switcher data in shell function format
74+
echo "switcher_group_${name}() {"
75+
echo " priority=${PRIORITY[$name]}"
76+
echo " points=(${LINK[$name]}:${ALTERNATIVE[$name]}${DEPENDENTS[$name]})"
77+
echo "}"
78+
done
79+
} > "./share/pacman-switch/${name_alternatives}.sw"
80+
)
81+
else
82+
# Handle postinst script
83+
[[ -f postinst ]] && mv postinst{,.orig}
84+
85+
{ # Splice in the alternatives
86+
# Use the original shebang if there's a 'postinst.orig'
87+
[[ -f postinst.orig ]] && head -n1 postinst.orig || echo "#!${TERMUX_PREFIX}/bin/sh"
88+
# Boilerplate header comment and checks
89+
echo "# Automatically added by termux_step_setup_alternatives"
90+
echo "if [ \"\$1\" = 'configure' ] || [ \"\$1\" = 'abort-upgrade' ] || [ \"\$1\" = 'abort-deconfigure' ] || [ \"\$1\" = 'abort-remove' ]; then"
91+
echo " if [ -x \"${TERMUX_PREFIX}/bin/update-alternatives\" ]; then"
92+
# 'update-alternatives' command for each group
93+
for name in "${NAME[@]}"; do
94+
# Main alternative group
95+
printf '%b' \
96+
" # ${name}\n" \
97+
" update-alternatives" $' \\\n' \
98+
" --install \"${TERMUX_PREFIX}/${LINK[$name]}\" \"${name}\" \"${TERMUX_PREFIX}/${ALTERNATIVE[$name]}\" ${PRIORITY[$name]}"
99+
# If we have dependents, add those as well
100+
if [[ -n "${DEPENDENTS[$name]}" ]]; then
101+
# We need to add a ' \<lf>' to the --install line,
102+
# and remove the last ' \<lf>' from the dependents.
103+
printf ' \\\n%s' "${DEPENDENTS[$name]%$' \\\n'}"
104+
fi
105+
echo ""
106+
done
107+
# Close up boilerplate and add end comment
108+
echo " fi"
109+
echo "fi"
110+
echo "# End automatically added section"
111+
} > postinst
112+
if [[ -f postinst.orig ]]; then
113+
tail -n+2 postinst.orig >> postinst
114+
rm postinst.orig
115+
fi
116+
117+
# Handle prerm script
118+
[[ -f prerm ]] && mv prerm{,.orig}
119+
120+
{ # Splice in the alternatives
121+
# Use the original shebang if there's a 'prerm.orig'
122+
[[ -f prerm.orig ]] && head -n1 prerm.orig || echo "#!${TERMUX_PREFIX}/bin/sh"
123+
# Boilerplate header comment and checks
124+
echo "# Automatically added by termux_step_setup_alternatives"
125+
echo "if [ \"\$1\" = 'remove' ] || [ \"\$1\" != 'upgrade' ]; then"
126+
echo " if [ -x \"${TERMUX_PREFIX}/bin/update-alternatives\" ]; then"
127+
# Remove each group
128+
for name in "${NAME[@]}"; do
129+
# Log message for this alternative group
130+
printf 'INFO: %s\n' "${name} -> ${ALTERNATIVE[$name]} (${PRIORITY[$name]})" 1>&2
131+
# Removal line
132+
printf '%s\n' " update-alternatives --remove \"${name}\" \"${TERMUX_PREFIX}/${ALTERNATIVE[$name]}\""
133+
done
134+
# Close up boilerplate and add end comment
135+
echo " fi"
136+
echo "fi"
137+
echo "# End automatically added section"
138+
} > prerm
139+
if [[ -f prerm.orig ]]; then
140+
tail -n+2 prerm.orig >> prerm
141+
rm prerm.orig
142+
fi
143+
fi
144+
done
145+
}

scripts/build/termux_step_update_alternatives.sh

Lines changed: 0 additions & 108 deletions
This file was deleted.

0 commit comments

Comments
 (0)