Nant: expanding properties in a string

nant

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

Best Answer

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>
Related Topic