110 lines
3.0 KiB
Plaintext

inherit config multilib
DESCRIPTION="Manage electron versions"
MAINTAINER="rexerton.rexy@tutamail.com"
VERSION="2.2"
USR_PATH="${EROOT%/}/usr"
OPT_PATH="${EROOT%/}/opt"
active_slot() {
if [ -h "${USR_PATH}/bin/electron" ];then
name="$(readlink "${USR_PATH}/bin/electron" | xargs basename)"
if get_slots | grep -q "$name";then
echo "$name"
return
fi
fi
echo "(none)"
}
get_slots() {
local found_slots=()
for slot in $(find "${USR_PATH}/bin" -mindepth 1 -maxdepth 1 \( -type f -o -type l \) -regex "${USR_PATH}"'/bin/electron\(-bin\)?-[1-9][0-9.]+\(-r[1-9]+\)?$');do
[ -x "${slot}" ] && found_slots+=( "${slot##*/}" )
done
echo ${found_slots[@]}
}
describe_list() {
echo "List available electron slots"
}
do_list() {
write_list_start "Available electron slots"
if $(is_output_mode brief);then
echo $(get_slots)
else
i=1
for slot in $(get_slots);do
case "${slot}" in
"$(active_slot)" )
write_kv_list_entry "$i) $(highlight_marker ${slot})";;
*)
write_kv_list_entry "$i) ${slot}";;
esac
i=$((i+1))
done
[ -z "$(get_slots)" ] && write_warning_msg "No slots available."
fi
}
describe_show() {
echo "Get active slot"
}
do_show() {
echo "$(active_slot)"
}
describe_set() {
echo "Set the symlink for the electron application"
}
_create_symlinks() {
local version_name="$1"
local libdir="$(get_libdir)"
[ -h "${USR_PATH}/bin/electron" ] && rm "${USR_PATH}/bin/electron"
ln -sf "${USR_PATH}/bin/${version_name}" "${USR_PATH}/bin/electron" || die
if [ -d "${USR_PATH}/${libdir}/${version_name}" ];then
[ -h "${USR_PATH}/${libdir}/electron" ] && rm "${USR_PATH}/${libdir}/electron"
ln -sf "${USR_PATH}/${libdir}/${version_name}" "${USR_PATH}/${libdir}/electron" || die
elif [ -d "${OPT_PATH}/electron/${version_name}" ];then
[ -h "${USR_PATH}/${libdir}/electron" ] && rm "${USR_PATH}/${libdir}/electron"
ln -sf "${OPT_PATH}/electron/${version_name}" "${USR_PATH}/${libdir}/electron" || die
elif [ -h "${USR_PATH}/${libdir}/electron" ];then
rm "${USR_PATH}/${libdir}/electron"
fi
if [ -d "${USR_PATH}/include/${version_name}" ];then
[ -h "${USR_PATH}/include/electron" ] && rm "${USR_PATH}/include/electron"
ln -sf "${USR_PATH}/include/${version_name}" "${USR_PATH}/include/electron" || die
elif [ -h "${USR_PATH}/include/electron" ];then
rm "${USR_PATH}/include/electron"
fi
}
do_set() {
local selected="$1"
if [ -e "${USR_PATH}/bin/${selected}" ] && [[ "${selected}" =~ ^electron(-bin)?-[1-9][0-9.]+(-r[1-9]+)? ]];then
_create_symlinks "${selected}"
return
elif [[ "${selected}" =~ ^[1-9][0-9]*$ ]];then
slots=( $(get_slots) )
if [ "${selected}" -le ${#slots[@]} ];then
_create_symlinks "${slots[$((selected-1))]}"
return
fi
fi
die -q "Not a valid slot"
}
describe_unset() {
echo "Remove electron symlinks"
}
do_unset() {
[ -h "${USR_PATH}/bin/electron" ] && rm "${USR_PATH}/bin/electron"
[ -h "${USR_PATH}/$(get_libdir)/electron" ] && rm "${USR_PATH}/$(get_libdir)/electron"
[ -h "${USR_PATH}/include/electron" ] && rm "${USR_PATH}/include/electron"
}