Skip to content

Commit

Permalink
Ensure correct shortcut path by querying menuinst.
Browse files Browse the repository at this point in the history
Add aliases to all shell init files in user mode.
Fixed issue where post-install script would prematurely exit when adding alias to shell init files.
Use menuinst to remove shortcut when uninstalling.
  • Loading branch information
mrclary committed Jan 17, 2024
1 parent 872b512 commit 852cdf5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 45 deletions.
26 changes: 16 additions & 10 deletions installers-conda/resources/post-install.bat
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
:: This script launches Spyder after install
@echo off

:: Mark as conda-based-app
echo. > %PREFIX%\envs\spyder-runtime\Menu\conda-based-app
set spy_rt=%PREFIX%\envs\spyder-runtime
set menu=%spy_rt%\Menu\spyder-menu.json
set mode=system
if exist "%PREFIX%\.nonadmin" set mode=user

echo %PREFIX% | findstr /b "%USERPROFILE%" > nul && (
set shortcut_root=%APPDATA%
) || (
set shortcut_root=%ALLUSERSPROFILE%
:: Get shortcut path
for /F "tokens=*" %%i in (
'%PREFIX%\python -c "from menuinst.api import _load; menu, menu_items = _load(r'%menu%', target_prefix=r'%spy_rt%', base_prefix=r'%PREFIX%', _mode='%mode%'); print(menu_items[0]._paths()[0])"'
) do (
set shortcut=%%~fi
)
set shortcut="%shortcut_root%\Microsoft\Windows\Start Menu\Programs\spyder\Spyder.lnk"

:: Mark as conda-based-app
echo. > "%spy_rt%\Menu\conda-based-app"

:: Launch Spyder
set tmpdir=%TMP%\spyder
set launch_script=%tmpdir%\launch_script.bat

mkdir %tmpdir% 2> nul
if not exist "%tmpdir%" mkdir "%tmpdir%"
(
echo @echo off
echo :loop
Expand All @@ -23,9 +29,9 @@ echo if "%%errorlevel%%"=="0" ^(
echo timeout /t 1 /nobreak ^> nul
echo goto loop
echo ^) else ^(
echo start "" /B %shortcut%
echo start "" /B "%shortcut%"
echo exit
echo ^)
) > %launch_script%
) > "%launch_script%"

C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle hidden -Command "& {Start-Process -FilePath %launch_script% -NoNewWindow}"
63 changes: 28 additions & 35 deletions installers-conda/resources/post-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,22 @@ env | sort
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)

alias_text="alias uninstall-spyder=${u_spy_exe}"
if [[ "$OSTYPE" = "darwin"* ]]; then
shortcut_path="/Applications/${INSTALLER_NAME}.app"
if [[ "$all_user" = "false" ]]; then
shortcut_path="${HOME}${shortcut_path}"
else
unset alias_text # Do not create uninstall alias
fi
else
shortcut_path="/share/applications/${name_lower}_${name_lower}.desktop"
if [[ "$all_user" = "true" ]]; then
shortcut_path="/usr${shortcut_path}"
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
fi
name_lower="$(echo ${INSTALLER_NAME} | tr 'A-Z' 'a-z')"
spy_rt="${PREFIX}/envs/spyder-runtime"
spy_exe="${spy_rt}/bin/spyder"
u_spy_exe="${PREFIX}/uninstall-spyder.sh"
mode=$([[ -e "${PREFIX}/.nonadmin" ]] && echo "user" || echo "system")
menu="${spy_rt}/Menu/spyder-menu.json"

shortcut_path=$(${PREFIX}/bin/python - <<EOF
from menuinst.api import _load
menu, menu_items = _load("$menu", target_prefix="$spy_rt", base_prefix="$PREFIX", _mode="$mode")
print(menu_items[0]._paths()[0])
EOF
)

[[ "$OSTYPE" = "linux"* ]] && alias_text="alias spyder=${spy_exe}"
[[ "$mode" = "user" ]] && alias_text="${alias_text:+${alias_text}\n}alias uninstall-spyder=${u_spy_exe}"

# BSD sed requires extra "" after -i flag
if [[ $(sed --version 2>/dev/null) ]]; then
Expand All @@ -48,9 +40,9 @@ m1="# >>> Added by Spyder >>>"
m2="# <<< Added by Spyder <<<"

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

# BSD sed does not like semicolons; newlines work for both BSD and GNU.
Expand All @@ -72,20 +64,17 @@ add_alias() {
}

# ----
if [[ "$all_user" = "true" && "$OSTYPE" = "darwin"* ]]; then
if [[ "$mode" = "system" && "$OSTYPE" = "darwin"* ]]; then
shell_init_list=("/etc/zshrc" "/etc/bashrc")
elif [[ "$all_user" = "true" ]]; then
elif [[ "$mode" = "system" ]]; then
shell_init_list=("/etc/zsh/zshrc" "/etc/bash.bashrc")
else
case $SHELL in
(*"zsh") shell_init_list=("$HOME/.zshrc") ;;
(*"bash") shell_init_list=("$HOME/.bashrc") ;;
esac
shell_init_list=("$HOME/.zshrc" "$HOME/.bashrc")
fi

for shell_init in ${shell_init_list[@]}; do
[[ -z "$shell_init" || -z "$alias_text" ]] && continue
[[ "$all_user" = "true" && ! -f "$shell_init" ]] && continue # Don't create non-existent global init file
[[ "$mode" = "system" && ! -f "$shell_init" ]] && continue # Don't create non-existent global init file
echo "Creating aliases in $shell_init ..."
add_alias
done
Expand Down Expand Up @@ -142,8 +131,12 @@ for x in ${shell_init_list[@]}; do
done
# Remove shortcut and environment
echo "Removing Spyder and environment..."
rm -rf ${shortcut_path}
echo "Removing Spyder shortcut and environment..."
${PREFIX}/bin/python - <<EOF
from menuinst.api import remove
remove("$menu", target_prefix="$spy_rt", base_prefix="$PREFIX")
EOF
rm -rf ${PREFIX}
echo "Spyder successfully uninstalled."
Expand All @@ -168,7 +161,7 @@ distributions with the command:
$ spyder
EOF
if [[ "$all_user" = "true" ]]; then
if [[ "$mode" = "system" ]]; then
cat <<EOF
This command will only be available in new shell sessions.
Expand Down

0 comments on commit 852cdf5

Please sign in to comment.