Using xmlpeek in Nant script gives odd error - nant

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>

Related

Why is NAnt executing my sql scripts in the wrong order?

Here is my Script:
<?xml version="1.0"?>
<project name="createAndPopulateDB" default="deploy">
<property name="sql.connstring" value="Provider=SQLOLEDB;Server=G-PC\sqlexpress;Integrated Security=SSPI" />
<property name="createDB" value="BuildTestDatabase.sql" />
<property name="populateDB" value="CreateTables.sql"/>
<target name="deploy">
<echo message="* Connecting to ${sql.connstring}"/>
<foreach item="File" property="sql.script">
<in>
<items>
<include name="${createDB}" />
<include name="${populateDB}" />
</items>
</in>
<do>
<echo message="* Executing ${path::get-file-name(sql.script)}"/>
<sql connstring="${sql.connstring}" delimiter="go" delimstyle="Line" batch="false" source="${sql.script}"/>
</do>
</foreach>
</target>
</project>
The NAnt script is supposed to call two tsql programs. The first tsql is designed to drop a database if it is present, and if it isn't, create it. The second checks to see if a table is present, and if so, delete it. Similarly if it isn't, it populates the created database with a simple table.
My question is why does it run the populateDB script first?
I found that the best way to determine the order in which the tsql programs are run is through a depends attribute attached to separate targets. This will run them in a predetermined order and is extremely easy to follow logically if the NAnt script is a part of a repository.

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" />

Setting multiple Phing properties from command line

<?xml version="1.0" ?>
<project name="first" basedir="." default="build-skeleton">
<property name="dirName" value="module" />
<property name="fileName" value="config" />
<target name="build-skeleton" description="Making folders">
<mkdir dir="./${dirName}/Block" />
<touch file="./${dirName}/etc/${fileName}.xml" />
</target>
</project>
phing -f mage_module.xml -DdirName=moduleX,fileName=config
phing -f mage_module.xml -DdirName=moduleX fileName=config
Both throw an error - no surprise there.
Is it possible to set multiple properties in Phing via command line?
Just repeating the -D parameter should work:
phing -f mage_module.xml -DdirName=moduleX -DfileName=config

Nant: Find file by pattern

What I am trying to do, is to find a file with NAnt. This file could by anywhere in a directory structure of a given folder.
I tried to this with the NAnt-foreach task (this works) but I am not quite convinced of that:
<target name="find-file">
<fail message="Property param.dir must be set" unless="${property::exists('param.dir')}" />
<fail message="Property param.pattern must be set" unless="${property::exists('param.pattern')}" />
<property name="return.file" value="" />
<foreach item="File" property="iterator.file">
<in>
<items>
<include name="${param.dir}\**\${param.pattern}" />
</items>
</in>
<do>
<property name="return.file" value="${iterator.file}" if="${string::get-length(return.file) == 0}" />
</do>
</foreach>
</target>
Is there anybody aware of a better approach? If not how can I accomplish to exit the foreach-loop after the first element is found?
This nantcontrib function will put the matching filenames into a delimited string..
If you know that only one matching file will exist then it may get you what you want. If there are several then you could use the nant substring function to just get the first match by taking the substring up to the first delimiter.
The following nant script:
<?xml version="1.0" encoding="utf-8"?>
<project default="find-file2">
<property name="NantContrib.dir" value="C:\Program Files\nantcontrib-0.85\" readonly="true" />
<target name="LoadNantContrib">
<loadtasks assembly="${NantContrib.dir}bin\NAnt.Contrib.Tasks.dll" />
</target>
<target name="find-file2" depends="LoadNantContrib">
<fileset id="find.set">
<include name="${param.dir}\**\${param.pattern}" />
</fileset>
<property name="return.file" value="${fileset::to-string('find.set', ' | ')}" />
<echo message="return.file=${return.file}"/>
<echo message="Found ${fileset::get-file-count('find.set')} files"/>
</target>
</project>
...and the following folder structure:
\---folderroot
+---folder1
| dontfindme.txt
| findme.txt
|
+---folder2
| dontfindme.txt
|
\---folderempty
...works as expected. Searching for findme.txt finds one file. Searching for dontfindme.txt finds two files. Searching for *.txt finds three files.
Example call:
nant -D:param.dir=folderroot -D:param.pattern=findme.txt
Example output:
find-file2:
[echo] return.file=C:\Documents and Settings\rbaker\My Documents\nantfindfile\folderroot\folder1\findme.txt
[echo] Found 1 files
BUILD SUCCEEDED

nant: expanding properties in a string

Summary:
How do I expand a property with value "download\${bulidmode}\project\setup.msi" to "download\Debug\project\setup.msi" if the property buildmode contained debug so I can use it as the file="" part of < copy >
Detail:
I have a bit of a requirement to be able to expand properties within a string in nant.
For example I have a target that is copying file A to B. A and B both come from a simple two field CSV file which I'm iterating through using
<foreach item="Line" in="filelist.csv" delim="," property="source.file,target.file">
<property name="sourcefile" value="${path::combine(source.dir,source)}" />
<property name="targetfile" value="${path::combine(download.dir,destination)}" />
<echo message="Copy ${sourcefile} to ${targetfile}" />
<copy file="${sourcefile" tofile="${destination}" />
</foreach>
and the filelist.csv will be
build\manifest.xml
solutiondirectory\setup-proj-directory\Release\setupproj.msi,ProductA\ProductA.msi
solutiondirectory\another-proj-dir\Release\setupproj.msi,ProductB\ProductB.msi
(The reason we split these out is that we write multi-tiered applications and deploy by MSI to each tier - so one product has multiple msi's all built with the same version numbers)
Anyway - I want to change this to that I no longer have "Release" in the filelist.csv file but something like ${build.mode}. I would wrap the above code with a
<foreach item="String" in="Release,Debug" delim="," property="build.mode">
....as above
</foreach>
and the property embedded within the string in the file gets expanded.
I've been beating my head against a brick wall for a few hours, but just can't figure it out.
Thanks
It is possible with a custom function :
<?xml version="1.0"?>
<project>
<script language="C#" prefix="vbfox" >
<code>
<![CDATA[
[Function("expand")]
public string ExpandString(string str)
{
return Project.Properties.ExpandProperties(str, Location.UnknownLocation);
}
]]>
</code>
</script>
<property name="hello" value="{path::combine('_hello_', '_world_')}" />
<property name="hello" value="${'$' + hello}" />
<echo message="${hello}" />
<echo message="${vbfox::expand(hello)}" />
</project>