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

This commit is contained in:
Tony 2024-06-04 09:27:17 +08:00 committed by GitHub
parent 019a74e970
commit ea78bf5558
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 77 additions and 52 deletions

View File

@ -0,0 +1,5 @@
---
"tauri-bundler": "patch:enhance"
---
For NSIS installer, migrate old shortcuts by setting the path only instead of re-creating a new one

View File

@ -344,10 +344,10 @@ Var AppStartMenuFolder
; Use show readme button in the finish page as a button create a desktop shortcut ; Use show readme button in the finish page as a button create a desktop shortcut
!define MUI_FINISHPAGE_SHOWREADME !define MUI_FINISHPAGE_SHOWREADME
!define MUI_FINISHPAGE_SHOWREADME_TEXT "$(createDesktop)" !define MUI_FINISHPAGE_SHOWREADME_TEXT "$(createDesktop)"
!define MUI_FINISHPAGE_SHOWREADME_FUNCTION CreateDesktopShortcut !define MUI_FINISHPAGE_SHOWREADME_FUNCTION CreateOrUpdateDesktopShortcut
; Show run app after installation. ; Show run app after installation.
!define MUI_FINISHPAGE_RUN "$INSTDIR\${MAINBINARYNAME}.exe" !define MUI_FINISHPAGE_RUN "$INSTDIR\${MAINBINARYNAME}.exe"
!define MUI_PAGE_CUSTOMFUNCTION_PRE SkipIfPassiveButUpdateShortcutIfUpdate !define MUI_PAGE_CUSTOMFUNCTION_PRE SkipIfPassive
!insertmacro MUI_PAGE_FINISH !insertmacro MUI_PAGE_FINISH
; Uninstaller Pages ; Uninstaller Pages
@ -387,16 +387,19 @@ FunctionEnd
Function .onInit Function .onInit
${GetOptions} $CMDLINE "/P" $PassiveMode ${GetOptions} $CMDLINE "/P" $PassiveMode
IfErrors +2 0 ${IfNot} ${Errors}
StrCpy $PassiveMode 1 StrCpy $PassiveMode 1
${EndIf}
${GetOptions} $CMDLINE "/NS" $NoShortcutMode ${GetOptions} $CMDLINE "/NS" $NoShortcutMode
IfErrors +2 0 ${IfNot} ${Errors}
StrCpy $NoShortcutMode 1 StrCpy $NoShortcutMode 1
${EndIf}
${GetOptions} $CMDLINE "/UPDATE" $UpdateMode ${GetOptions} $CMDLINE "/UPDATE" $UpdateMode
IfErrors +2 0 ${IfNot} ${Errors}
StrCpy $UpdateMode 1 StrCpy $UpdateMode 1
${EndIf}
!if "${DISPLAYLANGUAGESELECTOR}" == "true" !if "${DISPLAYLANGUAGESELECTOR}" == "true"
!insertmacro MUI_LANGDLL_DISPLAY !insertmacro MUI_LANGDLL_DISPLAY
@ -435,7 +438,7 @@ FunctionEnd
Section EarlyChecks Section EarlyChecks
; Abort silent installer if downgrades is disabled ; Abort silent installer if downgrades is disabled
!if "${ALLOWDOWNGRADES}" == "false" !if "${ALLOWDOWNGRADES}" == "false"
IfSilent 0 silent_downgrades_done ${If} ${Silent}
; If downgrading ; If downgrading
${If} $R0 = -1 ${If} $R0 = -1
System::Call 'kernel32::AttachConsole(i -1)i.r0' System::Call 'kernel32::AttachConsole(i -1)i.r0'
@ -446,7 +449,7 @@ Section EarlyChecks
${EndIf} ${EndIf}
Abort Abort
${EndIf} ${EndIf}
silent_downgrades_done: ${EndIf}
!endif !endif
SectionEnd SectionEnd
@ -579,37 +582,37 @@ Section Install
; Create start menu shortcut ; Create start menu shortcut
!insertmacro MUI_STARTMENU_WRITE_BEGIN Application !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
Call CreateStartMenuShortcut Call CreateOrUpdateStartMenuShortcut
!insertmacro MUI_STARTMENU_WRITE_END !insertmacro MUI_STARTMENU_WRITE_END
; Create desktop shortcut for silent and passive installers ; Create desktop shortcut for silent and passive installers
; because finish page will be skipped ; because finish page will be skipped
IfSilent create_shortcut 0 ${If} $PassiveMode = 1
StrCmp $PassiveMode "1" create_shortcut shortcut_done ${OrIf} ${Silent}
create_shortcut: Call CreateOrUpdateDesktopShortcut
Call CreateDesktopShortcut ${EndIf}
shortcut_done:
!ifdef NSIS_HOOK_POSTINSTALL !ifdef NSIS_HOOK_POSTINSTALL
!insertmacro "${NSIS_HOOK_POSTINSTALL}" !insertmacro "${NSIS_HOOK_POSTINSTALL}"
!endif !endif
; Auto close this page for passive mode ; Auto close this page for passive mode
${IfThen} $PassiveMode = 1 ${|} SetAutoClose true ${|} ${If} $PassiveMode = 1
SetAutoClose true
${EndIf}
SectionEnd SectionEnd
Function .onInstSuccess Function .onInstSuccess
; Check for `/R` flag only in silent and passive installers because ; Check for `/R` flag only in silent and passive installers because
; GUI installer has a toggle for the user to (re)start the app ; GUI installer has a toggle for the user to (re)start the app
IfSilent check_r_flag 0 ${If} $PassiveMode = 1
${IfThen} $PassiveMode = 1 ${|} Goto check_r_flag ${|} ${OrIf} ${Silent}
Goto run_done
check_r_flag:
${GetOptions} $CMDLINE "/R" $R0 ${GetOptions} $CMDLINE "/R" $R0
IfErrors run_done 0 ${IfNot} ${Errors}
${GetOptions} $CMDLINE "/ARGS" $R0 ${GetOptions} $CMDLINE "/ARGS" $R0
Exec '"$INSTDIR\${MAINBINARYNAME}.exe" $R0' Exec '"$INSTDIR\${MAINBINARYNAME}.exe" $R0'
run_done: ${EndIf}
${EndIf}
FunctionEnd FunctionEnd
Function un.onInit Function un.onInit
@ -621,9 +624,15 @@ Function un.onInit
!insertmacro MUI_UNGETLANGUAGE !insertmacro MUI_UNGETLANGUAGE
${GetOptions} $CMDLINE "/P" $PassiveMode
${IfNot} ${Errors}
StrCpy $PassiveMode 1
${EndIf}
${GetOptions} $CMDLINE "/UPDATE" $UpdateMode ${GetOptions} $CMDLINE "/UPDATE" $UpdateMode
IfErrors +2 0 ${IfNot} ${Errors}
StrCpy $UpdateMode 1 StrCpy $UpdateMode 1
${EndIf}
FunctionEnd FunctionEnd
Section Uninstall Section Uninstall
@ -712,9 +721,9 @@ Section Uninstall
!endif !endif
; Auto close if passive mode ; Auto close if passive mode
${GetOptions} $CMDLINE "/P" $R0 ${If} $PassiveMode = 1
IfErrors +2 0
SetAutoClose true SetAutoClose true
${EndIf}
SectionEnd SectionEnd
Function RestorePreviousInstallLocation Function RestorePreviousInstallLocation
@ -727,39 +736,17 @@ Function SkipIfPassive
${IfThen} $PassiveMode = 1 ${|} Abort ${|} ${IfThen} $PassiveMode = 1 ${|} Abort ${|}
FunctionEnd FunctionEnd
Function SkipIfPassiveButUpdateShortcutIfUpdate Function CreateOrUpdateStartMenuShortcut
${If} $PassiveMode = 1 ; We used to use product name as MAINBINARYNAME
Call CreateDesktopShortcut ; migrate old shortcuts to target the new MAINBINARYNAME
Abort ${If} ${FileExists} "$DESKTOP\${PRODUCTNAME}.lnk"
${EndIf} !insertmacro SetShortcutTarget "$DESKTOP\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
FunctionEnd
Function CreateDesktopShortcut
; Skip creating shortcut if in update mode
; and shortcuts doesn't exist, which means user deleted it
; so we respect that.
${If} $UpdateMode = 1
IfFileExists "$DESKTOP\${PRODUCTNAME}.lnk" +2 0
Return
${EndIf}
${If} $NoShortcutMode = 1
Return Return
${EndIf} ${EndIf}
CreateShortcut "$DESKTOP\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe" ; Skip creating shortcut if in update mode or no shortcut mode
!insertmacro SetLnkAppUserModelId "$DESKTOP\${PRODUCTNAME}.lnk"
FunctionEnd
Function CreateStartMenuShortcut
; Skip creating shortcut if in update mode.
; See `CreateDesktopShortcut` above.
${If} $UpdateMode = 1 ${If} $UpdateMode = 1
IfFileExists "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk" +2 0 ${OrIf} $NoShortcutMode = 1
Return
${EndIf}
${If} $NoShortcutMode = 1
Return Return
${EndIf} ${EndIf}
@ -767,3 +754,21 @@ Function CreateStartMenuShortcut
CreateShortcut "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe" CreateShortcut "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
!insertmacro SetLnkAppUserModelId "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk" !insertmacro SetLnkAppUserModelId "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk"
FunctionEnd FunctionEnd
Function CreateOrUpdateDesktopShortcut
; We used to use product name as MAINBINARYNAME
; migrate old shortcuts to target the new MAINBINARYNAME
${If} ${FileExists} "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk"
!insertmacro SetShortcutTarget "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
Return
${EndIf}
; Skip creating shortcut if in update mode or no shortcut mode
${If} $UpdateMode = 1
${OrIf} $NoShortcutMode = 1
Return
${EndIf}
CreateShortcut "$DESKTOP\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
!insertmacro SetLnkAppUserModelId "$DESKTOP\${PRODUCTNAME}.lnk"
FunctionEnd

View File

@ -116,3 +116,18 @@
${IUnknown::Release} $0 "" ${IUnknown::Release} $0 ""
${EndIf} ${EndIf}
!macroend !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