Read the text file ,split the line in a text file and send the email for that line using nant scripting - nant

I have a below requirement to be done using nant scripting
1) Read the text file line by line and split the line
2) Each line should be sent as an email to the concerned team

Using the <foreach /> and <mail /> tasks. Here's a quick snippet from the excellent NAnt documentation:
<foreach item="Line" in="yourfile.txt" delim="," property="x,y">
<mail message="Read pair ${x}=${y}" />
</foreach>

Related

NLog not using UTF-8 encoding

Since I upgraded NLog to the latest version 5.1.0, logging special characters (e.g. Ö, ä or ß) stopped working. Outout to file is like: ö ä ü for these chars.
I checked the log file with NotePad++ and it says that the encoding is ANSI. However, I have configured NLog to use utf-8. My config:
<target name="logfile" xsi:type="File" fileName="${basedir}/Logs/nLog.csv" archiveAboveSize="50000000" archiveNumbering="Sequence" maxArchiveFiles="3" encoding="utf-8" keepFileOpen="true">
<layout xsi:type="CsvLayout">
<column name="time" layout="${longdate}" />
<column name="level" layout="${level}"/>
<column name="category" layout="${event-context:item=category}" />
<column name="message" layout="${message}" />
</layout>
</target>
What am I missing?
Based on the comments, I was able to solve the problem. However, I do not know what the real cause of the problem was.
Solution: remove encoding="utf-8" from the nLog config AND read the log file as Encoding.UTF-8 (i.e. when opening the file with a StreamReader).
I don't know why this works since nLog uses UTF-8 as default...

How to ignore special characters in File Transformation Task of Azure DevOps Pipeline

I am having xyz.config file like below. I have created xyx.Release.config file to use in File Transformation task.
<formatters>
<add template="Timestamp: {timestamp}
Message: {message}
name="Reduced Text Formatter" />
</formatters>
After the File Transformation task in pipeline it transformed as below. The special characters are replaced after the execution of File Transformation task.
<formatters>
<add template="Timestamp: {timestamp}
Message: {message}
-
</formatters>
Anyone please let me know how to ignore special character transformation as part of File Transformation task.
Thanks in advance.
Mohan
As far as I know, the File transform task doesn't support to ignore special character transformation.
The file transfrom task will use the built-in method to do the file transform option. It doesn't support to use custom method to do the option.
Here is the doc about the built-in method: Web.config Transformation Syntax for Web Project Deployment Using Visual Studio
It supports overall replacement of strings, but currently does not support replacing only the part of value in the string and ignoring others.
For a workaround, you can use the replace method then just change the part of value in the string to keep the format of the string.
For example: xyx.Release.config
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<formatters>
<add template="Timestamp: test1
Message: test2
" name="Reduced Text Formatter" xdt:Transform="Replace" xdt:Locator="Match(template)" />
</formatters>
</configuration>

URLEncode within NAnt

Is there a way to URLEncode something in NAnt generally - specifically in an echo to a file? One of my build processes enumerates all the PDF output files in a folder and makes an index.html, but some of the PDF files have [] characters in them and they need to be URLEncoded to %5D/%5B in the hrefs
<echo append="false" file="${reportlayout.dir}\index.html"><html><head><title>Product ${modulename} Report PDFs</title><head><body></echo>
<foreach item="File" property="filename">
<in>
<items>
<include name="${reportlayout.dir}\${modulename}/*.pdf" />
</items>
</in>
<do>
<echo append="true" file="${reportlayout.dir}\index.html"><a href="${modulename}/${path::get-file-name(filename)}">${path::get-file-name(filename)}</a><br/></echo>
</do>
</foreach>
<echo append="true" file="${reportlayout.dir}\index.html"></body></html></echo>
This is where href="${modulename}/${path::get-file-name(filename)}" contains characters coming from the filename that need to be URLEncoded, but I can't find a function to do that in the NAnt function list and I don't know if there is a way to get it to call through to .NET HttpUtility.URLEncode or similar.

How do I optionally require a command line arguement for Ant?

I'm new to ant, and I want to require a file name if something other than the default target is used, so the calling syntax would be something like this:
ant specifictarget -Dfile=myfile
I'm using the ant contrib package to give me additional functionality, so I have this:
<if>
<equals arg1="${file}" arg2="" />
<then>
<!-- fail here -->
</then>
</if>
My thinking is that if file was not specified it might be equal to the empty string. Obviously, this didn't work, and I'm not finding any examples on google or the right syntax in the manual.
So what syntax should I use?
You don't really need the contrib package. This is more conveniently done using built-in ant capabilities like if/unless and depends. See below:
<target name="check" unless="file" description="check that the file property is set" >
<fail message="set file property in the command line (e.g. -Dfile=someval)"/>
</target>
<target name="specifictarget" if="file" depends="check" description=" " >
<echo message="do something ${file}"/>
</target>
You've got the right idea. The
ant specifictarget -Dfile=myfile
sets Ant Properties from the command line. All you really need is
<property name="file" value=""/>
for your default value. That way if file is not specified, it will be equal to the empty string.
Since properties are not mutable in Ant, you can add this:
<property name="file" value="" />
This will set the property file to an empty string if it hasn't already been set on the command line. Then your equality test will work as you intended.
Alternately, you can use escape the value since ant just spits out the actual text when it can't do a property substitution.
<if>
<equals arg1="${file}" arg2="$${file}" />
<then>
<echo>BARF!</echo>
</then>
</if>

MSBuild ReadLinesFromFile all text on one line

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 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)
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))
Instead of #(FileContents->'%(Identity)', '%0a%0d') I believe you can do #(FileContents, '%0a%0d')
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.