Skip to content

Commit 2c3d8c2

Browse files
committed
feat(nsis): per-machine installer automatically removes old version #621
1 parent d40e0b5 commit 2c3d8c2

File tree

13 files changed

+150
-170
lines changed

13 files changed

+150
-170
lines changed

src/appInfo.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class AppInfo {
1414
readonly buildNumber: string
1515
readonly buildVersion: string
1616

17+
readonly productName: string
1718
readonly productFilename: string
1819

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

36+
this.productName = getProductName(this.metadata, this.devMetadata)
3537
this.productFilename = sanitizeFileName(this.productName)
3638
}
3739

@@ -68,10 +70,6 @@ export class AppInfo {
6870
return metadata.category || old
6971
}
7072

71-
get productName(): string {
72-
return getProductName(this.metadata, this.devMetadata)
73-
}
74-
7573
get copyright(): string {
7674
const metadata = this.devMetadata.build
7775
const old = (<any>metadata)["app-copyright"]

src/winPackager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export class WinPackager extends PlatformPackager<WinBuildOptions> {
154154
"--set-version-string", "CompanyName", appInfo.companyName,
155155
"--set-version-string", "FileDescription", appInfo.description,
156156
"--set-version-string", "ProductName", appInfo.productName,
157-
"--set-version-string", "InternalName", appInfo.productName,
157+
"--set-version-string", "InternalName", path.basename(appInfo.productFilename, ".exe"),
158158
"--set-version-string", "LegalCopyright", appInfo.copyright,
159159
"--set-version-string", "OriginalFilename", "",
160160
"--set-file-version", appInfo.buildVersion,

templates/nsis/boringInstaller.nsh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
!insertmacro MUI_UNPAGE_INSTFILES
2323
!endif
2424

25-
!macro initMultiUser UNINSTALLER_FUNCPREFIX
25+
!macro initMultiUser
2626
!insertmacro UAC_PageElevation_OnInit
2727

2828
${If} ${UAC_IsInnerInstance}
@@ -60,17 +60,17 @@
6060
${endif}
6161

6262
${if} $hasPerUserInstallation == "1"
63-
${andif} $hasPerMachineInstallation == "0"
64-
Call ${UNINSTALLER_FUNCPREFIX}installMode.CurrentUser
63+
${andif} $hasPerMachineInstallation == "0"
64+
!insertmacro setInstallModePerUser
6565
${elseif} $hasPerUserInstallation == "0"
6666
${andif} $hasPerMachineInstallation == "1"
67-
Call ${UNINSTALLER_FUNCPREFIX}installMode.AllUsers
67+
!insertmacro setInstallModePerAllUsers
6868
${else}
6969
# if there is no installation, or there is both per-user and per-machine
7070
!ifdef INSTALL_MODE_PER_ALL_USERS
71-
Call ${UNINSTALLER_FUNCPREFIX}installMode.AllUsers
71+
!insertmacro setInstallModePerAllUsers
7272
!else
73-
Call ${UNINSTALLER_FUNCPREFIX}installMode.CurrentUser
73+
!insertmacro setInstallModePerUser
7474
!endif
7575
${endif}
7676
!macroend

templates/nsis/install.nsh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
${IfNot} ${Silent}
2+
SetDetailsPrint none
3+
4+
!ifdef ONE_CLICK
5+
!ifdef HEADER_ICO
6+
SpiderBanner::Show /MODERN /ICON "$PLUGINSDIR\installerHeaderico.ico"
7+
!else
8+
SpiderBanner::Show /MODERN
9+
!endif
10+
!endif
11+
${endif}
12+
13+
!insertmacro CHECK_APP_RUNNING "install"
14+
15+
${if} $installMode == "all"
16+
ReadRegStr $R0 HKEY_LOCAL_MACHINE "${UNINSTALL_REGISTRY_KEY}" UninstallString
17+
${if} $R0 != ""
18+
ExecWait "$R0 /S"
19+
${endif}
20+
${endif}
21+
22+
RMDir /r $INSTDIR
23+
SetOutPath $INSTDIR
24+
25+
!ifdef APP_64
26+
${If} ${RunningX64}
27+
Nsis7z::Extract "$PLUGINSDIR\app-64.7z"
28+
${Else}
29+
Nsis7z::Extract "$PLUGINSDIR\app-32.7z"
30+
${endif}
31+
!else
32+
Nsis7z::Extract "$PLUGINSDIR\app-32.7z"
33+
!endif
34+
35+
File "/oname=${UNINSTALL_FILENAME}" "${UNINSTALLER_OUT_FILE}"
36+
37+
!insertmacro registryAddInstallInfo
38+
39+
StrCpy $startMenuLink "$SMPROGRAMS\${PRODUCT_FILENAME}.lnk"
40+
StrCpy $desktopLink "$DESKTOP\${PRODUCT_FILENAME}.lnk"
41+
42+
# create shortcuts in the start menu and on the desktop
43+
# shortcut for uninstall is bad cause user can choose this by mistake during search, so, we don't add it
44+
CreateShortCut "$startMenuLink" "$INSTDIR\${APP_EXECUTABLE_FILENAME}" "" "$INSTDIR\${APP_EXECUTABLE_FILENAME}" 0 "" "" "${APP_DESCRIPTION}"
45+
CreateShortCut "$desktopLink" "$INSTDIR\${APP_EXECUTABLE_FILENAME}" "" "$INSTDIR\${APP_EXECUTABLE_FILENAME}" 0 "" "" "${APP_DESCRIPTION}"
46+
47+
WinShell::SetLnkAUMI "$startMenuLink" "${APP_ID}"
48+
WinShell::SetLnkAUMI "$desktopLink" "${APP_ID}"
49+
50+
!ifmacrodef registerFileAssociations
51+
!insertmacro registerFileAssociations
52+
!endif
53+
54+
!ifmacrodef customInstall
55+
!insertmacro customInstall
56+
!endif
57+
58+
${IfNot} ${Silent}
59+
!ifdef ONE_CLICK
60+
# otherwise app window will be in backround
61+
HideWindow
62+
!ifdef RUN_AFTER_FINISH
63+
Call StartApp
64+
!endif
65+
!endif
66+
${EndIf}

templates/nsis/installer.nsi

Lines changed: 7 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Function .onInit
2828
Quit
2929
!else
3030
!insertmacro check64BitAndSetRegView
31-
!insertmacro initMultiUser ""
31+
!insertmacro initMultiUser
3232

3333
!ifdef ONE_CLICK
3434
!insertmacro ALLOW_ONLY_ONE_INSTALLER_INSTACE
@@ -59,70 +59,12 @@ Function .onInit
5959
!endif
6060
FunctionEnd
6161

62-
!ifndef BUILD_UNINSTALLER
63-
Section "install"
64-
${IfNot} ${Silent}
65-
SetDetailsPrint none
66-
67-
!ifdef ONE_CLICK
68-
!ifdef HEADER_ICO
69-
SpiderBanner::Show /MODERN /ICON "$PLUGINSDIR\installerHeaderico.ico"
70-
!else
71-
SpiderBanner::Show /MODERN
72-
!endif
73-
!endif
74-
${EndIf}
75-
76-
!insertmacro CHECK_APP_RUNNING "install"
77-
78-
RMDir /r $INSTDIR
79-
SetOutPath $INSTDIR
80-
81-
!ifdef APP_64
82-
${If} ${RunningX64}
83-
Nsis7z::Extract "$PLUGINSDIR\app-64.7z"
84-
${Else}
85-
Nsis7z::Extract "$PLUGINSDIR\app-32.7z"
86-
${EndIf}
87-
!else
88-
Nsis7z::Extract "$PLUGINSDIR\app-32.7z"
89-
!endif
90-
91-
File "/oname=${UNINSTALL_FILENAME}" "${UNINSTALLER_OUT_FILE}"
92-
93-
!insertmacro registryAddInstallInfo
94-
95-
StrCpy $startMenuLink "$SMPROGRAMS\${PRODUCT_FILENAME}.lnk"
96-
StrCpy $desktopLink "$DESKTOP\${PRODUCT_FILENAME}.lnk"
97-
98-
# create shortcuts in the start menu and on the desktop
99-
# shortcut for uninstall is bad cause user can choose this by mistake during search, so, we don't add it
100-
CreateShortCut "$startMenuLink" "$INSTDIR\${APP_EXECUTABLE_FILENAME}" "" "$INSTDIR\${APP_EXECUTABLE_FILENAME}" 0 "" "" "${APP_DESCRIPTION}"
101-
CreateShortCut "$desktopLink" "$INSTDIR\${APP_EXECUTABLE_FILENAME}" "" "$INSTDIR\${APP_EXECUTABLE_FILENAME}" 0 "" "" "${APP_DESCRIPTION}"
102-
103-
WinShell::SetLnkAUMI "$startMenuLink" "${APP_ID}"
104-
WinShell::SetLnkAUMI "$desktopLink" "${APP_ID}"
105-
106-
!ifmacrodef registerFileAssociations
107-
!insertmacro registerFileAssociations
108-
!endif
109-
110-
!ifmacrodef customInstall
111-
!insertmacro customInstall
112-
!endif
62+
Section "install"
63+
!ifndef BUILD_UNINSTALLER
64+
!include "install.nsh"
65+
!endif
66+
SectionEnd
11367

114-
${IfNot} ${Silent}
115-
!ifdef ONE_CLICK
116-
# otherwise app window will be in backround
117-
HideWindow
118-
!ifdef RUN_AFTER_FINISH
119-
Call StartApp
120-
!endif
121-
!endif
122-
${EndIf}
123-
SectionEnd
124-
!else
125-
Section
126-
SectionEnd
68+
!ifdef BUILD_UNINSTALLER
12769
!include "uninstaller.nsh"
12870
!endif

templates/nsis/multiUser.nsh

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
!define UNINSTALL_REGISTRY_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APP_GUID}"
88
!define UNINSTALL_DISPLAY_NAME "${PRODUCT_NAME} ${VERSION}"
99

10-
# current Install Mode ("AllUsers" or "CurrentUser")
10+
# current Install Mode ("all" or "CurrentUser")
1111
Var installMode
1212

1313
!ifndef INSTALL_MODE_PER_ALL_USERS
@@ -28,7 +28,7 @@ Var installMode
2828
System::Call '*$2(&w${NSIS_MAX_STRLEN} .r1)'
2929
StrCpy $0 $1
3030
System::Call 'Ole32::CoTaskMemFree(ir2)'
31-
${EndIf}
31+
${endif}
3232
StrCpy $INSTDIR "$0\${PRODUCT_FILENAME}"
3333
!endif
3434

@@ -38,21 +38,13 @@ Var installMode
3838
StrCpy $INSTDIR $perUserInstallationFolder
3939
${endif}
4040
!macroend
41-
42-
!ifndef BUILD_UNINSTALLER
43-
Function installMode.CurrentUser
44-
!insertmacro setInstallModePerUser
45-
FunctionEnd
46-
!endif
4741
!endif
4842

4943
!ifdef INSTALL_MODE_PER_ALL_USERS_REQUIRED
5044
Var perMachineInstallationFolder
5145

5246
!macro setInstallModePerAllUsers
53-
# Install mode initialization - per-machine
54-
StrCpy $installMode AllUsers
55-
47+
StrCpy $installMode all
5648
SetShellVarContext all
5749

5850
StrCpy $INSTDIR "$PROGRAMFILES\${PRODUCT_FILENAME}"
@@ -63,12 +55,6 @@ Var installMode
6355
StrCpy $INSTDIR $perMachineInstallationFolder
6456
${endif}
6557
!macroend
66-
67-
!ifndef BUILD_UNINSTALLER
68-
Function installMode.AllUsers
69-
!insertmacro setInstallModePerAllUsers
70-
FunctionEnd
71-
!endif
7258
!endif
7359

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

7965
# Write the uninstall keys for Windows
80-
${if} $installMode == "AllUsers"
66+
${if} $installMode == "all"
8167
WriteRegStr SHCTX "${UNINSTALL_REGISTRY_KEY}" DisplayName "${UNINSTALL_DISPLAY_NAME}"
8268
WriteRegStr SHCTX "${UNINSTALL_REGISTRY_KEY}" UninstallString '"$INSTDIR\${UNINSTALL_FILENAME}" /allusers'
8369
${else}

templates/nsis/multiUserUi.nsh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ Var RadioButtonLabel1
3333
${If} ${UAC_IsInnerInstance}
3434
${AndIf} ${UAC_IsAdmin}
3535
# inner Process (and Admin) - skip selection, inner process is always used for elevation (machine-wide)
36-
Call ${UNINSTALLER_FUNCPREFIX}installMode.AllUsers
36+
!insertmacro setInstallModePerAllUsers
3737
Abort
3838
${EndIf}
3939

4040
${GetParameters} $R0
4141
${GetOptions} $R0 "/allusers" $R1
4242
${IfNot} ${Errors}
43-
Call ${UNINSTALLER_FUNCPREFIX}installMode.AllUsers
43+
!insertmacro setInstallModePerAllUsers
4444
Abort
4545
${EndIf}
4646

4747
${GetOptions} $R0 "/currentuser" $R1
4848
${IfNot} ${Errors}
49-
Call ${UNINSTALLER_FUNCPREFIX}installMode.CurrentUser
49+
!insertmacro setInstallModePerUser
5050
Abort
5151
${EndIf}
5252

@@ -56,11 +56,11 @@ Var RadioButtonLabel1
5656
!ifdef BUILD_UNINSTALLER
5757
${if} $hasPerUserInstallation == "1"
5858
${andif} $hasPerMachineInstallation == "0"
59-
Call un.installMode.CurrentUser
59+
!insertmacro setInstallModePerUser
6060
Abort
6161
${elseif} $hasPerUserInstallation == "0"
6262
${andif} $hasPerMachineInstallation == "1"
63-
Call un.installMode.AllUsers
63+
!insertmacro setInstallModePerAllUsers
6464
Abort
6565
${endif}
6666

@@ -113,7 +113,7 @@ Var RadioButtonLabel1
113113
${NSD_CreateLabel} 0u 110u 280u 50u ""
114114
Pop $RadioButtonLabel1
115115

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

131131
${if} $MultiUser.InstallModePage.ReturnValue = ${BST_CHECKED}
132132
${if} ${UAC_IsAdmin}
133-
Call ${UNINSTALLER_FUNCPREFIX}installMode.AllUsers
133+
!insertmacro setInstallModePerAllUsers
134134
${else}
135135
!ifdef MULTIUSER_INSTALLMODE_ALLOW_ELEVATION
136136
GetDlgItem $9 $HWNDParent 1
@@ -158,7 +158,7 @@ Var RadioButtonLabel1
158158
!endif
159159
${endif}
160160
${else}
161-
Call ${UNINSTALLER_FUNCPREFIX}installMode.CurrentUser
161+
!insertmacro setInstallModePerUser
162162
${endif}
163163

164164
!insertmacro MUI_PAGE_FUNCTION_CUSTOM LEAVE

templates/nsis/oneClick.nsh

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,10 @@ AutoCloseWindow true
2525
RequestExecutionLevel user
2626
!endif
2727

28-
!macro initMultiUser UNINSTALLER_FUNCPREFIX
28+
!macro initMultiUser
2929
!ifdef INSTALL_MODE_PER_ALL_USERS
30-
!ifdef BUILD_UNINSTALLER
31-
Call un.installMode.AllUsers
32-
!else
33-
Call installMode.AllUsers
34-
!endif
30+
!insertmacro setInstallModePerAllUsers
3531
!else
36-
!ifdef BUILD_UNINSTALLER
37-
Call un.installMode.CurrentUser
38-
!else
39-
Call installMode.CurrentUser
40-
!endif
32+
!insertmacro setInstallModePerUser
4133
!endif
4234
!macroend

0 commit comments

Comments
 (0)