core: 优化依赖包安装

This commit is contained in:
bin456789 2023-09-16 20:00:06 +08:00
parent baf928ab0f
commit ef67973707
No known key found for this signature in database
GPG Key ID: EE301B386DE6C11B

View File

@ -555,57 +555,74 @@ verify_os_string() {
usage_and_exit usage_and_exit
} }
apt_install() {
[ -z "$apk_updated" ] && apt update && apk_updated=1
apt install -y $pkgs
}
install_pkg() { install_pkg() {
cmds=$*
is_in_windows && return is_in_windows && return
need_install=false for cmd in "$@"; do
for cmd in $cmds; do if ! command -v $cmd ||
if ! command -v $cmd >/dev/null; then # gentoo 默认编译的 unsquashfs 不支持 xz
need_install=true { [ "$cmd" = unsquashfs ] &&
break command -v emerge &&
! unsquashfs |& grep -w xz &&
echo "unsquashfs not supported xz. need rebuild."; }; then
if ! find_pkg_mgr; then
error_and_exit "Can't find compatible package manager. Please manually install $cmd."
fi
cmd_to_pkg
install_pkg_real
fi fi
done done
if $need_install; then find_pkg_mgr() {
if is_in_alpine; then if [ -z "$pkg_mgr" ]; then
add_community_repo_for_alpine # command -v 有先后顺序dnf放yum前面
if ! pkg_mgr=$(command -v dnf yum apt pacman zypper emerge apk | head -1 | awk -F/ '{print $NF}' | grep .); then
return 1
fi fi
# cmds to pkgs fi
for cmd in $cmds; do }
pkg=
cmd_to_pkg() {
unset USE
case $cmd in case $cmd in
unsquashfs) command -v zypper && pkg=squashfs || pkg=squashfs-tools ;; lsmem | lsblk) pkg="util-linux" ;;
lsmem | lsblk) pkg=util-linux ;; unsquashfs)
case "$pkg_mgr" in
zypper) pkg="squashfs" ;;
emerge) pkg="squashfs-tools" && export USE="lzma" ;;
*) pkg="squashfs-tools" ;;
esac
;;
nslookup | dig) nslookup | dig)
{ command -v apk && pkg="bind-tools"; } || case "$pkg_mgr" in
{ command -v apt && pkg="dnsutils"; } || apt) pkg="dnsutils" ;;
{ command -v pacman && pkg="bind"; } || pacman) pkg="bind" ;;
{ command -v yum dnf zypper && pkg="bind-utils"; } apk | emerge) pkg="bind-tools" ;;
yum | dnf | zypper) pkg="bind-utils" ;;
esac
;; ;;
*) pkg=$cmd ;; *) pkg=$cmd ;;
esac esac
pkgs+=" $pkg" }
done
# command -v 有先后顺序dnf放yum前面 install_pkg_real() {
pkg_mgr=$(command -v dnf yum apt pacman zypper apk | head -1 | awk -F/ '{print $NF}')
case $pkg_mgr in case $pkg_mgr in
dnf) dnf install -y --setopt=install_weak_deps=False $pkgs ;; dnf) dnf install -y --setopt=install_weak_deps=False $pkg ;;
yum) yum install -y $pkgs ;; yum) yum install -y $pkg ;;
apk) apk add $pkgs ;; emerge) emerge --oneshot $pkg ;;
apt) apt_install $pkgs ;; pacman) pacman -Syu --noconfirm --needed $pkg ;;
pacman) pacman -Syu --noconfirm --needed $pkgs ;; zypper) zypper install -y $pkg ;;
zypper) zypper install -y $pkgs ;; apk)
*) error_and_exit "Unexpected package manager: $pkg_mgr" ;; add_community_repo_for_alpine
apk add $pkg
;;
apt)
[ -z "$apk_updated" ] && apt update && apk_updated=1
apt install -y $pkg
;;
esac esac
fi }
} }
check_ram() { check_ram() {