Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nsis): migrate old shortcuts by setting the path only instead of re-creating a new one #9915

Merged
merged 12 commits into from
Jun 4, 2024
5 changes: 5 additions & 0 deletions .changes/nsis-migrate-shortcut.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-bundler": "patch:bug"
---

For NSIS installer, migrate old shortcuts by setting the path only instead of re-creating a new one
47 changes: 23 additions & 24 deletions tooling/bundler/src/bundle/windows/templates/installer.nsi
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ Var AppStartMenuFolder
!define MUI_FINISHPAGE_SHOWREADME_FUNCTION CreateDesktopShortcut
; Show run app after installation.
!define MUI_FINISHPAGE_RUN "$INSTDIR\${MAINBINARYNAME}.exe"
!define MUI_PAGE_CUSTOMFUNCTION_PRE SkipIfPassiveButUpdateShortcutIfUpdate
!define MUI_PAGE_CUSTOMFUNCTION_PRE SkipIfPassive
!insertmacro MUI_PAGE_FINISH

; Uninstaller Pages
Expand Down Expand Up @@ -577,6 +577,10 @@ Section Install
WriteRegDWORD SHCTX "${UNINSTKEY}" "NoRepair" "1"
WriteRegDWORD SHCTX "${UNINSTKEY}" "EstimatedSize" "${ESTIMATEDSIZE}"

; We used to use product name as MAINBINARYNAME
; migrate old shortcuts to target the new MAINBINARYNAME
Call MigrateProductNameExeShortcuts

; Create start menu shortcut
!insertmacro MUI_STARTMENU_WRITE_BEGIN Application
Call CreateStartMenuShortcut
Expand Down Expand Up @@ -727,22 +731,25 @@ Function SkipIfPassive
${IfThen} $PassiveMode = 1 ${|} Abort ${|}
FunctionEnd

Function SkipIfPassiveButUpdateShortcutIfUpdate
${If} $PassiveMode = 1
Call CreateDesktopShortcut
Abort
Function CreateStartMenuShortcut
; Skip creating shortcut if in update mode or no shortcut mode
${If} $UpdateMode = 1
Return
${EndIf}
${If} $NoShortcutMode = 1
Return
${EndIf}

CreateDirectory "$SMPROGRAMS\$AppStartMenuFolder"
CreateShortcut "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
!insertmacro SetLnkAppUserModelId "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk"
FunctionEnd

Function CreateDesktopShortcut
; Skip creating shortcut if in update mode
; and shortcuts doesn't exist, which means user deleted it
; so we respect that.
; Skip creating shortcut if in update mode or no shortcut mode
${If} $UpdateMode = 1
IfFileExists "$DESKTOP\${PRODUCTNAME}.lnk" +2 0
Return
Return
${EndIf}

${If} $NoShortcutMode = 1
Return
${EndIf}
Expand All @@ -751,19 +758,11 @@ Function CreateDesktopShortcut
!insertmacro SetLnkAppUserModelId "$DESKTOP\${PRODUCTNAME}.lnk"
FunctionEnd

Function CreateStartMenuShortcut
; Skip creating shortcut if in update mode.
; See `CreateDesktopShortcut` above.
${If} $UpdateMode = 1
IfFileExists "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk" +2 0
Return
Function MigrateProductNameExeShortcuts
${If} ${FileExists} "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk"
!insertmacro SetShortcutTarget "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
${EndIf}

${If} $NoShortcutMode = 1
Return
${If} ${FileExists} "$DESKTOP\${PRODUCTNAME}.lnk"
!insertmacro SetShortcutTarget "$DESKTOP\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
${EndIf}

CreateDirectory "$SMPROGRAMS\$AppStartMenuFolder"
CreateShortcut "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
!insertmacro SetLnkAppUserModelId "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk"
FunctionEnd
15 changes: 15 additions & 0 deletions tooling/bundler/src/bundle/windows/templates/utils.nsh
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,18 @@
${IUnknown::Release} $0 ""
${EndIf}
!macroend

; Set target path for a .lnk shortcut
!macro SetShortcutTarget shortcut target
!insertmacro ComHlpr_CreateInProcInstance ${CLSID_ShellLink} ${IID_IShellLink} r0 ""
${If} $0 P<> 0
${IUnknown::QueryInterface} $0 '("${IID_IPersistFile}",.r1)'
${If} $1 P<> 0
${IPersistFile::Load} $1 '("${shortcut}", ${STGM_READWRITE})'
${IShellLink::SetPath} $0 '(w "${target}")'
${IPersistFile::Save} $1 '("${shortcut}",1)'
${IUnknown::Release} $1 ""
${EndIf}
${IUnknown::Release} $0 ""
${EndIf}
!macroend