nsis脚本编写列子

  1. nsis 构建C/S服务的脚本,相关依赖脚本无法编译的可以删除
; -------- 需要手动修改与定义的常量 --------
#
!define SOFTWARE_NAME "**"  ;软件名称
!define SOFTWARE_OUTNAME "**"         ;安装包输出名称
!define APP_NAME "**" ;主程序名称
; ------ 需要手动修改与定义的常量结束 ------

; -------- 安装程序初始定义常量 ------
!define PRODUCT_NAME "${SOFTWARE_NAME}"
!define PRODUCT_VERSION "$%SvnRevision%"
!define PRODUCT_PUBLISHER "**公司"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"
!define THIS_SCRIPT "SetupScript.nsi"
!define OUT_FILE "${SOFTWARE_OUTNAME}.exe"
!include "x64.nsh"
SetCompressor lzma
; -------- 安装程序初始定义常量结束 ------

; ------ MUI 现代界面定义 ------
!include "MUI.nsh"
!define MUI_ABORTWARNING
!define MUI_ICON "logo.ico"
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico"
!insertmacro MUI_PAGE_WELCOME ; 欢迎页面
!insertmacro MUI_PAGE_LICENSE "License.txt" ; 许可协议页面
!insertmacro MUI_PAGE_LICENSE "更新说明.txt";更新说明页面
!insertmacro MUI_PAGE_DIRECTORY ; 安装目录选择页面
!insertmacro MUI_PAGE_COMPONENTS ; 组件选择页面
!insertmacro MUI_PAGE_INSTFILES ; 安装过程页面
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_UNPAGE_INSTFILES ; 安装卸载过程页面
!insertmacro MUI_LANGUAGE "SimpChinese" ; 安装界面包含的语言设置
!insertmacro MUI_RESERVEFILE_INSTALLOPTIONS ; 安装预释放文件
; ------ MUI 现代界面定义结束 ------

; ------- 变量初始化定义 ----------
AllowRootDirInstall false
RequestExecutionLevel admin
Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "${OUT_FILE}"
InstallDir "$PROGRAMFILES\${PRODUCT_NAME}"
InstallDirRegKey HKLM "${PRODUCT_UNINST_KEY}" "UninstallString"
ShowInstDetails show
BrandingText "**"
ShowUnInstDetails show
; ------- 变量初始化定义结束 ----------

; ------ 记录安装日志 -------------
Section "-LogSetOn"
  LogSet on
SectionEnd
; ------ 记录安装日志结束 -------------

; --------自定义头文件引用 -----------
!define ATINSTALL_FRAMEWORK45
!include "framework45.nsh"
!define MYSQL_USER
!include "mysql2.nsh"
; --------自定义头文件引用结束 -----------

; ---------文件释放与注册------------------
!define SECTION_01_PATH "exe输出路径"
!define SECTION_01_SOURCEPATH "exe编译后路径"

!define SECTION_02_PATH "exe输出路径"
!define SECTION_02_SOURCEPATH "exe编译后路径"

Section
;添加依赖项
  SetOutPath "$INSTDIR\公共文件"
	SetOverwrite ifnewer
	File /nonfatal /r /x "*.pdb" /x "*.svn"  "..\bin\release\Common\*.*"
	SetOverwrite on
SectionEnd

Section "服务名称" SEC01 
  SetOutPath "$INSTDIR\${SECTION_01_PATH}"
  SetOverwrite ifnewer
  File /nonfatal /r /x "*.pdb" /x "*.svn"  "${SECTION_01_SOURCEPATH}\*.*" 
SectionEnd

Section "服务名称" SEC02 
  SetOutPath "$INSTDIR\${SECTION_02_PATH}"
  SetOverwrite ifnewer
  File /nonfatal /r /x "*.pdb" /x "*.svn"  "${SECTION_02_SOURCEPATH}\*.*" 
SectionEnd

  CreateDirectory "$SMPROGRAMS\${PRODUCT_NAME}" 
  SetOutPath "$INSTDIR\${SECTION_04_PATH}"
  CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\服务名称.lnk" "$INSTDIR\${SECTION_04_PATH}\**.exe"
  CreateShortCut "$DESKTOP\服务名称.lnk" "$INSTDIR\${SECTION_04_PATH}\**.exe"
SectionEnd

!include "x64.nsh"

Section
${If} ${RunningX64}
ExecWait "$INSTDIR\Driver\DRVSETUP64.exe"
${Else}
ExecWait "$INSTDIR\Driver\SETUP.EXE"
${EndIf}
SectionEnd


; ---------文件释放与注册结束 --------------

; ---------快捷方式与注册表项 ---------------
Section -AdditionalIcons
  CreateDirectory "$SMPROGRAMS\${PRODUCT_NAME}" 
  SetOutPath "$INSTDIR"
  CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\Uninstall.lnk" "$INSTDIR\uninst.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}" "DisplayIcon" "$INSTDIR\${APP_NAME}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
SectionEnd
; ---------快捷方式与注册表项结束 ---------------

; ---------卸载流程------------------------
!macro DelFileByLog LogFile
  ifFileExists `${LogFile}` 0 +4
    Push `${LogFile}`
    Call un.DelFileByLog
    Delete `${LogFile}`
!macroend

Section Uninstall
	call un.OnKillProc
  !insertmacro DelFileByLog "$INSTDIR\install.log"
	MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "是否保留 $(^Name) 及其组件所保存的用户数据?" IDYES +4
	Call un.RemoveByDir
	Call un.ClearDir
	Call un.mysql
  DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  !insertmacro Mysql_Start
  SetAutoClose false
SectionEnd

Function un.OnKillProc
    KillProcDll::KillProc "服务.exe"      
FunctionEnd
Function un.RemoveByDir
    RMDir /r "$INSTDIR\${SECTION_01_PATH}"
    RMDir /r "$INSTDIR\${SECTION_03_PATH}"
    RMDir /r "$INSTDIR\${SECTION_04_PATH}"
    RMDir /r "$INSTDIR\${SECTION_05_PATH}"
    RMDir /r "$INSTDIR\${SECTION_06_PATH}"
    RMDir /r "$INSTDIR\Driver"
FunctionEnd

Function un.ClearDir
    RMDir /r "$INSTDIR\*"
    RMDir   "$INSTDIR"
    RMDir /r "$SMPROGRAMS\${PRODUCT_NAME}"
FunctionEnd

;停止服务
Function un.mysql
    DetailPrint "正在停止mysql服务......"
    nsExec::Exec 'cmd/c sc stop MySQL'
    DetailPrint "停止mysql服务成功......"
    RMDir /r "C:\Program Files (x86)\MySQL"
FunctionEnd

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


; ---------卸载流程结束--------------------

; ---------根据日志文件清理文件(禁止修改)------------
Function un.DelFileByLog
  Exch $R0
  Push $R1
  Push $R2
  Push $R3
  FileOpen $R0 $R0 r
  ${Do}
    FileRead $R0 $R1
    ${IfThen} $R1 == `` ${|} ${ExitDo} ${|}
    StrCpy $R1 $R1 -2
    StrCpy $R2 $R1 11
    StrCpy $R3 $R1 20
    ${If} $R2 == "File: wrote"
    ${OrIf} $R2 == "File: skipp"
    ${OrIf} $R3 == "CreateShortCut: out:"
    ${OrIf} $R3 == "created uninstaller:"
      Push $R1
      Push `"`
      Call un.DelFileByLog.StrLoc
      Pop $R2
      ${If} $R2 != ""
        IntOp $R2 $R2 + 1
        StrCpy $R3 $R1 "" $R2
        Push $R3
        Push `"`
        Call un.DelFileByLog.StrLoc
        Pop $R2
        ${If} $R2 != ""
          StrCpy $R3 $R3 $R2
          Delete /REBOOTOK $R3
        ${EndIf}
      ${EndIf}
    ${EndIf}
    StrCpy $R2 $R1 7
    ${If} $R2 == "Rename:"
      Push $R1
      Push "->"
      Call un.DelFileByLog.StrLoc
      Pop $R2
      ${If} $R2 != ""
        IntOp $R2 $R2 + 2
        StrCpy $R3 $R1 "" $R2
        Delete /REBOOTOK $R3
      ${EndIf}
    ${EndIf}
  ${Loop}
  FileClose $R0
  Pop $R3
  Pop $R2
  Pop $R1
  Pop $R0
FunctionEnd

Function un.DelFileByLog.StrLoc
  Exch $R0
  Exch
  Exch $R1
  Push $R2
  Push $R3
  Push $R4
  Push $R5
  StrLen $R2 $R0
  StrLen $R3 $R1
  StrCpy $R4 0
  ${Do}
    StrCpy $R5 $R1 $R2 $R4
    ${If} $R5 == $R0
    ${OrIf} $R4 = $R3
      ${ExitDo}
    ${EndIf}
    IntOp $R4 $R4 + 1
  ${Loop}
  ${If} $R4 = $R3
    StrCpy $R0 ""
  ${Else}
    StrCpy $R0 $R4
  ${EndIf}
  Pop $R5
  Pop $R4
  Pop $R3
  Pop $R2
  Pop $R1
  Exch $R0
FunctionEnd
;----------根据日志文件清理文件结束(禁止修改)--------

例子2

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

; 安装程序初始定义常量
!define PRODUCT_NAME "服务名称"
!define PRODUCT_VERSION "$%SvnRevision%"
!define PRODUCT_PUBLISHER "公司名称"
!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\Server.exe"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"

SetCompressor lzma

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

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

; 欢迎页面
!insertmacro MUI_PAGE_WELCOME
; 许可协议页面
!insertmacro MUI_PAGE_LICENSE "License.txt"
; 更新说明页面
!insertmacro MUI_PAGE_LICENSE "更新说明.txt"
; 安装目录选择页面
!insertmacro MUI_PAGE_DIRECTORY
; 安装过程页面
!insertmacro MUI_PAGE_INSTFILES
; 安装完成页面
;!define MUI_FINISHPAGE_RUN "$INSTDIR\Server\Server.exe"
!insertmacro MUI_PAGE_FINISH

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

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

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

Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "ServerInstall_$WCREV$.exe"
InstallDir "$PROGRAMFILES\服务名称"
InstallDirRegKey HKLM "${PRODUCT_UNINST_KEY}" "UninstallString"
ShowInstDetails show
ShowUnInstDetails show
BrandingText "服务名称安装程序"

!define ATINSTALL_VCREDIST2008_X86
!include "vcredist2008x86.nsh"

Section "MainSection" SEC01
  SetOverwrite ifnewer
  SetOutPath "$INSTDIR\Server"
  File /r /x "*.pdb" /x "*.lib" /x "*.exp" /x "*.svn" /x "*.temp" "..\Bin\Server\Release\*.*"
  CreateDirectory "$INSTDIR\Server\BData"
  CreateDirectory "$INSTDIR\Server\SpectrumAssessData"
  ExecWait '"$INSTDIR\Server\vcredist_x86.exe" /q'
  File "Server.ico"
  CreateDirectory "$SMPROGRAMS\服务名称"
  ;CreateShortCut "$SMPROGRAMS\服务名称\DeviceManager.lnk" "$INSTDIR\Server\Server.exe" "" "$INSTDIR\Server\Server.ico"
  ;CreateShortCut "$DESKTOP\DeviceManager.lnk" "$INSTDIR\Server\Server.exe" "" "$INSTDIR\Server\Server.ico"
  
  SetOutPath "$INSTDIR\ProcessMonitor"
  File /r "ProcessMonitor\*.*"
  
  CreateShortCut "$SMPROGRAMS\服务名称\Server.lnk" "$INSTDIR\ProcessMonitor\ProcessMonitor.exe" "-Debug"
  CreateShortCut "$DESKTOP\Server.lnk"  "$INSTDIR\ProcessMonitor\ProcessMonitor.exe" "-Debug"
  CreateShortCut "$SMSTARTUP\Server.lnk" "$INSTDIR\ProcessMonitor\ProcessMonitor.exe" "-Debug"
  
SectionEnd

Section -AdditionalIcons
  SetOutPath $INSTDIR
  CreateShortCut "$SMPROGRAMS\服务名称\Uninstall.lnk" "$INSTDIR\uninst.exe"
SectionEnd

Section -Post
  WriteUninstaller "$INSTDIR\uninst.exe"
  WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\Server.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}" "DisplayIcon" "$INSTDIR\Server.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
SectionEnd

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

Section Uninstall

	KillProcDll::KillProc "ProcessMonitor.exe"
	KillProcDll::KillProc "Server.exe"
	KillProcDll::KillProc "TimeClient.exe"
	KillProcDll::KillProc "TimeServer.exe"


    Delete "$DESKTOP\DeviceManager.lnk"
  

  RMDir /r "$INSTDIR\TimeSynch"
  RMDir /r "$INSTDIR\Server"
  RMDir /r "$INSTDIR\ProcessMonitor"
  RMDir "$INSTDIR"

 

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

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

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

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

你可能感兴趣的:(构建,测试)