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

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>

Related

XDT Transform not working for applicationHost.xdt on Azure - Environment variables are ignored

It seems like environment variables are being ignored in my xdt transform for applicationHost.
I've created the following file applicationHost.xdt on azure in the \home\site folder. It does NOT perform the transform on applicationHost.config
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">>
<system.applicationHost>
<applicationPools>
<add name="%WEBSITE_SITE_NAME%" xdt:Locator="Match(name)">
<recycling disallowOverlappingRotation="true" xdt:Transform="Insert" />
</add>
<add name="%WEBSITE_IIS_SITE_NAME%" xdt:Locator="Match(name)">
<recycling disallowOverlappingRotation="true" xdt:Transform="Insert" />
</add>
</applicationPools>
</system.applicationHost>
</configuration>
If I adjust the %WEBSITE_SITE_NAME% to say "dev-mysitename.com" the transforms work properly.
Why are the Environment variables not working properly? I need this to work so my different environments will work properly.
You cannot use Environment Variables like this. Its just not possible.

How do I replace the connection string element

I have an issue I cannot seem to solve using XML Transform. My web.config contains the following
<connectionStrings>
<add name="SQLConnString"
connectionString="yada yada"
providerName="System.Data.SQLClient"/>
</connectionStrings>
My QA server and above has this
<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
<EncryptedData>
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
</EncryptedData>
</connectionStrings>
What task do I use in DevOps to achieve this kind of transform or what's the best way to go about this kind of transformation.
jc
Not sure the exactly caused you mentioned is what, but you can use File transform task to achieve the above transformation. I have tested it again and it work very fine on my side.
steps:
- task: FileTransform#1
displayName: 'File Transform: '
inputs:
folderPath: '$(System.DefaultWorkingDirectory)'
enableXmlTransform: true
xmlTransformationRules: '-transform **\*.Dev.config -xml **\*.config'
fileType: xml
For the rules syntax in this task definition, please follow:
-transform <path to the transform file> -xml <path to the source file> -result <path to the result file>
In your scenario, you need also ensure your transform file is configured available. Before run it in azure devops pipeline, you can verify it firstly here.
Updated in 2/20/2020:
Based on your comments, the trouble for you to apply file transform is on transforma file configured. Though you have got the method of powershell, but I also help you written a transform file so that you can have more choice here.
<connectionStrings xdt:Transform="Remove">
<add name="SQLConnString"
connectionString="yada yada"
providerName="System.Data.SQLClient" />
</connectionStrings>
<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider" xdt:Transform="Insert">
<EncryptedData>
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" xdt:Transform="Replace"/>
</EncryptedData>
</connectionStrings>
You can see my transformation result:

Update Build number in App config xml file on build pipeline

I have a build pipeline in Azure DevOps, I need to update the build number in my apconfig exe file that will be $(Build.BuildNumber).
I just tried this way:
Adding a variable name = BuildNumber value = $(Build.BuildNumber).
And in my apconfig.exe file have a key same like <add key="BuildNumber" value="1812201901" />.
Why I have tried like this way: thinking like it will update in the config file if variable name match with the key.
But it is not working. can anyone please help? I have just started in CI/CD.
Update Build number in App config xml file on build pipeline
Just like the Shayki said, using the Replace Tokens extension should be the directly way to resolve this issue.
But since you need to request to get this extension, as workaround, you could also use power shell scripts to resolve this issue, you can check below my test powershell scripts:
$currentDirectory = [IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Path)
$appConfigFile = [IO.Path]::Combine($currentDirectory, 'App.config')
$appConfig = New-Object XML
$appConfig.Load($appConfigFile)
foreach($BuildNumber in $appConfig.configuration.add)
{
'name: ' + $BuildNumber.name
'BuildNumber: ' + $BuildNumber.value
$BuildNumber.value = '123456789'
}
$appConfig.Save($appConfigFile)
As result, the app.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<add key="BuildNumber" value="123456789" />
</configuration>
Note: Set the powershell scripts at the same folder of the app.config file.
Hope this helps.
You can use the Replace Tokens extension and in the apconfig.exe file put this:
<add key="BuildNumber" value="__BuildNumber__" />
Configure the task to search variables with __ prefix and suffix:
Now the value will be replaced with the value of the BuildNumber variable you configured (equal to Build.BuildNumber).

How to write AssemblyVersion to file using MSBuild?

FinalEdit: Despite relative directories not working in the first post, it worked if I simply removed the $(MsBuildThisFileDirectory) from the Exec line.
Edit2: I added the new targets to the DefaultTargets. Which now runs them by default. However, timing was now off with the postbuild command. I added <Exec Command="call $(MsBuildThisFileDirectory)documentation\tools\GenerateDocumentation.bat" IgnoreExitCode="false" /> to the target, but it gives an error that C:\Users\my is not a valid batch file because of the space which is actually C:\Users\my program\documentation\tools\GenerateDocumentation.bat. Putting quotes around the path gives me error MSB4025 that Name cannot begin with $.
Edit: I have tried stijn's code and it works when I explicitly run it from the command line using /t:RetrieveIdentities, but for some reason it doesn't seem to run otherwise.
I have been using Doxygen to generate documentation for my source code, however, I would like to be able to do it automatically. I wrote a simple .bat script to run Doxygen with my desired config file and compile the output into a .chm help file, but I have been unable to change the revision number automatically in Doxygen.
I was attempting to simply update the config file by adding a new line to the config file with the new revision number using MSBuild, but I have been unable to get anything to print or even create a new file when none is present.
The code I have so far I have gotten from other similar questions, but I cannot seem to get it to work.
<ItemGroup>
<MyTextFile Include="\documentation\DoxygenConfigFile.doxyconfig"/>
<MyItems Include="PROJECT_NUMBER = %(MyAssemblyIdentitiesAssemblyInfo.Version)"/>
</ItemGroup>
<Target Name="RetrieveIdentities">
<GetAssemblyIdentity AssemblyFiles="bin\foo.exe">
<Output TaskParameter="Assemblies" ItemName="MyAssemblyIdentities"/>
</GetAssemblyIdentity>
<WriteLinesToFile File="#(MyTextFile)" Lines="#(MyItems)" Overwrite="false" Encoding="UTF8" />
</Target>
Encoding is wrong, it should be UTF-8
When working with items/properties, the % and # and $ must come right before the (, no spacing in between: %(MyAssemblyIdentitiesAssemblyInfo.Version)
MyAssemblyIdentitiesAssemblyInfo does not exist, you probably meant MyAssemblyIdentities
Look up how msbuild evaluates properties and items. Basically what it will do in your script is evaluate MyItems, but at that time MyAssemblyIdentities does not yet exist so is empty, and only afterwards the GetAssemblyIdentity gets executed. Fix this by enforcing correct evaluation order: put your items inside the target and make it depend on another target that creates MyAssemblyIdentities before evaluating your items.
To summarize:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="GetAssemblyIdentities">
<GetAssemblyIdentity AssemblyFiles="bin\foo.exe">
<Output TaskParameter="Assemblies" ItemName="MyAssemblyIdentities"/>
</GetAssemblyIdentity>
</Target>
<Target Name="RetrieveIdentities" DependsOnTargets="GetAssemblyIdentities">
<ItemGroup>
<MyTextFile Include="\documentation\DoxygenConfigFile.doxyconfig"/>
<MyItems Include="PROJECT_NUMBER = %(MyAssemblyIdentities.Version)"/>
</ItemGroup>
<WriteLinesToFile File="#(MyTextFile)" Lines="#(MyItems)"
Overwrite="false" Encoding="UTF-8" />
</Target>
</Project>
Note this will only work if you invoke msbuild in the directory where the script is, else the paths (documentation/foo) will be wrong. That could be fixed by using eg $(MsBuildThisFileDirectory)\bin\foo.exe)

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.