Is there an MSBuild task that will write lines to

2019-06-24 07:20发布

问题:

I'm using a WriteLinesToFile to update a change log file (txt). It appends the text to the end of the file. Ideally, I'd like to be able to write the changes to the start of this file.

Is there a simple task (e.g. in the Community or Extension packs) that does this?

回答1:

I haven't seen something like that in the custom task pack.

You could cheat by using ReadLinesFromFile and WriteLinesToFile :

<PropertyGroup>
  <LogFile>log.txt</LogFile>
</PropertyGroup>

<ItemGroup>
  <Log Include="Line1"/>
  <Log Include="Line2"/>
</ItemGroup>

<Target Name="WriteFromStart">
  <ReadLinesFromFile File="$(LogFile)" Condition="Exists('$(LogFile)')">
    <Output TaskParameter="Lines" ItemName="Log"/>
  </ReadLinesFromFile>

  <WriteLinesToFile File="$(LogFile)" 
                    Lines="@(Log)" 
                    Condition="@(Log) != '' And (@(Log) != '\r\n' Or @(Log) != '\n')"
                    Overwrite="true">
  </WriteLinesToFile>
</Target>

Or you could create a custom task.