当前位置: 首页 > 工具软件 > NSIS > 使用案例 >

NSIS 安装脚本

冯新知
2023-12-01

; 该脚本使用 HM VNISEdit 脚本编辑器向导产生

; 安装程序初始定义常量
!define PRODUCT_NAME "HutafUI"
!define PRODUCT_VERSION "1.0"
!define PRODUCT_PUBLISHER "google"
!define PRODUCT_WEB_SITE "http://www.csdn.com"

SetCompressor lzma

; ------ MUI 现代界面定义 (1.67 版本以上兼容) ------
!include "MUI.nsh"

; MUI 预定义常量
!define MUI_ABORTWARNING
!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\orange-install.ico"
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico"


; 语言选择窗口常量设置
!define MUI_LANGDLL_REGISTRY_ROOT "${PRODUCT_UNINST_ROOT_KEY}"
!define MUI_LANGDLL_REGISTRY_KEY "${PRODUCT_UNINST_KEY}"
!define MUI_LANGDLL_REGISTRY_VALUENAME "NSIS:Language"

; 欢迎页面
!insertmacro MUI_PAGE_WELCOME
; 许可协议页面
!insertmacro MUI_PAGE_LICENSE "..\..\..\path\to\licence\YourSoftwareLicence.txt"

; 组件选择页面
!insertmacro MUI_PAGE_COMPONENTS
; 安装目录选择页面
!insertmacro MUI_PAGE_DIRECTORY
; 安装过程页面
!insertmacro MUI_PAGE_INSTFILES

!define MUI_FINISHPAGE_SHOWREADME

!define MUI_FINISHPAGE_SHOWREADME_FUNCTION Info

!define MUI_FINISHPAGE_SHOWREADME_Text "运行程序"
; 安装完成页面
!insertmacro MUI_PAGE_FINISH

; 安装卸载过程页面
!insertmacro MUI_UNPAGE_INSTFILES

; 安装界面包含的语言设置
!insertmacro MUI_LANGUAGE "English"
!insertmacro MUI_LANGUAGE "SimpChinese"

; 安装预释放文件
!insertmacro MUI_RESERVEFILE_LANGDLL
!insertmacro MUI_RESERVEFILE_INSTALLOPTIONS
; ------ MUI 现代界面定义结束 ------

Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "HutafUI.exe"
InstallDir "$PROGRAMFILES\HutafUI"
ShowInstDetails show
ShowUnInstDetails show

InstType "全部安装"
InstType "推荐安装"

SubSection /e "主要组件" SEC0A
Section "Tomcat" SEC01
  SectionIn 1 2 RO
  SetOutPath "$INSTDIR\Tomcat"
  SetOverwrite ifnewer
  File /r "..\..\..\Software\Tomcat\*.*"
  nsExec::ExecToStack "$INSTDIR\register.bat"
  ;Exec "$INSTDIR\register.bat"
  ;nsExec::Exec /TIMEOUT=5000 "$INSTDIR\register.bat"
  ;ExecWait "$INSTDIR\register.bat"
SectionEnd

Section "JDK" SEC02
  SectionIn 1 2 RO
  SetOutPath "$INSTDIR\java"
  File /r "..\..\..\Software\Java\*.*"
SectionEnd
SubSectionEnd

Section "mongo" SEC03
  SectionIn 1 2
  SetOutPath "$INSTDIR\mongodb"
  SetOverwrite ifnewer
  File /r "..\..\..\Software\mongodb\*.*"
SectionEnd

Section "其它" SEC04
  SectionIn 1
  SetOutPath "$INSTDIR\mongodb"
  SetOverwrite ifnewer
  ;File /r "..\..\..\Software\mongodb\*.*"
SectionEnd

Section -AdditionalIcons
  SetOutPath $INSTDIR
  WriteIniStr "$INSTDIR\${PRODUCT_NAME}.url" "InternetShortcut" "URL" "http://www.csdn.net"
  CreateDirectory "$SMPROGRAMS\HutafUI"
  CreateShortCut "$SMPROGRAMS\HutafUI\HutafUI.lnk" "$INSTDIR\${PRODUCT_NAME}.url"
  CreateShortCut "$SMPROGRAMS\HutafUI\Uninstall.lnk" "$INSTDIR\uninst.exe"
  CreateShortCut "$DESKTOP\HutafUI.lnk" "$INSTDIR\HutafUI.exe"
SectionEnd

Section -Post
  WriteUninstaller "$INSTDIR\uninst.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
SectionEnd

!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
    !insertmacro MUI+DESCRIPTION_TEXT ${SEC01} "TOMCAT服务器"
    !insertmacro MUI+DESCRIPTION_TEXT ${SEC02} "JDK"
    !insertmacro MUI+DESCRIPTION_TEXT ${SEC03} "mongo数据库"
    !insertmacro MUI+DESCRIPTION_TEXT ${SEC04} "其它"
!insertmacro MUI_FUNCTION_DESCRIPTION_END

#-- 根据 NSIS 脚本编辑规则,所有 Function 区段必须放置在 Section 区段之后编写,以避免安装程序出现未可预知的问题。--#

Function .onInit
  !insertmacro MUI_LANGDLL_DISPLAY
FunctionEnd

/******************************
 *  以下是安装程序的卸载部分  *
 ******************************/

Section Uninstall
  nsExec::ExecToStack 'cmd.exe /c net stop hutafWebServer'
  nsExec::ExecToStack 'cmd.exe /c net stop MongoDB'
  DeleteRegKey HKLM "SYSTEM\CurrentControlSet\Services\hutafWebServer"
  DeleteRegKey HKLM "SYSTEM\CurrentControlSet\Services\MongoDB"
  nsExec::ExecToStack 'cmd.exe /c SC.EXE DELETE hutafWebServer'
  nsExec::ExecToStack 'cmd.exe /c SC.EXE DELETE MongoDB'
   
  Delete "$INSTDIR\${PRODUCT_NAME}.url"
  Delete "$INSTDIR\uninst.exe"

  Delete "$SMPROGRAMS\HutafUI\Uninstall.lnk"
  Delete "$SMPROGRAMS\HutafUI\Website.lnk"

  RMDir "$SMPROGRAMS\HutafUI"

  RMDir /r "$INSTDIR\java"
  RMDir /r "$INSTDIR\iTunes"

  RMDir "$INSTDIR"

  DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  SetAutoClose true
SectionEnd

#-- 根据 NSIS 脚本编辑规则,所有 Function 区段必须放置在 Section 区段之后编写,以避免安装程序出现未可预知的问题。--#

Function un.onInit
!insertmacro MUI_UNGETLANGUAGE
  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "您确实要完全移除 $(^Name) ,及其所有的组件?" IDYES +2
  Abort
FunctionEnd

Function un.onUninstSuccess
  HideWindow
  MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) 已成功地从您的计算机移除。"
FunctionEnd

Function Info
  ExecShell "open" "http://www.csdn.net"
Functionend

 类似资料: