如何使用msbuild替换文件中的字符串?

时间:2011-10-20 14:40:21

标签: msbuild msbuild-task msbuild-propertygroup msbuild-buildengine

我想在另一个文件xy.xml中使用字符串“我很好”替换文件test.xml中的“how r u”字符串。使用ms build中的正则表达式。

即我必须从一个文件(xy.xml)中读取字符串并将其替换为另一个文件test.xml。 所以请通过示例

提供解决此问题的必要步骤

8 个答案:

答案 0 :(得分:83)

现在不再需要了......现在可以将C#注入项目/构建文件......

定义自定义任务和参数,如下所示:

<UsingTask TaskName="ReplaceFileText" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
  <ParameterGroup>
    <InputFilename ParameterType="System.String" Required="true" />
    <OutputFilename ParameterType="System.String" Required="true" />
    <MatchExpression ParameterType="System.String" Required="true" />
    <ReplacementText ParameterType="System.String" Required="true" />
  </ParameterGroup>
  <Task>
    <Reference Include="System.Core" />
    <Using Namespace="System" />
    <Using Namespace="System.IO" />
    <Using Namespace="System.Text.RegularExpressions" />
    <Code Type="Fragment" Language="cs">
      <![CDATA[
            File.WriteAllText(
                OutputFilename,
                Regex.Replace(File.ReadAllText(InputFilename), MatchExpression, ReplacementText)
                );
          ]]>
    </Code>
  </Task>
</UsingTask>

然后只需像任何其他MSBuild任务一样调用它

<Target Name="AfterBuild">
  <ReplaceFileText 
    InputFilename="$(OutputPath)File.exe.config" 
    OutputFilename="$(OutputPath)File.exe.config" 
    MatchExpression="\$version\$" 
    ReplacementText="1.0.0.2" />
</Target>

以上示例替换了&#34; $ version $&#34;用&#34; 1.0.0.2&#34;在&#34; File.exe.config&#34;位于输出目录中。

答案 1 :(得分:4)

编辑:这个答案已经过时了。使用以下解决方案......

使用ReadLinesFromFile任务从xy.xml文件中获取替换字符串。 Check this

然后使用xy.xml中的值作为FileUpdate任务的替换字符串。 Check this

并将它们放在一起;)

答案 2 :(得分:4)

有一种非常简单的方法可以替换文件中的字符串:

  <Target Name="Replace" AfterTargets="CoreCompile">
      <PropertyGroup>
          <InputFile>c:\input.txt</InputFile>
          <OutputFile>c:\output.txt</OutputFile>
      </PropertyGroup>
      <WriteLinesToFile
          File="$(OutputFile)"
          Lines="$([System.IO.File]::ReadAllText($(InputFile)).Replace('from','to'))"
          Overwrite="true"
          Encoding="Unicode"/>
  </Target>

请参见https://docs.microsoft.com/en-us/visualstudio/msbuild/property-functions?view=vs-2019 探索难以理解的C#代码。 [System.Text.RegularExpressions.Regex]已包含在列表中。

答案 3 :(得分:3)

您可以使用文章中所述的MSBuild社区任务中的任务FileUpdate http://geekswithblogs.net/mnf/archive/2009/07/03/msbuild-task-to-replace-content-in-text-files.aspx

答案 4 :(得分:1)

@csharptest.net的回答很好,但是在DotNetCore上不起作用。我本来可以将此添加为评论,但我的声誉不高。

在DotNetCore上,您必须更新:

  • 任务工厂到“ RoslynCodeTaskFactory”
  • 任务组装到“ $(MSBuildToolsPath)\ Microsoft.Build.Tasks.Core.dll”
  • 删除对“ System.Core”的引用
  • 使用方必须将“ AfterTargets”属性指定为“ Build”

其他所有内容都应该相同:

<Project Sdk="Microsoft.NET.Sdk.Web">
  ...

  <UsingTask
    TaskName="ReplaceFileText"
    TaskFactory="RoslynCodeTaskFactory"
    AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
    <ParameterGroup>
      <InputFilename ParameterType="System.String" Required="true" />
      <OutputFilename ParameterType="System.String" Required="true" />
      <MatchExpression ParameterType="System.String" Required="true" />
      <ReplacementText ParameterType="System.String" Required="true" />
    </ParameterGroup>
    <Task>
      <Using Namespace="System"/>
      <Using Namespace="System.IO"/>
      <Using Namespace="System.Text.RegularExpressions" />
      <Code Type="Fragment" Language="cs">
        <![CDATA[  
          File.WriteAllText(
            OutputFilename,
            Regex.Replace(File.ReadAllText(InputFilename), MatchExpression, ReplacementText)
            );
        ]]>
      </Code>
    </Task>
  </UsingTask>

  <Target Name="AfterBuildStep" AfterTargets="Build">
    <ReplaceFileText
       InputFilename="$(OutputPath)File.exe.config" 
       OutputFilename="$(OutputPath)File.exe.config" 
       MatchExpression="\$version\$" 
       ReplacementText="1.0.0.2" />
  </Target>
</Project>

答案 5 :(得分:1)

如果您不想使用第三方(社区)二进制文件,也不希望将代码嵌入到msbuild项目中,建议您创建一个简单的任务库,该库实现File.WriteAllText并可以以后托管其他任务:

using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

public class FileWriteAllText : Task
{
    [Required]
    public string Path { get; set; }
    [Required]
    public string Contents { get; set; }

    public override bool Execute()
    {
        File.WriteAllText(Path, Contents);
        return true;
    }
}

然后您可以在msbuild中进行替换,追加等操作

<UsingTask TaskName="FileWriteAllText" AssemblyFile="MyTasks.dll" />
<FileWriteAllText Path="test.xml"
     Contents="$([System.Text.RegularExpressions.Regex]::Replace(
         $([System.IO.File]::ReadAllText('test.xml')), 'how r u', 'i am fine'))" />

答案 6 :(得分:0)

我针对位于Unix驱动器上的同一个文件运行了这两个替换,并使用了unc路径到它\ server \ path ...:

<ReplaceFileText
  InputFilename="$(fileToUpdate)"
  OutputFilename="$(fileToUpdate)"
  MatchExpression="15.0.0"
  ReplacementText="15.3.1"/>


<FileUpdate Files="$(fileToUpdate2)"
            Regex="15.0.0"
            ReplacementText="15.3.1" />

并且上面的cs自定义操作不会添加bom;但是FileUpdate做了:

%head -2 branding.h branding2.h
==> branding.h <==
#/* branding.h
#** This file captures common branding strings in a format usable by both sed and C-preprocessor.

==> branding2.h <==
#/* branding.h
#** This file captures common branding strings in a format usable by both sed and C-preprocessor.

谢谢csharptest.net - 我正在使用perl替换命令执行exec的unix版本。

答案 7 :(得分:0)

更新以回答詹姆斯的问题

  <UsingTask TaskName="ReplaceTextInFiles" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.$(VsBuildTaskBinarySuffix).dll">
<ParameterGroup>
  <MatchExpression ParameterType="System.String" Required="true" />
  <ReplacementExpression ParameterType="System.String" Required="true" />
  <InputFile ParameterType="Microsoft.Build.Framework.ITaskItem" Required="true" />      
  <IsTextReplaced ParameterType="System.Boolean"  Output="True"/>
</ParameterGroup>
<Task>
  <Reference Include="System.Core" />
  <Using Namespace="System" />
  <Using Namespace="System.IO" />
  <Using Namespace="System.Text.RegularExpressions" />
  <Code Type="Fragment" Language="cs">
    <![CDATA[
      bool isMatchFound = false;
      string filecontent = "";
      string path = InputFile.ItemSpec;

      Log.LogMessage(MessageImportance.High, "[ReplaceTextInFiles]: Match= " + MatchExpression);
      Log.LogMessage(MessageImportance.High, "[ReplaceTextInFiles]: Replace= " + ReplacementExpression);

      IsTextReplaced = false;
      using(StreamReader rdr = new StreamReader(path))
      {
        filecontent = rdr.ReadToEnd();
        if (Regex.Match(filecontent, MatchExpression).Success)
        {
          filecontent = Regex.Replace(filecontent, MatchExpression, ReplacementExpression);
          isMatchFound = true;            
        }
      }

      if(isMatchFound){
        using(StreamWriter wrtr = new StreamWriter(path))
        {
          wrtr.Write(filecontent);
          IsTextReplaced = true;
          Log.LogMessage(MessageImportance.Normal, "[ReplaceTextInFiles]: Replaced text in file:" + path);
        }
      }       
    ]]>
  </Code>
</Task>

相关问题