Skip to content

Commit 91cc3f5

Browse files
committed
[update] : Divide the process into functions
1 parent 139416f commit 91cc3f5

1 file changed

Lines changed: 103 additions & 86 deletions

File tree

system/aur.sh

Lines changed: 103 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -71,70 +71,11 @@ remove() {
7171
}
7272

7373
# user_check <name>
74-
function user_check () {
74+
user_check () {
7575
if [[ ! -v 1 ]]; then return 2; fi
7676
getent passwd "${1}" > /dev/null
7777
}
7878

79-
# Creating a aur user.
80-
if ! user_check "${aur_username}"; then
81-
useradd -m -d "/aurbuild_temp" "${aur_username}"
82-
fi
83-
mkdir -p "/aurbuild_temp"
84-
chmod 700 -R "/aurbuild_temp"
85-
chown "${aur_username}:${aur_username}" -R "/aurbuild_temp"
86-
echo "${aur_username} ALL=(ALL) NOPASSWD:ALL" > "/etc/sudoers.d/aurbuild"
87-
88-
# Setup keyring
89-
pacman-key --init
90-
pacman-key --populate
91-
92-
# Un comment the mirror list.
93-
#sed -i "s/#Server/Server/g" "/etc/pacman.d/mirrorlist"
94-
95-
# Set pacman args
96-
pacman_args=("--config" "/etc/alteriso-pacman.conf" "--noconfirm")
97-
if [[ "${pacman_debug}" = true ]]; then
98-
pacman_args+=("--debug")
99-
fi
100-
101-
# Install
102-
if ! pacman -Qq "${aur_helper_package}" 1> /dev/null 2>&1; then
103-
_oldpwd="$(pwd)"
104-
105-
# Install depends
106-
for _pkg in "${aur_helper_depends[@]}"; do
107-
if ! pacman -Qq "${_pkg}" > /dev/null 2>&1 | grep -q "${_pkg}"; then
108-
# --asdepsをつけているのでaur.shで削除される --neededをつけているので明示的にインストールされている場合削除されない
109-
pacman -S --asdeps --needed "${pacman_args[@]}" "${_pkg}"
110-
#remove_list+=("${_pkg}")
111-
fi
112-
done
113-
114-
# Build
115-
sudo -u "${aur_username}" git clone "https://aur.archlinux.org/${aur_helper_package}.git" "/tmp/${aur_helper_package}"
116-
cd "/tmp/${aur_helper_package}"
117-
sudo -u "${aur_username}" makepkg --ignorearch --clean --cleanbuild --force --skippgpcheck --noconfirm --syncdeps
118-
119-
# Install
120-
for _pkg in $(cd "/tmp/${aur_helper_package}"; sudo -u "${aur_username}" makepkg --packagelist); do
121-
pacman "${pacman_args[@]}" -U "${_pkg}"
122-
done
123-
124-
# Remove debtis
125-
cd ..
126-
remove "/tmp/${aur_helper_package}"
127-
cd "${_oldpwd}"
128-
fi
129-
130-
if ! type -p "${aur_helper_command}" > /dev/null; then
131-
echo "Failed to install ${aur_helper_package}"
132-
exit 1
133-
fi
134-
135-
# Update database
136-
pacman -Syy "${pacman_args[@]}"
137-
13879
installpkg(){
13980
yes | sudo -u "${aur_username}" \
14081
"${aur_helper_command}" -S \
@@ -146,37 +87,113 @@ installpkg(){
14687
}
14788

14889

149-
# Build and install
150-
chmod +s /usr/bin/sudo
151-
for _pkg in "${@}"; do
152-
pacman -Qq "${_pkg}" > /dev/null 2>&1 && continue
153-
installpkg "${_pkg}"
90+
#-- main funtions --#
91+
prepare_env(){
92+
# Creating a aur user.
93+
if ! user_check "${aur_username}"; then
94+
useradd -m -d "/aurbuild_temp" "${aur_username}"
95+
fi
96+
mkdir -p "/aurbuild_temp"
97+
chmod 700 -R "/aurbuild_temp"
98+
chown "${aur_username}:${aur_username}" -R "/aurbuild_temp"
99+
echo "${aur_username} ALL=(ALL) NOPASSWD:ALL" > "/etc/sudoers.d/aurbuild"
100+
101+
# Setup keyring
102+
pacman-key --init
103+
pacman-key --populate
104+
105+
# Un comment the mirror list.
106+
#sed -i "s/#Server/Server/g" "/etc/pacman.d/mirrorlist"
107+
154108

155-
if ! pacman -Qq "${_pkg}" > /dev/null 2>&1; then
156-
echo -e "\n[aur.sh] Failed to install ${_pkg}\n"
157-
failedpkg+=("${_pkg}")
109+
# Set pacman args
110+
pacman_args=("--config" "/etc/alteriso-pacman.conf" "--noconfirm")
111+
if [[ "${pacman_debug}" = true ]]; then
112+
pacman_args+=("--debug")
158113
fi
159-
done
114+
}
160115

161-
# Reinstall failed package
162-
for _pkg in "${failedpkg[@]}"; do
163-
installpkg "${_pkg}"
164-
if ! pacman -Qq "${_pkg}" > /dev/null 2>&1; then
165-
echo -e "\n[aur.sh] Failed to install ${_pkg}\n"
116+
install_aur_helper(){
117+
# Install
118+
if ! pacman -Qq "${aur_helper_package}" 1> /dev/null 2>&1; then
119+
_oldpwd="$(pwd)"
120+
121+
# Install depends
122+
for _pkg in "${aur_helper_depends[@]}"; do
123+
if ! pacman -Qq "${_pkg}" > /dev/null 2>&1 | grep -q "${_pkg}"; then
124+
# --asdepsをつけているのでaur.shで削除される --neededをつけているので明示的にインストールされている場合削除されない
125+
pacman -S --asdeps --needed "${pacman_args[@]}" "${_pkg}"
126+
#remove_list+=("${_pkg}")
127+
fi
128+
done
129+
130+
# Build
131+
sudo -u "${aur_username}" git clone "https://aur.archlinux.org/${aur_helper_package}.git" "/tmp/${aur_helper_package}"
132+
cd "/tmp/${aur_helper_package}"
133+
sudo -u "${aur_username}" makepkg --ignorearch --clean --cleanbuild --force --skippgpcheck --noconfirm --syncdeps
134+
135+
# Install
136+
for _pkg in $(cd "/tmp/${aur_helper_package}"; sudo -u "${aur_username}" makepkg --packagelist); do
137+
pacman "${pacman_args[@]}" -U "${_pkg}"
138+
done
139+
140+
# Remove debtis
141+
cd ..
142+
remove "/tmp/${aur_helper_package}"
143+
cd "${_oldpwd}"
144+
fi
145+
146+
if ! type -p "${aur_helper_command}" > /dev/null; then
147+
echo "Failed to install ${aur_helper_package}"
166148
exit 1
167149
fi
168-
done
150+
}
151+
152+
install_aur_pkgs(){
153+
# Update database
154+
pacman -Syy "${pacman_args[@]}"
155+
156+
# Build and install
157+
chmod +s /usr/bin/sudo
158+
for _pkg in "${@}"; do
159+
pacman -Qq "${_pkg}" > /dev/null 2>&1 && continue
160+
installpkg "${_pkg}"
161+
162+
if ! pacman -Qq "${_pkg}" > /dev/null 2>&1; then
163+
echo -e "\n[aur.sh] Failed to install ${_pkg}\n"
164+
failedpkg+=("${_pkg}")
165+
fi
166+
done
167+
168+
# Reinstall failed package
169+
for _pkg in "${failedpkg[@]}"; do
170+
installpkg "${_pkg}"
171+
if ! pacman -Qq "${_pkg}" > /dev/null 2>&1; then
172+
echo -e "\n[aur.sh] Failed to install ${_pkg}\n"
173+
exit 1
174+
fi
175+
done
176+
}
177+
178+
cleanup(){
179+
# Remove packages
180+
readarray -t -O "${#remove_list[@]}" remove_list < <(pacman -Qttdq)
181+
(( "${#remove_list[@]}" != 0 )) && pacman -Rsnc "${remove_list[@]}" "${pacman_args[@]}"
182+
183+
# Clean up
184+
"${aur_helper_command}" -Sccc "${pacman_args[@]}"
185+
186+
# remove user and file
187+
userdel "${aur_username}"
188+
remove /aurbuild_temp
189+
remove /etc/sudoers.d/aurbuild
190+
remove "/etc/alteriso-pacman.conf"
191+
remove "/var/cache/pacman/pkg/"
192+
}
169193

170-
# Remove packages
171-
readarray -t -O "${#remove_list[@]}" remove_list < <(pacman -Qttdq)
172-
(( "${#remove_list[@]}" != 0 )) && pacman -Rsnc "${remove_list[@]}" "${pacman_args[@]}"
173194

174-
# Clean up
175-
"${aur_helper_command}" -Sccc "${pacman_args[@]}"
195+
prepare_env
196+
install_aur_helper
197+
install_aur_pkgs "$@"
198+
cleanup
176199

177-
# remove user and file
178-
userdel "${aur_username}"
179-
remove /aurbuild_temp
180-
remove /etc/sudoers.d/aurbuild
181-
remove "/etc/alteriso-pacman.conf"
182-
remove "/var/cache/pacman/pkg/"

0 commit comments

Comments
 (0)