Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an MSBuild task that will write lines to the *start* of a file?

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?

like image 921
dommer Avatar asked Dec 16 '22 22:12

dommer


1 Answers

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.

like image 120
Julien Hoarau Avatar answered May 17 '23 09:05

Julien Hoarau