MSBuild ReadLinesFromFile 一行上的所有文本 [英] MSBuild ReadLinesFromFile all text on one line

查看:18
本文介绍了MSBuild ReadLinesFromFile 一行上的所有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

</ReadLinesFromFile></目标><Target Name="MailUsers" DependsOnTargets="ReadReleaseNotes" ><Mail SmtpServer="$(MailServer)"To="$(MyEMail)"来自="$(MyEMail)"主题="测试邮件任务"Body="@(ReleaseNoteItems)"/></目标><目标名称="部署"><CallTarget Targets="MailUsers"/></目标></项目>

我从通常看起来像这样的文件中获取文本

<块引用>

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

就这样出来

<块引用>

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

我知道 ReadLinesFromFile 的代码会一次拉出一行数据并去掉回车符.

有没有办法把它们放回去?所以我的电子邮件看起来格式很好?

谢谢

解决方案

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

<块引用>

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

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

输出如下所示:

 ItemGroup Color: Red;Blue;Green

因此,虽然解决您的问题的最佳方法是编写一个 MSBuild 任务,该任务将文件作为字符串而不是项目列表读入 属性 中,但这实际上不是您所要求的.您询问是否有方法将它们放回原处,并且正在使用 MSBuild 转换.

转换用于从另一个列表创建一个列表,并且还能够使用自定义分隔符进行转换.因此,答案是将使用 ReadItemsFromFile 读入的列表转换为另一个带有换行符的列表.这是一个可以做到这一点的示例:

Test.text 看起来像:

红色绿蓝色

输出如下所示:

[C:	emp]:: msbuild test.projMicrosoft (R) 构建引擎版本 3.5.21022.8[Microsoft .NET Framework,版本 2.0.50727.1433]版权所有 (C) Microsoft Corporation 2007.保留所有权利.构建于 2008 年 11 月 8 日上午 8:16:59 开始.在节点 0(默认目标)上项目C:	emp	est.proj".文件内容:红色;绿色;蓝色文件内容转换:红色绿蓝色完成构建项目C:	emp	est.proj"(默认目标).构建成功.0 警告0 错误已用时间 00:00:00.03

这里发生的事情有两件事.

@(FileContents->'%(Identity)', '%0a%0d')

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

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)MSBuildCommunityTasksMSBuild.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

解决方案

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:	emp]:: 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:	emp	est.proj" on node 0 (default targets).
  FileContents: Red;Green;Blue
  FileContents Transformed: Red
Green
Blue
Done Building Project "C:	emp	est.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)

这篇关于MSBuild ReadLinesFromFile 一行上的所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆