【NSIS】nsis制作win exe部署包,自定义页面读取用户输入的ip作为程序配置

test_app.nsi内容样例如下:

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

; 安装程序初始定义常量
!define PRODUCT_NAME "test_app"
!define PRODUCT_VERSION "1.0"
!define PRODUCT_PUBLISHER "xxx科技有限责任公司"
!define PRODUCT_WEB_SITE "http://www.xxxx.com"
!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\test_app.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"
!include nsDialogs.nsh
!include "StrFunc.nsh"
${StrStr}
${StrStrAdv}
; MUI 预定义常量
!define MUI_ABORTWARNING
!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\agent.ico"
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico"

; 欢迎页面
!insertmacro MUI_PAGE_WELCOME

; 安装目录选择页面
!insertmacro MUI_PAGE_DIRECTORY

; 组件选择页面
!insertmacro MUI_PAGE_COMPONENTS
;自定义页面
Page custom PageInitFunc PageLeaveFunc ""


; 安装过程页面
!insertmacro MUI_PAGE_INSTFILES
; 安装完成页面
!insertmacro MUI_PAGE_FINISH

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

; 安装界面包含的语言设置
!insertmacro MUI_LANGUAGE "SimpChinese"
; ------ MUI 现代界面定义结束 ------

Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "ScipAgentSetup.exe"
InstallDir "D:\app\test_app"
DirText "安装程序将把 $(^NameDA)安装到以下目录。要安装到另一个目录,请点击 [浏览(B)...] 并选择其他文件夹,注意路径中不要有空格。$\r$\n$\r$\n$_CLICK"
InstallDirRegKey HKLM "${PRODUCT_UNINST_KEY}" "UninstallString"
ShowInstDetails show
ShowUnInstDetails show
BrandingText "test_app安装"
Var YQ_NODE_GUEST_IP
Var YQ_NODE_GUEST_IP_GUSET
Var SHOW_PAGE  # 根据客户勾选情况决定显示自定义页面(节点配置信息)


Section "dotnet-6.0.5" SEC01
  InitPluginsDir
  ExecWait '"$EXEDIR\dotnet-hosting-6.0.5-win.exe" /S' $0
  ${If} $0 <> 0
		MessageBox mb_iconstop "安装.net 6运行时失败!"
		Abort
  ${EndIf}
SectionEnd

Section "agent" SEC02
	SetOutPath "$INSTDIR"
  SetOverwrite ifnewer
  File /r "scip_main"
  File "test_app.exe"
  File "test_app.xml"
  ;File "install.ini" 不要打包到setUp程序中,而是根据exe目录下的install.ini动态读取
  execWait 'cp $EXEDIR\install.ini $INSTDIR'
  File "setConfig.bat"
  nsExec::Exec '"$INSTDIR\setConfig.bat" $YQ_NODE_GUEST_IP' ;静默执行,不弹窗
  pop $0
	${If} $0 <> 0
	  MessageBox mb_iconstop "配置修改失败!$0"
  ${EndIf}

  nsExec::Exec '$INSTDIR\test_app.exe install'
  pop $0
  ${If} $0 <> 0
	  MessageBox mb_iconstop "服务安装失败!$0"
  ${EndIf}

  CreateShortCut "$DESKTOP\restart.lnk" "$INSTDIR\test_app.exe" "restart"
  nsExec::Exec '$INSTDIR\test_app.exe start'
  pop $0
  ${If} $0 <> 0
	  MessageBox mb_iconstop "服务已安装但启动失败!$0"
  ${EndIf}

SectionEnd

Section -AdditionalIcons
  WriteIniStr "$INSTDIR\${PRODUCT_NAME}.url" "InternetShortcut" "URL" "${PRODUCT_WEB_SITE}"
SectionEnd

Section -Post
  WriteUninstaller "$INSTDIR\uninst.exe"
  WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\test_app.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\test_app.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

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

; 区段组件描述
!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
  !insertmacro MUI_DESCRIPTION_TEXT ${SEC01} "dotnet-6.0.5运行时环境"
  !insertmacro MUI_DESCRIPTION_TEXT ${SEC02} "agent主程序"
!insertmacro MUI_FUNCTION_DESCRIPTION_END

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

Section Uninstall
  Delete "$DESKTOP\restart.lnk"
  ExecWait "$INSTDIR\test_app.exe stop"  $0
  ${If} $0 <> 0
	MessageBox mb_iconstop "服务停止失败,请先手动停止test_app服务!$0"
	Abort
  ${EndIf}
  ExecWait "$INSTDIR\test_app.exe uninstall" $1
  ${If} $1 <> 0
	MessageBox mb_iconstop "服务卸载失败!$1"
	Abort
  ${EndIf}
  RMDir /r /REBOOTOK "$INSTDIR"

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




#-- 根据 NSIS 脚本编辑规则,所有 Function 区段必须放置在 Section 区段之后编写,以避免安装程序出现未可预知的问题。--#
Function .onInit
  StrCpy $SHOW_PAGE "show" ;设置默认显示自定义页面(输入配置页面)

	;获取客户端ip,如果能找到和主节点匹配的段,则默认设置该值,如果找不到,则取第一个ip
	Var /GLOBAL master_net ;主节点网段
	;取install.ini的第一个ip作为主节点ip
  ReadINIStr $0 $EXEDIR\install.ini "node" "YQ_NODE_INTERCOM_IPS"
  ${StrStrAdv} $1 $0 " " ">" "<" "0" "0" "0" ;(192.168.10.203 192.168.10.203 192.168.10.203) =>(192.168.10.203
  ${StrStrAdv} $0 $1 "(" ">" ">" "0" "0" "0" ;(192.168.10.203 ==> 192.168.10.203
  ${StrStrAdv} $master_net $0 "." "<" "<" "0" "0" "0" ;192.168.10.203 ==192.168.10 主节点网段

	Var /GLOBAL t_ip #如果没有找到符合条件的地址,就取第一个地址
  ip::get_ip
  Pop $0
  Loop:
	  Push $0
	  Call GetNextIp
		Pop $2 ;192.168.10.1
		Pop $1 ;下一个变量 192.168.10.2,192.168.10.3
		Pop $0 ;当前变量 192.168.10.1,192.168.10.2,192.168.10.3
		StrCmp $t_ip '' '' +2
			StrCpy $t_ip $2 ;记录第一个地址
		${StrStr} $3 $2 $master_net ;$2中包含主节点网段192.168,则复制给$3
	  StrCmp $3 '' Continue ''
      StrCpy $t_ip $2 ;$3有值,说明找到了符合条件的ip,存入$t_ip
		  goto ExitLoop
		Continue:
			StrCpy $0 $1 ;将下一个变量压入栈
		  StrCmp $0 '' ExitLoop Loop
  ExitLoop:
  StrCpy $YQ_NODE_GUEST_IP $t_ip
FunctionEnd

Function PageInitFunc
	StrCmp $SHOW_PAGE "show" 0 ExitFunc ;标记
	!insertmacro MUI_HEADER_TEXT "" "请输入配置信息"
	nsDialogs::Create 1018
	Pop $0

	${NSD_CreateLabel} 0 0 100% 12u "ip:"
	Pop $0

	${NSD_CreateText} 0 12u 93% 12u "$YQ_NODE_GUEST_IP"
	Pop $YQ_NODE_GUEST_IP_GUSET
	nsDialogs::Show
	ExitFunc:
FunctionEnd

Function PageLeaveFunc
  ${NSD_GetText} $YQ_NODE_GUEST_IP_GUSET $YQ_NODE_GUEST_IP
FunctionEnd


Function .onSelChange
  Push $0
    SectionGetFlags ${SEC02} $0  # 检测 SEC1 的选择状态,1为已勾选该组件
    IntOp $0 $0 & ${SF_selectED} # 只过滤勾选的状态,Checkbox 的状态可能包含多位
    ;如果为 1 则设置显示自定义页面
    IntCmp $0 ${SF_selectED} showpage
      StrCpy $SHOW_PAGE "" # 设置不显示自定义页面
      Goto done
    showpage:
      StrCpy $SHOW_PAGE "show" # 设置显示自定义页面
    done:
  Pop $0
FunctionEnd




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



; Function GetNextIp
; input: head of stack
; format: 'ip1;ip2;ip3;ip4;'
; output: 'ip1' head of stack
;         'ip2;ip3;ip4;' second entry of stack

Function GetNextIp
  Exch $0
  Push $1
  Push $2
  Strcpy $2 0             ; Counter
  Loop:
    IntOp $2 $2 + 1
    StrCpy $1 $0 1 $2
    StrCmp $1 '' ExitLoop
    StrCmp $1 ';' '' Loop
    StrCpy $1 $0 $2       ; IP-address
    IntOp $2 $2 + 1
    StrCpy $0 $0 '' $2    ; Remaining string
  ExitLoop:
  Pop $2
  Push $0
  Exch 2
  Pop $0
  Exch $1
FunctionEnd

最终效果如下:
【NSIS】nsis制作win exe部署包,自定义页面读取用户输入的ip作为程序配置_第1张图片

你可能感兴趣的:(测试,部署,nsis)