Skip to content

Commit

Permalink
Ensure that aliases are created in shell startup for all users if all…
Browse files Browse the repository at this point in the history
…-user install on Linux.

Do not alias uninstall script for all-user installation.
  • Loading branch information
mrclary committed Jul 16, 2023
1 parent 33fdc82 commit 020f1fc
Showing 1 changed file with 57 additions and 30 deletions.
87 changes: 57 additions & 30 deletions installers-conda/resources/post-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,45 @@ echo ""
name_lower=$(echo ${INSTALLER_NAME} | tr 'A-Z' 'a-z')
spy_exe=${PREFIX}/envs/spyder-runtime/bin/spyder
u_spy_exe=${PREFIX}/uninstall-spyder.sh
all_user=$([[ -e ${PREFIX}/.nonadmin ]] && echo false || echo true)

sed_opts=("-i")
user_list=("$HOME:$SHELL")
alias_text="alias uninstall-spyder=${u_spy_exe}"
if [[ $OSTYPE = "darwin"* ]]; then
if [[ "$OSTYPE" = "darwin"* ]]; then
shortcut_path="/Applications/${INSTALLER_NAME}.app"
[[ -e ${PREFIX}/.nonadmin ]] && shortcut_path="${HOME}${shortcut_path}"
if [[ "$all_user" = "true" ]]; then
# Do not create aliases
unset alias_text
unset user_list
else
shortcut_path="${HOME}${shortcut_path}"
fi
sed_opts+=("", "-e")
else
shortcut_path="/share/applications/${name_lower}_${name_lower}.desktop"
[[ -e ${PREFIX}/.nonadmin ]] && shortcut_path="${HOME}/.local${shortcut_path}" || shortcut_path="/usr${shortcut_path}"
alias_text="alias spyder=${spy_exe}\n${alias_text}"
if [[ "$all_user" = "true" ]]; then
shortcut_path="/usr${shortcut_path}"
user_list=($(cut -d: -f3,6,7 /etc/passwd | egrep '^[0-9]{4}:' | cut -d: -f2,3))
alias_text="alias spyder=${spy_exe}" # Do not create uninstall alias
else
shortcut_path="${HOME}/.local${shortcut_path}"
alias_text="alias spyder=${spy_exe}\n${alias_text}"
fi
sed_opts=("-i")
fi

case $SHELL in
(*"zsh") shell_init=$HOME/.zshrc ;;
(*"bash") shell_init=$HOME/.bashrc ;;
esac

m1="# >>> Added by Spyder >>>"
m2="# <<< Added by Spyder <<<"

add_alias() (
if [[ ! -f "$shell_init" || ! -s "$shell_init" ]]; then
echo -e "$m1\n$1\n$m2" > $shell_init
add_alias() {
if [[ ! -f "$user_shell_init" || ! -s "$user_shell_init" ]]; then
echo -e "$m1\n$1\n$m2" > $user_shell_init
exit 0
fi

# Remove old-style markers, if present; discard after EXPERIMENTAL
# installer attrition.
sed ${sed_opts[@]} "/# <<<< Added by Spyder <<<</,/# >>>> Added by Spyder >>>>/d" $shell_init
sed ${sed_opts[@]} "/# <<<< Added by Spyder <<<</,/# >>>> Added by Spyder >>>>/d" $user_shell_init

# Posix compliant sed does not like semicolons.
# Must use newlines to work on macOS
Expand All @@ -60,8 +69,22 @@ add_alias() (
H
}
x
}" $shell_init
)
}" $user_shell_init
}

# ----
for x in ${user_list[@]}; do
user_home=$(echo $x | cut -d: -f1)
user_shell=$(echo $x | cut -d: -f2)
case $user_shell in
(*"zsh") user_shell_init=$user_home/.zshrc ;;
(*"bash") user_shell_init=$user_home/.bashrc ;;
esac
if [[ -n "$user_shell_init" && -n "$alias_text" ]]; then
echo "Creating aliases in $user_shell_init ..."
add_alias "$alias_text"
fi
done

# ----
echo "Creating uninstall script..."
Expand All @@ -78,8 +101,9 @@ shift \$((\$OPTIND - 1))
if [[ -z \$force ]]; then
cat <<EOF
You are about to uninstall Spyder.
If you proceed, aliases will be removed from ${shell_init}
(if present) and the following will be removed:
If you proceed, aliases will be removed for users:
${user_list[@]}
and the following will be removed:
${shortcut_path}
${PREFIX}
Expand All @@ -93,16 +117,25 @@ EOF
fi
fi
# Quit Spyder
if [[ \$OSTYPE = "darwin"* ]]; then
echo "Quitting Spyder.app..."
osascript -e 'quit app "Spyder.app"' 2> /dev/null
fi
# Remove aliases from shell startup
if [[ -f "$shell_init" ]]; then
echo "Removing shell commands..."
sed ${sed_opts[@]} "/$m1/,/$m2/d" $shell_init
fi
for x in ${user_list[@]}; do
user_home=\$(echo \$x | cut -d: -f1)
user_shell=\$(echo \$x | cut -d: -f2)
case \$user_shell in
(*"zsh") user_shell_init=\$user_home/.zshrc ;;
(*"bash") user_shell_init=\$user_home/.bashrc ;;
esac
if [[ -f "\$user_shell_init" ]]; then
echo "Removing shell commands..."
sed ${sed_opts[@]} "/$m1/,/$m2/d" \$user_shell_init
fi
done
# Remove shortcut and environment
echo "Removing Spyder and environment..."
Expand All @@ -113,12 +146,6 @@ echo "Spyder successfully uninstalled."
END
chmod u+x ${u_spy_exe}

# ----
if [[ -n "$shell_init" ]]; then
echo "Creating aliases in $shell_init ..."
add_alias "$alias_text"
fi

# ----
if [[ "$OSTYPE" = "linux"* ]]; then
cat <<EOF
Expand All @@ -137,9 +164,9 @@ To uninstall Spyder, run the following from the command line:
$ uninstall-spyder
These commands will only be available in new shell sessions. To make them
available in this session, you must source your $shell_init file with:
available in this session, you must source your $user_shell_init file with:
$ source $shell_init
$ source $user_shell_init
###############################################################################
Expand Down

0 comments on commit 020f1fc

Please sign in to comment.