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
The problem here is you are using the
ReadLinesFromFiletask 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
ReadItemsFromFileinto 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)