MSBuild ReadLinesFromFile 一行上的所有文本

发布于 2024-07-08 17:19:09 字数 1359 浏览 10 评论 0原文

当我对 MSBUILD 中的文件执行 ReadLinesFromFile 并再次输出该文件时,我将所有文本放在一行上。 所有回车符和换行符都被删除。

<Project DefaultTargets = "Deploy"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
<Import  Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>

<ItemGroup>
    <MyTextFile Include="$(ReleaseNotesDir)$(NewBuildNumber).txt"/>
</ItemGroup>

<Target Name="ReadReleaseNotes">
    <ReadLinesFromFile
        File="@(MyTextFile)" >
        <Output
            TaskParameter="Lines"
            ItemName="ReleaseNoteItems"/>
    </ReadLinesFromFile>
</Target>

<Target Name="MailUsers" DependsOnTargets="ReadReleaseNotes" >
    <Mail SmtpServer="$(MailServer)"
        To="$(MyEMail)"
        From="$(MyEMail)"
        Subject="Test Mail Task"
        Body="@(ReleaseNoteItems)" />
</Target>
<Target Name="Deploy">
    <CallTarget Targets="MailUsers" />
</Target>

</Project>

我从文件中获取文本,通常如下所示

- BLAH 的新部署工具 

  - 随机其他东西()"" 
  

像这样出来

- BLAH 的新部署工具;- 随机其他东西()"" 
  

我知道 ReadLinesFromFile 的代码将一次将数据拉入一行并去掉回车符。

有办法把它们放回去吗? 那么我的电子邮件看起来格式很好吗?

谢谢

When I do a ReadLinesFromFile on a file in MSBUILD and go to output that file again, I get all the text on one line. All the Carriage returns and LineFeeds are stripped out.

<Project DefaultTargets = "Deploy"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
<Import  Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>

<ItemGroup>
    <MyTextFile Include="$(ReleaseNotesDir)$(NewBuildNumber).txt"/>
</ItemGroup>

<Target Name="ReadReleaseNotes">
    <ReadLinesFromFile
        File="@(MyTextFile)" >
        <Output
            TaskParameter="Lines"
            ItemName="ReleaseNoteItems"/>
    </ReadLinesFromFile>
</Target>

<Target Name="MailUsers" DependsOnTargets="ReadReleaseNotes" >
    <Mail SmtpServer="$(MailServer)"
        To="$(MyEMail)"
        From="$(MyEMail)"
        Subject="Test Mail Task"
        Body="@(ReleaseNoteItems)" />
</Target>
<Target Name="Deploy">
    <CallTarget Targets="MailUsers" />
</Target>

</Project>

I get the text from the file which normally looks like this

- New Deployment Tool for BLAH

- Random other stuff()""

Coming out like this

- New Deployment Tool for BLAH;- Random other stuff()""

I know that the code for ReadLinesFromFile will pull the data in one line at a time and strip out the carriage returns.

Is there a way to put them back in?
So my e-mail looks all nicely formatted?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

坠似风落 2024-07-15 17:19:09

这里的问题是您以非预期的方式使用 ReadLinesFromFile 任务。

ReadLinesFromFile 任务
从文本文件中读取项目列表。

因此,它不仅仅是从文件中读取所有文本,而是从文件中读取各个项目并返回 ITaskItems 的项目组。 每当您使用 @() 语法输出项目列表时,您都会得到一个分隔列表,默认值是分号。 此示例说明了这种行为:

<?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>

输出如下所示:

  ItemGroup Color: Red;Blue;Green

因此,虽然问题的最佳解决方案是编写一个 MSBuild 任务,将文件作为字符串而不是项目列表读入属性 ,这确实不是您所要求的。 您询问是否有方法将它们放回去,可以使用MSBuild 转换

转换用于从另一个列表创建一个列表,并且还能够使用自定义分隔符进行转换。 因此,答案是将使用 ReadItemsFromFile 读取的列表转换为另一个带有换行符的列表。 下面是一个执行此操作的示例:

<?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 如下所示:

Red
Green
Blue

输出如下所示:

[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

这里发生的是两件事。

@(FileContents->'%(Identity)', '%0a%0d')   
  • 我们正在使用相同的值 (Identity) 但使用自定义分隔符 '%0a%0d' 将列表从一种类型转换为另一种类型,
  • 我们正在使用 MSBuild 转义 以转义换行符 (%0a) 和回车符(%0d)

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)
李不 2024-07-15 17:19:09

如果您使用的是 MSBuild 4.0,则可以执行以下操作来获取文件的内容:

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

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))
携君以终年 2024-07-15 17:19:09

而不是 @(FileContents->'%(Identity)', '%0a%0d') 我相信你可以做 @(FileContents, '%0a%0d')

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

疾风者 2024-07-15 17:19:09

您可以将 WriteLinesToFile 与

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

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

This 结合使用,这将准确复制您的文件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文