MSBuild ReadLinesFromFileFile一行上的所有文本 [英] MSBuild ReadLinesFromFile all text on one line
问题描述
当我在MSBUILD中的文件上执行ReadLinesFromFile并再次输出该文件时,我将所有文本都放在一行中。所有的回车和LineFeeds都被删除。
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()""
像这样出来
- New Deployment Tool for BLAH;- Random other stuff()""
我知道ReadLinesFromFile的代码将一次拉一条线,并删除回车。
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?
谢谢
推荐答案
这里的问题是您以不想要的方式使用 ReadLinesFromFile
任务。
ReadLinesFromFile任务
从文本文件中读取项目的列表。
所以不仅仅是从文件中读取所有的文本,而是从一个文件读取个别的项目并返回一个ITaskItems的项目组。每当您使用 @()
语法输出项目列表时,您将得到一个分隔的列表,其默认值为分号。这个例子说明了这个行为:
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>
输出如下所示:
ItemGroup Color: Red;Blue;Green
最好的解决方案是编写一个MSBuild任务,将一个文件读入一个属性,作为一个不是项目列表的字符串,这不是你要求的。你问是否有一个方式将它们放回来,并且使用 MSBuild变换。
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.
变换用于从另一个创建一个列表,并且还可以使用自定义分隔符进行转换。所以答案是使用 ReadItemsFromFile
将您的列表转换为带有换行符的另一个列表。这是一个例子:
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看起来像:
Test.text looks like:
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')
- 我们正在使用 MSBuild Escaping 以转义换行符(
%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
)
<我们正在使用相同的值(
Identity
)将列表从一个类型转换为另一个类型,但是自定义分隔符'%0a%0d'
这篇关于MSBuild ReadLinesFromFileFile一行上的所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!