Skip to content

Commit ea78bf5

Browse files
fix(nsis): migrate old shortcuts by setting the path only instead of re-creating a new one (#9915)
1 parent 019a74e commit ea78bf5

File tree

3 files changed

+74
-49
lines changed

3 files changed

+74
-49
lines changed

.changes/nsis-migrate-shortcut.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-bundler": "patch:enhance"
3+
---
4+
5+
For NSIS installer, migrate old shortcuts by setting the path only instead of re-creating a new one

tooling/bundler/src/bundle/windows/templates/installer.nsi

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,10 @@ Var AppStartMenuFolder
344344
; Use show readme button in the finish page as a button create a desktop shortcut
345345
!define MUI_FINISHPAGE_SHOWREADME
346346
!define MUI_FINISHPAGE_SHOWREADME_TEXT "$(createDesktop)"
347-
!define MUI_FINISHPAGE_SHOWREADME_FUNCTION CreateDesktopShortcut
347+
!define MUI_FINISHPAGE_SHOWREADME_FUNCTION CreateOrUpdateDesktopShortcut
348348
; Show run app after installation.
349349
!define MUI_FINISHPAGE_RUN "$INSTDIR\${MAINBINARYNAME}.exe"
350-
!define MUI_PAGE_CUSTOMFUNCTION_PRE SkipIfPassiveButUpdateShortcutIfUpdate
350+
!define MUI_PAGE_CUSTOMFUNCTION_PRE SkipIfPassive
351351
!insertmacro MUI_PAGE_FINISH
352352

353353
; Uninstaller Pages
@@ -387,16 +387,19 @@ FunctionEnd
387387

388388
Function .onInit
389389
${GetOptions} $CMDLINE "/P" $PassiveMode
390-
IfErrors +2 0
390+
${IfNot} ${Errors}
391391
StrCpy $PassiveMode 1
392+
${EndIf}
392393

393394
${GetOptions} $CMDLINE "/NS" $NoShortcutMode
394-
IfErrors +2 0
395+
${IfNot} ${Errors}
395396
StrCpy $NoShortcutMode 1
397+
${EndIf}
396398

397399
${GetOptions} $CMDLINE "/UPDATE" $UpdateMode
398-
IfErrors +2 0
400+
${IfNot} ${Errors}
399401
StrCpy $UpdateMode 1
402+
${EndIf}
400403

401404
!if "${DISPLAYLANGUAGESELECTOR}" == "true"
402405
!insertmacro MUI_LANGDLL_DISPLAY
@@ -435,7 +438,7 @@ FunctionEnd
435438
Section EarlyChecks
436439
; Abort silent installer if downgrades is disabled
437440
!if "${ALLOWDOWNGRADES}" == "false"
438-
IfSilent 0 silent_downgrades_done
441+
${If} ${Silent}
439442
; If downgrading
440443
${If} $R0 = -1
441444
System::Call 'kernel32::AttachConsole(i -1)i.r0'
@@ -446,7 +449,7 @@ Section EarlyChecks
446449
${EndIf}
447450
Abort
448451
${EndIf}
449-
silent_downgrades_done:
452+
${EndIf}
450453
!endif
451454

452455
SectionEnd
@@ -579,37 +582,37 @@ Section Install
579582

580583
; Create start menu shortcut
581584
!insertmacro MUI_STARTMENU_WRITE_BEGIN Application
582-
Call CreateStartMenuShortcut
585+
Call CreateOrUpdateStartMenuShortcut
583586
!insertmacro MUI_STARTMENU_WRITE_END
584587

585588
; Create desktop shortcut for silent and passive installers
586589
; because finish page will be skipped
587-
IfSilent create_shortcut 0
588-
StrCmp $PassiveMode "1" create_shortcut shortcut_done
589-
create_shortcut:
590-
Call CreateDesktopShortcut
591-
shortcut_done:
590+
${If} $PassiveMode = 1
591+
${OrIf} ${Silent}
592+
Call CreateOrUpdateDesktopShortcut
593+
${EndIf}
592594

593595
!ifdef NSIS_HOOK_POSTINSTALL
594596
!insertmacro "${NSIS_HOOK_POSTINSTALL}"
595597
!endif
596598

597599
; Auto close this page for passive mode
598-
${IfThen} $PassiveMode = 1 ${|} SetAutoClose true ${|}
600+
${If} $PassiveMode = 1
601+
SetAutoClose true
602+
${EndIf}
599603
SectionEnd
600604

601605
Function .onInstSuccess
602606
; Check for `/R` flag only in silent and passive installers because
603607
; GUI installer has a toggle for the user to (re)start the app
604-
IfSilent check_r_flag 0
605-
${IfThen} $PassiveMode = 1 ${|} Goto check_r_flag ${|}
606-
Goto run_done
607-
check_r_flag:
608+
${If} $PassiveMode = 1
609+
${OrIf} ${Silent}
608610
${GetOptions} $CMDLINE "/R" $R0
609-
IfErrors run_done 0
611+
${IfNot} ${Errors}
610612
${GetOptions} $CMDLINE "/ARGS" $R0
611613
Exec '"$INSTDIR\${MAINBINARYNAME}.exe" $R0'
612-
run_done:
614+
${EndIf}
615+
${EndIf}
613616
FunctionEnd
614617

615618
Function un.onInit
@@ -621,9 +624,15 @@ Function un.onInit
621624

622625
!insertmacro MUI_UNGETLANGUAGE
623626

627+
${GetOptions} $CMDLINE "/P" $PassiveMode
628+
${IfNot} ${Errors}
629+
StrCpy $PassiveMode 1
630+
${EndIf}
631+
624632
${GetOptions} $CMDLINE "/UPDATE" $UpdateMode
625-
IfErrors +2 0
633+
${IfNot} ${Errors}
626634
StrCpy $UpdateMode 1
635+
${EndIf}
627636
FunctionEnd
628637

629638
Section Uninstall
@@ -712,9 +721,9 @@ Section Uninstall
712721
!endif
713722

714723
; Auto close if passive mode
715-
${GetOptions} $CMDLINE "/P" $R0
716-
IfErrors +2 0
724+
${If} $PassiveMode = 1
717725
SetAutoClose true
726+
${EndIf}
718727
SectionEnd
719728

720729
Function RestorePreviousInstallLocation
@@ -727,43 +736,39 @@ Function SkipIfPassive
727736
${IfThen} $PassiveMode = 1 ${|} Abort ${|}
728737
FunctionEnd
729738

730-
Function SkipIfPassiveButUpdateShortcutIfUpdate
731-
${If} $PassiveMode = 1
732-
Call CreateDesktopShortcut
733-
Abort
739+
Function CreateOrUpdateStartMenuShortcut
740+
; We used to use product name as MAINBINARYNAME
741+
; migrate old shortcuts to target the new MAINBINARYNAME
742+
${If} ${FileExists} "$DESKTOP\${PRODUCTNAME}.lnk"
743+
!insertmacro SetShortcutTarget "$DESKTOP\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
744+
Return
734745
${EndIf}
735-
FunctionEnd
736746

737-
Function CreateDesktopShortcut
738-
; Skip creating shortcut if in update mode
739-
; and shortcuts doesn't exist, which means user deleted it
740-
; so we respect that.
747+
; Skip creating shortcut if in update mode or no shortcut mode
741748
${If} $UpdateMode = 1
742-
IfFileExists "$DESKTOP\${PRODUCTNAME}.lnk" +2 0
743-
Return
744-
${EndIf}
745-
746-
${If} $NoShortcutMode = 1
749+
${OrIf} $NoShortcutMode = 1
747750
Return
748751
${EndIf}
749752

750-
CreateShortcut "$DESKTOP\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
751-
!insertmacro SetLnkAppUserModelId "$DESKTOP\${PRODUCTNAME}.lnk"
753+
CreateDirectory "$SMPROGRAMS\$AppStartMenuFolder"
754+
CreateShortcut "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
755+
!insertmacro SetLnkAppUserModelId "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk"
752756
FunctionEnd
753757

754-
Function CreateStartMenuShortcut
755-
; Skip creating shortcut if in update mode.
756-
; See `CreateDesktopShortcut` above.
757-
${If} $UpdateMode = 1
758-
IfFileExists "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk" +2 0
759-
Return
758+
Function CreateOrUpdateDesktopShortcut
759+
; We used to use product name as MAINBINARYNAME
760+
; migrate old shortcuts to target the new MAINBINARYNAME
761+
${If} ${FileExists} "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk"
762+
!insertmacro SetShortcutTarget "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
763+
Return
760764
${EndIf}
761765

762-
${If} $NoShortcutMode = 1
766+
; Skip creating shortcut if in update mode or no shortcut mode
767+
${If} $UpdateMode = 1
768+
${OrIf} $NoShortcutMode = 1
763769
Return
764770
${EndIf}
765771

766-
CreateDirectory "$SMPROGRAMS\$AppStartMenuFolder"
767-
CreateShortcut "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
768-
!insertmacro SetLnkAppUserModelId "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk"
772+
CreateShortcut "$DESKTOP\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
773+
!insertmacro SetLnkAppUserModelId "$DESKTOP\${PRODUCTNAME}.lnk"
769774
FunctionEnd

tooling/bundler/src/bundle/windows/templates/utils.nsh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,18 @@
116116
${IUnknown::Release} $0 ""
117117
${EndIf}
118118
!macroend
119+
120+
; Set target path for a .lnk shortcut
121+
!macro SetShortcutTarget shortcut target
122+
!insertmacro ComHlpr_CreateInProcInstance ${CLSID_ShellLink} ${IID_IShellLink} r0 ""
123+
${If} $0 P<> 0
124+
${IUnknown::QueryInterface} $0 '("${IID_IPersistFile}",.r1)'
125+
${If} $1 P<> 0
126+
${IPersistFile::Load} $1 '("${shortcut}", ${STGM_READWRITE})'
127+
${IShellLink::SetPath} $0 '(w "${target}")'
128+
${IPersistFile::Save} $1 '("${shortcut}",1)'
129+
${IUnknown::Release} $1 ""
130+
${EndIf}
131+
${IUnknown::Release} $0 ""
132+
${EndIf}
133+
!macroend

0 commit comments

Comments
 (0)