Allow ghcup to be sourced

This commit is contained in:
Julian Ospald 2018-11-04 18:20:50 +08:00
parent 72acd59025
commit 91f046f6e9
No known key found for this signature in database
GPG Key ID: 511B62C09D50CD28

49
ghcup
View File

@ -43,7 +43,9 @@ VERSION=0.0.6
# @VARIABLE: SCRIPT
# @DESCRIPTION:
# Name of this script.
# Name of this script. This will be the
# shell name if this script is sourced, so
# only rely on this for echos and trivial things.
SCRIPT="$(basename "$0")"
# @VARIABLE: VERBOSE
@ -905,6 +907,34 @@ posix_realpath() {
unset mysource mydir posix_realpath_error
}
# @FUNCTION: is_sourced
# @DESCRIPTION:
# Tries to figure out if we are being sourced. Based on
# https://stackoverflow.com/a/28776166
# @RETURNS: 0 if we are being sourced, 1 otherwise
is_sourced() {
if [ -n "$ZSH_EVAL_CONTEXT" ]; then
case $ZSH_EVAL_CONTEXT in
*:file)
return 0 ;;
esac
elif [ -n "$KSH_VERSION" ]; then
# shellcheck disable=SC2154
[ "$(cd "$(dirname -- "$0")" && pwd -P)/$(basename -- "$0")" != "$(cd "$(dirname -- "${.sh.file}")" && pwd -P)/$(basename -- "${.sh.file}")" ] && return 0
elif [ -n "$BASH_VERSION" ]; then
# shellcheck disable=SC2128
[ "$0" != "$BASH_SOURCE" ] && return 0
else # All other shells: examine $0 for known shell binary filenames
# Detects `sh` and `dash`; add additional shell filenames as needed.
case ${0##*/} in
sh|dash)
return 0 ;;
esac
fi
# assume we are not sourced, if our above checks didn't find it
return 1
}
@ -1428,9 +1458,11 @@ if [ -z "$HOME" ] ; then
die "HOME env not set, cannot operate"
fi
edo mkdir -p "${INSTALL_BASE}"
edo mkdir -p "${BIN_LOCATION}"
edo mkdir -p "${CACHE_LOCATION}"
if ! is_sourced ; then
edo mkdir -p "${INSTALL_BASE}"
edo mkdir -p "${BIN_LOCATION}"
edo mkdir -p "${CACHE_LOCATION}"
fi
@ -1439,10 +1471,11 @@ edo mkdir -p "${CACHE_LOCATION}"
#--[ Command line parsing and entry point ]--#
##############################################
if ! is_sourced ; then
[ $# -lt 1 ] && usage
[ $# -lt 1 ] && usage
while [ $# -gt 0 ] ; do
while [ $# -gt 0 ] ; do
case $1 in
-v|--verbose)
VERBOSE=true
@ -1477,7 +1510,6 @@ while [ $# -gt 0 ] ; do
command_exists "${com}" || die "Command \"${com}\" is required, but does not exist! Please install."
done
unset com
case $1 in
install)
shift 1
@ -1625,6 +1657,7 @@ while [ $# -gt 0 ] ; do
esac
break;;
esac
done
done
fi # is_sourced
# vim: tabstop=4 shiftwidth=4 expandtab