Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNate McMaster <natemcmaster@users.noreply.github.com>2019-02-20 22:20:34 +0300
committerGitHub <noreply@github.com>2019-02-20 22:20:34 +0300
commit6fea3a6e95a36c3fff49057a8cf67632272ac4d0 (patch)
treedee441a24a422295410459c0e4d76cb2c7caf215 /activate.sh
parent9a2db43848e9527d969b41cbee9c7739f8dbb0c5 (diff)
Show error message when activate.sh/ps1 is invoked directly instead of sourced (#7731)
Helps users avoid a common mistake which is otherwise not obvious.
Diffstat (limited to 'activate.sh')
-rw-r--r--activate.sh33
1 files changed, 29 insertions, 4 deletions
diff --git a/activate.sh b/activate.sh
index 894479a8b9..4986e1b297 100644
--- a/activate.sh
+++ b/activate.sh
@@ -3,10 +3,35 @@
# You cannot run it directly.
# To exit from the environment this creates, execute the 'deactivate' function.
+_RED="\033[0;31m"
_MAGENTA="\033[0;95m"
_YELLOW="\033[0;33m"
_RESET="\033[0m"
+# This detects if a script was sourced or invoked directly
+# See https://stackoverflow.com/a/28776166/2526265
+sourced=0
+if [ -n "$ZSH_EVAL_CONTEXT" ]; then
+ case $ZSH_EVAL_CONTEXT in *:file) sourced=1;; esac
+ THIS_SCRIPT="${0:-}"
+elif [ -n "$KSH_VERSION" ]; then
+ [ "$(cd $(dirname -- $0) && pwd -P)/$(basename -- $0)" != "$(cd $(dirname -- ${.sh.file}) && pwd -P)/$(basename -- ${.sh.file})" ] && sourced=1
+ THIS_SCRIPT="${0:-}"
+elif [ -n "$BASH_VERSION" ]; then
+ (return 2>/dev/null) && sourced=1
+ THIS_SCRIPT="$BASH_SOURCE"
+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) sourced=1;; esac
+ THIS_SCRIPT="${0:-}"
+fi
+
+if [ $sourced -eq 0 ]; then
+ printf "${_RED}This script cannot be invoked directly.${_RESET}\n"
+ printf "${_RED}To function correctly, this script file must be sourced by calling \"source $0\".${_RESET}\n"
+ exit 1
+fi
+
deactivate () {
# reset old environment variables
@@ -38,7 +63,7 @@ deactivate () {
# Cleanup the environment
deactivate init
-DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+DIR="$( cd "$( dirname "$THIS_SCRIPT" )" && pwd )"
_OLD_PATH="$PATH"
# Tell dotnet where to find itself
export DOTNET_ROOT="$DIR/.dotnet"
@@ -60,10 +85,10 @@ if [ -n "${BASH:-}" ] || [ -n "${ZSH_VERSION:-}" ] ; then
hash -r 2>/dev/null
fi
-echo "${_MAGENTA}Enabled the .NET Core environment. Execute 'deactivate' to exit.${_RESET}"
+printf "${_MAGENTA}Enabled the .NET Core environment. Execute 'deactivate' to exit.${_RESET}\n"
if [ ! -f "$DOTNET_ROOT/dotnet" ]; then
- echo "${_YELLOW}.NET Core has not been installed yet. Run $DIR/restore.sh to install it.${_RESET}"
+ printf "${_YELLOW}.NET Core has not been installed yet. Run $DIR/restore.sh to install it.${_RESET}\n"
else
- echo "dotnet = $DOTNET_ROOT/dotnet"
+ printf "dotnet = $DOTNET_ROOT/dotnet\n"
fi