check for file existence using nmake and delete it - nmake

I am using Windows version of NMAKE. I would like to check for file existence in a make file. If it exists I need to delete it. Here is my code:
!IF EXIST ("C:\ABC.XML")
#del ABC.XML
!ELSE
#echo "FILE DOESN'T EXIST
!ENDIF
The above code is not working. I could not figure it out the problem. Please help.

Your code doesn't work because !IF, !ELSE and !ENDIF are a preprocessing directives and the result of preprocessing must produce a valid makefile. Commands are only allowed as part of what Microsoft calls a description block which are required to start with a dependency line with one or more targets and zero or more dependents.
You can get around this by executing your commands during the preprocessing stage by including them in a preprocessing directive surrounded by brackets ([]). Something like this:
!IF EXIST(C:\ABC.XML)
! IF [del C:\ABC.XML]
! ENDIF
!ELSEIF [echo FILE DOESN'T EXIST]
!ENDIF
The second !IF and the !ELSEIF directives are used to provide a context for the commands so they're executed during the preprocessing phase.
However I think you'd probably be better moving the del command to a description block where it's actually needed. For example if file ABC.XML needs to be deleted before it can be rebuilt, use something like this:
ABC.XML: ABC.CSV
-rem The csv2xml translator requires that the XML file not already exist
-#del ABC.XML 2> NUL
csv2xml ABC.CSV ABC.XML

Related

Where can I add command line in the NSIS script

I would add some command line to customize my NSIS intaller. I already read some topics, I know I must use ${GetParameters} and ${GetOptions}. But the NSIS script is very long do I put him somewhere in the OnInit function or in a section at the beginning of the script ?
I want, for example, to add an --quiet command line which display all the pages except the license( something which seems to /S), I want to try something like that:
Var DisplayAllPages
Var DisplayLicense
${GetParameters} "quiet"
${GetOptions} "quiet" "--quiet"=DisplayLicense
But I don't Know where can I write
You can use ${GetParameters} and ${GetOptions} in any function and/or section.
Only .onInit and sections are executed when the installer is started with /S so if you wish to turn silent mode off you need to put the code in .onInit.
If you are storing the result in a global variable then .onInit is also a good place to call them so that the information is available to the rest of the installer.

Error when trying to use File inside ${ForEachIn}?

I have the following code block inside the .onInit function of my NSIS script.
; Split the supplied artifacts array.
nsArray::Split ARTIFACT_ARRAY "${ARTIFACTS}" ";"
${ForEachIn} ARTIFACT_ARRAY $R0 $R1
File ${IVY_ROOT}\"$R1"
${Next}
The ${ARTIFACTS} is a passed in property from ANT at NSIS compile time and is basically a comma seperated list of files. When I attempt to compile the script I get the error below.
[exec] File: "C:\My_Workspaces\WEnDL\\deployments\ivy\"$R1"" -> no files found.
[exec] Usage: File [/nonfatal] [/a] ([/r] [/x filespec [...]] filespec [...] |
[exec] /oname=outfile one_file_only)
[exec] Error in script "C:\My_Workspaces\WEnDL\/deployments/selections.nsis" on line 394 -- aborting creation process
Any pointers appreciated.
You cannot use variables in File instructions because variables are only evaluated on the end-users system. The same is true for plug-ins. You need to stick with ${defines} and the instructions starting with ! when parsing at compile-time.
The best option is letting your build system generate a .nsh with the File instructions if possible:
Section
SetOutPath $InstDir
!include "generatedfilelist.nsh"
SectionEnd
Another option is to call external tools or batch-files with !system and let them parse the list and generate the .nsh.
Finally, in NSIS v3 it might be possible to pull this off with macro recursion and !searchparse+!searchreplace but the available recursion depth is limited so it might not work depending on the number of files in your list.

NSIS: Reading from a file at compile time

I want to read some values from a file (config.json) into some variables when I compile my nsis script.
How can I possibly do that?
Thanks in advance.
The !include command can include any file (at compile time) at the point where it is placed in the nsis script. But the included file must be compliant with the nsis syntax (e.g. it should !define some values).
The !execute command could help you: if you need absolutely to process a json file you could code a third-party batch command file to pre-process the json file and translate it into a suitable nsis file.
You can use !define to pass some value which can be used in compile time. For example lets imagine that you have got this code in you nsis source file:
!define PATHTOFILE "C:\thisfilewillbedeleted.ext"
Delete $PATHTOFILE
If you want to change this walue on compile time you can call nsis in this way:
makensis /DPATHTOFILE="C:\otherfiletodelete.ext"
[EDIT]
If you got *.json file which is generated using external tool and you must use this kind of file I will suggest you to use some building system, for example ant. You can create build.xml which read, parse data from json file and then write those data to *.nsh file. I think it will be better and cleaner than do it all in nsis script.
If you just need to parse your json file on runtime, you can use !define with the /file option:
!define /file OPTIONS json.txt
It will define OPTIONS with the content of json.txt.
If you want to utilize your json file in compile time to alter the generated exe, then you need some kind of precompiler, which is what you're actually doing.
You may use the !searchparse command with the /file switch.
Example :
# search filename.cpp for a line '#define APP_VERSION "2.5"' and set ${VER_MAJOR} to 2, ${VER_MINOR} to 5.
!searchparse /file filename.cpp `#define APP_VERSION "` VER_MAJOR `.` VER_MINOR `"`

Exec not working in NSIS installer

I am new to NSIS i am trying to execute an executable while installation similar to pre request. I tried the below code which copies the exe to the installation path but it is not executing it.
Section "example" example
SetOutPath "$INSTDIR"
File "setup.exe"
Exec "$INSTDIR\setup.exe"
BringToFront
SectionEnd
The answer from Seki is mostly correct, I'd just like to add that the correct syntax for Exec/ExecWait is always Exec '"c:\path\app.exe" param1 "par am2" param3'
Parameters are of course optional but the path to the app should always be quoted, not just because in your case where $INSTDIR could contain spaces but at least on Win9x it will fail no matter what if you don't quote (According to the NSIS manual)
If the spaces/lack of quotes is not the issue then there are a couple of other things you might want to look into:
$OUTDIR is the working directory for the new process (SetOutPath sets this)
Missing dll's etc (Check with Process Monitor)
Do the $INSTDIR variable maps to a directory whose name contains spaces? If so, you should add simple quotes to include the double quotes into the Execargument :
Exec '"$INSTDIR\setup.exe"'

nmake: can a batch file run as part of a make command block, affect the environment of the nmake.exe process?

I think in nmake if I do this:
example :
set value=77
echo %%value%%
The result will display 77 on the console.
Is there a way for me to invoke a .cmd or .bat file that will affect the environment of the nmake.exe process? Suppose I put the statement set value=77 in a file called "setvalue.cmd". Then change the makefile to this:
example :
setvalue
echo %%value%%
I get:
%value%
Alternatively, if there's a way to set a macro within a command block, that would also work. Or, a way to set the value of a macro from a batch file, even outside a command block.
You can create an nmake snippet during makefile pre-processing, and read that in. Assuming batch.cmd outputs valid nmake syntax, then
!if [batch.cmd >makefile.auto]
!error *** Could not create makefile.auto
!endif
!include makefile.auto
You should ensure batch.cmd sets %errorlevel% appropriately (e.g., exit /b 22).
makefile.auto can contain anything, but you would probably want stuff like value=77. A couple of points:
Dereference value using nmake syntax ($(value))
You can pass parameters to batch.cmd if necessary ([batch.cmd $(OBJECTS) >makefile.auto])
No, I don't think so.

Resources