install.zsh: allow alternatives in deps

Specify a dependency as a|b to check if either a or b is available as a
command. The script itself must check at runtime, which one is actually
available and use the right one. Helper methods for this are provided in
lib/common.zsh:
  - `alternatives a b` aliases a to b, if a is not available
  - `alternatives-noparam a b` replaces a with b, if a is not available,
    and discards any parameters (e.g. for replacing a formatter with cat)
This commit is contained in:
Alexander Gehrke 2021-01-11 12:21:46 +01:00
parent 0ca3e95262
commit 7c14c8ae1a
2 changed files with 17 additions and 8 deletions

View file

@ -6,14 +6,15 @@ typeset -A interpreter_checked
have_dependency() {
if [[ ! $interpreter_checked[$1] ]]; then
check "Checking for $1"
if which $1 &> /dev/null; then
succeed
interpreter_checked[$1]=1
return 0
else
fail "not found in path"
return 1
fi
for variant in ${(s.|.)1}; do
if which $variant &> /dev/null; then
succeed
interpreter_checked[$1]=1
return 0
fi
done
fail "not found in path"
return 1
fi
}

View file

@ -26,6 +26,14 @@ exists() {
type "$1" &>/dev/null
}
alternative() {
if ! exists $1; then eval "$1(){ $2 \$@}"; fi
}
alternative-noparam() {
if ! exists $1; then eval "$1(){ $2 }"; fi
}
depend() {
local missing
local i