scripts/install.zsh

73 lines
1.5 KiB
Bash
Raw Normal View History

2020-04-02 08:38:36 +00:00
#!/bin/zsh
source ${$(realpath "$0"):h}/lib/common.zsh
typeset -A interpreter_checked
have_dependency() {
2020-04-02 08:38:36 +00:00
if [[ ! $interpreter_checked[$1] ]]; then
check "Checking for $1"
if which $1 &> /dev/null; then
2020-04-02 08:38:36 +00:00
succeed
interpreter_checked[$1]=1
return 0
else
fail "not found in path"
return 1
fi
fi
}
uses_interpreter() {
head -n 1 $2 | grep -q $1
}
get_dependencies() {
sed -n '2{/^#dep:/{s/^#dep://;p;q}}' $1
}
2020-04-02 08:38:36 +00:00
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.
2020-04-02 08:38:36 +00:00
HELP
exit 1
fi
zparseopts -D -E p:=install_path -path:=install_path f=force -force=force \
s=skip -skip=skip
2020-04-02 08:38:36 +00:00
if [[ ! $install_path ]]; then
install_path=$HOME/.local/bin
else
install_path=${install_path[2]}
2020-04-02 08:38:36 +00:00
fi
for prog in $@; do
for lang in zsh ruby python amm; do
2020-04-02 08:38:36 +00:00
if uses_interpreter $lang $prog; then
have_dependency $lang || exit 1
2020-04-02 08:38:36 +00:00
fi
done
for dep in $(get_dependencies $prog); do
2021-01-11 09:35:04 +00:00
if ! have_dependency $dep; then
if [[ $skip ]]; then
continue 2
else
exit 1
fi
fi
done
2020-04-02 08:38:36 +00:00
if [[ -e $install_path/${prog:t} && ! $force ]]; then
2020-04-02 08:43:50 +00:00
warning "$prog already exists at $install_path. Skipping."
2020-04-02 08:38:36 +00:00
else
check "Installing $prog"
ln -fsr $prog $install_path || fail
succeed
2020-04-02 08:38:36 +00:00
fi
done