Skip to content

Commit

Permalink
feat(nsis): per-machine installer automatically removes old version #621
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Aug 17, 2016
1 parent d40e0b5 commit 2c3d8c2
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 170 deletions.
6 changes: 2 additions & 4 deletions src/appInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class AppInfo {
readonly buildNumber: string
readonly buildVersion: string

readonly productName: string
readonly productFilename: string

constructor(public metadata: AppMetadata, private devMetadata: DevMetadata, buildVersion?: string | null) {
Expand All @@ -32,6 +33,7 @@ export class AppInfo {
this.buildVersion = buildVersion!
}

this.productName = getProductName(this.metadata, this.devMetadata)
this.productFilename = sanitizeFileName(this.productName)
}

Expand Down Expand Up @@ -68,10 +70,6 @@ export class AppInfo {
return metadata.category || old
}

get productName(): string {
return getProductName(this.metadata, this.devMetadata)
}

get copyright(): string {
const metadata = this.devMetadata.build
const old = (<any>metadata)["app-copyright"]
Expand Down
2 changes: 1 addition & 1 deletion src/winPackager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class WinPackager extends PlatformPackager<WinBuildOptions> {
"--set-version-string", "CompanyName", appInfo.companyName,
"--set-version-string", "FileDescription", appInfo.description,
"--set-version-string", "ProductName", appInfo.productName,
"--set-version-string", "InternalName", appInfo.productName,
"--set-version-string", "InternalName", path.basename(appInfo.productFilename, ".exe"),
"--set-version-string", "LegalCopyright", appInfo.copyright,
"--set-version-string", "OriginalFilename", "",
"--set-file-version", appInfo.buildVersion,
Expand Down
12 changes: 6 additions & 6 deletions templates/nsis/boringInstaller.nsh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
!insertmacro MUI_UNPAGE_INSTFILES
!endif

!macro initMultiUser UNINSTALLER_FUNCPREFIX
!macro initMultiUser
!insertmacro UAC_PageElevation_OnInit

${If} ${UAC_IsInnerInstance}
Expand Down Expand Up @@ -60,17 +60,17 @@
${endif}

${if} $hasPerUserInstallation == "1"
${andif} $hasPerMachineInstallation == "0"
Call ${UNINSTALLER_FUNCPREFIX}installMode.CurrentUser
${andif} $hasPerMachineInstallation == "0"
!insertmacro setInstallModePerUser
${elseif} $hasPerUserInstallation == "0"
${andif} $hasPerMachineInstallation == "1"
Call ${UNINSTALLER_FUNCPREFIX}installMode.AllUsers
!insertmacro setInstallModePerAllUsers
${else}
# if there is no installation, or there is both per-user and per-machine
!ifdef INSTALL_MODE_PER_ALL_USERS
Call ${UNINSTALLER_FUNCPREFIX}installMode.AllUsers
!insertmacro setInstallModePerAllUsers
!else
Call ${UNINSTALLER_FUNCPREFIX}installMode.CurrentUser
!insertmacro setInstallModePerUser
!endif
${endif}
!macroend
Expand Down
66 changes: 66 additions & 0 deletions templates/nsis/install.nsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
${IfNot} ${Silent}
SetDetailsPrint none

!ifdef ONE_CLICK
!ifdef HEADER_ICO
SpiderBanner::Show /MODERN /ICON "$PLUGINSDIR\installerHeaderico.ico"
!else
SpiderBanner::Show /MODERN
!endif
!endif
${endif}

!insertmacro CHECK_APP_RUNNING "install"

${if} $installMode == "all"
ReadRegStr $R0 HKEY_LOCAL_MACHINE "${UNINSTALL_REGISTRY_KEY}" UninstallString
${if} $R0 != ""
ExecWait "$R0 /S"
${endif}
${endif}

RMDir /r $INSTDIR
SetOutPath $INSTDIR

!ifdef APP_64
${If} ${RunningX64}
Nsis7z::Extract "$PLUGINSDIR\app-64.7z"
${Else}
Nsis7z::Extract "$PLUGINSDIR\app-32.7z"
${endif}
!else
Nsis7z::Extract "$PLUGINSDIR\app-32.7z"
!endif

File "/oname=${UNINSTALL_FILENAME}" "${UNINSTALLER_OUT_FILE}"

!insertmacro registryAddInstallInfo

StrCpy $startMenuLink "$SMPROGRAMS\${PRODUCT_FILENAME}.lnk"
StrCpy $desktopLink "$DESKTOP\${PRODUCT_FILENAME}.lnk"

# create shortcuts in the start menu and on the desktop
# shortcut for uninstall is bad cause user can choose this by mistake during search, so, we don't add it
CreateShortCut "$startMenuLink" "$INSTDIR\${APP_EXECUTABLE_FILENAME}" "" "$INSTDIR\${APP_EXECUTABLE_FILENAME}" 0 "" "" "${APP_DESCRIPTION}"
CreateShortCut "$desktopLink" "$INSTDIR\${APP_EXECUTABLE_FILENAME}" "" "$INSTDIR\${APP_EXECUTABLE_FILENAME}" 0 "" "" "${APP_DESCRIPTION}"

WinShell::SetLnkAUMI "$startMenuLink" "${APP_ID}"
WinShell::SetLnkAUMI "$desktopLink" "${APP_ID}"

!ifmacrodef registerFileAssociations
!insertmacro registerFileAssociations
!endif

!ifmacrodef customInstall
!insertmacro customInstall
!endif

${IfNot} ${Silent}
!ifdef ONE_CLICK
# otherwise app window will be in backround
HideWindow
!ifdef RUN_AFTER_FINISH
Call StartApp
!endif
!endif
${EndIf}
72 changes: 7 additions & 65 deletions templates/nsis/installer.nsi
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Function .onInit
Quit
!else
!insertmacro check64BitAndSetRegView
!insertmacro initMultiUser ""
!insertmacro initMultiUser

!ifdef ONE_CLICK
!insertmacro ALLOW_ONLY_ONE_INSTALLER_INSTACE
Expand Down Expand Up @@ -59,70 +59,12 @@ Function .onInit
!endif
FunctionEnd

!ifndef BUILD_UNINSTALLER
Section "install"
${IfNot} ${Silent}
SetDetailsPrint none

!ifdef ONE_CLICK
!ifdef HEADER_ICO
SpiderBanner::Show /MODERN /ICON "$PLUGINSDIR\installerHeaderico.ico"
!else
SpiderBanner::Show /MODERN
!endif
!endif
${EndIf}

!insertmacro CHECK_APP_RUNNING "install"

RMDir /r $INSTDIR
SetOutPath $INSTDIR

!ifdef APP_64
${If} ${RunningX64}
Nsis7z::Extract "$PLUGINSDIR\app-64.7z"
${Else}
Nsis7z::Extract "$PLUGINSDIR\app-32.7z"
${EndIf}
!else
Nsis7z::Extract "$PLUGINSDIR\app-32.7z"
!endif

File "/oname=${UNINSTALL_FILENAME}" "${UNINSTALLER_OUT_FILE}"

!insertmacro registryAddInstallInfo

StrCpy $startMenuLink "$SMPROGRAMS\${PRODUCT_FILENAME}.lnk"
StrCpy $desktopLink "$DESKTOP\${PRODUCT_FILENAME}.lnk"

# create shortcuts in the start menu and on the desktop
# shortcut for uninstall is bad cause user can choose this by mistake during search, so, we don't add it
CreateShortCut "$startMenuLink" "$INSTDIR\${APP_EXECUTABLE_FILENAME}" "" "$INSTDIR\${APP_EXECUTABLE_FILENAME}" 0 "" "" "${APP_DESCRIPTION}"
CreateShortCut "$desktopLink" "$INSTDIR\${APP_EXECUTABLE_FILENAME}" "" "$INSTDIR\${APP_EXECUTABLE_FILENAME}" 0 "" "" "${APP_DESCRIPTION}"

WinShell::SetLnkAUMI "$startMenuLink" "${APP_ID}"
WinShell::SetLnkAUMI "$desktopLink" "${APP_ID}"

!ifmacrodef registerFileAssociations
!insertmacro registerFileAssociations
!endif

!ifmacrodef customInstall
!insertmacro customInstall
!endif
Section "install"
!ifndef BUILD_UNINSTALLER
!include "install.nsh"
!endif
SectionEnd

${IfNot} ${Silent}
!ifdef ONE_CLICK
# otherwise app window will be in backround
HideWindow
!ifdef RUN_AFTER_FINISH
Call StartApp
!endif
!endif
${EndIf}
SectionEnd
!else
Section
SectionEnd
!ifdef BUILD_UNINSTALLER
!include "uninstaller.nsh"
!endif
22 changes: 4 additions & 18 deletions templates/nsis/multiUser.nsh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
!define UNINSTALL_REGISTRY_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APP_GUID}"
!define UNINSTALL_DISPLAY_NAME "${PRODUCT_NAME} ${VERSION}"

# current Install Mode ("AllUsers" or "CurrentUser")
# current Install Mode ("all" or "CurrentUser")
Var installMode

!ifndef INSTALL_MODE_PER_ALL_USERS
Expand All @@ -28,7 +28,7 @@ Var installMode
System::Call '*$2(&w${NSIS_MAX_STRLEN} .r1)'
StrCpy $0 $1
System::Call 'Ole32::CoTaskMemFree(ir2)'
${EndIf}
${endif}
StrCpy $INSTDIR "$0\${PRODUCT_FILENAME}"
!endif

Expand All @@ -38,21 +38,13 @@ Var installMode
StrCpy $INSTDIR $perUserInstallationFolder
${endif}
!macroend

!ifndef BUILD_UNINSTALLER
Function installMode.CurrentUser
!insertmacro setInstallModePerUser
FunctionEnd
!endif
!endif

!ifdef INSTALL_MODE_PER_ALL_USERS_REQUIRED
Var perMachineInstallationFolder

!macro setInstallModePerAllUsers
# Install mode initialization - per-machine
StrCpy $installMode AllUsers

StrCpy $installMode all
SetShellVarContext all

StrCpy $INSTDIR "$PROGRAMFILES\${PRODUCT_FILENAME}"
Expand All @@ -63,12 +55,6 @@ Var installMode
StrCpy $INSTDIR $perMachineInstallationFolder
${endif}
!macroend

!ifndef BUILD_UNINSTALLER
Function installMode.AllUsers
!insertmacro setInstallModePerAllUsers
FunctionEnd
!endif
!endif

# SHCTX is the hive HKLM if SetShellVarContext all, or HKCU if SetShellVarContext user
Expand All @@ -77,7 +63,7 @@ Var installMode
WriteRegStr SHCTX "${INSTALL_REGISTRY_KEY}" InstallLocation "$INSTDIR"

# Write the uninstall keys for Windows
${if} $installMode == "AllUsers"
${if} $installMode == "all"
WriteRegStr SHCTX "${UNINSTALL_REGISTRY_KEY}" DisplayName "${UNINSTALL_DISPLAY_NAME}"
WriteRegStr SHCTX "${UNINSTALL_REGISTRY_KEY}" UninstallString '"$INSTDIR\${UNINSTALL_FILENAME}" /allusers'
${else}
Expand Down
16 changes: 8 additions & 8 deletions templates/nsis/multiUserUi.nsh
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ Var RadioButtonLabel1
${If} ${UAC_IsInnerInstance}
${AndIf} ${UAC_IsAdmin}
# inner Process (and Admin) - skip selection, inner process is always used for elevation (machine-wide)
Call ${UNINSTALLER_FUNCPREFIX}installMode.AllUsers
!insertmacro setInstallModePerAllUsers
Abort
${EndIf}

${GetParameters} $R0
${GetOptions} $R0 "/allusers" $R1
${IfNot} ${Errors}
Call ${UNINSTALLER_FUNCPREFIX}installMode.AllUsers
!insertmacro setInstallModePerAllUsers
Abort
${EndIf}

${GetOptions} $R0 "/currentuser" $R1
${IfNot} ${Errors}
Call ${UNINSTALLER_FUNCPREFIX}installMode.CurrentUser
!insertmacro setInstallModePerUser
Abort
${EndIf}

Expand All @@ -56,11 +56,11 @@ Var RadioButtonLabel1
!ifdef BUILD_UNINSTALLER
${if} $hasPerUserInstallation == "1"
${andif} $hasPerMachineInstallation == "0"
Call un.installMode.CurrentUser
!insertmacro setInstallModePerUser
Abort
${elseif} $hasPerUserInstallation == "0"
${andif} $hasPerMachineInstallation == "1"
Call un.installMode.AllUsers
!insertmacro setInstallModePerAllUsers
Abort
${endif}

Expand Down Expand Up @@ -113,7 +113,7 @@ Var RadioButtonLabel1
${NSD_CreateLabel} 0u 110u 280u 50u ""
Pop $RadioButtonLabel1

${if} $installMode == "AllUsers" ; setting defaults
${if} $installMode == "all"
SendMessage $MultiUser.InstallModePage.AllUsers ${BM_SETCHECK} ${BST_CHECKED} 0 ; set as default
SendMessage $MultiUser.InstallModePage.AllUsers ${BM_CLICK} 0 0 ; trigger click event
${else}
Expand All @@ -130,7 +130,7 @@ Var RadioButtonLabel1

${if} $MultiUser.InstallModePage.ReturnValue = ${BST_CHECKED}
${if} ${UAC_IsAdmin}
Call ${UNINSTALLER_FUNCPREFIX}installMode.AllUsers
!insertmacro setInstallModePerAllUsers
${else}
!ifdef MULTIUSER_INSTALLMODE_ALLOW_ELEVATION
GetDlgItem $9 $HWNDParent 1
Expand Down Expand Up @@ -158,7 +158,7 @@ Var RadioButtonLabel1
!endif
${endif}
${else}
Call ${UNINSTALLER_FUNCPREFIX}installMode.CurrentUser
!insertmacro setInstallModePerUser
${endif}

!insertmacro MUI_PAGE_FUNCTION_CUSTOM LEAVE
Expand Down
14 changes: 3 additions & 11 deletions templates/nsis/oneClick.nsh
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,10 @@ AutoCloseWindow true
RequestExecutionLevel user
!endif

!macro initMultiUser UNINSTALLER_FUNCPREFIX
!macro initMultiUser
!ifdef INSTALL_MODE_PER_ALL_USERS
!ifdef BUILD_UNINSTALLER
Call un.installMode.AllUsers
!else
Call installMode.AllUsers
!endif
!insertmacro setInstallModePerAllUsers
!else
!ifdef BUILD_UNINSTALLER
Call un.installMode.CurrentUser
!else
Call installMode.CurrentUser
!endif
!insertmacro setInstallModePerUser
!endif
!macroend
Loading

0 comments on commit 2c3d8c2

Please sign in to comment.