当前位置:网站首页>II Simple NSIS installation package

II Simple NSIS installation package

2022-07-05 07:34:00 sukhoi27smk

New script : The wizard

Let's start with a simple NSIS Installation package start , Just like the front. (NSIS Introduce ) said , Although we have read the user manual , But there is no way to write the installation script , Then our editing tool HM NIS Edit That comes in handy .

open HM NIS Edit, Click menu “ file ”->“ New script : The wizard ”, There will be wizards that let us enter information step by step , Finally, according to our input .nsi Script files , Let's demonstrate step by step :

New script : Script files

Finally, we save the generated script file as MyApp.nsi, Open view script ( Check the items set in the screenshot above with the user manual , You will know the basic structure and basic instruction usage of a complete script file )

; Script generated by the HM NIS Edit Script Wizard.

; HM NIS Edit Wizard helper defines
!define PRODUCT_NAME "My application"
!define PRODUCT_VERSION "1.0"
!define PRODUCT_PUBLISHER "My company, Inc."
!define PRODUCT_WEB_SITE "http://www.mycompany.com"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"

; MUI 1.67 compatible ------
!include "MUI.nsh"

; MUI Settings
!define MUI_ABORTWARNING
!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\modern-install.ico"
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico"

; Welcome page
!insertmacro MUI_PAGE_WELCOME
; License page
!insertmacro MUI_PAGE_LICENSE "E:\ZZL\ADWeb\ Installation package production \Licence.txt"
; Directory page
!insertmacro MUI_PAGE_DIRECTORY
; Instfiles page
!insertmacro MUI_PAGE_INSTFILES
; Finish page
!insertmacro MUI_PAGE_FINISH

; Uninstaller pages
!insertmacro MUI_UNPAGE_INSTFILES

; Language files
!insertmacro MUI_LANGUAGE "SimpChinese"

; MUI end ------

Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "Setup.exe"
InstallDir "$PROGRAMFILES\My application"
ShowInstDetails show
ShowUnInstDetails show

Section "MainSection" SEC01
  SetOutPath "$INSTDIR"
  SetOverwrite try
  File "E:\ZZL\ADWeb\ Installation package production \Release\404.aspx"
  File "E:\ZZL\ADWeb\ Installation package production \Release\Default.aspx"
  File "E:\ZZL\ADWeb\ Installation package production \Release\Error.aspx"
  SetOutPath "$INSTDIR\Images"
  File "E:\ZZL\ADWeb\ Installation package production \Release\Images\1_close.png"
  File "E:\ZZL\ADWeb\ Installation package production \Release\Images\1_open.png"
  File "E:\ZZL\ADWeb\ Installation package production \Release\Images\add.png"
  SetOutPath "$INSTDIR\Scripts"
  File "E:\ZZL\ADWeb\ Installation package production \Release\Scripts\CommonScript.js"
  SetOutPath "$INSTDIR\SystemLog"
  File "E:\ZZL\ADWeb\ Installation package production \Release\SystemLog\LogStatisticsDetail.aspx"
  File "E:\ZZL\ADWeb\ Installation package production \Release\SystemLog\SystemLogList.aspx"
  File "E:\ZZL\ADWeb\ Installation package production \Release\SystemLog\SystemLogStatistics.aspx"
  SetOutPath "$INSTDIR"
  File "E:\ZZL\ADWeb\ Installation package production \Release\Web.config"
SectionEnd

Section -Post
  WriteUninstaller "$INSTDIR\uninst.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}" "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


Function un.onUninstSuccess
  HideWindow
  MessageBox MB_ICONINFORMATION|MB_OK "$(^Name)  Successfully removed from your computer ."
FunctionEnd

Function un.onInit
  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 " You really want to completely remove  $(^Name) , It and all its components ?" IDYES +2
  Abort
FunctionEnd

Section Uninstall
  Delete "$INSTDIR\uninst.exe"
  Delete "$INSTDIR\Web.config"
  Delete "$INSTDIR\SystemLog\SystemLogStatistics.aspx"
  Delete "$INSTDIR\SystemLog\SystemLogList.aspx"
  Delete "$INSTDIR\SystemLog\LogStatisticsDetail.aspx"
  Delete "$INSTDIR\Scripts\CommonScript.js"
  Delete "$INSTDIR\Images\add.png"
  Delete "$INSTDIR\Images\1_open.png"
  Delete "$INSTDIR\Images\1_close.png"
  Delete "$INSTDIR\Error.aspx"
  Delete "$INSTDIR\Default.aspx"
  Delete "$INSTDIR\404.aspx"

  RMDir "$INSTDIR\SystemLog"
  RMDir "$INSTDIR\Scripts"
  RMDir "$INSTDIR\Images"
  RMDir "$INSTDIR"

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

It is worth noting that , From the third step of the new script wizard, we choose... By default “ modern ”, The corresponding script introduces NSIS Modern user interface header files (!include "MUI.nsh"), The corresponding installation page is marked with !insertmacro MUI_PAGE_*** start ( Such as : The welcome page !insertmacro MUI_PAGE_WELCOME), For detailed instructions, please see NSIS Modern User Interface.

Installation package : Modern interface

Let's see the effect of the installation package , stay HM NIS Edit Interface , Click menu “NSIS”->“ Compile and run ”, The output window will show the compilation process , If there is no error , It will directly execute the compilation Setup.exe, The installation screenshot is as follows :

From the screenshot above, we can see that our installation packages share 5 A user interface , This corresponds to the script file :

; Welcome page
!insertmacro MUI_PAGE_WELCOME
; License page
!insertmacro MUI_PAGE_LICENSE "E:\ZZL\ADWeb\ Installation package production \Licence.txt"
; Directory page
!insertmacro MUI_PAGE_DIRECTORY
; Instfiles page
!insertmacro MUI_PAGE_INSTFILES
; Finish page
!insertmacro MUI_PAGE_FINISH

among “ Installation record page ” The instructions in the installation section will be actually executed , These instructions can extract files and read , Read and write registry 、INI Documents or ordinary documents , perform Powershell Script , Create directory , Create shortcuts and so on . If an installer has multiple components , Each component has its own code block , When the user chooses to install this component , Then the installer will execute the corresponding code , Then each component needs to correspond to a section , Specific information about optional components will be introduced in later chapters . We currently have only one installation section , Is to put the deployment package file into the installation directory :

Section "MainSection" SEC01
  SetOutPath "$INSTDIR"
  SetOverwrite try
  File "E:\ZZL\ADWeb\ Installation package production \Release\404.aspx"
  File "E:\ZZL\ADWeb\ Installation package production \Release\Default.aspx"
  File "E:\ZZL\ADWeb\ Installation package production \Release\Error.aspx"
  SetOutPath "$INSTDIR\Images"
  File "E:\ZZL\ADWeb\ Installation package production \Release\Images\1_close.png"
  File "E:\ZZL\ADWeb\ Installation package production \Release\Images\1_open.png"
  File "E:\ZZL\ADWeb\ Installation package production \Release\Images\add.png"
  Sleep 20000
  SetOutPath "$INSTDIR\Scripts"
  File "E:\ZZL\ADWeb\ Installation package production \Release\Scripts\CommonScript.js"
  SetOutPath "$INSTDIR\SystemLog"
  File "E:\ZZL\ADWeb\ Installation package production \Release\SystemLog\LogStatisticsDetail.aspx"
  File "E:\ZZL\ADWeb\ Installation package production \Release\SystemLog\SystemLogList.aspx"
  File "E:\ZZL\ADWeb\ Installation package production \Release\SystemLog\SystemLogStatistics.aspx"
  SetOutPath "$INSTDIR"
  File "E:\ZZL\ADWeb\ Installation package production \Release\Web.config"
SectionEnd

The information about the interface is set in the script wizard ( Such as application name 、 edition 、 company 、 Icon 、 Authorization information 、 Language, etc. ), It can be modified in the script :

; HM NIS Edit Wizard helper defines
!define PRODUCT_NAME "My application"
!define PRODUCT_VERSION "1.0"
!define PRODUCT_PUBLISHER "My company, Inc."
!define PRODUCT_WEB_SITE "http://www.mycompany.com"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"

; MUI 1.67 compatible ------
!include "MUI.nsh"

; MUI Settings
!define MUI_ABORTWARNING
!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\modern-install.ico"
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico"

There is also some information about the installation package itself ( Such as : name 、 Default installation path 、 Generate installation package name 、 Whether to display installation details, etc ):

Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "Setup.exe"
InstallDir "$PROGRAMFILES\ADWebManager"
InstallDirRegKey HKLM "${PRODUCT_DIR_REGKEY}" ""
ShowInstDetails show
ShowUnInstDetails show
BrandingText " MyCompany Ltd."

Pay attention to add a line of command BrandingText " Prolliance Ltd.", In this way, the user installation interface is not NSIS The information of , The comparison is as follows :

Modify the script : Standard interface

We use HM NIS Edit The wizard of created with “ Modern interface ” Installation package , The script file introduces NSIS Modern user interface header files (!include "MUI.nsh"), Now let's not introduce additional header files , Change to NSIS Try the standard interface , As we can see from the user manual , We can go through Page command ( Or more advanced settings such as PageEx). Our modified script file is as follows :

; Script generated by the HM NIS Edit Script Wizard.

; HM NIS Edit Wizard helper defines
!define PRODUCT_NAME "My application"
!define PRODUCT_VERSION "1.0"
!define PRODUCT_PUBLISHER "My company, Inc."
!define PRODUCT_WEB_SITE "http://www.mycompany.com"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"

; pages
PageEx license
       LicenseText " License page "
       LicenseData "E:\ZZL\ADWeb\ Installation package production \Licence.txt"
       ;LicenseForceSelection
PageExEnd
PageEx directory
       DirText " Directory selection page " " Destination folder " " Browse ..."
PageExEnd
Page instfiles
UninstPage uninstConfirm
UninstPage instfiles

; MUI end ------

Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "Setup.exe"
InstallDir "$PROGRAMFILES\My application"
ShowInstDetails show
ShowUnInstDetails show
BrandingText " MyCompany Ltd."

Section "MainSection" SEC01
  SetOutPath "$INSTDIR"
  SetOverwrite try
  File "E:\ZZL\ADWeb\ Installation package production \Release\404.aspx"
  File "E:\ZZL\ADWeb\ Installation package production \Release\Default.aspx"
  File "E:\ZZL\ADWeb\ Installation package production \Release\Error.aspx"
  SetOutPath "$INSTDIR\Images"
  File "E:\ZZL\ADWeb\ Installation package production \Release\Images\1_close.png"
  File "E:\ZZL\ADWeb\ Installation package production \Release\Images\1_open.png"
  File "E:\ZZL\ADWeb\ Installation package production \Release\Images\add.png"
  Sleep 20000
  SetOutPath "$INSTDIR\Scripts"
  File "E:\ZZL\ADWeb\ Installation package production \Release\Scripts\CommonScript.js"
  SetOutPath "$INSTDIR\SystemLog"
  File "E:\ZZL\ADWeb\ Installation package production \Release\SystemLog\LogStatisticsDetail.aspx"
  File "E:\ZZL\ADWeb\ Installation package production \Release\SystemLog\SystemLogList.aspx"
  File "E:\ZZL\ADWeb\ Installation package production \Release\SystemLog\SystemLogStatistics.aspx"
  SetOutPath "$INSTDIR"
SectionEnd

Section -Post
  WriteUninstaller "$INSTDIR\uninst.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}" "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


Function un.onUninstSuccess
  HideWindow
  MessageBox MB_ICONINFORMATION|MB_OK "$(^Name)  Successfully removed from your computer ."
FunctionEnd

Function un.onInit
  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 " You really want to completely remove  $(^Name) , It and all its components ?" IDYES +2
  Abort
FunctionEnd

Section Uninstall
  Delete "$INSTDIR\uninst.exe"
  Delete "$INSTDIR\Web.config"
  Delete "$INSTDIR\SystemLog\SystemLogStatistics.aspx"
  Delete "$INSTDIR\SystemLog\SystemLogList.aspx"
  Delete "$INSTDIR\SystemLog\LogStatisticsDetail.aspx"
  Delete "$INSTDIR\Scripts\CommonScript.js"
  Delete "$INSTDIR\Images\add.png"
  Delete "$INSTDIR\Images\1_open.png"
  Delete "$INSTDIR\Images\1_close.png"
  Delete "$INSTDIR\Error.aspx"
  Delete "$INSTDIR\Default.aspx"
  Delete "$INSTDIR\404.aspx"

  RMDir "$INSTDIR\SystemLog"
  RMDir "$INSTDIR\Scripts"
  RMDir "$INSTDIR\Images"
  RMDir "$INSTDIR"

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

Installation package : Standard interface

Now let's see the effect of the installation package generated by the script of the standard interface , After implementation HM NIS Edit Interface , Click menu “NSIS”->“ Compile and run ”, The output window will show the compilation process , If there is no error , It will directly execute the compilation Setup.exe, The installation screenshot is as follows :

Is it compared “ Modern user interface ”, The standard interface will be ugly , and NSIS The built-in standard installation interface is not welcome 、 Complete the interface ( Only License 、 Catalog selection 、 Components 、 Installation record page ) , in addition “ The previous step ”、“ next step ”、“ Cancel ” And other buttons require additional language pack files to load , Not as good as “ Modern user interface ” Just one instruction (!insertmacro MUI_LANGUAGE "SimpChinese") To specify the language , So next we will adopt “ Modern user interface ” To make the installation package , Of course, the icons on these interfaces , Text anywhere can be customized .

The following chapters are written with Application in practice As an example , Then the example is expanded to illustrate the customization of the page 、PowerShell Command invocation 、SQL Server Database access 、Web Application deployment 、 Failed rollback and the execution of the uninstaller, etc .

原网站

版权声明
本文为[sukhoi27smk]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140553428779.html