在 MSBuild 中,我可以对 MetaData 项使用 String.Replace 函数吗? [英] In MSBuild, can I use the String.Replace function on a MetaData item?

查看:12
本文介绍了在 MSBuild 中,我可以对 MetaData 项使用 String.Replace 函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 MSBuild v4 中,可以在 Properties 上使用函数(如 string.replace).但是如何在元数据上使用函数呢?

In MSBuild v4 one can use functions (like string.replace) on Properties. But how can I use functions on Metadata?

我想使用 string.replace 函数如下:

I'd like to use the string.replace function as below:

<Target Name="Build">
        <Message Text="@(Files->'%(Filename).Replace(&quot;.config&quot;,&quot;&quot;)')" />
</Target>   

不幸的是,这输出为(不是我想要的):log4net.Replace(".config","");ajaxPro.Replace(".config","");appSettings.Replace(".config","");cachingConfiguration20.Replace(".config","");cmsSiteConfiguration.Replace(".config","");dataProductsGraphConfiguration.Replace(".config","");ajaxPro.Replace(".config","");appSettings.Replace(".config","");cachingConfiguration20.Replace(".config","");cmsSiteConfiguratio

Unfortunately this outputs as (not quite what I was going for): log4net.Replace(".config","");ajaxPro.Replace(".config","");appSettings.Replace(".config","");cachingConfiguration20.Replace(".config","");cmsSiteConfiguration.Replace(".config","");dataProductsGraphConfiguration.Replace(".config","");ajaxPro.Replace(".config","");appSettings.Replace(".config","");cachingConfiguration20.Replace(".config","");cmsSiteConfiguratio

有什么想法吗?

推荐答案

你可以用一点技巧来做到这一点:

You can do this with a little bit of trickery:

$([System.String]::Copy('%(Filename)').Replace('config',''))

基本上,我们调用静态方法 'Copy' 来创建一个新字符串(出于某种原因,如果你只是尝试 $('%(Filename)'.Replace('.config',''))),然后对字符串调用替换函数.

Basically, we call the static method 'Copy' to create a new string (for some reason it doesn't like it if you just try $('%(Filename)'.Replace('.config',''))), then call the replace function on the string.

全文应如下所示:

<Target Name="Build">
        <Message Text="@(Files->'$([System.String]::Copy(&quot;%(Filename)&quot;).Replace(&quot;.config&quot;,&quot;&quot;))')" />
</Target>

<小时>

MSBuild 12.0 似乎打破了上述方法.作为替代方案,我们可以向所有现有的 Files 项添加新的元数据条目.我们在定义元数据项时执行替换,然后我们可以像访问任何其他元数据项一样访问修改后的值.


MSBuild 12.0 seems to have broken the above method. As an alternative, we can add a new metadata entry to all existing Files items. We perform the replace while defining the metadata item, then we can access the modified value like any other metadata item.

例如

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <ItemGroup>
        <Files Include="Alice.jpg"/>
        <Files Include="Bob.not-config.gif"/>
        <Files Include="Charlie.config.txt"/>
    </ItemGroup>

    <Target Name="Build">
        <ItemGroup>
            <!-- 
            Modify all existing 'Files' items so that they contain an entry where we have done our replace.
            Note: This needs to be done WITHIN the '<Target>' (it's a requirment for modifying existing items like this
            -->
            <Files>
                <FilenameWithoutConfig>$([System.String]::Copy('%(Filename)').Replace('.config', ''))</FilenameWithoutConfig>
            </Files>
        </ItemGroup>

        <Message Text="@(Files->'%(FilenameWithoutConfig)')" Importance="high" />
    </Target>
</Project>

结果:

D:	emp>"c:Program Files (x86)MSBuild12.0BinMSBuild.exe" /nologo test.xml
Build started 2015/02/11 11:19:10 AM.
Project "D:	emp	est.xml" on node 1 (default targets).
Build:
  Alice;Bob.not-config;Charlie
Done Building Project "D:	emp	est.xml" (default targets).

这篇关于在 MSBuild 中,我可以对 MetaData 项使用 String.Replace 函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆