NSIS pages and sections execution - nsis

probably I am not getting basics of pages and sections in nsis script.
I have to analyse installation script which was not made by me. In the top of the script there are macros of MUI pages for example
!insertmacro MUI_PAGE_LICENSE $(license)
!insertmacro MUI_PAGE_INSTFILES ....
And then further down the code there are sections
Section "MainSection" SEC01
SetShellVarContext current
SetOutPath "$INSTDIR"
SetOverwrite ifnewer
File "${xy_TEMP_SRC}\InstallSrc\xy.exe"
File "${xy_TEMP_SRC}\InstallSrc\xy.exe.config"
SetOutPath "$INSTDIR\sk"
File "${xy_TEMP_SRC}\InstallSrc\sk\xy.resources.dll"
SetOutPath "$INSTDIR"
CreateDirectory "$SMPROGRAMS\xy"
CreateShortCut "$SMPROGRAMS\xy\xy.lnk" "$INSTDIR\xy.exe"
CreateShortCut "$DESKTOP\xy.lnk" "$INSTDIR\xy.exe"
SectionEnd
+ another sections for instance unninstall section
My question is how and when the sections are executed when there is no reference from pages to the sections.
My brain is telling me that the sections should be executed sometimes during the pages confirmation during the installation process, but I guess it's wrong, so please can anyone tell me how it actualy works?

All sections are executed on the instfiles page and in the order of your sections. If you need stuff to be executed before, after or in between, you can use functions (e.g. pre- or leave functions)

!insertmacro MUI_PAGE_INSTFILES Execute the sections.

Related

Space required on MUI_PAGE_DIRECTORY is 0.0KB

I'm writing an installer for Windows application. I'm using MUI for NSIS. One of the pages of the installer is Directory Page, where I have the "Space required" field. The problem is that the space required is always 0.0KB.
I was looking for some answers, but all I found was that space is calculated automatically. I wonder if there is some way to check which folder size this macro gets? Or any other ideas?
;Pages
; Installation
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE "#CPACK_RESOURCE_FILE_LICENSE#"
!insertmacro MUI_PAGE_DIRECTORY
Page custom pgAppLanguageCreate
The size is automatically calculated by the File instructions in Sections plus any extra you add with AddSize.
If you are not happy with this calculation you can force a specific size in .onInit with SectionSetSize.
I found what causes the problem.
In the "Installer Section" I used a function that copies all files.
Instead of calling it here, I just paste the entire function and now that works. It looks like you cant use "File" instruction in function because it doesn't add size to the required space this way.
If you use it in section, it works perfectly.
; Installer Sections
Section "install" Install
${If} $instState != "1"
Call deleteAllFiles
Call deleteInfoFromRegistry
Call deleteShortcut
${EndIf}
SetOutPath "$INSTDIR"
; copy files
File ${ICON}
File "slicer_license"
File /r "${INSTALL_FILE}\package\"
;File "README.txt" ;Readme file.
Call createShortcut
Call addInfoToRegistry
Call setAppLanguageShortcut
Call createLanguageFile
SectionEnd

NSIS nsi script error :- !insertmacro: macro named "SECTION_BEGIN" not found

In nsi script with MUI2.nsh
Code:-
!macro SECTION_BEGIN
Section ""
Call zip2exe.SetOutPath
!macroend
!macro SECTION_END
SectionEnd
!macroend
But If I want to define two or more section then in that case how to incorporate SECTION_BEGIN part?
Section "Main Component" MainCom
#SectionIn RO # Just means if in component mode this is locked
Call zip2exe.SetOutPath
;Store installation folder in registry
WriteRegStr HKLM "Software\${ZIP2EXE_NAME}" "" $INSTDIR
;Registry information for add/remove programs
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${ZIP2EXE_NAME}" "DisplayName" "${ZIP2EXE_NAME}"
WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${ZIP2EXE_NAME}" "NoRepair" 1
;Create optional start menu shortcut for uninstaller and Main component
;Create uninstaller
WriteUninstaller "${ZIP2EXE_NAME}_uninstaller.exe"
#!macroend
#!macro SECTION_END
SectionEnd
#!macroend
;--------------------------------
;Uninstaller Section
Section "Uninstall"
;Delete the appdata directory + files
RMDir /r "${INSTDIR_DATA}\*.*"
RMDir "${INSTDIR_DATA}"
;Delete Start Menu Shortcuts
Delete "$SMPROGRAMS\${ZIP2EXE_NAME}\*.*"
RmDir "$SMPROGRAMS\${ZIP2EXE_NAME}"
SectionEnd
#!macro SECTION_END
If we omit SECTION_BEGIN part then error comes. If we mention SECTION_BEGIN in both sections then also error comes.
What will be the solution to this problem?
If actually want to use Zip2Exe then you can modify parts of NSIS\Contrib\zip2exe\Base.nsh from
!macro SECTION_END
SectionEnd
!macroend
to something like this
!macro SECTION_END
SectionEnd
!if /FileExists "c:\mycustomzip2exefiles\mycustomsections.nsh"
!include "c:\mycustomzip2exefiles\mycustomsections.nsh"
!endif
!macroend
You can then put whatever code you want in c:\mycustomzip2exefiles\mycustomsections.nsh:
Section "My other section"
SetOutPath $InstDir
File "anotherfile.txt"
SectionEnd
However, Zip2Exe is mainly something you use to create simple self-extracting executables, you should not use it to create full installers.
When you create a real installer you don't use Zip2Exe, you use MakeNSIS and there is no such thing as a SECTION_BEGIN macro, you just add as many sections as you want to your .NSI file.
Example2.nsi contains a basic installer/uninstaller.

NSIS do things if section is not checked

I have created an NSIS Installer which works fine. Now I want to add another section called "install as update" which only will do things when it is NOT checked.
Why:
When the full version is installed, it will overwrite certain files which contain the activation codes of the software.
I can do it otherwise, and make a section called "install full version", but that makes less sense.
Section /o "Install as update" SecUpdate
*if(checked == false){
SetOutPath "$INSTDIR\data"
File "data\ConfigFile.xml"
File "..."
File "..."
File "..."
File "..."
File "..."
File "..."
File "..."
*}
SectionEnd
*these two lines represent what I would like to do.
If a section is unchecked then the code in it will not execute no matter what you do so you have to put the code somewhere else. A hidden section is a good solution:
!include LogicLib.nsh
!include Sections.nsh
Page Components
Page InstFiles
Section "Program files"
SectionIn RO
;SetOutPath ...
;File ...
SectionEnd
Section /o "Install as update" SID_UPDATE
SectionEnd
Section -OverwriteActivation SID_OWACTIVATION
SetOutPath "$INSTDIR\data"
File "whatever.xml"
SectionEnd
Function .onSelChange
${If} ${SectionIsSelected} ${SID_UPDATE}
!insertmacro UnselectSection ${SID_OWACTIVATION}
${Else}
!insertmacro SelectSection ${SID_OWACTIVATION}
${EndIf}
FunctionEnd

Creating shortcut with NSIS

I am trying to create an installer using NSIS, installing just the program that I have made works just fine but when I add code to the script to install a shortcut as well things aren't working.
I am very new to this program and what happens is the "Start in:" property path is incomplete but if i add the "\IndieBrowser\IndieBrowser\bin\Debug\IndieBrowser.exe"" at the end it just turns a lot of things into folders.
Full code:
!include "MUI2.nsh"
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "English"
Section "Desktop Shortcut" SectionS
SetShellVarContext current
SetOutPath "$INSTDIR"
CreateShortCut "$DESKTOP\IndieBrowser.lnk" "$INSTDIR\IndieBrowser\IndieBrowser\bin\Debug\IndieBrowser.exe"
SectionEnd
Here is an example:
; Create application shortcut (first in installation dir to have the correct "start in" target)
SetOutPath "$INSTDIR\bin"
CreateShortCut "$INSTDIR\bin\${NAME}.lnk" "$INSTDIR\bin\${NAME}.exe"
; Start menu entries
SetOutPath "$SMPROGRAMS\${NAME}\"
CopyFiles "$INSTDIR\bin\${NAME}.lnk" "$SMPROGRAMS\${NAME}\"
Delete "$INSTDIR\bin\${NAME}.lnk"

Require Component Installation with MUI

As a new NSIS user, I have defaulted to the HM NIS Edit wizard to create my installers so far. The default output takes advantage of MUI, which has been very useful to me. It also, however, includes a components page from the MUI macro library, so I cannot pick and choose which components are mandatory. I have looked at this question: How to make a SectionGroup mandatory in NSIS script, but I'm not sure where to put it in my code below, or even how to modify the wizard's output code to include it. The installer is for several apps with distinct (selectable) executables that all require the same (ideally mandatory) support files.
; HM NIS Edit Wizard helper defines
!define PRODUCT_NAME "Product"
!define PRODUCT_VERSION "1.0"
!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\App1.exe"
!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
; Instfiles page
!insertmacro MUI_PAGE_INSTFILES
; Finish page
!insertmacro MUI_PAGE_FINISH
; Uninstaller pages
!insertmacro MUI_UNPAGE_INSTFILES
; Language files
!insertmacro MUI_LANGUAGE "English"
; MUI end ------
Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "Setup.exe"
InstallDir "$PROGRAMFILES\Product"
InstallDirRegKey HKLM "${PRODUCT_DIR_REGKEY}" ""
ShowInstDetails show
ShowUnInstDetails show
Section "Support Files" SEC01
SetOutPath "$INSTDIR"
SetOverwrite try
File "..\Support\File\Path\file.txt"
SectionEnd
Section "App1" SEC02
SetOutPath "$INSTDIR"
SetOverwrite try
File "..\App1\File\Path\App1.exe"
SectionEnd
Section "App2" SEC03
SetOutPath "$INSTDIR"
SetOverwrite try
File "..\App2\File\Path\App2.exe"
SectionEnd
In short, I need to know how to make the Support Files section mandatory, without losing the GUI. My only experience, though, is with the above-mentioned wizard.
The SectionIn instruction is used to assign sections to install types (Combobox on the components page) but it can also make sections read-only:
Section "Support Files" SEC01
SectionIn RO ;Make it read-only
SetOutPath "$INSTDIR"
SetOverwrite try
File "..\Support\File\Path\file.txt"
SectionEnd

Resources