Install from dynamic location - nsis

I have 2 versions of the same exe file for my project. The installer is supposed to pick one of the 2 versions depending on some conditions.
In a normal case i would do File executable\myExe.exe. Because i now have 2 versions of the file, i would have to do something like File "${ExeSourcePath}\myExe.exe", and $ExeSourcePath is determined by checking various conditions. When compiling this code i get
File: "${ExeSourcePath}\myExe.exe" -> no files found.
Anyone knows why? I'm only allowed to use fixed paths with the File command or am i doing something wrong?

${ExeSourcePath} is a precompiler define and $ExeSourcePath is a variable used at runtime, the File command can only use precompiler defines.
There are two ways you can handle this:
A) Include both files and decide at runtime based on the users system or choices made during install:
!include LogicLib.nsh
Section
ReadRegStr $0 HKLM "Software\foo\bar" baz
${If} $0 > 5
File "c:\myproject\version2\app.exe"
${Else}
File "c:\myproject\version1\app.exe"
${EndIf}
SectionEnd
B) Only include one file based on command line passed to makensis (/Dusev2 app.nsi) or something on your system:
Section
!define projectroot "c:\myproject"
!searchparse /noerrors /file ....... usev2 ;Or you can use !system etc
!ifdef usev2
File "${projectroot}\version2\app.exe"
!else
File "${projectroot}\version1\app.exe"
!endif
SectionEnd

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

How to move a subdirectory with its contents to another directory with NSIS

this is the first time I work with NSIS, I want to edit an script NSIS to allow me:
If first instalation -> create the directory "Affaires" in the location "C:\Users\ali.ait-mouhoub.ext\AppData".
If it is an update (So the directory "Affaires" it already exists in "C:\N.O.E") -> move the directory "Affaires" with its contents to "C:\Users\ali.ait-mouhoub.ext\AppData".
The current script creates the directory "Affaires" in "C:\N.O.E".
Can you please help me modify my script to meet my needs?
If the both the new and old locations are on the same volume then you can use Rename:
Section
Rename "$InstDir\Stuff" "$InstDir\OldStuff"
SectionEnd
If the new location might be on a different volume then you have to copy+delete:
!include LogicLib.nsh
Section
ClearErrors
CopyFiles /Silent "$InstDir\Stuff" "x:\Backup"
${If} ${Errors}
MessageBox MB_ICONSTOP "Unable to move!"
${Else}
RMDir /R "$InstDir\Stuff"
${EndIf}
SectionEnd

How do I execute the app right after installation ... with arguments?

My installation EXE should unzip itself in a temp folder, then execute an app therein (with passing args), return and delete the temp folder again.
How do I write the nsi script?
It sounds to me like you are trying to create a portable application. Portable applications are always better when the original author adds support for it because it can handle registry and other configuration files correctly.
If you still want to create a launcher application you can do something like this:
OutFile "MyLauncher.exe"
RequestExecutionLevel User
SilentInstall Silent
SetCompressor LZMA
!include FileFunc.nsh
!insertmacro GetParameters
Section
${GetParameters} $1
InitPluginsDir
SetOutPath $PluginsDir
File "c:\myfiles\MyApp.exe"
File /r "c:\myfiles\otherfiles\*.*" ; If you need to include other files required by the application
ExecWait '"$PluginsDir\MyApp.exe" /param1 "pa ra m2" /param3 $1' $0 ; $1 contains the parameters passed to your launcher, remove it if you don't want to pass those arguments
SetErrorLevel $0
SetOutPath $Temp ; Don't lock $PluginsDir so it can be deleted automatically by the installer
SectionEnd
Answering my own question.
The required nsi script skeleton should look like this:
# The name of the installer (arbitrary)
Name "hello"
# The name of the installation file
OutFile "hello.exe"
# where put the installation - other options would be $TEMP, etc.
InstallDir $DESKTOP
RequestExecutionLevel user # no Windows UAC popup please!
SilentInstall silent # completely silent install
SetCompressor /SOLID /FINAL lzma # max compression for inst. file
# The stuff to install
Section ""
SetOutPath $INSTDIR # where to install (overwritable by user!)
File /r D:\...\... # where the install material lives
SectionEnd
# this function auto-runs after installation is fine
Function .onInstSuccess
# parameter are passed through via $CMDLINE
ExecWait '"$OUTDIR\hello.dist\hello.exe" $CMDLINE'
RMDir /r "$OUTDIR\hello.dist" # remove install folder again
FunctionEnd

IfFileExists command run 3 command lines

How do I the result of IfFileExists command run three lines and not only the first.
In the code below is the result of IfFileExists is TemWSConfig, must perform the 3 lines to the NaoTemWSConfig command.
Presently the line 2 and 3 after TemWSConfig, always run
IfFileExists "$INSTDIR\IntegradorWS.exe.Config" TemWSConfig NaoTemWSConfig
TemWSConfig:
File "..\IntegradorWS\bin\x86\Release\AppInstalado.config"
Rename "$INSTDIR\IntegradorWS.exe.Config" "$INSTDIR\Antigo_IntegradorWS.exe.Config"
Rename "$INSTDIR\AppInstalado.config" "$INSTDIR\IntegradorWS.exe.Config"
NaoTemWSConfig:
File "..\IntegradorWS\bin\x86\Release\IntegradorWS.exe.Config"
NSIS does not skip around like this, you can verify that by switching out the problematic instructions:
Section
; Fake the IntegradorWS.exe.Config file for this example:
StrCpy $INSTDIR $temp
File "/oname=$INSTDIR\IntegradorWS.exe.Config" "${__FILE__}"
IfFileExists "$INSTDIR\IntegradorWS.exe.Config" TemWSConfig NaoTemWSConfig
TemWSConfig:
DetailPrint "TemWSConfig:1"
DetailPrint "TemWSConfig:2"
DetailPrint "TemWSConfig:3"
NaoTemWSConfig:
DetailPrint "NaoTemWSConfig:1"
; Clean up
Delete "$temp\IntegradorWS.exe.Config"
SectionEnd
This means the problem must be with the File instruction. Make sure you have called SetOutPath so it knows where to extract. File extraction can be skipped if you have changed SetOverwrite etc.

NSIS script condition compilation based upon existence of a file

I have an NSIS based installer that I need to be able generate slightly different versions of under different conditions.
The conditions are easy to establish at compile time, if a particular file exists on the disk then the alternative branding can be used. I know I could use a command-line option to the makensis.exe to provide this behaviour, but it would be better if the compiler could take care of this for me.
Is there a way of making a compile time "IfExist" type logic?
!macro CompileTimeIfFileExist path define
!tempfile tmpinc
!system 'IF EXIST "${path}" echo !define ${define} > "${tmpinc}"'
!include "${tmpinc}"
!delfile "${tmpinc}"
!undef tmpinc
!macroend
Section
!insertmacro CompileTimeIfFileExist "$%windir%\explorer.exe" itsThere
!ifdef itsThere
MessageBox mb_Topmost yes
!else
MessageBox mb_Topmost no
!endif
SectionEnd
Note: the !system command used here assumes you are compiling on windows
I don't have the answer to your generic question of compile-time file detection, but I do have a solution to what it sounds like you're trying to accomplish.
My installers use something like this:
In the file CustomBranding.nsh:
!define CUSTOM_BRANDING
!define APPNAME "Brand X"
!define LOGO "C:\Brand_X_Logo.png"
In the main installer script:
!include /NONFATAL "CustomBranding.nsh"
!ifndef CUSTOM_BRANDING
!define APPNAME "Generic"
!define LOGO "C:\Generic_Logo.png"
!endif
Is that the kind of "alternative branding" you're asking about?

Resources