Skip to content

Commit d8e0665

Browse files
authored
refactor, build local PKGBUILD when no args are provided (#191)
1 parent b808cbd commit d8e0665

File tree

1 file changed

+81
-46
lines changed

1 file changed

+81
-46
lines changed

useful-tools/make-aur-package.sh

Lines changed: 81 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,59 +18,94 @@ _info_msg() {
1818
printf "$g%s$o\n" "$l" "${*:-$l}" "$l"
1919
}
2020

21-
# check for basic build dependencies
22-
for d in base-devel git; do
23-
if ! pacman -Q "$d" 2>/dev/null; then
24-
_info_msg "Adding build dependency: $d"
25-
pacman -Syu --noconfirm "$d"
21+
_prepare() {
22+
for d in base-devel git; do
23+
if ! pacman -Q "$d" 2>/dev/null; then
24+
_info_msg "Adding build dependency: $d"
25+
pacman -S --noconfirm "$d"
26+
fi
27+
done
28+
29+
if ! grep -q 'EUID == 69' /usr/bin/makepkg; then
30+
# makepkg cannot not as root by default
31+
sed -i -e 's|EUID == 0|EUID == 69|g' /usr/bin/makepkg
32+
sed -i \
33+
-e 's|-O2|-O3|' \
34+
-e 's|MAKEFLAGS=.*|MAKEFLAGS="-j$(nproc)"|' \
35+
-e 's|#MAKEFLAGS|MAKEFLAGS|' \
36+
/etc/makepkg.conf
37+
cat /etc/makepkg.conf
2638
fi
27-
done
28-
29-
# makepkg does not run when root
30-
sed -i -e 's|EUID == 0|EUID == 69|g' /usr/bin/makepkg
31-
sed -i \
32-
-e 's|-O2|-O3|' \
33-
-e 's|MAKEFLAGS=.*|MAKEFLAGS="-j$(nproc)"|' \
34-
-e 's|#MAKEFLAGS|MAKEFLAGS|' \
35-
/etc/makepkg.conf
36-
cat /etc/makepkg.conf
37-
38-
if [ "$1" = '--chaotic-aur' ]; then
39-
shift
40-
pacman-key --init
41-
pacman-key --recv-key 3056513887B78AEB --keyserver keyserver.ubuntu.com
42-
pacman-key --lsign-key 3056513887B78AEB
43-
pacman --noconfirm -U 'https://cdn-mirror.chaotic.cx/chaotic-aur/chaotic-keyring.pkg.tar.zst'
44-
pacman --noconfirm -U 'https://cdn-mirror.chaotic.cx/chaotic-aur/chaotic-mirrorlist.pkg.tar.zst'
45-
echo '[chaotic-aur]' >> /etc/pacman.conf
46-
echo 'Include = /etc/pacman.d/chaotic-mirrorlist' >> /etc/pacman.conf
39+
}
40+
41+
_setup_chaotic_aur() {
42+
if ! grep -q '^\[chaotic-aur\]' /etc/pacman.conf; then
43+
pacman-key --init
44+
pacman-key --recv-key 3056513887B78AEB --keyserver keyserver.ubuntu.com
45+
pacman-key --lsign-key 3056513887B78AEB
46+
pacman --noconfirm -U 'https://cdn-mirror.chaotic.cx/chaotic-aur/chaotic-keyring.pkg.tar.zst'
47+
pacman --noconfirm -U 'https://cdn-mirror.chaotic.cx/chaotic-aur/chaotic-mirrorlist.pkg.tar.zst'
48+
echo '[chaotic-aur]' >> /etc/pacman.conf
49+
echo 'Include = /etc/pacman.d/chaotic-mirrorlist' >> /etc/pacman.conf
50+
fi
51+
}
52+
53+
_install_chaotic_aur_pkg() {
54+
_setup_chaotic_aur
4755
_info_msg "Adding Chaotic AUR packages: $*"
48-
pacman -Sy --noconfirm "$@"
56+
# no -Syu here since it can remove the debloated packages
57+
sudo pacman -Sy --noconfirm "$@"
4958
exit 0
50-
elif [ "$1" = '--archlinux-pkg' ]; then
51-
shift
59+
}
60+
61+
_get_archlinux_pkgbuild() {
5262
git clone --depth 1 https://gitlab.archlinux.org/archlinux/packaging/packages/"$1" ./"$1"
53-
else
63+
cd ./"$1"
64+
}
65+
66+
_get_aur_pkgbuild() {
5467
git clone --depth 1 https://aur.archlinux.org/"$1" ./"$1"
55-
fi
56-
cd ./"$1"
68+
cd ./"$1"
69+
}
5770

58-
if ! grep -q "arch=.*$ARCH" ./PKGBUILD; then
59-
sed -i -e "s|x86_64|$ARCH|" ./PKGBUILD
60-
fi
71+
_local_pkgbuild() {
72+
if [ ! -f "$PWD"/PKGBUILD ]; then
73+
>&2 echo "ERROR: No PKGBUILD found in $PWD"
74+
exit 1
75+
fi
76+
}
6177

62-
# Run extra commands from env var
63-
if [ -n "$PRE_BUILD_CMDS" ]; then
64-
_info_msg "Running additional pre-build commands..."
65-
while IFS= read -r CMD; do
66-
if [ -n "$CMD" ]; then
67-
_info_msg "Running: $CMD"
68-
eval "$CMD"
69-
fi
70-
done <<-EOF
71-
$PRE_BUILD_CMDS
72-
EOF
73-
fi
78+
_configure_arch() {
79+
if ! grep -q "arch=.*$ARCH" ./PKGBUILD; then
80+
sed -i -e "s|x86_64|$ARCH|" ./PKGBUILD
81+
fi
82+
}
83+
84+
_run_precmds() {
85+
if [ -n "$PRE_BUILD_CMDS" ]; then
86+
_info_msg "Running additional pre-build commands..."
87+
while IFS= read -r CMD; do
88+
if [ -n "$CMD" ]; then
89+
_info_msg "Running: $CMD"
90+
eval "$CMD"
91+
fi
92+
done <<-EOF
93+
$PRE_BUILD_CMDS
94+
EOF
95+
fi
96+
}
97+
98+
_prepare
99+
100+
case "$1" in
101+
--chaotic-aur) shift; _install_chaotic_aur_pkg "$@";;
102+
--archlinux-pkg) shift; _get_archlinux_pkgbuild "$@";;
103+
'') _local_pkgbuild ;;
104+
*) _get_aur_pkgbuild "$@";;
105+
esac
106+
107+
_configure_arch
108+
_run_precmds
74109

75110
_info_msg "To build:"
76111
cat ./PKGBUILD

0 commit comments

Comments
 (0)