forked from electron-userland/electron-builder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstallUtil.nsh
248 lines (206 loc) · 5.97 KB
/
installUtil.nsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
!macro moveFile FROM TO
ClearErrors
Rename `${FROM}` `${TO}`
${if} ${errors}
# not clear - can NSIS rename on another drive or not, so, in case of error, just copy
ClearErrors
!insertmacro copyFile `${FROM}` `${TO}`
Delete `${FROM}`
${endif}
!macroend
!macro copyFile FROM TO
${StdUtils.GetParentPath} $R5 `${TO}`
CreateDirectory `$R5`
ClearErrors
CopyFiles /SILENT `${FROM}` `${TO}`
!macroend
Function GetInQuotes
Exch $R0
Push $R1
Push $R2
Push $R3
StrCpy $R2 -1
IntOp $R2 $R2 + 1
StrCpy $R3 $R0 1 $R2
StrCmp $R3 "" 0 +3
StrCpy $R0 ""
Goto Done
StrCmp $R3 '"' 0 -5
IntOp $R2 $R2 + 1
StrCpy $R0 $R0 "" $R2
StrCpy $R2 0
IntOp $R2 $R2 + 1
StrCpy $R3 $R0 1 $R2
StrCmp $R3 "" 0 +3
StrCpy $R0 ""
Goto Done
StrCmp $R3 '"' 0 -5
StrCpy $R0 $R0 $R2
Done:
Pop $R3
Pop $R2
Pop $R1
Exch $R0
FunctionEnd
!macro GetInQuotes Var Str
Push "${Str}"
Call GetInQuotes
Pop "${Var}"
!macroend
Function GetFileParent
Exch $R0
Push $R1
Push $R2
Push $R3
StrCpy $R1 0
StrLen $R2 $R0
loop:
IntOp $R1 $R1 + 1
IntCmp $R1 $R2 get 0 get
StrCpy $R3 $R0 1 -$R1
StrCmp $R3 "\" get
Goto loop
get:
StrCpy $R0 $R0 -$R1
Pop $R3
Pop $R2
Pop $R1
Exch $R0
FunctionEnd
Var /GLOBAL isTryToKeepShortcuts
!macro setIsTryToKeepShortcuts
StrCpy $isTryToKeepShortcuts "true"
!ifdef allowToChangeInstallationDirectory
${ifNot} ${isUpdated}
StrCpy $isTryToKeepShortcuts "false"
${endIf}
!endif
!macroend
# https://nsis-dev.github.io/NSIS-Forums/html/t-172971.html
!macro readReg VAR ROOT_KEY SUB_KEY NAME
${if} "${ROOT_KEY}" == "SHELL_CONTEXT"
ReadRegStr "${VAR}" SHELL_CONTEXT "${SUB_KEY}" "${NAME}"
${elseif} "${ROOT_KEY}" == "HKEY_CURRENT_USER"
ReadRegStr "${VAR}" HKEY_CURRENT_USER "${SUB_KEY}" "${NAME}"
${elseif} "${ROOT_KEY}" == "HKEY_LOCAL_MACHINE"
ReadRegStr "${VAR}" HKEY_LOCAL_MACHINE "${SUB_KEY}" "${NAME}"
${else}
MessageBox MB_OK "Unsupported ${ROOT_KEY}"
${endif}
!macroend
Function handleUninstallResult
Var /GLOBAL rootKey_uninstallResult
Exch $rootKey_uninstallResult
${if} "$rootKey_uninstallResult" == "SHELL_CONTEXT"
!ifmacrodef customUnInstallCheck
!insertmacro customUnInstallCheck
Return
!endif
${elseif} "$rootKey_uninstallResult" == "HKEY_CURRENT_USER"
!ifmacrodef customUnInstallCheckCurrentUser
!insertmacro customUnInstallCheckCurrentUser
Return
!endif
${endif}
IfErrors 0 +3
DetailPrint `Uninstall was not successful. Not able to launch uninstaller!`
Return
${if} $R0 != 0
MessageBox MB_OK|MB_ICONEXCLAMATION "$(uninstallFailed): $R0"
DetailPrint `Uninstall was not successful. Uninstaller error code: $R0.`
SetErrorLevel 2
Quit
${endif}
FunctionEnd
!macro handleUninstallResult ROOT_KEY
Push "${ROOT_KEY}"
Call handleUninstallResult
!macroend
# http://stackoverflow.com/questions/24595887/waiting-for-nsis-uninstaller-to-finish-in-nsis-installer-either-fails-or-the-uni
Function uninstallOldVersion
Var /GLOBAL uninstallerFileName
Var /Global uninstallerFileNameTemp
Var /GLOBAL installationDir
Var /GLOBAL uninstallString
Var /GLOBAL rootKey
ClearErrors
Exch $rootKey
Push 0
Pop $R0
!insertmacro readReg $uninstallString "$rootKey" "${UNINSTALL_REGISTRY_KEY}" UninstallString
${if} $uninstallString == ""
!ifdef UNINSTALL_REGISTRY_KEY_2
!insertmacro readReg $uninstallString "$rootKey" "${UNINSTALL_REGISTRY_KEY_2}" UninstallString
!endif
${if} $uninstallString == ""
ClearErrors
Return
${endif}
${endif}
# uninstaller should be copied out of app installation dir (because this dir will be deleted), so, extract uninstaller file name
!insertmacro GetInQuotes $uninstallerFileName "$uninstallString"
!insertmacro readReg $installationDir "$rootKey" "${INSTALL_REGISTRY_KEY}" InstallLocation
${if} $installationDir == ""
${andIf} $uninstallerFileName != ""
# https://github.com/electron-userland/electron-builder/issues/735#issuecomment-246918567
Push $uninstallerFileName
Call GetFileParent
Pop $installationDir
${endif}
${if} $installationDir == ""
${andIf} $uninstallerFileName == ""
ClearErrors
Return
${endif}
${if} $installMode == "CurrentUser"
${orIf} $rootKey == "HKEY_CURRENT_USER"
StrCpy $0 "/currentuser"
${else}
StrCpy $0 "/allusers"
${endif}
!insertMacro setIsTryToKeepShortcuts
${if} $isTryToKeepShortcuts == "true"
!insertmacro readReg $R5 "$rootKey" "${INSTALL_REGISTRY_KEY}" KeepShortcuts
# if true, it means that old uninstaller supports --keep-shortcuts flag
${if} $R5 == "true"
${andIf} ${FileExists} "$appExe"
StrCpy $0 "$0 --keep-shortcuts"
${endIf}
${endIf}
${if} ${isDeleteAppData}
StrCpy $0 "$0 --delete-app-data"
${else}
# always pass --updated flag - to ensure that if DELETE_APP_DATA_ON_UNINSTALL is defined, user data will be not removed
StrCpy $0 "$0 --updated"
${endif}
StrCpy $uninstallerFileNameTemp "$PLUGINSDIR\old-uninstaller.exe"
!insertmacro copyFile "$uninstallerFileName" "$uninstallerFileNameTemp"
# Retry counter
StrCpy $R5 0
UninstallLoop:
IntOp $R5 $R5 + 1
${if} $R5 > 5
MessageBox MB_RETRYCANCEL|MB_ICONEXCLAMATION "$(appCannotBeClosed)" /SD IDCANCEL IDRETRY OneMoreAttempt
Return
${endIf}
OneMoreAttempt:
ExecWait '"$uninstallerFileNameTemp" /S /KEEP_APP_DATA $0 _?=$installationDir' $R0
ifErrors TryInPlace CheckResult
TryInPlace:
# the execution failed - might have been caused by some group policy restrictions
# we try to execute the uninstaller in place
ExecWait '"$uninstallerFileName" /S /KEEP_APP_DATA $0 _?=$installationDir' $R0
ifErrors DoesNotExist
CheckResult:
${if} $R0 == 0
Return
${endIf}
Sleep 1000
Goto UninstallLoop
DoesNotExist:
SetErrors
FunctionEnd
!macro uninstallOldVersion ROOT_KEY
Push "${ROOT_KEY}"
Call uninstallOldVersion
!macroend