完整安装包


在制作windows平台安装包之前,涉及到的electron.exe图标资源的更换、应用的asar打包并放置在electron的resources目录下,同时保证asar包名称为app.asar。

在windows平台下制作安装包的工具有很多,暂时介绍NSIS工具来制作安装包,我们可以在其官网下载打包的NSIS工具 ,对应NSIS打包工具的制作还有个HM NSIS Edit工具。

NSIS环境配置

  • 下载NSIS的编译程序
    nsis3.01是最新版本,可以从官方下载

http://sourceforge.net/projects/nsis/files/NSIS%202/2.46/nsis-2.46-setup.exe/download

或从csdn下载

http://download.csdn.net/download/signjing/4461091

制作nsis安装包,至少需要一个编译程序,它将nsis脚本转化为安装包。NSIS安装目录下的makensis.exe是实际意义上的编译程序,它有一个可3种方式加载脚本的图形化界面,非常易于使用。安装了NSIS,要创建一个安装程序,可以拷贝脚本到文本编辑器中,保存成.nsi扩展名的文件,加载到makensis编译程序中进行编译。

  • 下载NSIS的IDE
    NSIS开发环境在这里先只介绍HM NIS EDIT,初期的开发环境也将以HM NIS EDIT为主,HM NIS EDIT 2.0.3是最好的Nullsoft脚本安装系统(NSIS)开源脚本编辑器。

nis edit 2.0.3是最新版,可以从官方下载

http://prdownloads.sourceforge.net/hmne/nisedit2.0.3.exe?download

或从csdn下载

http://download.csdn.net/download/signjing/4461120

  • 编译环境
    可以依赖HM NIS EDIT来开发和编译脚本,也可以在控制台用makensis.exe来编译脚本,两种方式都可以,但是建议用NIS EDIT来开发和编译脚本。如果不需要开发,仅仅只是编译脚本就不需要安装NIS EDIT了,只需要在命令行下用makensis.exe来编译脚本。
  1. makensis /D参数名=参数值 ... NSIS脚本名称.nsi

NSIS脚本制作

在完整安装包制作过程的脚本中有如下功能点:

  • windows 7平台检测,因为electron只支持windows 7以上的平台,所以安装包脚本能够检测windows 7以下平台,则提示用户退出安装。
  1. Function .onInit
  2. ${nsProcess::KillProcess} "Titan.exe" $R2
  3. Sleep 2000
  4. ${If} ${AtLeastWin7}
  5. DetailPrint "OS版本为: windows 7+"
  6. ${Else}
  7. MessageBox MB_OK "本程序只能安装在 Windows 7 版本以上的操作系统!"
  8. Abort
  9. ${EndIf}
  10. FunctionEnd
  • 注册表里面写入安装目录
    因为在自动更新过程中需要用到被更新的程序所在的目录,所以在完整安装时就需要写入到注册表
  1. !define PRODUCT_NAME "Titan"
  2. !define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\${PRODUCT_NAME}.exe"
  3. Section -Post
  4. WriteUninstaller "$INSTDIR\uninst.exe"
  5. WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "Path" "$INSTDIR"
  6. WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\${PRODUCT_NAME}.exe"
  7. WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
  8. WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe"
  9. WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\${PRODUCT_NAME}.exe"
  10. WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
  11. WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
  12. WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
  13. SectionEnd

 完整安装包  - 图1

  • 对程序目录进行压缩成安装包
    对文件夹所有文件进行压缩,同时创建快捷方式,其中参数${PRODUCT_DIR}为传入进来的electron文件夹的路径
  1. Section "MainSection" SEC01
  2. SetOutPath "$INSTDIR"
  3. SetOverwrite on
  4. File /r "${PRODUCT_DIR}\*.*"
  5. CreateDirectory "$SMPROGRAMS\${PRODUCT_NAME}"
  6. CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk" "$INSTDIR\${PRODUCT_NAME}.exe"
  7. CreateShortCut "$DESKTOP\${PRODUCT_NAME}.lnk" "$INSTDIR\${PRODUCT_NAME}.exe"
  8. SectionEnd

添加卸载快捷方式

  1. Section -AdditionalIcons
  2. WriteIniStr "$INSTDIR\${PRODUCT_NAME}.url" "InternetShortcut" "URL" "${PRODUCT_WEB_SITE}"
  3. CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\Website.lnk" "$INSTDIR\${PRODUCT_NAME}.url"
  4. CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\Uninstall.lnk" "$INSTDIR\uninst.exe"
  5. SectionEnd
  • 程序卸载
    删除文件夹下所有文件,同时删除快捷方式和注册表里面的信息。
  1. Section Uninstall
  2. SetShellVarContext current
  3. RMDir /r "$INSTDIR"
  4. Delete "$SMPROGRAMS\${PRODUCT_NAME}\Uninstall.lnk"
  5. Delete "$SMPROGRAMS\${PRODUCT_NAME}\Website.lnk"
  6. Delete "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk"
  7. Delete "$SMPROGRAMS\${PRODUCT_NAME}"
  8. RMDir /r "$SMPROGRAMS\${PRODUCT_NAME}"
  9. Delete "$DESKTOP\${PRODUCT_NAME}.lnk"
  10. DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  11. DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}"
  12. SetAutoClose true
  13. SetShellVarContext all
  14. RMDir /r "$INSTDIR"
  15. Delete "$SMPROGRAMS\${PRODUCT_NAME}\Uninstall.lnk"
  16. Delete "$SMPROGRAMS\${PRODUCT_NAME}\Website.lnk"
  17. Delete "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk"
  18. Delete "$SMPROGRAMS\${PRODUCT_NAME}"
  19. RMDir /r "$SMPROGRAMS\${PRODUCT_NAME}"
  20. Delete "$DESKTOP\${PRODUCT_NAME}.lnk"
  21. DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  22. DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}"
  23. SetAutoClose true
  24. SectionEnd

最后用命令行来打包,下面是一条打包的测试命令:

  1. makensis.exe /DPRODUCT_VERSION="6.0" /DPRODUCT_DIR="E:/electron" Titan_Setup.nsi

附录

完整的安装包打包脚本示例:

  1. ; HM NIS Edit Wizard helper defines
  2. !define PRODUCT_NAME "Titan"
  3. ;!define PRODUCT_VERSION "6.0"
  4. !define PRODUCT_PUBLISHER "erayt, Inc."
  5. !define PRODUCT_WEB_SITE "http://www.erayt.com"
  6. !define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\${PRODUCT_NAME}.exe"
  7. !define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
  8. !define PRODUCT_UNINST_ROOT_KEY "HKLM"
  9. ; MUI 1.67 compatible ------
  10. !include "MUI.nsh"
  11. !include "nsProcess.nsh"
  12. !include "WinVer.nsh"
  13. ; MUI Settings
  14. !define MUI_ABORTWARNING
  15. !define MUI_ICON "logo.ico"
  16. !define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico"
  17. ; Welcome page
  18. !insertmacro MUI_PAGE_WELCOME
  19. ; License page
  20. ;!insertmacro MUI_PAGE_LICENSE "c:\path\to\licence\YourSoftwareLicence.txt"
  21. ; Directory page
  22. !insertmacro MUI_PAGE_DIRECTORY
  23. ; Instfiles page
  24. !insertmacro MUI_PAGE_INSTFILES
  25. ; Finish page
  26. !define MUI_FINISHPAGE_RUN "$INSTDIR\${PRODUCT_NAME}.exe"
  27. !insertmacro MUI_PAGE_FINISH
  28. ;!define MUI_CUSTOMFUNCTION_GUIINIT myGuiInit
  29. ; Uninstaller pages
  30. !insertmacro MUI_UNPAGE_INSTFILES
  31. ; Language files
  32. !insertmacro MUI_LANGUAGE "English"
  33. ; MUI end ------
  34. Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
  35. OutFile "Setup.exe"
  36. InstallDir "$PROGRAMFILES\${PRODUCT_NAME}"
  37. InstallDirRegKey HKLM "${PRODUCT_DIR_REGKEY}" ""
  38. ShowInstDetails show
  39. ShowUnInstDetails show
  40. Function .onInit
  41. ${nsProcess::KillProcess} "${PRODUCT_NAME}.exe" $R2
  42. Sleep 2000
  43. ${If} ${AtLeastWin7}
  44. DetailPrint "OS版本为: windows 7+"
  45. ${Else}
  46. MessageBox MB_OK "本程序只能安装在 Windows 7 版本以上的操作系统!"
  47. Abort
  48. ${EndIf}
  49. FunctionEnd
  50. Section "MainSection" SEC01
  51. SetOutPath "$INSTDIR"
  52. SetOverwrite on
  53. File /r "${PRODUCT_DIR}\*.*"
  54. CreateDirectory "$SMPROGRAMS\${PRODUCT_NAME}"
  55. CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk" "$INSTDIR\${PRODUCT_NAME}.exe"
  56. CreateShortCut "$DESKTOP\${PRODUCT_NAME}.lnk" "$INSTDIR\${PRODUCT_NAME}.exe"
  57. SectionEnd
  58. Section -AdditionalIcons
  59. WriteIniStr "$INSTDIR\${PRODUCT_NAME}.url" "InternetShortcut" "URL" "${PRODUCT_WEB_SITE}"
  60. CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\Website.lnk" "$INSTDIR\${PRODUCT_NAME}.url"
  61. CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}\Uninstall.lnk" "$INSTDIR\uninst.exe"
  62. SectionEnd
  63. Section -Post
  64. WriteUninstaller "$INSTDIR\uninst.exe"
  65. WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "Path" "$INSTDIR"
  66. WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\${PRODUCT_NAME}.exe"
  67. WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
  68. WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe"
  69. WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\${PRODUCT_NAME}.exe"
  70. WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
  71. WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
  72. WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
  73. SectionEnd
  74. Function un.onUninstSuccess
  75. HideWindow
  76. MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) 已成功地从你的计算机移除。"
  77. FunctionEnd
  78. Function un.onInit
  79. MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "你确实要完全移除 $(^Name) ,其及所有的组件?" IDYES +2
  80. Abort
  81. FunctionEnd
  82. Section Uninstall
  83. SetShellVarContext current
  84. RMDir /r "$INSTDIR"
  85. Delete "$SMPROGRAMS\${PRODUCT_NAME}\Uninstall.lnk"
  86. Delete "$SMPROGRAMS\${PRODUCT_NAME}\Website.lnk"
  87. Delete "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk"
  88. Delete "$SMPROGRAMS\${PRODUCT_NAME}"
  89. RMDir /r "$SMPROGRAMS\${PRODUCT_NAME}"
  90. Delete "$DESKTOP\${PRODUCT_NAME}.lnk"
  91. DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  92. DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}"
  93. SetAutoClose true
  94. SetShellVarContext all
  95. RMDir /r "$INSTDIR"
  96. Delete "$SMPROGRAMS\${PRODUCT_NAME}\Uninstall.lnk"
  97. Delete "$SMPROGRAMS\${PRODUCT_NAME}\Website.lnk"
  98. Delete "$SMPROGRAMS\${PRODUCT_NAME}\${PRODUCT_NAME}.lnk"
  99. Delete "$SMPROGRAMS\${PRODUCT_NAME}"
  100. RMDir /r "$SMPROGRAMS\${PRODUCT_NAME}"
  101. Delete "$DESKTOP\${PRODUCT_NAME}.lnk"
  102. DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  103. DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}"
  104. SetAutoClose true
  105. SectionEnd