MSBuild ReadLinesFromFile all text on one line

纵饮孤独 提交于 2019-11-28 07:39:05
Todd

The problem here is you are using the ReadLinesFromFile task in a manner it wasn't intended.

ReadLinesFromFile Task
Reads a list of items from a text file.

So it's not just reading all the text from a file, it's reading individual items from a file and returning an item group of ITaskItems. Whenever you output a list of items using the @() syntax you will get a separated list, the default of which is a semicolon. This example illustrates this behavior:

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

    <ItemGroup>
        <Color Include="Red" />
        <Color Include="Blue" />
        <Color Include="Green" />
</ItemGroup>

<Target Name="Build">
        <Message Text="ItemGroup Color: @(Color)" />
</Target>

</Project>

And the output looks like this:

  ItemGroup Color: Red;Blue;Green

So while the best solution to your problem is to write an MSBuild task that reads a file into a property as a string an not a list of items, that's really not what you asked for. You asked if there was a way to put them back, and there is using MSBuild Transforms.

Transforms are used to create one list from another and also have the ability to transform using a custom separator. So the answer is to transform your list read in using ReadItemsFromFile into another list with newlines. Here is an example that does just that:

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

    <ItemGroup>
        <File Include="$(MSBuildProjectDirectory)\Test.txt" />
    </ItemGroup>

    <Target Name="Build">
        <ReadLinesFromFile File="@(File)">
            <Output TaskParameter="Lines" ItemName="FileContents" />
        </ReadLinesFromFile>

        <Message Text="FileContents: @(FileContents)" />
        <Message Text="FileContents Transformed: @(FileContents->'%(Identity)', '%0a%0d')" />
    </Target>

</Project>

Test.text looks like:

Red
Green
Blue

And the output looks like this:

[C:\temp]:: msbuild test.proj
Microsoft (R) Build Engine Version 3.5.21022.8
[Microsoft .NET Framework, Version 2.0.50727.1433]
Copyright (C) Microsoft Corporation 2007. All rights reserved.

Build started 11/8/2008 8:16:59 AM.
Project "C:\temp\test.proj" on node 0 (default targets).
  FileContents: Red;Green;Blue
  FileContents Transformed: Red
Green
Blue
Done Building Project "C:\temp\test.proj" (default targets).


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.03

What's going on here is two things.

@(FileContents->'%(Identity)', '%0a%0d')   
  • We are transforming the list from one type to another using the same values (Identity) but a custom separator '%0a%0d'
  • We are using MSBuild Escaping to escape the line feed (%0a) and carriage return (%0d)

If you are using MSBuild 4.0, you can do the following instead, to get the contents of a file:

$([System.IO.File]::ReadAllText($FilePath))

Instead of @(FileContents->'%(Identity)', '%0a%0d') I believe you can do @(FileContents, '%0a%0d')

romi ares

You can use WriteLinesToFile combined with

$([System.IO.File]::ReadAllText($(SourceFilePath))):

< WriteLinesToFile File="$(DestinationFilePath)"  Lines="$([System.IO.File]::ReadAllText($(SourceFilePath)))"
                      Overwrite="true" 
                      />

This will copy your file exactly at it is.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!