installer.nsh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. # functions (nsis macro) for installer
  2. !include "extractAppPackage.nsh"
  3. !ifdef APP_PACKAGE_URL
  4. !include webPackage.nsh
  5. !endif
  6. !macro installApplicationFiles
  7. !ifdef APP_BUILD_DIR
  8. File /r "${APP_BUILD_DIR}\*.*"
  9. !else
  10. !ifdef APP_PACKAGE_URL
  11. Var /GLOBAL packageFile
  12. Var /GLOBAL isPackageFileExplicitlySpecified
  13. ${StdUtils.GetParameter} $packageFile "package-file" ""
  14. ${if} $packageFile == ""
  15. !ifdef APP_64_NAME
  16. !ifdef APP_32_NAME
  17. !ifdef APP_ARM64_NAME
  18. ${if} ${IsNativeARM64}
  19. StrCpy $packageFile "${APP_ARM64_NAME}"
  20. StrCpy $1 "${APP_ARM64_HASH}"
  21. ${elseif} ${IsNativeAMD64}
  22. StrCpy $packageFile "${APP_64_NAME}"
  23. StrCpy $1 "${APP_64_HASH}"
  24. ${else}
  25. StrCpy $packageFile "${APP_32_NAME}"
  26. StrCpy $1 "${APP_32_HASH}"
  27. ${endif}
  28. !else
  29. ${if} ${RunningX64}
  30. StrCpy $packageFile "${APP_64_NAME}"
  31. StrCpy $1 "${APP_64_HASH}"
  32. ${else}
  33. StrCpy $packageFile "${APP_32_NAME}"
  34. StrCpy $1 "${APP_32_HASH}"
  35. ${endif}
  36. !endif
  37. !else
  38. StrCpy $packageFile "${APP_64_NAME}"
  39. StrCpy $1 "${APP_64_HASH}"
  40. !endif
  41. !else
  42. StrCpy $packageFile "${APP_32_NAME}"
  43. StrCpy $1 "${APP_32_HASH}"
  44. !endif
  45. StrCpy $4 "$packageFile"
  46. StrCpy $packageFile "$EXEDIR/$packageFile"
  47. StrCpy $isPackageFileExplicitlySpecified "false"
  48. ${else}
  49. StrCpy $isPackageFileExplicitlySpecified "true"
  50. ${endIf}
  51. # we do not check file hash is specifed explicitly using --package-file because it is clear that user definitely want to use this file and it is user responsibility to check
  52. # 1. auto-updater uses --package-file and validates checksum
  53. # 2. user can user another package file (use case - one installer suitable for any app version (use latest version))
  54. ${if} ${FileExists} "$packageFile"
  55. ${if} $isPackageFileExplicitlySpecified == "true"
  56. Goto fun_extract
  57. ${else}
  58. ${StdUtils.HashFile} $3 "SHA2-512" "$packageFile"
  59. ${if} $3 == $1
  60. Goto fun_extract
  61. ${else}
  62. MessageBox MB_OK "Package file $4 found locally, but checksum doesn't match — expected $1, actual $3.$\r$\nLocal file is ignored and package will be downloaded from Internet."
  63. ${endIf}
  64. ${endIf}
  65. ${endIf}
  66. !insertmacro downloadApplicationFiles
  67. fun_extract:
  68. !insertmacro extractUsing7za "$packageFile"
  69. # electron always uses per user app data
  70. ${if} $installMode == "all"
  71. SetShellVarContext current
  72. ${endif}
  73. !insertmacro moveFile "$packageFile" "$LOCALAPPDATA\${APP_PACKAGE_STORE_FILE}"
  74. ${if} $installMode == "all"
  75. SetShellVarContext all
  76. ${endif}
  77. !else
  78. !insertmacro extractEmbeddedAppPackage
  79. # electron always uses per user app data
  80. ${if} $installMode == "all"
  81. SetShellVarContext current
  82. ${endif}
  83. !insertmacro copyFile "$EXEPATH" "$LOCALAPPDATA\${APP_INSTALLER_STORE_FILE}"
  84. ${if} $installMode == "all"
  85. SetShellVarContext all
  86. ${endif}
  87. !endif
  88. !endif
  89. File "/oname=${UNINSTALL_FILENAME}" "${UNINSTALLER_OUT_FILE}"
  90. !macroend
  91. !macro registryAddInstallInfo
  92. WriteRegStr SHELL_CONTEXT "${INSTALL_REGISTRY_KEY}" InstallLocation "$INSTDIR"
  93. WriteRegStr SHELL_CONTEXT "${INSTALL_REGISTRY_KEY}" KeepShortcuts "true"
  94. WriteRegStr SHELL_CONTEXT "${INSTALL_REGISTRY_KEY}" ShortcutName "${SHORTCUT_NAME}"
  95. !ifdef MENU_FILENAME
  96. WriteRegStr SHELL_CONTEXT "${INSTALL_REGISTRY_KEY}" MenuDirectory "${MENU_FILENAME}"
  97. !endif
  98. ${if} $installMode == "all"
  99. StrCpy $0 "/allusers"
  100. StrCpy $1 ""
  101. ${else}
  102. StrCpy $0 "/currentuser"
  103. StrCpy $1 ""
  104. ${endIf}
  105. WriteRegStr SHELL_CONTEXT "${UNINSTALL_REGISTRY_KEY}" DisplayName "${UNINSTALL_DISPLAY_NAME}$1"
  106. # https://github.com/electron-userland/electron-builder/issues/750
  107. StrCpy $2 "$INSTDIR\${UNINSTALL_FILENAME}"
  108. WriteRegStr SHELL_CONTEXT "${UNINSTALL_REGISTRY_KEY}" UninstallString '"$2" $0'
  109. WriteRegStr SHELL_CONTEXT "${UNINSTALL_REGISTRY_KEY}" QuietUninstallString '"$2" $0 /S'
  110. WriteRegStr SHELL_CONTEXT "${UNINSTALL_REGISTRY_KEY}" "DisplayVersion" "${VERSION}"
  111. !ifdef UNINSTALLER_ICON
  112. WriteRegStr SHELL_CONTEXT "${UNINSTALL_REGISTRY_KEY}" "DisplayIcon" "$INSTDIR\uninstallerIcon.ico"
  113. !else
  114. WriteRegStr SHELL_CONTEXT "${UNINSTALL_REGISTRY_KEY}" "DisplayIcon" "$appExe,0"
  115. !endif
  116. !ifdef COMPANY_NAME
  117. WriteRegStr SHELL_CONTEXT "${UNINSTALL_REGISTRY_KEY}" "Publisher" "${COMPANY_NAME}"
  118. !endif
  119. WriteRegDWORD SHELL_CONTEXT "${UNINSTALL_REGISTRY_KEY}" NoModify 1
  120. WriteRegDWORD SHELL_CONTEXT "${UNINSTALL_REGISTRY_KEY}" NoRepair 1
  121. # allow user to define ESTIMATED_SIZE to avoid GetSize call
  122. !ifdef ESTIMATED_SIZE
  123. IntFmt $0 "0x%08X" ${ESTIMATED_SIZE}
  124. !else
  125. ${GetSize} "$INSTDIR" "/S=0K" $0 $1 $2
  126. IntFmt $0 "0x%08X" $0
  127. !endif
  128. WriteRegDWORD SHELL_CONTEXT "${UNINSTALL_REGISTRY_KEY}" "EstimatedSize" "$0"
  129. !macroend
  130. !macro cleanupOldMenuDirectory
  131. ${if} $oldMenuDirectory != ""
  132. !ifdef MENU_FILENAME
  133. ${if} $oldMenuDirectory != "${MENU_FILENAME}"
  134. RMDir "$SMPROGRAMS\$oldMenuDirectory"
  135. ${endIf}
  136. !else
  137. RMDir "$SMPROGRAMS\$oldMenuDirectory"
  138. !endif
  139. ${endIf}
  140. !macroend
  141. !macro createMenuDirectory
  142. !ifdef MENU_FILENAME
  143. CreateDirectory "$SMPROGRAMS\${MENU_FILENAME}"
  144. ClearErrors
  145. !endif
  146. !macroend
  147. !macro addStartMenuLink keepShortcuts
  148. !ifndef DO_NOT_CREATE_START_MENU_SHORTCUT
  149. # The keepShortcuts mechanism is NOT enabled.
  150. # Menu shortcut will be recreated.
  151. ${if} $keepShortcuts == "false"
  152. !insertmacro cleanupOldMenuDirectory
  153. !insertmacro createMenuDirectory
  154. CreateShortCut "$newStartMenuLink" "$appExe" "" "$appExe" 0 "" "" "${APP_DESCRIPTION}"
  155. # clear error (if shortcut already exists)
  156. ClearErrors
  157. WinShell::SetLnkAUMI "$newStartMenuLink" "${APP_ID}"
  158. # The keepShortcuts mechanism IS enabled.
  159. # The menu shortcut could either not exist (it shouldn't be recreated) or exist in an obsolete location.
  160. ${elseif} $oldStartMenuLink != $newStartMenuLink
  161. ${andIf} ${FileExists} "$oldStartMenuLink"
  162. !insertmacro createMenuDirectory
  163. Rename $oldStartMenuLink $newStartMenuLink
  164. WinShell::UninstShortcut "$oldStartMenuLink"
  165. WinShell::SetLnkAUMI "$newStartMenuLink" "${APP_ID}"
  166. !insertmacro cleanupOldMenuDirectory
  167. ${endIf}
  168. !endif
  169. !macroend
  170. !macro addDesktopLink keepShortcuts
  171. !ifndef DO_NOT_CREATE_DESKTOP_SHORTCUT
  172. # https://github.com/electron-userland/electron-builder/pull/1432
  173. ${ifNot} ${isNoDesktopShortcut}
  174. # The keepShortcuts mechanism is NOT enabled.
  175. # Shortcuts will be recreated.
  176. ${if} $keepShortcuts == "false"
  177. CreateShortCut "$newDesktopLink" "$appExe" "" "$appExe" 0 "" "" "${APP_DESCRIPTION}"
  178. ClearErrors
  179. WinShell::SetLnkAUMI "$newDesktopLink" "${APP_ID}"
  180. # The keepShortcuts mechanism IS enabled.
  181. # The desktop shortcut could exist in an obsolete location (due to name change).
  182. ${elseif} $oldDesktopLink != $newDesktopLink
  183. ${andIf} ${FileExists} "$oldDesktopLink"
  184. Rename $oldDesktopLink $newDesktopLink
  185. WinShell::UninstShortcut "$oldDesktopLink"
  186. WinShell::SetLnkAUMI "$newDesktopLink" "${APP_ID}"
  187. !ifdef RECREATE_DESKTOP_SHORTCUT
  188. ${elseif} $oldDesktopLink != $newDesktopLink
  189. ${orIfNot} ${FileExists} "$oldDesktopLink"
  190. ${ifNot} ${isUpdated}
  191. CreateShortCut "$newDesktopLink" "$appExe" "" "$appExe" 0 "" "" "${APP_DESCRIPTION}"
  192. ClearErrors
  193. WinShell::SetLnkAUMI "$newDesktopLink" "${APP_ID}"
  194. ${endIf}
  195. !endif
  196. ${endIf}
  197. System::Call 'Shell32::SHChangeNotify(i 0x8000000, i 0, i 0, i 0)'
  198. ${endIf}
  199. !endif
  200. !macroend