installer.nsi 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. ; This file is copied from https://github.com/tauri-apps/tauri/blob/tauri-v1.5/tooling/bundler/src/bundle/windows/templates/installer.nsi
  2. ; and edit to fit the needs of the project. the latest tauri 2.x has a different base nsi script.
  3. Unicode true
  4. ; Set the compression algorithm. Default is LZMA.
  5. !if "{{compression}}" == ""
  6. SetCompressor /SOLID lzma
  7. !else
  8. SetCompressor /SOLID "{{compression}}"
  9. !endif
  10. !include MUI2.nsh
  11. !include FileFunc.nsh
  12. !include x64.nsh
  13. !include WordFunc.nsh
  14. !include "LogicLib.nsh"
  15. !include "StrFunc.nsh"
  16. ${StrCase}
  17. ${StrLoc}
  18. !define MANUFACTURER "{{manufacturer}}"
  19. !define PRODUCTNAME "{{product_name}}"
  20. !define VERSION "{{version}}"
  21. !define VERSIONWITHBUILD "{{version_with_build}}"
  22. !define SHORTDESCRIPTION "{{short_description}}"
  23. !define INSTALLMODE "{{install_mode}}"
  24. !define LICENSE "{{license}}"
  25. !define INSTALLERICON "{{installer_icon}}"
  26. !define SIDEBARIMAGE "{{sidebar_image}}"
  27. !define HEADERIMAGE "{{header_image}}"
  28. !define MAINBINARYNAME "{{main_binary_name}}"
  29. !define MAINBINARYSRCPATH "{{main_binary_path}}"
  30. !define BUNDLEID "{{bundle_id}}"
  31. !define COPYRIGHT "{{copyright}}"
  32. !define OUTFILE "{{out_file}}"
  33. !define ARCH "{{arch}}"
  34. !define PLUGINSPATH "{{additional_plugins_path}}"
  35. !define ALLOWDOWNGRADES "{{allow_downgrades}}"
  36. !define DISPLAYLANGUAGESELECTOR "{{display_language_selector}}"
  37. !define INSTALLWEBVIEW2MODE "{{install_webview2_mode}}"
  38. !define WEBVIEW2INSTALLERARGS "{{webview2_installer_args}}"
  39. !define WEBVIEW2BOOTSTRAPPERPATH "{{webview2_bootstrapper_path}}"
  40. !define WEBVIEW2INSTALLERPATH "{{webview2_installer_path}}"
  41. !define UNINSTKEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCTNAME}"
  42. !define MANUPRODUCTKEY "Software\${MANUFACTURER}\${PRODUCTNAME}"
  43. !define UNINSTALLERSIGNCOMMAND "{{uninstaller_sign_cmd}}"
  44. !define ESTIMATEDSIZE "{{estimated_size}}"
  45. Name "${PRODUCTNAME}"
  46. BrandingText "${COPYRIGHT}"
  47. OutFile "${OUTFILE}"
  48. VIProductVersion "${VERSIONWITHBUILD}"
  49. VIAddVersionKey "ProductName" "${PRODUCTNAME}"
  50. VIAddVersionKey "FileDescription" "${SHORTDESCRIPTION}"
  51. VIAddVersionKey "LegalCopyright" "${COPYRIGHT}"
  52. VIAddVersionKey "FileVersion" "${VERSION}"
  53. VIAddVersionKey "ProductVersion" "${VERSION}"
  54. ; Plugins path, currently exists for linux only
  55. !if "${PLUGINSPATH}" != ""
  56. !addplugindir "${PLUGINSPATH}"
  57. !endif
  58. !if "${UNINSTALLERSIGNCOMMAND}" != ""
  59. !uninstfinalize '${UNINSTALLERSIGNCOMMAND}'
  60. !endif
  61. ; Handle install mode, `perUser`, `perMachine` or `both`
  62. !if "${INSTALLMODE}" == "perMachine"
  63. RequestExecutionLevel highest
  64. !endif
  65. !if "${INSTALLMODE}" == "currentUser"
  66. RequestExecutionLevel user
  67. !endif
  68. !if "${INSTALLMODE}" == "both"
  69. !define MULTIUSER_MUI
  70. !define MULTIUSER_INSTALLMODE_INSTDIR "${PRODUCTNAME}"
  71. !define MULTIUSER_INSTALLMODE_COMMANDLINE
  72. !if "${ARCH}" == "x64"
  73. !define MULTIUSER_USE_PROGRAMFILES64
  74. !else if "${ARCH}" == "arm64"
  75. !define MULTIUSER_USE_PROGRAMFILES64
  76. !endif
  77. !define MULTIUSER_INSTALLMODE_DEFAULT_REGISTRY_KEY "${UNINSTKEY}"
  78. !define MULTIUSER_INSTALLMODE_DEFAULT_REGISTRY_VALUENAME "CurrentUser"
  79. !define MULTIUSER_INSTALLMODEPAGE_SHOWUSERNAME
  80. !define MULTIUSER_INSTALLMODE_FUNCTION RestorePreviousInstallLocation
  81. !define MULTIUSER_EXECUTIONLEVEL Highest
  82. !include MultiUser.nsh
  83. !endif
  84. ; installer icon
  85. !if "${INSTALLERICON}" != ""
  86. !define MUI_ICON "${INSTALLERICON}"
  87. !endif
  88. ; installer sidebar image
  89. !if "${SIDEBARIMAGE}" != ""
  90. !define MUI_WELCOMEFINISHPAGE_BITMAP "${SIDEBARIMAGE}"
  91. !endif
  92. ; installer header image
  93. !if "${HEADERIMAGE}" != ""
  94. !define MUI_HEADERIMAGE
  95. !define MUI_HEADERIMAGE_BITMAP "${HEADERIMAGE}"
  96. !endif
  97. ; Define registry key to store installer language
  98. !define MUI_LANGDLL_REGISTRY_ROOT "HKCU"
  99. !define MUI_LANGDLL_REGISTRY_KEY "${MANUPRODUCTKEY}"
  100. !define MUI_LANGDLL_REGISTRY_VALUENAME "Installer Language"
  101. ; Installer pages, must be ordered as they appear
  102. ; 1. Welcome Page
  103. !define MUI_PAGE_CUSTOMFUNCTION_PRE SkipIfPassive
  104. !insertmacro MUI_PAGE_WELCOME
  105. ; 2. License Page (if defined)
  106. !if "${LICENSE}" != ""
  107. !define MUI_PAGE_CUSTOMFUNCTION_PRE SkipIfPassive
  108. !insertmacro MUI_PAGE_LICENSE "${LICENSE}"
  109. !endif
  110. ; 3. Install mode (if it is set to `both`)
  111. !if "${INSTALLMODE}" == "both"
  112. !define MUI_PAGE_CUSTOMFUNCTION_PRE SkipIfPassive
  113. !insertmacro MULTIUSER_PAGE_INSTALLMODE
  114. !endif
  115. ; 4. Custom page to ask user if he wants to reinstall/uninstall
  116. ; only if a previous installtion was detected
  117. Var ReinstallPageCheck
  118. Page custom PageReinstall PageLeaveReinstall
  119. Function PageReinstall
  120. ; Uninstall previous WiX installation if exists.
  121. ;
  122. ; A WiX installer stores the isntallation info in registry
  123. ; using a UUID and so we have to loop through all keys under
  124. ; `HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall`
  125. ; and check if `DisplayName` and `Publisher` keys match ${PRODUCTNAME} and ${MANUFACTURER}
  126. ;
  127. ; This has a potentional issue that there maybe another installation that matches
  128. ; our ${PRODUCTNAME} and ${MANUFACTURER} but wasn't installed by our WiX installer,
  129. ; however, this should be fine since the user will have to confirm the uninstallation
  130. ; and they can chose to abort it if doesn't make sense.
  131. StrCpy $0 0
  132. !insertmacro CheckAllVergeProcesses
  133. wix_loop:
  134. EnumRegKey $1 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" $0
  135. StrCmp $1 "" wix_done ; Exit loop if there is no more keys to loop on
  136. IntOp $0 $0 + 1
  137. ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$1" "DisplayName"
  138. ReadRegStr $R1 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$1" "Publisher"
  139. StrCmp "$R0$R1" "${PRODUCTNAME}${MANUFACTURER}" 0 wix_loop
  140. ReadRegStr $R0 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$1" "UninstallString"
  141. ${StrCase} $R1 $R0 "L"
  142. ${StrLoc} $R0 $R1 "msiexec" ">"
  143. StrCmp $R0 0 0 wix_done
  144. StrCpy $R7 "wix"
  145. StrCpy $R6 "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$1"
  146. Goto compare_version
  147. wix_done:
  148. ; Check if there is an existing installation, if not, abort the reinstall page
  149. ReadRegStr $R0 SHCTX "${UNINSTKEY}" ""
  150. ReadRegStr $R1 SHCTX "${UNINSTKEY}" "UninstallString"
  151. ${IfThen} "$R0$R1" == "" ${|} Abort ${|}
  152. ; Compare this installar version with the existing installation
  153. ; and modify the messages presented to the user accordingly
  154. compare_version:
  155. StrCpy $R4 "$(older)"
  156. ${If} $R7 == "wix"
  157. ReadRegStr $R0 HKLM "$R6" "DisplayVersion"
  158. ${Else}
  159. ReadRegStr $R0 SHCTX "${UNINSTKEY}" "DisplayVersion"
  160. ${EndIf}
  161. ${IfThen} $R0 == "" ${|} StrCpy $R4 "$(unknown)" ${|}
  162. nsis_tauri_utils::SemverCompare "${VERSION}" $R0
  163. Pop $R0
  164. ; Reinstalling the same version
  165. ${If} $R0 == 0
  166. StrCpy $R1 "$(alreadyInstalledLong)"
  167. StrCpy $R2 "$(addOrReinstall)"
  168. StrCpy $R3 "$(uninstallApp)"
  169. !insertmacro MUI_HEADER_TEXT "$(alreadyInstalled)" "$(chooseMaintenanceOption)"
  170. StrCpy $R5 "2"
  171. ; Upgrading
  172. ${ElseIf} $R0 == 1
  173. StrCpy $R1 "$(olderOrUnknownVersionInstalled)"
  174. StrCpy $R2 "$(uninstallBeforeInstalling)"
  175. StrCpy $R3 "$(dontUninstall)"
  176. !insertmacro MUI_HEADER_TEXT "$(alreadyInstalled)" "$(choowHowToInstall)"
  177. StrCpy $R5 "1"
  178. ; Downgrading
  179. ${ElseIf} $R0 == -1
  180. StrCpy $R1 "$(newerVersionInstalled)"
  181. StrCpy $R2 "$(uninstallBeforeInstalling)"
  182. !if "${ALLOWDOWNGRADES}" == "true"
  183. StrCpy $R3 "$(dontUninstall)"
  184. !else
  185. StrCpy $R3 "$(dontUninstallDowngrade)"
  186. !endif
  187. !insertmacro MUI_HEADER_TEXT "$(alreadyInstalled)" "$(choowHowToInstall)"
  188. StrCpy $R5 "1"
  189. ${Else}
  190. Abort
  191. ${EndIf}
  192. Call SkipIfPassive
  193. nsDialogs::Create 1018
  194. Pop $R4
  195. ${IfThen} $(^RTL) == 1 ${|} nsDialogs::SetRTL $(^RTL) ${|}
  196. ${NSD_CreateLabel} 0 0 100% 24u $R1
  197. Pop $R1
  198. ${NSD_CreateRadioButton} 30u 50u -30u 8u $R2
  199. Pop $R2
  200. ${NSD_OnClick} $R2 PageReinstallUpdateSelection
  201. ${NSD_CreateRadioButton} 30u 70u -30u 8u $R3
  202. Pop $R3
  203. ; disable this radio button if downgrading and downgrades are disabled
  204. !if "${ALLOWDOWNGRADES}" == "false"
  205. ${IfThen} $R0 == -1 ${|} EnableWindow $R3 0 ${|}
  206. !endif
  207. ${NSD_OnClick} $R3 PageReinstallUpdateSelection
  208. ; Check the first radio button if this the first time
  209. ; we enter this page or if the second button wasn't
  210. ; selected the last time we were on this page
  211. ${If} $ReinstallPageCheck != 2
  212. SendMessage $R2 ${BM_SETCHECK} ${BST_CHECKED} 0
  213. ${Else}
  214. SendMessage $R3 ${BM_SETCHECK} ${BST_CHECKED} 0
  215. ${EndIf}
  216. ${NSD_SetFocus} $R2
  217. nsDialogs::Show
  218. FunctionEnd
  219. Function PageReinstallUpdateSelection
  220. ${NSD_GetState} $R2 $R1
  221. ${If} $R1 == ${BST_CHECKED}
  222. StrCpy $ReinstallPageCheck 1
  223. ${Else}
  224. StrCpy $ReinstallPageCheck 2
  225. ${EndIf}
  226. FunctionEnd
  227. Function PageLeaveReinstall
  228. ${NSD_GetState} $R2 $R1
  229. ; $R5 holds whether we are reinstalling the same version or not
  230. ; $R5 == "1" -> different versions
  231. ; $R5 == "2" -> same version
  232. ;
  233. ; $R1 holds the radio buttons state. its meaning is dependant on the context
  234. StrCmp $R5 "1" 0 +2 ; Existing install is not the same version?
  235. StrCmp $R1 "1" reinst_uninstall reinst_done ; $R1 == "1", then user chose to uninstall existing version, otherwise skip uninstalling
  236. StrCmp $R1 "1" reinst_done ; Same version? skip uninstalling
  237. reinst_uninstall:
  238. HideWindow
  239. ClearErrors
  240. ${If} $R7 == "wix"
  241. ReadRegStr $R1 HKLM "$R6" "UninstallString"
  242. ExecWait '$R1' $0
  243. ${Else}
  244. ReadRegStr $4 SHCTX "${MANUPRODUCTKEY}" ""
  245. ReadRegStr $R1 SHCTX "${UNINSTKEY}" "UninstallString"
  246. ExecWait '$R1 /P _?=$4' $0
  247. ${EndIf}
  248. BringToFront
  249. ${IfThen} ${Errors} ${|} StrCpy $0 2 ${|} ; ExecWait failed, set fake exit code
  250. ${If} $0 <> 0
  251. ${OrIf} ${FileExists} "$INSTDIR\${MAINBINARYNAME}.exe"
  252. ${If} $0 = 1 ; User aborted uninstaller?
  253. StrCmp $R5 "2" 0 +2 ; Is the existing install the same version?
  254. Quit ; ...yes, already installed, we are done
  255. Abort
  256. ${EndIf}
  257. MessageBox MB_ICONEXCLAMATION "$(unableToUninstall)"
  258. Abort
  259. ${Else}
  260. StrCpy $0 $R1 1
  261. ${IfThen} $0 == '"' ${|} StrCpy $R1 $R1 -1 1 ${|} ; Strip quotes from UninstallString
  262. Delete $R1
  263. RMDir $INSTDIR
  264. ${EndIf}
  265. reinst_done:
  266. FunctionEnd
  267. ; 5. Choose install directoy page
  268. !define MUI_PAGE_CUSTOMFUNCTION_PRE SkipIfPassive
  269. !insertmacro MUI_PAGE_DIRECTORY
  270. ; 6. Start menu shortcut page
  271. !define MUI_PAGE_CUSTOMFUNCTION_PRE SkipIfPassive
  272. Var AppStartMenuFolder
  273. !insertmacro MUI_PAGE_STARTMENU Application $AppStartMenuFolder
  274. ; 7. Installation page
  275. !insertmacro MUI_PAGE_INSTFILES
  276. ; 8. Finish page
  277. ;
  278. ; Don't auto jump to finish page after installation page,
  279. ; because the installation page has useful info that can be used debug any issues with the installer.
  280. !define MUI_FINISHPAGE_NOAUTOCLOSE
  281. ; Use show readme button in the finish page as a button create a desktop shortcut
  282. !define MUI_FINISHPAGE_SHOWREADME
  283. !define MUI_FINISHPAGE_SHOWREADME_TEXT "$(createDesktop)"
  284. !define MUI_FINISHPAGE_SHOWREADME_FUNCTION CreateDesktopShortcut
  285. ; Show run app after installation.
  286. !define MUI_FINISHPAGE_RUN "$INSTDIR\${MAINBINARYNAME}.exe"
  287. !define MUI_PAGE_CUSTOMFUNCTION_PRE SkipIfPassive
  288. !insertmacro MUI_PAGE_FINISH
  289. ; Uninstaller Pages
  290. ; 1. Confirm uninstall page
  291. Var DeleteAppDataCheckbox
  292. Var DeleteAppDataCheckboxState
  293. !define /ifndef WS_EX_LAYOUTRTL 0x00400000
  294. !define MUI_PAGE_CUSTOMFUNCTION_SHOW un.ConfirmShow
  295. Function un.ConfirmShow
  296. FindWindow $1 "#32770" "" $HWNDPARENT ; Find inner dialog
  297. ${If} $(^RTL) == 1
  298. System::Call 'USER32::CreateWindowEx(i${__NSD_CheckBox_EXSTYLE}|${WS_EX_LAYOUTRTL},t"${__NSD_CheckBox_CLASS}",t "$(deleteAppData)",i${__NSD_CheckBox_STYLE},i 50,i 100,i 400, i 25,i$1,i0,i0,i0)i.s'
  299. ${Else}
  300. System::Call 'USER32::CreateWindowEx(i${__NSD_CheckBox_EXSTYLE},t"${__NSD_CheckBox_CLASS}",t "$(deleteAppData)",i${__NSD_CheckBox_STYLE},i 0,i 100,i 400, i 25,i$1,i0,i0,i0)i.s'
  301. ${EndIf}
  302. Pop $DeleteAppDataCheckbox
  303. SendMessage $HWNDPARENT ${WM_GETFONT} 0 0 $1
  304. SendMessage $DeleteAppDataCheckbox ${WM_SETFONT} $1 1
  305. FunctionEnd
  306. !define MUI_PAGE_CUSTOMFUNCTION_LEAVE un.ConfirmLeave
  307. Function un.ConfirmLeave
  308. SendMessage $DeleteAppDataCheckbox ${BM_GETCHECK} 0 0 $DeleteAppDataCheckboxState
  309. FunctionEnd
  310. !insertmacro MUI_UNPAGE_CONFIRM
  311. ; 2. Uninstalling Page
  312. !insertmacro MUI_UNPAGE_INSTFILES
  313. ;Languages
  314. {{#each languages}}
  315. !insertmacro MUI_LANGUAGE "{{this}}"
  316. {{/each}}
  317. !insertmacro MUI_RESERVEFILE_LANGDLL
  318. {{#each language_files}}
  319. !include "{{this}}"
  320. {{/each}}
  321. !macro SetContext
  322. !if "${INSTALLMODE}" == "currentUser"
  323. SetShellVarContext current
  324. !else if "${INSTALLMODE}" == "perMachine"
  325. SetShellVarContext all
  326. !endif
  327. ${If} ${RunningX64}
  328. !if "${ARCH}" == "x64"
  329. SetRegView 64
  330. !else if "${ARCH}" == "arm64"
  331. SetRegView 64
  332. !else
  333. SetRegView 32
  334. !endif
  335. ${EndIf}
  336. !macroend
  337. Var PassiveMode
  338. Function .onInit
  339. ${GetOptions} $CMDLINE "/P" $PassiveMode
  340. IfErrors +2 0
  341. StrCpy $PassiveMode 1
  342. !if "${DISPLAYLANGUAGESELECTOR}" == "true"
  343. !insertmacro MUI_LANGDLL_DISPLAY
  344. !endif
  345. !insertmacro SetContext
  346. ${If} $INSTDIR == ""
  347. ; Set default install location
  348. !if "${INSTALLMODE}" == "perMachine"
  349. ${If} ${RunningX64}
  350. !if "${ARCH}" == "x64"
  351. StrCpy $INSTDIR "$PROGRAMFILES64\${PRODUCTNAME}"
  352. !else if "${ARCH}" == "arm64"
  353. StrCpy $INSTDIR "$PROGRAMFILES64\${PRODUCTNAME}"
  354. !else
  355. StrCpy $INSTDIR "$PROGRAMFILES\${PRODUCTNAME}"
  356. !endif
  357. ${Else}
  358. StrCpy $INSTDIR "$PROGRAMFILES\${PRODUCTNAME}"
  359. ${EndIf}
  360. !else if "${INSTALLMODE}" == "currentUser"
  361. StrCpy $INSTDIR "$LOCALAPPDATA\${PRODUCTNAME}"
  362. !endif
  363. Call RestorePreviousInstallLocation
  364. ${EndIf}
  365. !if "${INSTALLMODE}" == "both"
  366. !insertmacro MULTIUSER_INIT
  367. !endif
  368. FunctionEnd
  369. !macro CheckAllVergeProcesses
  370. ; Check if Clash Verge.exe is running
  371. nsis_tauri_utils::FindProcess "Clash Verge.exe"
  372. ${If} $R0 != 0
  373. ; Kill the process
  374. !if "${INSTALLMODE}" == "currentUser"
  375. nsis_tauri_utils::KillProcessCurrentUser "Clash Verge.exe"
  376. !else
  377. nsis_tauri_utils::KillProcess "Clash Verge.exe"
  378. !endif
  379. ${EndIf}
  380. ; Check if clash-verge-service.exe is running
  381. nsis_tauri_utils::FindProcess "clash-verge-service.exe"
  382. ${If} $R0 != 0
  383. ; Kill the process
  384. !if "${INSTALLMODE}" == "currentUser"
  385. nsis_tauri_utils::KillProcessCurrentUser "clash-verge-service.exe"
  386. !else
  387. nsis_tauri_utils::KillProcess "clash-verge-service.exe"
  388. ${EndIf}
  389. ; Check if clash-meta-alpha.exe is running
  390. nsis_tauri_utils::FindProcess "clash-meta-alpha.exe"
  391. ${If} $R0 != 0
  392. ; Kill the process
  393. !if "${INSTALLMODE}" == "currentUser"
  394. nsis_tauri_utils::KillProcessCurrentUser "clash-meta-alpha.exe"
  395. !else
  396. nsis_tauri_utils::KillProcess "clash-meta-alpha.exe"
  397. ${EndIf}
  398. ; Check if clash-meta.exe is running
  399. nsis_tauri_utils::FindProcess "clash-meta.exe"
  400. ${If} $R0 != 0
  401. ; Kill the process
  402. !if "${INSTALLMODE}" == "currentUser"
  403. nsis_tauri_utils::KillProcessCurrentUser "clash-meta.exe"
  404. !else
  405. nsis_tauri_utils::KillProcess "clash-meta.exe"
  406. ${EndIf}
  407. !macroend
  408. Section
  409. !insertmacro CheckAllVergeProcesses
  410. SectionEnd
  411. Section EarlyChecks
  412. ; Abort silent installer if downgrades is disabled
  413. !if "${ALLOWDOWNGRADES}" == "false"
  414. IfSilent 0 silent_downgrades_done
  415. ; If downgrading
  416. ${If} $R0 == -1
  417. System::Call 'kernel32::AttachConsole(i -1)i.r0'
  418. ${If} $0 != 0
  419. System::Call 'kernel32::GetStdHandle(i -11)i.r0'
  420. System::call 'kernel32::SetConsoleTextAttribute(i r0, i 0x0004)' ; set red color
  421. FileWrite $0 "$(silentDowngrades)"
  422. ${EndIf}
  423. Abort
  424. ${EndIf}
  425. silent_downgrades_done:
  426. !endif
  427. SectionEnd
  428. Section WebView2
  429. ; Check if Webview2 is already installed and skip this section
  430. ${If} ${RunningX64}
  431. ReadRegStr $4 HKLM "SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}" "pv"
  432. ${Else}
  433. ReadRegStr $4 HKLM "SOFTWARE\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}" "pv"
  434. ${EndIf}
  435. ReadRegStr $5 HKCU "SOFTWARE\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}" "pv"
  436. StrCmp $4 "" 0 webview2_done
  437. StrCmp $5 "" 0 webview2_done
  438. ; Webview2 install modes
  439. !if "${INSTALLWEBVIEW2MODE}" == "downloadBootstrapper"
  440. Delete "$TEMP\MicrosoftEdgeWebview2Setup.exe"
  441. DetailPrint "$(webview2Downloading)"
  442. nsis_tauri_utils::download "https://go.microsoft.com/fwlink/p/?LinkId=2124703" "$TEMP\MicrosoftEdgeWebview2Setup.exe"
  443. Pop $0
  444. ${If} $0 == 0
  445. DetailPrint "$(webview2DownloadSuccess)"
  446. ${Else}
  447. DetailPrint "$(webview2DownloadError)"
  448. Abort "$(webview2AbortError)"
  449. ${EndIf}
  450. StrCpy $6 "$TEMP\MicrosoftEdgeWebview2Setup.exe"
  451. Goto install_webview2
  452. !endif
  453. !if "${INSTALLWEBVIEW2MODE}" == "embedBootstrapper"
  454. Delete "$TEMP\MicrosoftEdgeWebview2Setup.exe"
  455. File "/oname=$TEMP\MicrosoftEdgeWebview2Setup.exe" "${WEBVIEW2BOOTSTRAPPERPATH}"
  456. DetailPrint "$(installingWebview2)"
  457. StrCpy $6 "$TEMP\MicrosoftEdgeWebview2Setup.exe"
  458. Goto install_webview2
  459. !endif
  460. !if "${INSTALLWEBVIEW2MODE}" == "offlineInstaller"
  461. Delete "$TEMP\MicrosoftEdgeWebView2RuntimeInstaller.exe"
  462. File "/oname=$TEMP\MicrosoftEdgeWebView2RuntimeInstaller.exe" "${WEBVIEW2INSTALLERPATH}"
  463. DetailPrint "$(installingWebview2)"
  464. StrCpy $6 "$TEMP\MicrosoftEdgeWebView2RuntimeInstaller.exe"
  465. Goto install_webview2
  466. !endif
  467. Goto webview2_done
  468. install_webview2:
  469. DetailPrint "$(installingWebview2)"
  470. ; $6 holds the path to the webview2 installer
  471. ExecWait "$6 ${WEBVIEW2INSTALLERARGS} /install" $1
  472. ${If} $1 == 0
  473. DetailPrint "$(webview2InstallSuccess)"
  474. ${Else}
  475. DetailPrint "$(webview2InstallError)"
  476. Abort "$(webview2AbortError)"
  477. ${EndIf}
  478. webview2_done:
  479. SectionEnd
  480. !macro CheckIfAppIsRunning
  481. !if "${INSTALLMODE}" == "currentUser"
  482. nsis_tauri_utils::FindProcessCurrentUser "${MAINBINARYNAME}.exe"
  483. !else
  484. nsis_tauri_utils::FindProcess "${MAINBINARYNAME}.exe"
  485. !endif
  486. Pop $R0
  487. ${If} $R0 = 0
  488. IfSilent kill 0
  489. ${IfThen} $PassiveMode != 1 ${|} MessageBox MB_OKCANCEL "$(appRunningOkKill)" IDOK kill IDCANCEL cancel ${|}
  490. kill:
  491. !if "${INSTALLMODE}" == "currentUser"
  492. nsis_tauri_utils::KillProcessCurrentUser "${MAINBINARYNAME}.exe"
  493. !else
  494. nsis_tauri_utils::KillProcess "${MAINBINARYNAME}.exe"
  495. !endif
  496. Pop $R0
  497. Sleep 500
  498. ${If} $R0 = 0
  499. Goto app_check_done
  500. ${Else}
  501. IfSilent silent ui
  502. silent:
  503. System::Call 'kernel32::AttachConsole(i -1)i.r0'
  504. ${If} $0 != 0
  505. System::Call 'kernel32::GetStdHandle(i -11)i.r0'
  506. System::call 'kernel32::SetConsoleTextAttribute(i r0, i 0x0004)' ; set red color
  507. FileWrite $0 "$(appRunning)$\n"
  508. ${EndIf}
  509. Abort
  510. ui:
  511. Abort "$(failedToKillApp)"
  512. ${EndIf}
  513. cancel:
  514. Abort "$(appRunning)"
  515. ${EndIf}
  516. app_check_done:
  517. !macroend
  518. Section Install
  519. SetOutPath $INSTDIR
  520. !insertmacro CheckIfAppIsRunning
  521. ; Copy main executable
  522. File "${MAINBINARYSRCPATH}"
  523. ; Copy resources
  524. {{#each resources_dirs}}
  525. CreateDirectory "$INSTDIR\\{{this}}"
  526. {{/each}}
  527. {{#each resources}}
  528. File /a "/oname={{this.[1]}}" "{{@key}}"
  529. {{/each}}
  530. ; Copy external binaries
  531. {{#each binaries}}
  532. File /a "/oname={{this}}" "{{@key}}"
  533. {{/each}}
  534. ; Create uninstaller
  535. WriteUninstaller "$INSTDIR\uninstall.exe"
  536. ; Save $INSTDIR in registry for future installations
  537. WriteRegStr SHCTX "${MANUPRODUCTKEY}" "" $INSTDIR
  538. !if "${INSTALLMODE}" == "both"
  539. ; Save install mode to be selected by default for the next installation such as updating
  540. ; or when uninstalling
  541. WriteRegStr SHCTX "${UNINSTKEY}" $MultiUser.InstallMode 1
  542. !endif
  543. ; Registry information for add/remove programs
  544. WriteRegStr SHCTX "${UNINSTKEY}" "DisplayName" "${PRODUCTNAME}"
  545. WriteRegStr SHCTX "${UNINSTKEY}" "DisplayIcon" "$\"$INSTDIR\${MAINBINARYNAME}.exe$\""
  546. WriteRegStr SHCTX "${UNINSTKEY}" "DisplayVersion" "${VERSION}"
  547. WriteRegStr SHCTX "${UNINSTKEY}" "Publisher" "${MANUFACTURER}"
  548. WriteRegStr SHCTX "${UNINSTKEY}" "InstallLocation" "$\"$INSTDIR$\""
  549. WriteRegStr SHCTX "${UNINSTKEY}" "UninstallString" "$\"$INSTDIR\uninstall.exe$\""
  550. WriteRegDWORD SHCTX "${UNINSTKEY}" "NoModify" "1"
  551. WriteRegDWORD SHCTX "${UNINSTKEY}" "NoRepair" "1"
  552. WriteRegDWORD SHCTX "${UNINSTKEY}" "EstimatedSize" "${ESTIMATEDSIZE}"
  553. ; Create start menu shortcut (GUI)
  554. !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
  555. Call CreateStartMenuShortcut
  556. !insertmacro MUI_STARTMENU_WRITE_END
  557. ; Create shortcuts for silent and passive installers, which
  558. ; can be disabled by passing `/NS` flag
  559. ; GUI installer has buttons for users to control creating them
  560. IfSilent check_ns_flag 0
  561. ${IfThen} $PassiveMode == 1 ${|} Goto check_ns_flag ${|}
  562. Goto shortcuts_done
  563. check_ns_flag:
  564. ${GetOptions} $CMDLINE "/NS" $R0
  565. IfErrors 0 shortcuts_done
  566. Call CreateDesktopShortcut
  567. Call CreateStartMenuShortcut
  568. shortcuts_done:
  569. ; Auto close this page for passive mode
  570. ${IfThen} $PassiveMode == 1 ${|} SetAutoClose true ${|}
  571. SectionEnd
  572. Function .onInstSuccess
  573. ; Check for `/R` flag only in silent and passive installers because
  574. ; GUI installer has a toggle for the user to (re)start the app
  575. IfSilent check_r_flag 0
  576. ${IfThen} $PassiveMode == 1 ${|} Goto check_r_flag ${|}
  577. Goto run_done
  578. check_r_flag:
  579. ${GetOptions} $CMDLINE "/R" $R0
  580. IfErrors run_done 0
  581. Exec '"$INSTDIR\${MAINBINARYNAME}.exe"'
  582. run_done:
  583. FunctionEnd
  584. Function un.onInit
  585. !insertmacro SetContext
  586. !if "${INSTALLMODE}" == "both"
  587. !insertmacro MULTIUSER_UNINIT
  588. !endif
  589. !insertmacro MUI_UNGETLANGUAGE
  590. FunctionEnd
  591. Section Uninstall
  592. !insertmacro CheckIfAppIsRunning
  593. ; Delete the app directory and its content from disk
  594. ; Copy main executable
  595. Delete "$INSTDIR\${MAINBINARYNAME}.exe"
  596. ; Delete resources
  597. {{#each resources}}
  598. Delete "$INSTDIR\\{{this.[1]}}"
  599. {{/each}}
  600. ; Delete external binaries
  601. {{#each binaries}}
  602. Delete "$INSTDIR\\{{this}}"
  603. {{/each}}
  604. ; Delete uninstaller
  605. Delete "$INSTDIR\uninstall.exe"
  606. ${If} $DeleteAppDataCheckboxState == 1
  607. RMDir /R /REBOOTOK "$INSTDIR"
  608. ${Else}
  609. {{#each resources_ancestors}}
  610. RMDir /REBOOTOK "$INSTDIR\\{{this}}"
  611. {{/each}}
  612. RMDir "$INSTDIR"
  613. ${EndIf}
  614. ; Remove start menu shortcut
  615. !insertmacro MUI_STARTMENU_GETFOLDER Application $AppStartMenuFolder
  616. Delete "$SMPROGRAMS\$AppStartMenuFolder\${MAINBINARYNAME}.lnk"
  617. RMDir "$SMPROGRAMS\$AppStartMenuFolder"
  618. ; Remove desktop shortcuts
  619. Delete "$DESKTOP\${MAINBINARYNAME}.lnk"
  620. ; Remove registry information for add/remove programs
  621. !if "${INSTALLMODE}" == "both"
  622. DeleteRegKey SHCTX "${UNINSTKEY}"
  623. !else if "${INSTALLMODE}" == "perMachine"
  624. DeleteRegKey HKLM "${UNINSTKEY}"
  625. !else
  626. DeleteRegKey HKCU "${UNINSTKEY}"
  627. !endif
  628. DeleteRegValue HKCU "${MANUPRODUCTKEY}" "Installer Language"
  629. ; Delete app data
  630. ${If} $DeleteAppDataCheckboxState == 1
  631. SetShellVarContext current
  632. RmDir /r "$APPDATA\${BUNDLEID}"
  633. RmDir /r "$LOCALAPPDATA\${BUNDLEID}"
  634. ${EndIf}
  635. ${GetOptions} $CMDLINE "/P" $R0
  636. IfErrors +2 0
  637. SetAutoClose true
  638. SectionEnd
  639. Function RestorePreviousInstallLocation
  640. ReadRegStr $4 SHCTX "${MANUPRODUCTKEY}" ""
  641. StrCmp $4 "" +2 0
  642. StrCpy $INSTDIR $4
  643. FunctionEnd
  644. Function SkipIfPassive
  645. ${IfThen} $PassiveMode == 1 ${|} Abort ${|}
  646. FunctionEnd
  647. Function CreateDesktopShortcut
  648. CreateShortcut "$DESKTOP\${MAINBINARYNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
  649. ApplicationID::Set "$DESKTOP\${MAINBINARYNAME}.lnk" "${BUNDLEID}"
  650. FunctionEnd
  651. Function CreateStartMenuShortcut
  652. CreateDirectory "$SMPROGRAMS\$AppStartMenuFolder"
  653. CreateShortcut "$SMPROGRAMS\$AppStartMenuFolder\${MAINBINARYNAME}.lnk" "$INSTDIR\${MAINBINARYNAME}.exe"
  654. ApplicationID::Set "$SMPROGRAMS\$AppStartMenuFolder\${MAINBINARYNAME}.lnk" "${BUNDLEID}"
  655. FunctionEnd