scripts/install.zsh
Alexander Gehrke 7c14c8ae1a 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)
2021-01-11 12:29:39 +01:00

74 lines
1.5 KiB
Bash
Executable file

#!/bin/zsh
source ${$(realpath "$0"):h}/lib/common.zsh
typeset -A interpreter_checked
have_dependency() {
if [[ ! $interpreter_checked[$1] ]]; then
check "Checking for $1"
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
}
uses_interpreter() {
head -n 1 $2 | grep -q $1
}
get_dependencies() {
sed -n '2{/^#dep:/{s/^#dep://;p;q}}' $1
}
if [[ -z $1 ]]; then
<<-HELP
Usage: $0 [opts] <program_names>
Options:
-p, --path PATH target directory to install into (defaults to ~/.local/bin)
-f, --force overwrite existing files in the same dir
-s, --skip when installing several scripts, skip scripts with unmet
dependencies instead of aborting.
HELP
exit 1
fi
zparseopts -D -E p:=install_path -path:=install_path f=force -force=force \
s=skip -skip=skip
if [[ ! $install_path ]]; then
install_path=$HOME/.local/bin
else
install_path=${install_path[2]}
fi
for prog in $@; do
for lang in zsh ruby python amm; do
if uses_interpreter $lang $prog; then
have_dependency $lang || exit 1
fi
done
for dep in $(get_dependencies $prog); do
if ! have_dependency $dep; then
if [[ $skip ]]; then
continue 2
else
exit 1
fi
fi
done
if [[ -e $install_path/${prog:t} && ! $force ]]; then
warning "$prog already exists at $install_path. Skipping."
else
check "Installing $prog"
ln -fsr $prog $install_path || fail
succeed
fi
done