0%

NSIS脚本基础Demo

NSIS(Nullsoft Scriptable Install System)是一个开源的 Windows 系统下安装程序制作程序。它提供了安装、卸载、系统设置、文件解压缩等功能。如其名字所指出的那样,NSIS 是通过它的脚本语言来描述安装程序的行为和逻辑的。NSIS 的脚本语言和通常的编程语言有类似的结构和语法,但它是为安装程序这类应用所设计的。 NSIS应该是Windows下使用最为广泛的安装程序制作工具了,可以说长成下面这样的安装程序基本就可以判断是用NSIS制作的了。Electron生成的安装包也是基于NSIS生成的,用NSIS制作的也可以用WinRAR 好压等压缩软件直接打开来查看其中的文件。

如果在NSIS脚本中声明了强制压缩,那么在启动安装程序时还会先进行解压操作,就像下图这样,本文中提供的Demo脚本中也强制启用了压缩。

好记心不如烂笔头,最后在这里记录一个包含常用基础功能的NSIS脚本Demo,Demo不一定能直接跑起来,但都有注释,也蛮容易看懂的。安装NSIS,写完脚本后,和安装文件丢在一起,右键“Compile NSIS Script”就能生成安装包了。另外注意这里用了unicode,脚本本身的编码格式需要是“UCS-2 Little Endian格式”。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
;强制压缩
SetCompressor /SOLID LZMA
SetCompress force

;基本定义
!define PRODUCT_NAME "TestApp"
!define PRODUCT_VERSION "1.0.0"
!define PRODUCT_COMPANY "Test Company"

;文件属性
VIProductVersion "${PRODUCT_VERSION}"
VIAddVersionKey FileDescription "${PRODUCT_NAME} Installer"
VIAddVersionKey FileVersion "${PRODUCT_VERSION}"
VIAddVersionKey ProductName "${PRODUCT_NAME} Installer"
VIAddVersionKey ProductVersion "${PRODUCT_VERSION}"
VIAddVersionKey CompanyName "${PRODUCT_COMPANY}"
VIAddVersionKey LegalCopyright "Copyright (C) 2015-2020 ${PRODUCT_COMPANY}"

;使用现代外观
;--------------------------------
;Include Modern UI

!include "MUI2.nsh"

;输出文件名,默认安装目录,是否要求管理员权限运行
;--------------------------------
;General

;Properly display all languages (Installer will not work on Windows 95, 98 or ME!)
Unicode true

;Name and file
Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "TestInstaller v${PRODUCT_VERSION}.exe"

;Default installation folder
InstallDir "$PROGRAMFILES\TestCompany\TestApp"

;Get installation folder from registry if available
;InstallDirRegKey HKCU "Software\Modern UI Test" ""

;Request Admin privileges for Windows Vista
RequestExecutionLevel admin

;--------------------------------
;Variables

;--------------------------------
;Interface Settings

!define MUI_ABORTWARNING

;Show all languages, despite user's codepage
!define MUI_LANGDLL_ALLLANGUAGES

; 页面设置,可指定安装完成后运行xxx;
;--------------------------------
;Pages

!insertmacro MUI_PAGE_WELCOME
;!insertmacro MUI_PAGE_LICENSE "${NSISDIR}\Docs\Modern UI\License.txt"
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES

!define MUI_FINISHPAGE_RUN "$INSTDIR\Test.exe"
!insertmacro MUI_PAGE_FINISH

!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES


; 多语言
;--------------------------------
;Languages

!insertmacro MUI_LANGUAGE "SimpChinese"
!insertmacro MUI_LANGUAGE "English"

;--------------------------------
;Reserve Files

;If you are using solid compression, files that are required before
;the actual installation should be stored first in the data block,
;because this will make your installer start faster.

!insertmacro MUI_RESERVEFILE_LANGDLL

; "Section"定义,可以理解为实际的安装过程
;--------------------------------
;Installer Sections
Section "!Test" SecTest

SectionIn RO

SetOutPath "$INSTDIR"

; 拷贝文件和文件夹
;ADD YOUR OWN FILES HERE...
File /r "main"

File msvcp120.dll
File msvcr120.dll
File Updater.exe

;执行命令
Exec '"$INSTDIR\Updater.exe" -deleteservice'

;写注册表,自启动
;Regedit
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Run" "${PRODUCT_REG_AUTORUN_KEY}" '"$INSTDIR\${PRODUCT_NAME}.exe" /min'

;删注册表
;Clean the previous uninstall information
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${XXXX}"

;写注册表,可以在控制面板中添加卸载项
;Add uninstall information
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "DisplayName" "${PRODUCT_NAME}"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "UninstallString" "$\"$INSTDIR\uninstall.exe$\""
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}" "Publisher" "${PRODUCT_COMPANY}"

;桌面图标
;shortcut
SetShellVarContext all
CreateShortCut "$DESKTOP\Test.lnk" "$INSTDIR\${PRODUCT_NAME}.exe"

;生成卸载程序
;Create uninstaller
WriteUninstaller "$INSTDIR\Uninstall.exe"

SectionEnd

;安装/卸载程序启动时的执行的“函数”,这里FindProcDLL是需要另外安装的NSIS插件
;--------------------------------
;Installer Functions
;安装程序启动时执行的函数
Function .onInit

FindProcDLL::FindProc "Test.exe"
IntCmp $R0 1 0 TestNotRunning
MessageBox MB_OK|MB_ICONEXCLAMATION "Please close Test first" /SD IDOK
Abort
TestNotRunning:

!insertmacro MUI_LANGDLL_DISPLAY

FunctionEnd

;卸载程序启动时执行的函数
Function un.onInit
FunctionEnd

;多国语言文案
;--------------------------------
;Descriptions

;Language strings
LangString DESC_SecTest ${LANG_SIMPCHINESE} "Test主程序"
LangString DESC_SecTest ${LANG_ENGLISH} "Test main program"

;Assign language strings to sections
!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
!insertmacro MUI_DESCRIPTION_TEXT ${SecTest} $(DESC_SecTest)
!insertmacro MUI_FUNCTION_DESCRIPTION_END

;卸载“section”的实现
;--------------------------------
;Uninstaller Section
Section "Uninstall"
;uninstall Service
Exec '"$INSTDIR\Test.exe" -uninstall'

;删除文件
;ADD YOUR OWN FILES HERE...
RMDir /r "$INSTDIR\main"

Delete "$INSTDIR\msvcp120.dll"
Delete "$INSTDIR\msvcr120.dll"

;删除注册表
;Clean AutoRun
DeleteRegValue HKLM "Software\Microsoft\Windows\CurrentVersion\Run\" "${PRODUCT_REG_AUTORUN_KEY}"

;删除卸载项
;Clean uninstall information
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"

;删除桌面快捷方式
SetShellVarContext all
Delete "$DESKTOP\Test.lnk"

;删除卸载程序本身
Delete "$INSTDIR\Uninstall.exe"

RMDir "$INSTDIR"

SectionEnd