MSBuild ReadLinesFromFile 一行上的所有文本
当我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里的问题是您以非预期的方式使用
ReadLinesFromFile
任务。因此,它不仅仅是从文件中读取所有文本,而是从文件中读取各个项目并返回 ITaskItems 的项目组。 每当您使用
@()
语法输出项目列表时,您都会得到一个分隔列表,默认值是分号。 此示例说明了这种行为:输出如下所示:
因此,虽然问题的最佳解决方案是编写一个 MSBuild 任务,将文件作为字符串而不是项目列表读入属性 ,这确实不是您所要求的。 您询问是否有方法将它们放回去,可以使用MSBuild 转换。
转换用于从另一个列表创建一个列表,并且还能够使用自定义分隔符进行转换。 因此,答案是将使用
ReadItemsFromFile
读取的列表转换为另一个带有换行符的列表。 下面是一个执行此操作的示例:Test.text 如下所示:
输出如下所示:
这里发生的是两件事。
Identity
) 但使用自定义分隔符'%0a%0d'
将列表从一种类型转换为另一种类型,%0a
) 和回车符(%0d
)The problem here is you are using the
ReadLinesFromFile
task in a manner it wasn't intended.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:And the output looks like this:
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:Test.text looks like:
And the output looks like this:
What's going on here is two things.
Identity
) but a custom separator'%0a%0d'
%0a
) and carriage return (%0d
)如果您使用的是 MSBuild 4.0,则可以执行以下操作来获取文件的内容:
If you are using MSBuild 4.0, you can do the following instead, to get the contents of a file:
而不是 @(FileContents->'%(Identity)', '%0a%0d') 我相信你可以做 @(FileContents, '%0a%0d')
Instead of @(FileContents->'%(Identity)', '%0a%0d') I believe you can do @(FileContents, '%0a%0d')
您可以将 WriteLinesToFile 与
This 结合使用,这将准确复制您的文件。
You can use WriteLinesToFile combined with
This will copy your file exactly at it is.