Copying files from a folder with a pattern - nsis

As part of my installer I'm extracting a JRE which is in a folder that varies name (i.e. jre1.8.0_74). I'm trying to do that with a pattern, but it can't find it. I tried something like this:
SetOutPath "$INSTDIR\${APPDIR}\jre"
${If} ${RunningX64}
File /nonfatal /a /r "${SrcDir}\jre\jre_64\jre*\*.*"
${Else}
File /nonfatal /a /r "${SrcDir}\jre\jre_32\jre*\*.*"
${EndIf}
But found no files. I basically want to skip this "unknown" folder name to make it easier starting my application, etc.
I know it's a NSIS newbie basic mistake somewhere :P
Any idea?

When something is not known at compile-time you must use !system to execute a batch file or something else that is able to create a text file with the desired NSIS instruction that you later can !include.
For filesystem wildcards you can probably get away with just using dir or for:
!macro InvokeNSISCommandOnWildcardFolder parentdir wildcard commandprefix commandsuffix
!tempfile _InvokeNSISCommandOnWildcardFolder
!system 'for /D %A in ("${parentdir}\${wildcard}") do #(^>^> "${_InvokeNSISCommandOnWildcardFolder}" echo ${commandprefix}${parentdir}\%~nxA${commandsuffix})'
!include "${_InvokeNSISCommandOnWildcardFolder}"
!delfile "${_InvokeNSISCommandOnWildcardFolder}"
!undef"${_InvokeNSISCommandOnWildcardFolder}"
!macroend
Section
SetOutPath "$INSTDIR\${APPDIR}\jre"
!insertmacro InvokeNSISCommandOnWildcardFolder "${SrcDir}\jre\jre_32" "jre*" 'File /nonfatal /a /r "' '\*.*"'
SectionEnd
Because my example here just uses !system to invoke "DOS" commands directly there are some restrictions on the strings passed to this macro and <, >, (, and ) might have to be escaped.

Related

Generate installation logs

I am using special logging build to generate installation logs.
I have observed the logs are not generating when called another installer from installer script.
For ex -
ExecWait '"$INSTDIR\installer1.exe" /S _?=$INSTDIR'
The log is generating for main installer but not for installer1.exe
The installer1.exe contains lots of components and I need to print the logs for the same. I have tried enabling logset on in the installer1 script but no luck.
Tried using dumplog but it doesn't work with silent installation.
Any help would be appreciated!
Sample code from Main Installer script --
InstallDir "C:\MyFolder"
Name "${PRODUCT_NAME_VERSION}"
OutFile "${OUT_FILE}"
Section "Test"
SetOutPath $INSTDIR
LogSet on
ExecWait '"$EXEDIR\Packages\installer1.exe" /S /INST=$INSTDIR' $0
SectionEnd
Sample code from sub-installer script ---
InstallDir "C:\MyFolder"
Section "-Demo"
SetOutPath $INSTDIR
LogSet on
LogText "Print something"
SetOutPath $INSTDIR\ExternalFolder\Demo
File /nonfatal /a /r $INSTDIR\ExternalFolder\Demo\Test
ExecWait '"$INSTDIR\ExternalFolder\Demo\Test\TestSetup.exe" /silent '
SectionEnd
The sub-installer (installer1.exe) is pre-compiled and kept the exe in $EXEDIR\Packages\installer1.exe The patch is valid.
_?= is special syntax that is only supported by NSIS uninstallers, installers use /D=.
ExecWait '"$InstDir\installer.exe" /S /D=$InstDir'
Logging of course has to be enabled in this sub-installer as well.
/D= overrides the InstallDir attributes, forcing $InstDir to the specified path before .onInit is executed.
InstallDir $INSTDIR does not make sense, use something like InstallDir "$ProgramFiles\MyApp"

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.

Is it possible to conditionally add a file/folder to NSIS installer

Is it possible to conditionally add a file/folder and installation option to a NSIS installer?
My idea is that if the folder Foo exists at a given location it should be added to the installer and the option to install Foo should be added to the installer as well. But if the folder Foo does not exist, the NSIS script should just create the installer but leave Foo and the option to select Foo out of it.
You can try to include a file with /NONFATAL. If it exists, it will be included by the compiler. In runtime, you can check if installer was able to extract it.
File /NONFATAL "file.zip"
${If} ${FileExists} "$OUTDIR\file.zip"
...
${EndIf}
In NSIS 2 File /NONFATAL /R "c:\foo" is the best you can do without external tools and you need a little hack to hide the section when there are no files:
!include LogicLib.nsh
Page Components
Page InstFiles
Section "Main"
SetOutPath $InstDir
# File "C:\myfiles\myapp.exe"
SectionEnd
Section "Install Foo" SID_FOO
SetOutPath $InstDir
File /NONFATAL /r "C:\myfiles\foo\*.*"
SectionEnd
Function .onInit
SectionGetSize ${SID_FOO} $0
StrCmp $0 0 "" +3
SectionSetFlags ${SID_FOO} 0 ; Force all flags off including the checkmark
SectionSetText ${SID_FOO} "" ; Hide the section because its size is 0
FunctionEnd
If this is unacceptable you can use !system and get a little help from cmd.exe to check if something exists:
!tempfile INCEXIST
!system 'if exist "C:\myfiles\foo\*.*" echo !define HAVE_FOO > "${INCEXIST}"'
!include "${INCEXIST}"
!delfile "${INCEXIST}"
!ifdef HAVE_FOO
Section "Install Foo"
SetOutPath $InstDir
File /r "C:\myfiles\foo\*.*"
SectionEnd
!endif
In NSIS 3 !if supports a /FileExists switch:
!if /FileExists "C:\myfiles\foo\*.*"
Section "Install Foo"
SetOutPath $InstDir
File /r "C:\myfiles\foo\*.*"
SectionEnd
!endif
Example to replace file what depends on running service and exists or not at targget location
IfFileExists "$SYSDIR\my_file.dll" exist notexist
exist:
ExecWait 'net stop desired_service'
SetOutPath $SYSDIR
SetOverwrite on
File "/oname=$SYSDIR\my_file.dll" "Path to my file\my_file.dll"
ExecWait 'net start desired_service'
notexist:
.....what you want to do if doesn't exists

Install from dynamic location

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

Resources