Nant: expanding properties in a string



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 >


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


and the filelist.csv will be


(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"> above

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.


Best Solution

It is possible with a custom function :

<?xml version="1.0"?>
    <script language="C#" prefix="vbfox" >
            public string ExpandString(string str)
                return Project.Properties.ExpandProperties(str, Location.UnknownLocation);
    <property name="hello" value="{path::combine('_hello_', '_world_')}" />
    <property name="hello" value="${'$' + hello}" />
    <echo message="${hello}" />
    <echo message="${vbfox::expand(hello)}" />
Related Question