禁默安装运行软件所需的运行环境(Vc_redist, bonjoursdk)
思路:
一般运行环境都支持禁默,软件包安装后会写入注册表。检测注册表是否有相关的环境注册表项,如果没有安装,存在略过。
查找运行环境版本号对应的注册表项
找到运行环境安装包的版本号项,一般位于注册表中"HKLM\SOFTWARE\WOW6432Node"中,可以安装后通过版本号查询得到该注册表项,也可以使用工具对比安装前后的区别得到该注册表项。例如:vc_redist位于
SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall
Bonjoursdk 位于SOFTWARE\WOW6432Node\Apple Inc.\Bonjour中
由于依赖运行环境千差万别,下面将详细描述一般性的方法。下面以bonjoursdksetup.x64_3.0.0.10.exe 作为例子进行说明。
使用 Regshot建立快照A->全部注册表
Regshot下载地址
http://dx.198424.com/soft1/regshot.zip
手动安装运行环境,默认下一步即可
使用 regshot 建立快照B
根据对比文件,运行regedit找到安装运行环境的版本号对应的项目
最后再删除安装的运行环境,验证其正确性。
编写安装运行环境的NSIS脚本
NSIS 安装环境依赖的脚本
环境安装包解压到临时文件夹,检测安装包是否已经安装,如果没有使用禁默安装,代码如下:
!macro InstallExeDepand RegKey SubRegKey DepandExeName Arg
File /oname=$PLUGINSDIR\${DepandExeName} ".\depands\${DepandExeName}"
Push $R0
ClearErrors
;这里检测 该版本的运行时版本号是否存在
ReadRegStr $R0 HKLM "${RegKey}" "${SubRegKey}"
;
IfErrors 0 +2
; Exec "$PLUGINSDIR\${VcRedistName} /q" ;若不存在,执行静默安装
ExecWait "$PLUGINSDIR\${DepandExeName} ${Arg}" ; 调试安装
StrCpy $R0 "-1"
;MessageBox MB_OK "安装完毕"
pop $R0
!macroend
注意此脚本必须在模板《vimeo-template.nsi》中修改,否则每次修改完成后会新生成的脚本被覆盖。
安装依赖运行环境增加安装进度
注意安装运行环境的进度假定为7%
修改NSIS脚本模板 vimeo-template.nsi
# 安装环境依赖
Function InstallDepands
#!define REGVCPARENTPATH "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
!define REGVCPARENTPATH "SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\"
#Var /GLOBAL vcParentKey
#StrCpy $vcParentKey
# 安装vc runtime /q 安静模式
# 注意调用 发送进度不能超过 7%
!insertmacro InstallExeDepand "${REGVCPARENTPATH}{95ac1cfa-f4fb-4d1b-8912-7f9d5fbb140d}" "BundleVersion" "vc_redist.x64_2017.14.15.26706_95ac1.exe" "/q"
${UI_PLUGIN_NAME}::SetInstallStepDescription "Install : vc_redist.x64_2017.14.15.26706_95ac1.exe" 1.11
!insertmacro InstallExeDepand "${REGVCPARENTPATH}{7e9fae12-5bbf-47fb-b944-09c49e75c061}" "BundleVersion" "vc_redist.x86_2017.14.15.26706_7e9fa.exe" "/q"
${UI_PLUGIN_NAME}::SetInstallStepDescription "Install : vc_redist.x86_2017.14.15.26706_7e9fa.exe" 2.22
!insertmacro InstallExeDepand "${REGVCPARENTPATH}{c342d939-0a78-4259-be5b-11e4860e8bb7}" "BundleVersion" "vc_redist_x64_2013.12.0.40660_c342d.exe" "/q"
${UI_PLUGIN_NAME}::SetInstallStepDescription "Install : vc_redist_x64_2013.12.0.40660_c342d.exe" 3.33
!insertmacro InstallExeDepand "${REGVCPARENTPATH}{48e8f45a-c463-4b50-9c55-e7e638ec02ca}" "BundleVersion" "vc_redist_x86_2013.12.0.40660_48e8f.exe" "/q"
${UI_PLUGIN_NAME}::SetInstallStepDescription "Install : vc_redist_x86_2013.12.0.40660_48e8f.exe" 4.44
# 安装 bonjour /quiet 安静模式
!insertmacro InstallExeDepand "SOFTWARE\WOW6432Node\Apple Inc.\Bonjour" "Version" "bonjoursdksetup.x64_3.0.0.10.exe" "/quiet"
${UI_PLUGIN_NAME}::SetInstallStepDescription "Install : bonjoursdksetup.x64_3.0.0.10.exe" 5.55
FunctionEnd
修改NsisScriptGenerate.py
安装进度是NsisScriptGenerate.py生成,所以必须修改此脚本。
修改后的代码NsisScriptGenerate如下:
def do_main(nsis_script_template_path):
global g_root_dir
global g_extract_file_cmd_total
global g_cur_script_index
if g_root_dir[len(g_root_dir) - 1] == '\\':
g_root_dir = g_root_dir[:len(g_root_dir) - 1]
nsis_cmd_num_statistics(g_root_dir)
print ("file total: " + str(g_extract_file_cmd_total) + ", dir total: " + str(g_create_dir_cmd_total))
# 为了安装环境依赖显示进度条 使用下面规则,令环境依赖为总进度的7%
# 推算出总共文件的数量和依赖环境后进度的数量 g_cur_script_index
oldtotal = g_extract_file_cmd_total
g_extract_file_cmd_total = round(g_extract_file_cmd_total*100/(100-7));
g_cur_script_index = g_extract_file_cmd_total - oldtotal;
print ("calc file total: " + str(g_extract_file_cmd_total) + ", dir total: " + str(g_create_dir_cmd_total))
generate_nsis_script(g_root_dir)
g_insert_nsis_script_list.append(' ${UI_PLUGIN_NAME}::SetInstallStepDescription "Finished" 100')
f = open(nsis_script_template_path, "r", encoding='UTF-8')
all_nsis_script_lines = []
cur_line_index = -1
insert_line_index = -1
for s in f.readlines():
all_nsis_script_lines.append(s)
cur_line_index += 1
if s.find('Function ___ExtractFiles') != -1:
insert_line_index = cur_line_index + 3
f.close()
if insert_line_index == -1:
return
for s in g_insert_nsis_script_list:
all_nsis_script_lines.insert(insert_line_index, s)
insert_line_index += 1
nsis_script_path = nsis_script_template_path[:len(nsis_script_template_path) - len('-template.nsi')]
nsis_script_path += '.nsi'
f = open(nsis_script_path, "w")
all_script = '\n'.join(all_nsis_script_lines)
f.write(all_script)
f.close()