In NAnt <exec>, how to have a conditional <arg> based on property value? - nant

I've got an NAnt <exec> task. I want one argument presence to be conditional to some property being true.
For example, I want the -c command line argument of psExec to be conditional. It should be outputted only if ${pExec.copyprog == 'true'}.
The following does not work:
<property name="psExec.copyprog" value="false" />
...
<exec program="${psExec.path}" failonerror="false">
...
<arg line="-c" if="${psExec.copyprog}==true" />
</exec>
It yields the following error:
'false==true' is not a valid value for attribute 'if' of <arg ... />.
Cannot resolve 'false==true' to boolean value.
String was not recognized as a valid Boolean.
How can I achieve this?

Properties in NAnt are tricky since they don't have a type and simply are considered as of type string. So this would be the solution:
<exec program="${psExec.path}" failonerror="false">
<!-- ... -->
<arg line="-c" if="${bool::parse(psExec.copyprog)}" />
</exec>
Update: Mea culpa! I was wrong. if="${psExec.copyprog}" does also work. So there is some sort of property typing.

You'd need to put ==true inside {}, but you can also just skip it:
<arg line="-c" if="${psExec.copyprog}" />
Comparing a true boolean expression to true does not change the result.

Related

Passing a parameter to NAnt build script

This is my scenario:
I have a build.bat that holds:
call tools\nant-0.92\bin\nant.exe -buildfile:deploy.build %* -logfile:deploy_NAnt.log
Part of deploy.build holds:
<project
name="EdpClient"
basedir="." default="build"
xmlns="http://nant.sf.net/release/0.92/nantContrib.xsd">
<!--INIT -->
...
<property name="version" value="1.48.0.4" />
...
<!--RELEVANT TARGET-->
<target name="BuildProductionApplication" description="Build">
<property
name="publishFolderParameter"
value="/p:PublishDir=${productionPublishFolder}" />
<echo message="Building..." />
<exec
program="${msbuildExe}"
workingdir="." verbose="true">
<arg value="${projectFile}" />
<arg value="/target:Clean;Publish" />
<arg value="${publishFolderParameter}" />
<arg value="/property:ApplicationVersion=${version}" />
<arg value="/property:PublisherName="${publisherName}"" />
</exec>
<echo message="Built" />
</target>
...
</project>
Now my question is:
How can i call "build.bat -version 1.48.0.4" and replace the param
in my structure?
If the -version param is not supplied the script should throw some
sort of msg back in command line?
Thanks to all that help!
This is how i did it:
In deploy.build file i changed:
<property name="version" value="1.48.0.4" />
to:
<property name="version" value="${arg.version}" />
And this is how build.bat (batch) looks like now:
#ECHO OFF
IF %1.==. GOTO ERROR
ECHO:BUILDING VERSION: %1 ...
CALL tools\nant-0.92\bin\nant.exe -buildfile:deploy.build -D:arg.version=%1 -logfile:deploy_NAnt.log
GOTO END
:ERROR
ECHO.
ECHO Please provide the version parameter (eg. "deploy 1.1.1.1")
ECHO.
GOTO END
:END
Now I can call build 1.48.0.4 command.
It suits all my needs.

Using xmlpeek in Nant script gives odd error

As part of a CI process I am trying to create a buildlabel which consists of the content of an xml element within an xml structure. For this purpose I am using nant and xmlpeek. My problem is that I get an odd error stating:
"Nodeindex '0' is out of range"
This is only the case if the xml file I am xmlpeeking contains a namespace definition in the root node.
Removing the namespace from the xml file gives me the output I expect.
The nant target that generates the error can be boild down to:
<target name="TDSLabel">
<property name="element" value=""/>
<echo message="Getting element" />
<xmlpeek file="C:\xxx\test1.xml" xpath="//Project/PropertyGroup/ProductVersion" property="element"/>
<echo message="The found element value was: ${element}" />
</target>
and the test1.xml file looks like this:
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProductVersion>9.0.21022</ProductVersion>
</PropertyGroup>
</Project>
You already gave the right hint yourself. It's about the namespace. This should fix it:
<target name="TDSLabel">
<property name="element" value=""/>
<echo message="Getting element" />
<xmlpeek
file="C:\xxx\test1.xml"
xpath="//x:Project/x:PropertyGroup/x:ProductVersion"
property="element"
verbose="true">
<namespaces>
<namespace prefix="x" uri="http://schemas.microsoft.com/developer/msbuild/2003" />
</namespaces>
</xmlpeek>
<echo message="The found element value was: ${element}" />
</target>
Found a similar problem and the anwser to my problem here: XmlPoke and unique nodes. The problem was that I did not include the namespace definition within the xmlpeek element and afterwards omitted the necessary reference to the namespace in my xpath statement:
<xmlpeek file="C:\xxx\test1.xml" xpath="//x:Project/x:PropertyGroup/x:ProductVersion" property="element">
<namespaces>
<namespace prefix="x" uri="http://schemas.microsoft.com/developer/msbuild/2003" />
</namespaces>
</xmlpeek>

How to replace string in a file using NANT?

I am trying to replace the occurance of a string in a wxs file using Nant.
I have only found the following example, which uses <replaceString>, but it seems like it can only be used within the copied files. Are there any other way of replacing a string, without actually copying the files over?
<property name="NOW" value="${datetime::now()}" />
<copy todir="out">
<fileset basedir="in">
<include name="**/*" />
</fileset>
<filterchain>
<replacetokens>
<token key="NOW" value="${TODAY}" />
</replacetokens>
<tabstospaces />
</filterchain>
</copy>
Here's the code:
<loadfile file="token.txt" property="token-file">
<filterchain>
<replacetokens>
<token key="NOW" value="${datetime::now()}" />
</replacetokens>
</filterchain>
</loadfile>
The official NAnt docs for <loadfile> element contain the exact sample you need. See the bottom of the page.
Here's how I did it.
<loadfile file="${file}" property="file.content">
<filterchain>
<replacestring from="StringToMatch" to="StringToReplace" ignorecase="true" />
</filterchain>
</loadfile>
<echo file="${file}">${file.content}</echo>
So you are trying to modify a .wxs file which is XML, right?
In this particular case you might use <xmlpoke> if you are able to determine the position of the strings to replace via XPath.
I found a solution for you here: http://frank.overseakids.com/?p=182
<loadfile file=”${dir.template}\template.db_name.sql” property=”restore.db.sql.db_name”>
<filterchain>
<replacetokens>
<!– this looks for tokens like #blah.blah# in the file being loaded and replaces them–>
<token key=”restore.db.prefix” value=”${restore.db.prefix}” />
<token key=”backup.file.path” value=”${backup.file.path}” />
</replacetokens>
</filterchain>
</loadfile>
<property name=”current.db” value=”db_name” />
<property name=”current.log” value=”${dir.log}\${restore.db.logfile.prefix}_db_name.log” />
<property name=”current.file” value=”${dir.template}\restore.db_name.tmp.sql” />
<delete if=”${file::exists(current.file)}” file=”${current.file}” />
<echo file=”${current.file}”>${restore.db.sql.db_name}</echo>
You can wrap this in a <foreach /> element.
I never managed to get the filterchain and replacetokens to work properly. I ended up using this and it works great.
<replacetext filename="${filename}" src="stringToBeReplaced" replacement="replacementString" />
All these answers did not work for me, maybe because I needed to replace a string with spaces in it. Loading a file content with filterchain/replacetokens did nothing to the contents of the associated property. Maybe I'm using it wrong.
The tasks "replacestring" and "replacetext" suggested by #Ally and #John Sterne do not exist.
It's included in a Jenkins build process, thus the ENVIRONMENT variable must be set to the working dir.
<loadfile file="./my/batch.bat" property="file.content" />
<property name="file.content"
value="${string::replace(file.content, 'D:\path to\the working\space', environment::get-variable('WORKSPACE'))}" />
<property name="file.content"
value="${string::replace(file.content, 'Cd C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin', 'CD /D C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin')}" />
<echo file="./my/batch.bat">${file.content}</echo>
I had that problem today. To solve it I used the move command instead of loadfile or copy. This worked for me because since my file was pretty small. The other caution about this is that replacetokens needs a start identifier and end identifier of the token; begintoken and endtoke respectively. If those are not set the default values are the # symbol. So if you want to replace a value such as MY_SERVER_PLACE_HOLDER that means the value in your file must be #MY_SERVER_PLACE_HOLDER#. If you want your token to start with something different than you should specify the begintoken and endtoken values. That should give you an idea of the problems the begin token and endtoken will bring you.
So here is what I did in a nutshell
Moved the file to a temporary location. In that move I used
filterchain with removetokens to change the values in the file.
In step 2, I moved the file back to it's original location.
I then used the delete command to delete the temp folder I created.
Here is a what I did. (May not be syntactically correct since I am not in front of the code at the moment)
<move todir="temp">
<fileset basedir="in">
<include name="myfile.dat" />
</fileset>
<filterchain>
<replacetokens>
<token key="MY_SERVER_PLACE_HOLDER" value="http://www.someserver.com" />
</replacetokens>
<tabstospaces />
</filterchain>
</move>
<move todir="in">
<fileset basedir="temp">
<include name="myfile.dat" />
</fileset>
</move>
<delete dir="temp" />

Convert long name to short name in NANT?

I am working on a NANT build project to build a VS project. One of the build file has a property definition like this:
<property name="App.dir"
value ="${directory::get-current-directory()}\Source\App"/>
This property value has been used in many other places. For example:
<exec program=".\test.exe" workingdir=".">
<arg line="${App.dir}\project1\resources\res1.resx /arg2"/>
Here I need to use <arg line=...> instead of <arg value=...> is to pass specified 2 arguments to text.exe.
when I build this NANT project on another developer's machine, I got a build failure. Finally I found out that the ${App.dir} on that developer's machine is a path in long-form and there is a space in the path. As a result, the arguments to text.exe were actually 3 or more.
I am not sure if there is any way to convert the ${App.dir} path to short name with no spaces:
<property name="App.dir" value ="????"/>
where ???? is something to convert "${directory::get-current-directory()}\Source\App" to a path name in a short-form. Is there any way to do that?
I am using NANT in Windows XP. In Unix/Cgywin, there is one function to get dos name:
cygpath::get-dos-path
Can I use this one in my NANT build file?
Use <arg value="">, from the NANT docs:
value - A single command-line argument; can contain space characters.
I think that I found two ways to resolve the issue. Instead of converting path to DOS path (8.3 name), I can use quotes around my arguments. The second method is the one as ovado recommened to use <arg value=.../> for a single argument value. Here is my test example. All the test files are in my C:\Test folder. First, I created a test bat:
#REM This is the content of Test.bat file.
#echo COMMAND PARAMETERS: %*
#echo FIRST PARAMETER: %1
#echo SECOND PARAMETER: %2
dir %1
Then I created a test build file (Test.Build)"
<project>
<target name="Test" description="Test with one line" failonerror="true">
<property name="App.dir" value ="C:\Program Files"/>
<exec program="test.bat" workingdir=".">
<arg line='"${App.dir}\Microsoft*.*" ${App.dir}\test2 /test3' />
</exec>
</target>
<target name="Test2" description="Test2 with values" failonerror="true">
<property name="App.dir" value ="C:\Program Files"/>
<exec program="test.bat" workingdir=".">
<arg value="${App.dir}\Microsoft*.*" />
<arg value="${App.dir}\test2" />
<arg value="/test3" />
</exec>
</target>
</project>
Here I used " to close my first argument in <arg ='"..."' .../>. Notice that I used single quote for my attribute line.
In this way, I run my build by NANT.EXE. I got the result as I expected:
C:\Test>NANT.exe -buildfile:test.build Test
Test:
[exec] COMMAND PARAMETERS: "C:\Program Files\Microsoft*.*" C:\Program Files\test2 /test3
[exec] FIRST PARAMETER: "C:\Program Files\Microsoft*.*"
[exec] SECOND PARAMETER: C:\Program
[exec] C:\Test\>dir "C:\Program Files\Microsoft*.*"
[exec] Volume in drive C has...
....
I got the same result with the alternative Test2,
C:\Test>NANT.exe -buildfile:test.build Test2
...

How do I optionally require a command line arguement for Ant?

I'm new to ant, and I want to require a file name if something other than the default target is used, so the calling syntax would be something like this:
ant specifictarget -Dfile=myfile
I'm using the ant contrib package to give me additional functionality, so I have this:
<if>
<equals arg1="${file}" arg2="" />
<then>
<!-- fail here -->
</then>
</if>
My thinking is that if file was not specified it might be equal to the empty string. Obviously, this didn't work, and I'm not finding any examples on google or the right syntax in the manual.
So what syntax should I use?
You don't really need the contrib package. This is more conveniently done using built-in ant capabilities like if/unless and depends. See below:
<target name="check" unless="file" description="check that the file property is set" >
<fail message="set file property in the command line (e.g. -Dfile=someval)"/>
</target>
<target name="specifictarget" if="file" depends="check" description=" " >
<echo message="do something ${file}"/>
</target>
You've got the right idea. The
ant specifictarget -Dfile=myfile
sets Ant Properties from the command line. All you really need is
<property name="file" value=""/>
for your default value. That way if file is not specified, it will be equal to the empty string.
Since properties are not mutable in Ant, you can add this:
<property name="file" value="" />
This will set the property file to an empty string if it hasn't already been set on the command line. Then your equality test will work as you intended.
Alternately, you can use escape the value since ant just spits out the actual text when it can't do a property substitution.
<if>
<equals arg1="${file}" arg2="$${file}" />
<then>
<echo>BARF!</echo>
</then>
</if>