Skip to content

Instantly share code, notes, and snippets.

@lambdageek
Last active June 30, 2023 14:43
Show Gist options
  • Save lambdageek/92053418a73d981c2f42ea681efe5839 to your computer and use it in GitHub Desktop.
Save lambdageek/92053418a73d981c2f42ea681efe5839 to your computer and use it in GitHub Desktop.
A zsh script to activate/deactivate a dotnet SDK directory
# To install source this file from your .zshrc file
# Initialize colors.
autoload -U colors
colors
# Allow for functions in the prompt.
setopt PROMPT_SUBST
# Use in a PROMPT variable. For example (together with zsh-git-prompt):
# PROMPT='%B%m%~%b$(git_super_status)$(aleksey_active_dotnet) %# '
function aleksey_active_dotnet() {
local STATUS
STATUS=""
if [[ -n "$DOTNET_ROOT" ]]; then
# DOTNET_ROOT is usually a dir like "blah/blah/dotnet" remove the last "dotnet" and then take just the last directory component
STATUS="${DOTNET_ROOT:A:h:t}"
STATUS="${bold_color}(${reset_color}${STATUS}${bold_color})${reset_color}"
fi
echo "$STATUS"
}
function activate_dotnet () {
local NEW_ROOT
if [[ -n "${DOTNET_ROOT}" ]]; then
echo "Already activated ${DOTNET_ROOT}, use deactivate_dotnet"
return
fi
if [[ -n "$1" ]]; then
NEW_ROOT="$1"
else
NEW_ROOT=`pwd`
fi
# sanity check - there better be a `dotnet` command in NEW_ROOT
if [[ ! -f "${NEW_ROOT}/dotnet" || ! -x "${NEW_ROOT}/dotnet" ]]; then
echo "expected ${NEW_ROOT} to contain a dotnet executable"
return;
fi
export DOTNET_ROOT="${NEW_ROOT}"
export __OLD_PATH_ACTIVATE_DOTNET="${PATH}"
export PATH="${DOTNET_ROOT}:${PATH}"
}
function deactivate_dotnet () {
if [[ ! -n "${__OLD_PATH_ACTIVATE_DOTNET}" ]]; then
if [[ -n "${DOTNET_ROOT}" ]]; then
echo "DOTNET_ROOT was not set by activate_dotnet, deactivate manually"
return
fi
echo "activate_dotnet was not used, nothing to deactivate"
return
fi
unset DOTNET_ROOT
export PATH="${__OLD_PATH_ACTIVATE_DOTNET}"
unset __OLD_PATH_ACTIVATE_DOTNET
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment