This article was first published on the public account [an old code farmer]

NSIS is a special installation program for Windows system, which allows us to customize some operations during the installation of.exe files. Most desktop development technologies in the market can use NSIS, such as QT, MFC and ELECTRON.

NSIS configuration items

When packing in electron with the electric-Builder, we can configure the NSIS object in the package.json file to a certain extent that we set up the installer. Such as:

OneClick: Boolean type, true for one-click installation, false for step installation perMachine: Boolean type, whether to display the installation mode of the auxiliary installer installer page (select machine or user). True always installs as user. AllowToChangeInstallationDirectory: value of Boolean type, whether to allow the user to modify the installation directory, true to allow, false to not allow displayLanguageSelector: Boolean: whether the selected language is allowed (default system language). True: yes, false: no language: string type, default language GUID: It is a string that specifies a GUID for the application. This GUID is stored in the registry. If not specified, createDesktopShortcut is automatically generated: Boolean or "always", whether to create desktop shortcuts. True means to create desktop shortcuts, false means not to create desktop shortcuts. "always" means to create include shortcuts when reinstallingCopy the code

The above list only part of the field, is need to refer to the following website: www.electron.build/generated/n…

NSIS script

The nSIS configuration item above is very powerful, but it does not meet all our needs, so we need to write our own NSIS script and set the NSIS configuration item include field, as follows:

"include": "./build/installer.nsh"
Copy the code

Let me talk about a feature I implemented at work with the NSIS script: append folders to custom installation paths

When you click the EXE file to install, you are free to modify the installation directory. If you select a directory at will, the installation file may be messy, which is not conducive to search and maintenance. For example, if the user selects the root path of drive D or the desktop as the installation directory, all files after the installation will be stored in the root path or desktop.

This problem can be solved by using the NSIS script. When the user changes the installation path through the browse button, we will check whether the last folder of the path is myApp, if not, we will append a folder myApp. If the user selects the root path D:\ from drive D, the appended path is D:\myApp\

The specific approach is divided into two parts as follows:

1. Create an NSIS script file installer. NSH and set the NSIS configuration in package.json.

"nsis": {
    "oneClick": false,
    "perMachine": true,
    "allowToChangeInstallationDirectory": true,
    "include": "installer.nsh"
}
Copy the code

2. Run the following script in the installer. NSH file:

! define DIR_NAME "myApp" Function .onVerifyInstDir StrLen $0 "\${DIR_NAME}" StrCpy $1 "$INSTDIR" "" -$0 StrCmp $1 "\${DIR_NAME}" +2 0 StrCpy $INSTDIR "$INSTDIR\${DIR_NAME}" FunctionEndCopy the code

Let’s explain the above code sentence by sentence

! Define DIR_NAME “myApp” represents the macro constant that defines a DIR_NAME, the folder name we will append.

Function. OnVerifyInstDir where Function stands for a method. OnVerifyInstDir is a callback method provided by NSIS, which will be called after users modify the installation directory by browsing.

StrLen $0 “\${DIR_NAME}” calculates the string length of \myApp and assigns it to $0.

StrCpy $1 “$INSTDIR” “” -$0 Truncates the length of the modified installation path from back to front. And assign it to the length of 0. And assign it to the length of 0. And assign it to 1. $INSTDIR indicates the installation path after the modification.

StrCmp $1 “\${DIR_NAME}” +2 0: $1 = \myApp

StrCpy $INSTDIR “$INSTDIR\${DIR_NAME}” means to concatenate a \myApp folder after the user’s modified path.

FunctionEnd represents the end of this callback method

Of course, the NSIS script can also do a lot of things, such as changing the default installation path, modifying the registry, pop-up prompts based on actions, and so on. The NSIS I am also a beginner, about NSIS script writing, may refer to the official document: NSIS. Sourceforge. IO/Docs /

Pay attention to the public number [an old code farmers], more high-quality technical content waiting for you to the original address