NSIS.template.in 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. ; CPack install script designed for a nmake build
  2. ;--------------------------------
  3. ; You must define these values
  4. !define VERSION "@CPACK_PACKAGE_VERSION@"
  5. !define PATCH "@CPACK_PACKAGE_VERSION_PATCH@"
  6. !define INST_DIR "@CPACK_TEMPORARY_DIRECTORY@"
  7. ;--------------------------------
  8. ;Variables
  9. Var MUI_TEMP
  10. Var STARTMENU_FOLDER
  11. Var SV_ALLUSERS
  12. ;--------------------------------
  13. ;Include Modern UI
  14. !include "MUI.nsh"
  15. ;Default installation folder
  16. InstallDir "$PROGRAMFILES\@CPACK_PACKAGE_INSTALL_DIRECTORY@"
  17. ;--------------------------------
  18. ; determine admin versus local install
  19. ; Is install for "AllUsers" or "JustMe"?
  20. ; Default to "JustMe" - set to "AllUsers" if admin or on Win9x
  21. ; This function is used for the very first "custom page" of the installer.
  22. ; This custom page does not show up visibly, but it executes prior to the
  23. ; first visible page and sets up $INSTDIR properly...
  24. ; Choose different default installation folder based on SV_ALLUSERS...
  25. ; "Program Files" for AllUsers, "My Documents" for JustMe...
  26. Function .onInit
  27. StrCpy $SV_ALLUSERS "JustMe"
  28. StrCpy $INSTDIR "$DOCUMENTS\@CPACK_PACKAGE_INSTALL_DIRECTORY@"
  29. ClearErrors
  30. UserInfo::GetName
  31. IfErrors noLM
  32. Pop $0
  33. UserInfo::GetAccountType
  34. Pop $1
  35. StrCmp $1 "Admin" 0 +3
  36. SetShellVarContext all
  37. ;MessageBox MB_OK 'User "$0" is in the Admin group'
  38. StrCpy $SV_ALLUSERS "AllUsers"
  39. Goto done
  40. StrCmp $1 "Power" 0 +3
  41. SetShellVarContext all
  42. ;MessageBox MB_OK 'User "$0" is in the Power Users group'
  43. StrCpy $SV_ALLUSERS "AllUsers"
  44. Goto done
  45. noLM:
  46. StrCpy $SV_ALLUSERS "AllUsers"
  47. ;Get installation folder from registry if available
  48. done:
  49. StrCmp $SV_ALLUSERS "AllUsers" 0 +2
  50. StrCpy $INSTDIR "$PROGRAMFILES\@CPACK_PACKAGE_INSTALL_DIRECTORY@"
  51. FunctionEnd
  52. ;--------------------------------
  53. ;General
  54. ;Name and file
  55. Name "@CPACK_PACKAGE_INSTALL_DIRECTORY@"
  56. OutFile "@CPACK_TOPLEVEL_DIRECTORY@/@CPACK_OUTPUT_FILE_NAME@"
  57. ;--------------------------------
  58. ;Interface Settings
  59. !define MUI_HEADERIMAGE
  60. !define MUI_ABORTWARNING
  61. ;--------------------------------
  62. ; path functions
  63. !verbose 3
  64. !include "WinMessages.NSH"
  65. !verbose 4
  66. ; AddToPath - Adds the given dir to the search path.
  67. ; Input - head of the stack
  68. ; Note - Win9x systems requires reboot
  69. Function AddToPath
  70. Exch $0
  71. Push $1
  72. Push $2
  73. Push $3
  74. # don't add if the path doesn't exist
  75. IfFileExists "$0\*.*" "" AddToPath_done
  76. ReadEnvStr $1 PATH
  77. Push "$1;"
  78. Push "$0;"
  79. Call StrStr
  80. Pop $2
  81. StrCmp $2 "" "" AddToPath_done
  82. Push "$1;"
  83. Push "$0\;"
  84. Call StrStr
  85. Pop $2
  86. StrCmp $2 "" "" AddToPath_done
  87. GetFullPathName /SHORT $3 $0
  88. Push "$1;"
  89. Push "$3;"
  90. Call StrStr
  91. Pop $2
  92. StrCmp $2 "" "" AddToPath_done
  93. Push "$1;"
  94. Push "$3\;"
  95. Call StrStr
  96. Pop $2
  97. StrCmp $2 "" "" AddToPath_done
  98. Call IsNT
  99. Pop $1
  100. StrCmp $1 1 AddToPath_NT
  101. ; Not on NT
  102. StrCpy $1 $WINDIR 2
  103. FileOpen $1 "$1\autoexec.bat" a
  104. FileSeek $1 -1 END
  105. FileReadByte $1 $2
  106. IntCmp $2 26 0 +2 +2 # DOS EOF
  107. FileSeek $1 -1 END # write over EOF
  108. FileWrite $1 "$\r$\nSET PATH=%PATH%;$3$\r$\n"
  109. FileClose $1
  110. SetRebootFlag true
  111. Goto AddToPath_done
  112. AddToPath_NT:
  113. ReadRegStr $1 SHCTX "Environment" "PATH"
  114. StrCpy $2 $1 1 -1 # copy last char
  115. StrCmp $2 ";" 0 +2 # if last char == ;
  116. StrCpy $1 $1 -1 # remove last char
  117. StrCmp $1 "" AddToPath_NTdoIt
  118. StrCpy $0 "$1;$0"
  119. AddToPath_NTdoIt:
  120. WriteRegExpandStr SHCTX "Environment" "PATH" $0
  121. SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
  122. AddToPath_done:
  123. Pop $3
  124. Pop $2
  125. Pop $1
  126. Pop $0
  127. FunctionEnd
  128. ; RemoveFromPath - Remove a given dir from the path
  129. ; Input: head of the stack
  130. Function un.RemoveFromPath
  131. Exch $0
  132. Push $1
  133. Push $2
  134. Push $3
  135. Push $4
  136. Push $5
  137. Push $6
  138. IntFmt $6 "%c" 26 # DOS EOF
  139. Call un.IsNT
  140. Pop $1
  141. StrCmp $1 1 unRemoveFromPath_NT
  142. ; Not on NT
  143. StrCpy $1 $WINDIR 2
  144. FileOpen $1 "$1\autoexec.bat" r
  145. GetTempFileName $4
  146. FileOpen $2 $4 w
  147. GetFullPathName /SHORT $0 $0
  148. StrCpy $0 "SET PATH=%PATH%;$0"
  149. Goto unRemoveFromPath_dosLoop
  150. unRemoveFromPath_dosLoop:
  151. FileRead $1 $3
  152. StrCpy $5 $3 1 -1 # read last char
  153. StrCmp $5 $6 0 +2 # if DOS EOF
  154. StrCpy $3 $3 -1 # remove DOS EOF so we can compare
  155. StrCmp $3 "$0$\r$\n" unRemoveFromPath_dosLoopRemoveLine
  156. StrCmp $3 "$0$\n" unRemoveFromPath_dosLoopRemoveLine
  157. StrCmp $3 "$0" unRemoveFromPath_dosLoopRemoveLine
  158. StrCmp $3 "" unRemoveFromPath_dosLoopEnd
  159. FileWrite $2 $3
  160. Goto unRemoveFromPath_dosLoop
  161. unRemoveFromPath_dosLoopRemoveLine:
  162. SetRebootFlag true
  163. Goto unRemoveFromPath_dosLoop
  164. unRemoveFromPath_dosLoopEnd:
  165. FileClose $2
  166. FileClose $1
  167. StrCpy $1 $WINDIR 2
  168. Delete "$1\autoexec.bat"
  169. CopyFiles /SILENT $4 "$1\autoexec.bat"
  170. Delete $4
  171. Goto unRemoveFromPath_done
  172. unRemoveFromPath_NT:
  173. ReadRegStr $1 SHCTX "Environment" "PATH"
  174. StrCpy $5 $1 1 -1 # copy last char
  175. StrCmp $5 ";" +2 # if last char != ;
  176. StrCpy $1 "$1;" # append ;
  177. Push $1
  178. Push "$0;"
  179. Call un.StrStr ; Find `$0;` in $1
  180. Pop $2 ; pos of our dir
  181. StrCmp $2 "" unRemoveFromPath_done
  182. ; else, it is in path
  183. # $0 - path to add
  184. # $1 - path var
  185. StrLen $3 "$0;"
  186. StrLen $4 $2
  187. StrCpy $5 $1 -$4 # $5 is now the part before the path to remove
  188. StrCpy $6 $2 "" $3 # $6 is now the part after the path to remove
  189. StrCpy $3 $5$6
  190. StrCpy $5 $3 1 -1 # copy last char
  191. StrCmp $5 ";" 0 +2 # if last char == ;
  192. StrCpy $3 $3 -1 # remove last char
  193. WriteRegExpandStr SHCTX "Environment" "PATH" $3
  194. SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
  195. unRemoveFromPath_done:
  196. Pop $6
  197. Pop $5
  198. Pop $4
  199. Pop $3
  200. Pop $2
  201. Pop $1
  202. Pop $0
  203. FunctionEnd
  204. ###########################################
  205. # Utility Functions #
  206. ###########################################
  207. ; IsNT
  208. ; no input
  209. ; output, top of the stack = 1 if NT or 0 if not
  210. ;
  211. ; Usage:
  212. ; Call IsNT
  213. ; Pop $R0
  214. ; ($R0 at this point is 1 or 0)
  215. !macro IsNT un
  216. Function ${un}IsNT
  217. Push $0
  218. ReadRegStr $0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" CurrentVersion
  219. StrCmp $0 "" 0 IsNT_yes
  220. ; we are not NT.
  221. Pop $0
  222. Push 0
  223. Return
  224. IsNT_yes:
  225. ; NT!!!
  226. Pop $0
  227. Push 1
  228. FunctionEnd
  229. !macroend
  230. !insertmacro IsNT ""
  231. !insertmacro IsNT "un."
  232. ; StrStr
  233. ; input, top of stack = string to search for
  234. ; top of stack-1 = string to search in
  235. ; output, top of stack (replaces with the portion of the string remaining)
  236. ; modifies no other variables.
  237. ;
  238. ; Usage:
  239. ; Push "this is a long ass string"
  240. ; Push "ass"
  241. ; Call StrStr
  242. ; Pop $R0
  243. ; ($R0 at this point is "ass string")
  244. !macro StrStr un
  245. Function ${un}StrStr
  246. Exch $R1 ; st=haystack,old$R1, $R1=needle
  247. Exch ; st=old$R1,haystack
  248. Exch $R2 ; st=old$R1,old$R2, $R2=haystack
  249. Push $R3
  250. Push $R4
  251. Push $R5
  252. StrLen $R3 $R1
  253. StrCpy $R4 0
  254. ; $R1=needle
  255. ; $R2=haystack
  256. ; $R3=len(needle)
  257. ; $R4=cnt
  258. ; $R5=tmp
  259. loop:
  260. StrCpy $R5 $R2 $R3 $R4
  261. StrCmp $R5 $R1 done
  262. StrCmp $R5 "" done
  263. IntOp $R4 $R4 + 1
  264. Goto loop
  265. done:
  266. StrCpy $R1 $R2 "" $R4
  267. Pop $R5
  268. Pop $R4
  269. Pop $R3
  270. Pop $R2
  271. Exch $R1
  272. FunctionEnd
  273. !macroend
  274. !insertmacro StrStr ""
  275. !insertmacro StrStr "un."
  276. ;--------------------------------
  277. ; Define some macro setting for the gui
  278. !define MUI_HEADERIMAGE_BITMAP "@CPACK_PACKAGE_ICON@"
  279. ;--------------------------------
  280. ;Pages
  281. !insertmacro MUI_PAGE_DIRECTORY
  282. ;Start Menu Folder Page Configuration
  283. !define MUI_STARTMENUPAGE_REGISTRY_ROOT "SHCTX"
  284. !define MUI_STARTMENUPAGE_REGISTRY_KEY "Software\@CPACK_PACKAGE_VENDOR@\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@"
  285. !define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "Start Menu Folder"
  286. !insertmacro MUI_PAGE_STARTMENU Application $STARTMENU_FOLDER
  287. !insertmacro MUI_PAGE_INSTFILES
  288. !insertmacro MUI_UNPAGE_CONFIRM
  289. !insertmacro MUI_UNPAGE_INSTFILES
  290. ;--------------------------------
  291. ;Languages
  292. !insertmacro MUI_LANGUAGE "English"
  293. ;--------------------------------
  294. ;Installer Sections
  295. Section "Add to path"
  296. Push $INSTDIR\bin
  297. StrCmp "@CPACK_NSIS_MODIFY_PATH@" "ON" 0 +2
  298. Call AddToPath
  299. SectionEnd
  300. Section "Installer Section" InstSection
  301. ;Use the entire tree produced by the INSTALL target. Keep the
  302. ;list of directories here in sync with the RMDir commands below.
  303. SetOutPath "$INSTDIR"
  304. File /r "${INST_DIR}\*.*"
  305. ;Store installation folder
  306. WriteRegStr SHCTX "Software\@CPACK_PACKAGE_VENDOR@\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@" "" $INSTDIR
  307. ;Create uninstaller
  308. WriteUninstaller "$INSTDIR\Uninstall.exe"
  309. !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
  310. ;Create shortcuts
  311. CreateDirectory "$SMPROGRAMS\$STARTMENU_FOLDER"
  312. @CPACK_NSIS_CREATE_ICONS@
  313. CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Uninstall.lnk" "$INSTDIR\Uninstall.exe"
  314. @CPACK_NSIS_EXTRA_INSTALL_COMMANDS@
  315. !insertmacro MUI_STARTMENU_WRITE_END
  316. SectionEnd
  317. ;--------------------------------
  318. ; determine admin versus local install
  319. Function un.onInit
  320. ClearErrors
  321. UserInfo::GetName
  322. IfErrors noLM
  323. Pop $0
  324. UserInfo::GetAccountType
  325. Pop $1
  326. StrCmp $1 "Admin" 0 +3
  327. SetShellVarContext all
  328. ;MessageBox MB_OK 'User "$0" is in the Admin group'
  329. Goto done
  330. StrCmp $1 "Power" 0 +3
  331. SetShellVarContext all
  332. ;MessageBox MB_OK 'User "$0" is in the Power Users group'
  333. Goto done
  334. noLM:
  335. ;Get installation folder from registry if available
  336. done:
  337. FunctionEnd
  338. ;--------------------------------
  339. ;Uninstaller Section
  340. Section "Uninstall"
  341. @CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS@
  342. ;Remove files we installed.
  343. ;Keep the list of directories here in sync with the File commands above.
  344. @CPACK_NSIS_DELETE_FILES@
  345. @CPACK_NSIS_DELETE_DIRECTORIES@
  346. ;Remove the uninstaller itself.
  347. Delete "$INSTDIR\Uninstall.exe"
  348. ;Remove the installation directory if it is empty.
  349. RMDir "$INSTDIR"
  350. ; Remove the registry entries.
  351. DeleteRegKey SHCTX "Software\@CPACK_PACKAGE_VENDOR@\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@"
  352. !insertmacro MUI_STARTMENU_GETFOLDER Application $MUI_TEMP
  353. Delete "$SMPROGRAMS\$MUI_TEMP\Uninstall.lnk"
  354. @CPACK_NSIS_DELETE_ICONS@
  355. ;Delete empty start menu parent diretories
  356. StrCpy $MUI_TEMP "$SMPROGRAMS\$MUI_TEMP"
  357. startMenuDeleteLoop:
  358. ClearErrors
  359. RMDir $MUI_TEMP
  360. GetFullPathName $MUI_TEMP "$MUI_TEMP\.."
  361. IfErrors startMenuDeleteLoopDone
  362. StrCmp $MUI_TEMP $SMPROGRAMS startMenuDeleteLoopDone startMenuDeleteLoop
  363. startMenuDeleteLoopDone:
  364. DeleteRegKey /ifempty SHCTX "Software\@CPACK_PACKAGE_VENDOR@\@CPACK_PACKAGE_INSTALL_REGISTRY_KEY@"
  365. Push $INSTDIR\bin
  366. StrCmp "@CPACK_NSIS_MODIFY_PATH@" "ON" 0 +2
  367. Call un.RemoveFromPath
  368. SectionEnd