From 7c14c8ae1a1eed940dfc308224cfac0b8fada435 Mon Sep 17 00:00:00 2001 From: Alexander Gehrke Date: Mon, 11 Jan 2021 12:21:46 +0100 Subject: [PATCH] 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) --- install.zsh | 17 +++++++++-------- lib/common.zsh | 8 ++++++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/install.zsh b/install.zsh index 9bce967..f6634d1 100755 --- a/install.zsh +++ b/install.zsh @@ -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 } diff --git a/lib/common.zsh b/lib/common.zsh index d18c1d5..726aba6 100644 --- a/lib/common.zsh +++ b/lib/common.zsh @@ -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