软件安装包制作工具 NSIS (nullsoft scriptable install system)

前言

软件开发,最后还是免不了要发布,要做个安装,无奈 Revit 是个桌面软件,因此插件也只能是桌面软件的一部分。

NSIS

网上资料就不说了,本来想快速上手,结果没找到合适的。下载中文文档居然都要积分或者收费,也是醉了。直接去下载因为原版安装:https://sourceforge.net/projects/nsis/
在这里插入图片描述
上图是安装完成之后的界面,直接点击“Documentation” 下面的 “Example scripts”。直接从例子开始学习,效率最高。

example1.nsi

这是一个最简单的例子。
关键点:Page
实际上,安装程序会按照页面的顺序进行显示。

; Pages
Page directory
Page instfiles

页面: directory
在这里插入图片描述
页面:instfiles
在这里插入图片描述
脚本:

; example1.nsi
;
; 这是一个最简单的安装脚本,所有的设置都是默认设置。安装程序会弹出一个对话框询问用户安装路径,
; 然后拷贝一份 example1.nsi 到对应的目录。

;--------------------------------

; 安装程序的名称
Name "Example1"

; 输出的文件
OutFile "example1.exe"

; 安装要求的用户权限
RequestExecutionLevel user

; 安装程序是否是 Unicode
Unicode True

; 默认安装路径
InstallDir $DESKTOP\Example1

;--------------------------------

; 安装页面

Page directory
Page instfiles

;--------------------------------

; 安装的内容
Section "" ; 没有组件页面,Section 的名称就不重要了

  ; 设置安装输出目录
  SetOutPath $INSTDIR
  
  ; 安装的文件
  File example1.nsi
  
SectionEnd ; 结束 section

example2.nsi

与 example1.nsi 比,多了:

  1. 注册表
  2. 组件页面
  3. 卸载页面

这个例子有三个页面:

; Pages

Page components
Page directory
Page instfiles

components 页面的内容:
在这里插入图片描述
对应的代码:

;--------------------------------

; 需要安装的组件
Section "Example2 (required)"

  SectionIn RO
  
  ; 设置安装输出目录
  SetOutPath $INSTDIR
  
  ; 要拷贝的文件
  File "example2.nsi"
  
  ; 写入安装相关注册表
  WriteRegStr HKLM SOFTWARE\NSIS_Example2 "Install_Dir" "$INSTDIR"
  
  ; 写入卸载相关注册表
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Example2" "DisplayName" "NSIS Example2"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Example2" "UninstallString" '"$INSTDIR\uninstall.exe"'
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Example2" "NoModify" 1
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Example2" "NoRepair" 1
  WriteUninstaller "$INSTDIR\uninstall.exe"
  
SectionEnd

; 可选组件,创建快捷方式
Section "Start Menu Shortcuts"

  CreateDirectory "$SMPROGRAMS\Example2"
  CreateShortcut "$SMPROGRAMS\Example2\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
  CreateShortcut "$SMPROGRAMS\Example2\Example2 (MakeNSISW).lnk" "$INSTDIR\example2.nsi" "" "$INSTDIR\example2.nsi" 0
  
SectionEnd

;--------------------------------

整体代码:

; example2.nsi
;
; 这个例子基于 example1.nsi,但它能够记住安装路径,这样卸载的时候就知道从哪里卸载了。

;--------------------------------

; 安装程序的名称
Name "Example2"

; 安装程序输出的文件
OutFile "example2.exe"

; 要求管理员权限才能运行安装程序
RequestExecutionLevel admin

; 安装程序是否是 Unicode
Unicode True

; 默认安装路径
InstallDir $PROGRAMFILES\Example2

; 把安装路径加到注册表
InstallDirRegKey HKLM "Software\NSIS_Example2" "Install_Dir"

;--------------------------------

; 页面

Page components
Page directory
Page instfiles

UninstPage uninstConfirm
UninstPage instfiles

;--------------------------------

; 需要安装的组件
Section "Example2 (required)"

  SectionIn RO
  
  ; 设置安装输出目录
  SetOutPath $INSTDIR
  
  ; 要拷贝的文件
  File "example2.nsi"
  
  ; 写入安装相关注册表
  WriteRegStr HKLM SOFTWARE\NSIS_Example2 "Install_Dir" "$INSTDIR"
  
  ; 写入卸载相关注册表
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Example2" "DisplayName" "NSIS Example2"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Example2" "UninstallString" '"$INSTDIR\uninstall.exe"'
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Example2" "NoModify" 1
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Example2" "NoRepair" 1
  WriteUninstaller "$INSTDIR\uninstall.exe"
  
SectionEnd

; 可选组件,创建快捷方式
Section "Start Menu Shortcuts"

  CreateDirectory "$SMPROGRAMS\Example2"
  CreateShortcut "$SMPROGRAMS\Example2\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
  CreateShortcut "$SMPROGRAMS\Example2\Example2 (MakeNSISW).lnk" "$INSTDIR\example2.nsi" "" "$INSTDIR\example2.nsi" 0
  
SectionEnd

;--------------------------------

; 卸载程序

Section "Uninstall"
  
  ; 清空注册表项
  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Example2"
  DeleteRegKey HKLM SOFTWARE\NSIS_Example2

  ; 删除文件
  Delete $INSTDIR\example2.nsi
  Delete $INSTDIR\uninstall.exe

  ; 如果有快捷方式也需要删除
  Delete "$SMPROGRAMS\Example2\*.*"

  ; 删除安装路径
  RMDir "$SMPROGRAMS\Example2"
  RMDir "$INSTDIR"

SectionEnd

File

文件操作,官方文档:

File something.exe ; 把文件 something.exe 拷贝到安装目录
File /a something.exe ; 把文件 something.exe 带 attribute 拷贝到安装目录
File *.exe ; 把所有.exe 拷贝到安装目录
File /r *.dat ; 把所有.dat 拷贝到安装目录,包括子目录里面的
File /r data ; 把所有data拷贝到安装目录,包括子目录里面的文件和文件夹
File /oname=temp.dat somefile.ext ; somefile.ext 拷贝重命名为 temp.dat
File /oname=$TEMP\temp.dat somefile.ext ; somefile.ext 拷贝重命名为 $TEMP 目录下的 temp.dat
File "/oname=$TEMP\name with spaces.dat" somefile.ext ; 文件名带空格需要双引号
File /nonfatal "a file that might not exist" ; 文件不存在,不要报error,warning 就可以了
File /r /x CVS myproject\*.* ; 把所有myproject文件夹下面的内容拷贝到安装目录,不要包含 CVS 文件或者文件夹
File /r /x *.res /x *.obj /x *.pch source\*.* ; 把所有source文件夹下面的内容拷贝到安装目录,不要包含 .res, .obj, .pch 文件
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极客BIM工作室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值