refactor(bundler/nsis): fix migration from MSI to NSIS (#10996)

* refactor(bundler/nsis): fix migration from MSI to NSIS

* read into $OldMainBinaryName
This commit is contained in:
Amr Bashir 2024-09-14 16:45:58 +03:00 committed by GitHub
parent def875193a
commit 4ba7b94b59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 112 additions and 74 deletions

View File

@ -64,6 +64,8 @@ ${StrLoc}
Var PassiveMode Var PassiveMode
Var UpdateMode Var UpdateMode
Var NoShortcutMode Var NoShortcutMode
Var WixMode
Var OldMainBinaryName
Name "${PRODUCTNAME}" Name "${PRODUCTNAME}"
BrandingText "${COPYRIGHT}" BrandingText "${COPYRIGHT}"
@ -175,7 +177,7 @@ Function PageReinstall
StrCpy $0 0 StrCpy $0 0
wix_loop: wix_loop:
EnumRegKey $1 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" $0 EnumRegKey $1 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" $0
StrCmp $1 "" wix_done ; Exit loop if there is no more keys to loop on StrCmp $1 "" wix_loop_done ; Exit loop if there is no more keys to loop on
IntOp $0 $0 + 1 IntOp $0 $0 + 1
ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$1" "DisplayName" ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$1" "DisplayName"
ReadRegStr $R1 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$1" "Publisher" ReadRegStr $R1 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$1" "Publisher"
@ -183,11 +185,11 @@ Function PageReinstall
ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$1" "UninstallString" ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$1" "UninstallString"
${StrCase} $R1 $R0 "L" ${StrCase} $R1 $R0 "L"
${StrLoc} $R0 $R1 "msiexec" ">" ${StrLoc} $R0 $R1 "msiexec" ">"
StrCmp $R0 0 0 wix_done StrCmp $R0 0 0 wix_loop_done
StrCpy $R7 "wix" StrCpy $WixMode 1
StrCpy $R6 "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$1" StrCpy $R6 "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$1"
Goto compare_version Goto compare_version
wix_done: wix_loop_done:
; Check if there is an existing installation, if not, abort the reinstall page ; Check if there is an existing installation, if not, abort the reinstall page
ReadRegStr $R0 SHCTX "${UNINSTKEY}" "" ReadRegStr $R0 SHCTX "${UNINSTKEY}" ""
@ -198,7 +200,7 @@ Function PageReinstall
; and modify the messages presented to the user accordingly ; and modify the messages presented to the user accordingly
compare_version: compare_version:
StrCpy $R4 "$(older)" StrCpy $R4 "$(older)"
${If} $R7 == "wix" ${If} $WixMode = 1
ReadRegStr $R0 HKLM "$R6" "DisplayVersion" ReadRegStr $R0 HKLM "$R6" "DisplayVersion"
${Else} ${Else}
ReadRegStr $R0 SHCTX "${UNINSTKEY}" "DisplayVersion" ReadRegStr $R0 SHCTX "${UNINSTKEY}" "DisplayVersion"
@ -213,14 +215,12 @@ Function PageReinstall
StrCpy $R2 "$(addOrReinstall)" StrCpy $R2 "$(addOrReinstall)"
StrCpy $R3 "$(uninstallApp)" StrCpy $R3 "$(uninstallApp)"
!insertmacro MUI_HEADER_TEXT "$(alreadyInstalled)" "$(chooseMaintenanceOption)" !insertmacro MUI_HEADER_TEXT "$(alreadyInstalled)" "$(chooseMaintenanceOption)"
StrCpy $R5 "2"
; Upgrading ; Upgrading
${ElseIf} $R0 = 1 ${ElseIf} $R0 = 1
StrCpy $R1 "$(olderOrUnknownVersionInstalled)" StrCpy $R1 "$(olderOrUnknownVersionInstalled)"
StrCpy $R2 "$(uninstallBeforeInstalling)" StrCpy $R2 "$(uninstallBeforeInstalling)"
StrCpy $R3 "$(dontUninstall)" StrCpy $R3 "$(dontUninstall)"
!insertmacro MUI_HEADER_TEXT "$(alreadyInstalled)" "$(choowHowToInstall)" !insertmacro MUI_HEADER_TEXT "$(alreadyInstalled)" "$(choowHowToInstall)"
StrCpy $R5 "1"
; Downgrading ; Downgrading
${ElseIf} $R0 = -1 ${ElseIf} $R0 = -1
StrCpy $R1 "$(newerVersionInstalled)" StrCpy $R1 "$(newerVersionInstalled)"
@ -231,7 +231,6 @@ Function PageReinstall
StrCpy $R3 "$(dontUninstallDowngrade)" StrCpy $R3 "$(dontUninstallDowngrade)"
!endif !endif
!insertmacro MUI_HEADER_TEXT "$(alreadyInstalled)" "$(choowHowToInstall)" !insertmacro MUI_HEADER_TEXT "$(alreadyInstalled)" "$(choowHowToInstall)"
StrCpy $R5 "1"
${Else} ${Else}
Abort Abort
${EndIf} ${EndIf}
@ -242,8 +241,9 @@ Function PageReinstall
; of this function because we need to populate some variables ; of this function because we need to populate some variables
; related to current installed version if detected and whether ; related to current installed version if detected and whether
; we are downgrading or not. ; we are downgrading or not.
Call SkipIfPassive ${If} $PassiveMode = 1
Call PageLeaveReinstall
${Else}
nsDialogs::Create 1018 nsDialogs::Create 1018
Pop $R4 Pop $R4
${IfThen} $(^RTL) = 1 ${|} nsDialogs::SetRTL $(^RTL) ${|} ${IfThen} $(^RTL) = 1 ${|} nsDialogs::SetRTL $(^RTL) ${|}
@ -274,6 +274,7 @@ Function PageReinstall
${NSD_SetFocus} $R2 ${NSD_SetFocus} $R2
nsDialogs::Show nsDialogs::Show
${EndIf}
FunctionEnd FunctionEnd
Function PageReinstallUpdateSelection Function PageReinstallUpdateSelection
${NSD_GetState} $R2 $R1 ${NSD_GetState} $R2 $R1
@ -286,30 +287,54 @@ FunctionEnd
Function PageLeaveReinstall Function PageLeaveReinstall
${NSD_GetState} $R2 $R1 ${NSD_GetState} $R2 $R1
; $R5 holds whether we are reinstalling the same version or not ; If migrating from Wix, always uninstall
; $R5 == "1" -> different versions ${If} $WixMode = 1
; $R5 == "2" -> same version Goto reinst_uninstall
; ${EndIf}
; $R1 holds the radio buttons state. its meaning is dependent on the context
StrCmp $R5 "1" 0 +2 ; Existing install is not the same version? ; In update mode, always proceeds without uninstalling
StrCmp $R1 "1" reinst_uninstall reinst_done ; $R1 == "1", then user chose to uninstall existing version, otherwise skip uninstalling ${If} $UpdateMode = 1
StrCmp $R1 "1" reinst_done ; Same version? skip uninstalling Goto reinst_done
${EndIf}
; $R0 holds whether same(0)/upgrading(1)/downgrading(-1) version
; $R1 holds the radio buttons state:
; 1 => first choice was selected
; 0 => second choice was selected
${If} $R0 = 0 ; Same version, proceed
${If} $R1 = 1 ; User chose to add/reinstall
Goto reinst_done
${Else} ; User chose to uninstall
Goto reinst_uninstall
${EndIf}
${ElseIf} $R0 = 1 ; Upgrading
${If} $R1 = 1 ; User chose to uninstall
Goto reinst_uninstall
${Else}
Goto reinst_done ; User chose NOT to uninstall
${EndIf}
${ElseIf} $R0 = -1 ; Downgrading
${If} $R1 = 1 ; User chose to uninstall
Goto reinst_uninstall
${Else}
Goto reinst_done ; User chose NOT to uninstall
${EndIf}
${EndIf}
reinst_uninstall: reinst_uninstall:
HideWindow HideWindow
ClearErrors ClearErrors
${If} $R7 == "wix" ${If} $WixMode = 1
ReadRegStr $R1 HKLM "$R6" "UninstallString" ReadRegStr $R1 HKLM "$R6" "UninstallString"
ExecWait '$R1' $0 ExecWait '$R1' $0
${Else} ${Else}
ReadRegStr $4 SHCTX "${MANUPRODUCTKEY}" "" ReadRegStr $4 SHCTX "${MANUPRODUCTKEY}" ""
ReadRegStr $R1 SHCTX "${UNINSTKEY}" "UninstallString" ReadRegStr $R1 SHCTX "${UNINSTKEY}" "UninstallString"
${If} $UpdateMode = 1 ${IfThen} $UpdateMode = 1 ${|} StrCpy $R1 "$R1 /UPDATE" ${|} ; append /UPDATE
ExecWait '$R1 /UPDATE /P _?=$4' $0 ${IfThen} $PassiveMode = 1 ${|} StrCpy $R1 "$R1 /P" ${|} ; append /P
${Else} StrCpy $R1 "$R1 _?=$4" ; append uninstall directory
ExecWait '$R1 /P _?=$4' $0 ExecWait '$R1' $0
${EndIf}
${EndIf} ${EndIf}
BringToFront BringToFront
@ -318,18 +343,20 @@ Function PageLeaveReinstall
${If} $0 <> 0 ${If} $0 <> 0
${OrIf} ${FileExists} "$INSTDIR\${MAINBINARYNAME}.exe" ${OrIf} ${FileExists} "$INSTDIR\${MAINBINARYNAME}.exe"
${If} $0 = 1 ; User aborted uninstaller? ; User cancelled wix uninstaller? return to select un/reinstall page
StrCmp $R5 "2" 0 +2 ; Is the existing install the same version? ${If} $WixMode = 1
Quit ; ...yes, already installed, we are done ${AndIf} $0 = 1602
Abort Abort
${EndIf} ${EndIf}
; User cancelled NSIS uninstaller? return to select un/reinstall page
${If} $0 = 1
Abort
${EndIf}
; Other erros? show generic error message and return to select un/reinstall page
MessageBox MB_ICONEXCLAMATION "$(unableToUninstall)" MessageBox MB_ICONEXCLAMATION "$(unableToUninstall)"
Abort Abort
${Else}
StrCpy $0 $R1 1
${IfThen} $0 == '"' ${|} StrCpy $R1 $R1 -1 1 ${|} ; Strip quotes from UninstallString
Delete $R1
RMDir $INSTDIR
${EndIf} ${EndIf}
reinst_done: reinst_done:
FunctionEnd FunctionEnd
@ -409,6 +436,7 @@ FunctionEnd
Function un.ConfirmLeave Function un.ConfirmLeave
SendMessage $DeleteAppDataCheckbox ${BM_GETCHECK} 0 0 $DeleteAppDataCheckboxState SendMessage $DeleteAppDataCheckbox ${BM_GETCHECK} 0 0 $DeleteAppDataCheckboxState
FunctionEnd FunctionEnd
!define MUI_PAGE_CUSTOMFUNCTION_PRE un.SkipIfPassive
!insertmacro MUI_UNPAGE_CONFIRM !insertmacro MUI_UNPAGE_CONFIRM
; 2. Uninstalling Page ; 2. Uninstalling Page
@ -637,10 +665,10 @@ Section Install
!endif !endif
; Remove old main binary if it doesn't match new main binary name ; Remove old main binary if it doesn't match new main binary name
ReadRegStr $0 SHCTX "${UNINSTKEY}" "MainBinaryName" ReadRegStr $OldMainBinaryName SHCTX "${UNINSTKEY}" "MainBinaryName"
${If} $0 != "" ${If} $OldMainBinaryName != ""
${AndIf} $0 != "${MAINBINARYNAME}.exe" ${AndIf} $OldMainBinaryName != "${MAINBINARYNAME}.exe"
Delete "$INSTDIR\$0" Delete "$INSTDIR\$OldMainBinaryName"
${EndIf} ${EndIf}
; Save current MAINBINARYNAME for future updates ; Save current MAINBINARYNAME for future updates
@ -821,8 +849,9 @@ Section Uninstall
!insertmacro NSIS_HOOK_POSTUNINSTALL !insertmacro NSIS_HOOK_POSTUNINSTALL
!endif !endif
; Auto close if passive mode ; Auto close if passive mode or updating
${If} $PassiveMode = 1 ${If} $PassiveMode = 1
${OrIf} $UpdateMode = 1
SetAutoClose true SetAutoClose true
${EndIf} ${EndIf}
SectionEnd SectionEnd
@ -840,20 +869,23 @@ FunctionEnd
Function SkipIfPassive Function SkipIfPassive
${IfThen} $PassiveMode = 1 ${|} Abort ${|} ${IfThen} $PassiveMode = 1 ${|} Abort ${|}
FunctionEnd FunctionEnd
Function un.SkipIfPassive
${IfThen} $PassiveMode = 1 ${|} Abort ${|}
FunctionEnd
Function CreateOrUpdateStartMenuShortcut Function CreateOrUpdateStartMenuShortcut
; We used to use product name as MAINBINARYNAME ; We used to use product name as MAINBINARYNAME
; migrate old shortcuts to target the new MAINBINARYNAME ; migrate old shortcuts to target the new MAINBINARYNAME
StrCpy $R0 0 StrCpy $R0 0
!insertmacro IsShortcutTarget "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk" "$INSTDIR\${PRODUCTNAME}.exe" !insertmacro IsShortcutTarget "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk" "$INSTDIR\$OldMainBinaryName"
Pop $0 Pop $0
${If} $0 = 1 ${If} $0 = 1
!insertmacro SetShortcutTarget "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe" !insertmacro SetShortcutTarget "$SMPROGRAMS\$AppStartMenuFolder\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
StrCpy $R0 1 StrCpy $R0 1
${EndIf} ${EndIf}
!insertmacro IsShortcutTarget "$SMPROGRAMS\${PRODUCTNAME}.lnk" "$INSTDIR\${PRODUCTNAME}.exe" !insertmacro IsShortcutTarget "$SMPROGRAMS\${PRODUCTNAME}.lnk" "$INSTDIR\$OldMainBinaryName"
Pop $0 Pop $0
${If} $0 = 1 ${If} $0 = 1
!insertmacro SetShortcutTarget "$SMPROGRAMS\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe" !insertmacro SetShortcutTarget "$SMPROGRAMS\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
@ -865,10 +897,13 @@ Function CreateOrUpdateStartMenuShortcut
${EndIf} ${EndIf}
; Skip creating shortcut if in update mode or no shortcut mode ; Skip creating shortcut if in update mode or no shortcut mode
; but always create if migrating from wix
${If} $WixMode = 0
${If} $UpdateMode = 1 ${If} $UpdateMode = 1
${OrIf} $NoShortcutMode = 1 ${OrIf} $NoShortcutMode = 1
Return Return
${EndIf} ${EndIf}
${EndIf}
!if "${STARTMENUFOLDER}" != "" !if "${STARTMENUFOLDER}" != ""
CreateDirectory "$SMPROGRAMS\$AppStartMenuFolder" CreateDirectory "$SMPROGRAMS\$AppStartMenuFolder"
@ -883,7 +918,7 @@ FunctionEnd
Function CreateOrUpdateDesktopShortcut Function CreateOrUpdateDesktopShortcut
; We used to use product name as MAINBINARYNAME ; We used to use product name as MAINBINARYNAME
; migrate old shortcuts to target the new MAINBINARYNAME ; migrate old shortcuts to target the new MAINBINARYNAME
!insertmacro IsShortcutTarget "$DESKTOP\${PRODUCTNAME}.lnk" "$INSTDIR\${PRODUCTNAME}.exe" !insertmacro IsShortcutTarget "$DESKTOP\${PRODUCTNAME}.lnk" "$INSTDIR\$OldMainBinaryName"
Pop $0 Pop $0
${If} $0 = 1 ${If} $0 = 1
!insertmacro SetShortcutTarget "$DESKTOP\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe" !insertmacro SetShortcutTarget "$DESKTOP\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
@ -891,10 +926,13 @@ Function CreateOrUpdateDesktopShortcut
${EndIf} ${EndIf}
; Skip creating shortcut if in update mode or no shortcut mode ; Skip creating shortcut if in update mode or no shortcut mode
; but always create if migrating from wix
${If} $WixMode = 0
${If} $UpdateMode = 1 ${If} $UpdateMode = 1
${OrIf} $NoShortcutMode = 1 ${OrIf} $NoShortcutMode = 1
Return Return
${EndIf} ${EndIf}
${EndIf}
CreateShortcut "$DESKTOP\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe" CreateShortcut "$DESKTOP\${PRODUCTNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
!insertmacro SetLnkAppUserModelId "$DESKTOP\${PRODUCTNAME}.lnk" !insertmacro SetLnkAppUserModelId "$DESKTOP\${PRODUCTNAME}.lnk"