2

我正在使用 MSBuild 扩展包替换 .proj 文件中的行。我用多行替换多行。即使我进行转换,它输出的行末尾仍然有一个分号。

<ItemGroup>
    <TestFile Include="regextest.xml" />
    <MyLines Include ="%3CItemGroup%3E%0A"/>
    <MyLines Include ="%09%3CReference Include=%22Stuff%22%3E%0A" />
    <MyLines Include ="%09%09%3CHintPath%3E..\..\packages\secret.dll%3C/HintPath%3E%0A" />
    <MyLines Include ="%09%09%3CPrivate%3ETrue%3C/Private%3E%0A" />
    <MyLines Include ="%09%3C/Reference%3E%0A" />
    <MyLines Include ="%3C/ItemGroup%3E%0A" />
</ItemGroup>


<Target Name="Default">
    <MSBuild.ExtensionPack.FileSystem.File TaskAction="Replace" 
      TextEncoding="ASCII" 
      RegexPattern="%3CProjectReference"
      RegexOptionList="IgnoreCase"
      Replacement="@(MyLines->'%(Identity)')"
      Files="@(TestFile)" />
</Target>

这是输出:

<ItemGroup>
;   <Reference Include="Stuff">
;       <HintPath>..\..\packages\secret.dll</HintPath>
;       <Private>True</Private>
;   </Reference>
;</ItemGroup>

在没有转换的情况下进行操作仍然有它们。

4

1 回答 1

2

处理多行替换字符串的一种简单方法是将它们形成在CDATA属性内部的块中,而不是单行项的集合(这是分号的来源)。在这种情况下,您可以将多行替换字符串创建为属性,然后将其值分配给项目,然后将项目传递给Replace任务操作:

<PropertyGroup>
  <MyMultiLine>
  <![CDATA[
  %3CItemGroup%3E
    %3CReference Include="Stuff"%3E
      %3CHintPath%3E..\..\packages\secret.dll%3C/HintPath%3E
      %3CPrivate>True%3C/Private%3E
    %3C/Reference%3E
  %3C/ItemGroup%3E
  ]]>
  </MyMultiLine>
</PropertyGroup>

<ItemGroup>
  <TestFile Include="regextest.xml" />
  <MyMultiLineItem Include="$(MyMultiLine)" />
</ItemGroup>

<Target Name="Default">
    <MSBuild.ExtensionPack.FileSystem.File TaskAction="Replace" 
      TextEncoding="ASCII" 
      RegexPattern="%3CProjectReference"
      RegexOptionList="IgnoreCase"
      Replacement="@(MyMultiLineItem ->'%(Identity)')"
      Files="@(TestFile)" />
</Target>
于 2013-10-18T20:07:20.317 回答