foxshell/fn/-z4h-install-many
2025-07-04 11:48:40 -05:00

125 lines
3.5 KiB
Bash

#!/usr/bin/env zsh
local success
{
if (( ! $+_z4h_no_autoupdate && ! $+Z4H_SSH && !_z4h_dangerous_root && _z4h_zle )) &&
zstyle -t :z4h: auto-update ask && [[ -t 0 && -t 1 && -t 2 ]]; then
typeset -gri _z4h_no_autoupdate=1
local days
if zstyle -s :z4h: auto-update-days days && [[ $days == <-> ]]; then
# Check if update is required.
local -a last_update_ts
if zstat -A last_update_ts +mtime -- $Z4H/cache/last-update-ts 2>/dev/null &&
(( EPOCHSECONDS - last_update_ts[1] >= 86400 * days )) &&
[[ ! -e $Z4H/.updating ]]; then
local REPLY
{
read -q ${(%):-"?%F{3}z4h%f: update dependencies? [y/N]: "} && REPLY=y
} always {
print -u ${_z4h_tty_fd-2}
}
print -n >$Z4H/cache/last-update-ts || return
if [[ $REPLY == y ]]; then
z4h update && return
fi
print -Pru2 -- "%F{3}z4h%f: won't ask to update again for %B$days%b day(s)"
print -Pru2 -- ""
print -Pru2 -- "To update manually, type:"
print -Pru2 -- ""
print -Pru2 -- " %F{2}z4h%f %Bupdate%b"
print -Pru2 -- ""
fi
fi
fi
local -aU want=(${_z4h_install_queue%%@*})
local have=($Z4H/$^want(N))
have=(${have#$Z4H/})
local missing=(${want:|have})
(( $#missing )) || return 0
if [[ ! -v commands[uname] ]]; then
print -Pru2 -- '%F{3}z4h%f: command not found: %F{1}uname%f'
return 1
fi
local uname_sm
uname_sm=$(uname -sm) || return
case ${(L)uname_sm} in
'darwin arm64');;
'darwin x86_64');;
'linux aarch64');;
'linux armv6l');;
'linux armv7l');;
'linux armv8l');;
'linux x86_64');;
'linux i686');;
*)
print -Pru2 -- "%F{3}z4h%f: unsupported platform: %F{1}${uname_sm//\%/%%}%f"
return 1
;;
esac
local repo base queue=()
for repo in $_z4h_install_queue; do
base=${repo%%@*}
(( missing[(Ie)$base] )) || continue
missing=(${missing:#$base})
queue+=($repo)
done
if (( _z4h_dangerous_root )); then
for repo in $queue; do
print -Pru2 -- "%F{3}z4h%f: refusing to %Binstall%b as %F{1}root%f: %B${repo//\%/%%}%b"
done
return 1
fi
typeset -gi _z4h_installed_something=1
if (( $#queue == 1 )) || [[ -o interactive && ZSH_SUBSHELL -ne 0 ]] ||
! () { setopt localoptions monitor 2>/dev/null }; then
for repo in $queue; do
-z4h-install-one $repo || return
done
return
fi
if (( $+commands[mktemp] )); then
success="$(command mktemp -- $Z4H/tmp/install-many.XXXXXXXXXX)" || return
else
success=$Z4H/tmp/install-many.$$
zf_rm -rf -- $success || return
print -n >$success || return
fi
function -z4h-install-many-parallel() {
eval "$_z4h_opt"
local repo fd head=() tail=()
local omz=ohmyzsh/ohmyzsh
queue=(${(M)queue:#powerlevel10k} ${queue:#powerlevel10k})
queue=(${queue:#$omz} ${(M)queue:#$omz})
for repo in $queue; do
if (( $#head + $#tail == 8 )); then
fd=$head[1]
<&$fd >&2 || return
exec {fd}<&- || return
shift 1 head
fi
exec {fd}< <( (-z4h-install-one $repo 2>&1 ) || zf_rm -f -- $success ) || return
[[ $repo == powerlevel10k ]] && tail+=$fd || head+=$fd
done
for fd in $head $tail; do
<&$fd >&2 || return
exec {fd}<&- || return
done
}
-z4h-run-process-tree -z4h-install-many-parallel || return
[[ -e $success ]] || return
} always {
unset _z4h_install_queue
unset "functions[-z4h-install-many-parallel]"
[[ -z $success ]] || zf_rm -f -- $success
}