nant:在字符串中展开属性

3

概述:

如果属性buildmode包含debug,我该如何将带有值"download\${bulidmode}\project\setup.msi"的属性扩展为"download\Debug\project\setup.msi",以便将其用作<copy>标签中的file=""部分

详细说明:

我需要在nant中能够扩展字符串中的属性。例如,我有一个目标,将文件A复制到B。A和B都来自一个简单的两字段CSV文件,我正在使用迭代进行操作。

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

而filelist.csv将会是这样的

build\manifest.xml 
solutiondirectory\setup-proj-directory\Release\setupproj.msi,ProductA\ProductA.msi
solutiondirectory\another-proj-dir\Release\setupproj.msi,ProductB\ProductB.msi

我们将其拆分出来的原因是,我们编写分层应用程序并通过MSI部署到每个层 - 因此一个产品有多个使用相同版本号构建的msi文件。

无论如何 - 我想将这个更改为不再在filelist.csv文件中有"Release",而是像 ${build.mode} 这样的东西。我会使用以下代码:

<foreach item="String" in="Release,Debug" delim="," property="build.mode">
....as above
</foreach>

文件中嵌入的属性会被展开。

我已经苦思冥想几个小时了,但还是无法解决问题。

谢谢。

1个回答

4

通过自定义函数可以实现:

<?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>

非常感谢 - 运行得很好 - 但缺点是它打开了我之前没有真正意识到的全新 Nant 领域。总是有新东西要学习!Alan。 - Alan Mullett
能够在NAnt中使用自定义的C#任务/函数非常棒,而且能够内联执行而无需创建新的程序集更好。 - Julien Roncaglia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接