how to replace string in file using msbuild? - msbuild

I want to replace a string such "how r u" in file test.xml with a string "i am fine" in another file xy.xml.using regular expression in ms build.
ie i have to read string from one file(xy.xml) and replace it in another file test.xml.
so please provide necessary steps to solve this issue with example

This is no longer required... you can now inject C# into the project/build file...
Define a custom task and parameters as follows:
<UsingTask TaskName="ReplaceFileText" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<InputFilename ParameterType="System.String" Required="true" />
<OutputFilename ParameterType="System.String" Required="true" />
<MatchExpression ParameterType="System.String" Required="true" />
<ReplacementText ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Core" />
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs">
<![CDATA[
File.WriteAllText(
OutputFilename,
Regex.Replace(File.ReadAllText(InputFilename), MatchExpression, ReplacementText)
);
]]>
</Code>
</Task>
</UsingTask>
Then simply call it like any other MSBuild task
<Target Name="AfterBuild">
<ReplaceFileText
InputFilename="$(OutputPath)File.exe.config"
OutputFilename="$(OutputPath)File.exe.config"
MatchExpression="\$version\$"
ReplacementText="1.0.0.2" />
</Target>
The above example replaces "$version$" with "1.0.0.2" in the "File.exe.config" located in the output directory.

There is very simple approach to just replace string in a file:
<Target Name="Replace" AfterTargets="CoreCompile">
<PropertyGroup>
<InputFile>c:\input.txt</InputFile>
<OutputFile>c:\output.txt</OutputFile>
</PropertyGroup>
<WriteLinesToFile
File="$(OutputFile)"
Lines="$([System.IO.File]::ReadAllText($(InputFile)).Replace('from','to'))"
Overwrite="true"
Encoding="Unicode"/>
</Target>
See https://learn.microsoft.com/en-us/visualstudio/msbuild/property-functions?view=vs-2019
to explore inlinable C# code. [System.Text.RegularExpressions.Regex] included into the list.

The answer from #csharptest.net is good, but it doesn't work on DotNetCore. I would have added this as a comment, but I don't have enough reputation.
On DotNetCore you have to update:
Task Factory to "RoslynCodeTaskFactory"
Task Assembly to "$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll"
Remove the reference to "System.Core"
The consuming Target has to specify the "AfterTargets" attribute as "Build"
Everything else should be the same:
<Project Sdk="Microsoft.NET.Sdk.Web">
...
<UsingTask
TaskName="ReplaceFileText"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<InputFilename ParameterType="System.String" Required="true" />
<OutputFilename ParameterType="System.String" Required="true" />
<MatchExpression ParameterType="System.String" Required="true" />
<ReplacementText ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System"/>
<Using Namespace="System.IO"/>
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs">
<![CDATA[
File.WriteAllText(
OutputFilename,
Regex.Replace(File.ReadAllText(InputFilename), MatchExpression, ReplacementText)
);
]]>
</Code>
</Task>
</UsingTask>
<Target Name="AfterBuildStep" AfterTargets="Build">
<ReplaceFileText
InputFilename="$(OutputPath)File.exe.config"
OutputFilename="$(OutputPath)File.exe.config"
MatchExpression="\$version\$"
ReplacementText="1.0.0.2" />
</Target>
</Project>

EDIT: This answer is obsolete. Use solution below...
Use ReadLinesFromFile task to get replacement string from the xy.xml file. Check this
Then use value from xy.xml as a replacement string for FileUpdate task. Check this
And put it all together ;)

You can use the task FileUpdate from MSBuild Community Tasks as explained in the article
http://geekswithblogs.net/mnf/archive/2009/07/03/msbuild-task-to-replace-content-in-text-files.aspx

If you prefer not using third party (community) binaries, nor embedding code into your msbuild project, I'd suggest creating a simple task library which implements File.WriteAllText and can later host other tasks :
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
public class FileWriteAllText : Task
{
[Required]
public string Path { get; set; }
[Required]
public string Contents { get; set; }
public override bool Execute()
{
File.WriteAllText(Path, Contents);
return true;
}
}
Then you can replace, append, etc. in msbuild :
<UsingTask TaskName="FileWriteAllText" AssemblyFile="MyTasks.dll" />
<FileWriteAllText Path="test.xml"
Contents="$([System.Text.RegularExpressions.Regex]::Replace(
$([System.IO.File]::ReadAllText('test.xml')), 'how r u', 'i am fine'))" />

I ran the both replacements against same file that sits on a Unix drive and used the unc path to it \server\path...:
<ReplaceFileText
InputFilename="$(fileToUpdate)"
OutputFilename="$(fileToUpdate)"
MatchExpression="15.0.0"
ReplacementText="15.3.1"/>
<FileUpdate Files="$(fileToUpdate2)"
Regex="15.0.0"
ReplacementText="15.3.1" />
and the cs custom action above does not add the bom; however the FileUpdate did:
%head -2 branding.h branding2.h
==> branding.h <==
#/* branding.h
#** This file captures common branding strings in a format usable by both sed and C-preprocessor.
==> branding2.h <==
#/* branding.h
#** This file captures common branding strings in a format usable by both sed and C-preprocessor.
Thanks csharptest.net - I was doing doing exec's with perl subtitute commands for unix builds.

An updated to answer from James
<UsingTask TaskName="ReplaceTextInFiles" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.$(VsBuildTaskBinarySuffix).dll">
<ParameterGroup>
<MatchExpression ParameterType="System.String" Required="true" />
<ReplacementExpression ParameterType="System.String" Required="true" />
<InputFile ParameterType="Microsoft.Build.Framework.ITaskItem" Required="true" />
<IsTextReplaced ParameterType="System.Boolean" Output="True"/>
</ParameterGroup>
<Task>
<Reference Include="System.Core" />
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs">
<![CDATA[
bool isMatchFound = false;
string filecontent = "";
string path = InputFile.ItemSpec;
Log.LogMessage(MessageImportance.High, "[ReplaceTextInFiles]: Match= " + MatchExpression);
Log.LogMessage(MessageImportance.High, "[ReplaceTextInFiles]: Replace= " + ReplacementExpression);
IsTextReplaced = false;
using(StreamReader rdr = new StreamReader(path))
{
filecontent = rdr.ReadToEnd();
if (Regex.Match(filecontent, MatchExpression).Success)
{
filecontent = Regex.Replace(filecontent, MatchExpression, ReplacementExpression);
isMatchFound = true;
}
}
if(isMatchFound){
using(StreamWriter wrtr = new StreamWriter(path))
{
wrtr.Write(filecontent);
IsTextReplaced = true;
Log.LogMessage(MessageImportance.Normal, "[ReplaceTextInFiles]: Replaced text in file:" + path);
}
}
]]>
</Code>
</Task>

Related

MSBuild: How Do I Calculate a File Checksum Using MSBuild?

How Do I Calculate a File Checksum Using MSBuild?
I don't want to take a dependency on MSBuild extensions.
One option is to use C# code within the project file.
The sample shows how to pass parameter to and from C# code.
<?xml version="1.0" encoding="utf-8"?>
<Project>
<UsingTask TaskName="GenerateHash" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<InputFile ParameterType="System.String" Required="true" />
<Checksum ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.IO" />
<Using Namespace="System.Security.Cryptography" />
<Code Type="Fragment" Language="cs">
<![CDATA[
using (var md5 = MD5.Create())
{
using (var stream = new FileStream(InputFile, FileMode.Open))
{
byte[] bytes = md5.ComputeHash(stream);
// Convert byte array to a string
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
builder.Append(bytes[i].ToString("x2"));
}
Checksum = builder.ToString();
}
}
]]>
</Code>
</Task>
</UsingTask>
<Target Name="GenerateManifestJson" AfterTargets="PostBuild" >
<GenerateHash InputFile="$(FileName)">
<Output
TaskParameter="Checksum"
PropertyName="ArtifactChecksum" />
</GenerateHash>
<ItemGroup>
<ManifestJson Include="{" />
<ManifestJson Include="%20"checksum":"$(ArtifactChecksum)"" />
<ManifestJson Include="}" />
</ItemGroup>
<WriteLinesToFile File="c:\temp\hello.manifest.json" Lines="#(ManifestJson)" Overwrite="true" Encoding="utf-8" />
</Target>
</Project>
This has been added in MSBuild v16, see GetFileHash Task.
Example:
<Project>
<ItemGroup>
<FilesToHash Include="$(MSBuildThisFileDirectory)\*" />
</ItemGroup>
<Target Name="GetHash">
<GetFileHash Files="#(FilesToHash)">
<Output
TaskParameter="Items"
ItemName="FilesWithHashes" />
</GetFileHash>
<Message Importance="High"
Text="#(FilesWithHashes->'%(Identity): %(FileHash)')" />
</Target>
</Project>

how to use the absolute path in the msbuild config file(*.proj)?

the relative content in msbuild.proj file:
<Message Text="*****check the site exists*****" Importance="high"/>
<Exec Command="C:\WINDOWS\System32\inetsrv\appcmd.exe list site /name:$(WebAppSiteName) " ContinueOnError="true">
<Output TaskParameter="ExitCode" PropertyName="ErrorCode2" />
</Exec>
<Message Text="*****if not exists create site*****" Importance="high" Condition="'$(ErrorCode2)' > '0'" />
<Exec Command="C:\WINDOWS\System32\inetsrv\appcmd.exe add site /name:$(WebAppSiteName) /bindings:http/*:80:$(SiteDomain) /applicationDefaults.applicationPool:$(WebAppSiteName) /physicalPath:$(BuildSolutionDir)$(DeployDir)Website" Condition="$(WebAppSiteName)!='' and '$(ErrorCode2)' > '0'"></Exec>
in above: the actual parameter(/physicalPath) value of runtime is :
D:\YDJWebsite.Dev\deply\fw\..\..\mkltest2Website
actually, the path of above is equivalent to D:\YDJWebsite.Dev\mkltest2Website
when accessing the site, the error shows:
500 - internal server error,
resource not found
if i change the path to right format "D:\YDJWebsite.Dev\mkltest2Website", the error disappeared
the physical path of website in iis shows here.
now, i know the only solution is to convert the paramter value(/physicalPath) to right format?
but how to do it in proj file? is there any suggestion ? tks.
i have solved the problem with the msbuild's inline task.
<UsingTask TaskName="AbsolutePath" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<Path ParameterType="System.String" Required="true" />
<FullPath ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Core" />
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Text.RegularExpressions" />
<Using Namespace="Microsoft.Build.Framework" />
<Using Namespace="Microsoft.Build.Utilities" />
<Code Type="Fragment" Language="cs">
<![CDATA[
try {
if(string.IsNullOrWhiteSpace(Path))
{
FullPath = "";
}
else
{
DirectoryInfo dirInfo = new DirectoryInfo(Path);
FullPath = dirInfo.FullName;
}
}
catch (Exception ex) {
FullPath = "";
}
]]>
</Code>
</Task>
<AbsolutePath Path="$(BuildSolutionDir)$(DeployDir)Website">
<Output TaskParameter="FullPath" PropertyName="FullPath" />
</AbsolutePath>
<Message Text="$(FullPath)"/>
tks.

How can I insert lines into a file in an MSBuild Task?

I'm trying to insert some text into a file on the second line of the text. I've currently got it inserting the text at the top by using ReadLinesFromFile. Is there a way to break the list I get back from that into 2 pieces so I can insert on the second line?
What I have now:
<Target>
<ReadLinesFromFile File="targetfile.txt">
<Output TaskParameter="Lines" ItemName="TargetFileContents"/>
</ReadLinesFromFile>
<WriteLinesToFile File="targetfile.txt" Lines="$(TextToInsert)" Overwrite="true"/>
<WriteLinesToFile File="targetfile.txt" Lines="#(TargetFileContents)" Overwrite="false"/>
</Target>
It's a bit of a sledge hammer with all the scaffolding, but you can write a task into a project file (or included file, which often has the .targets extension):
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTarget="InsertLine">
<Target Name="InsertLine">
<InsertIntoFile FilePath="test.txt" LineNumber="999" Text="Test complete" />
<InsertIntoFile FilePath="test.txt" LineNumber="1" Text="2" />
<InsertIntoFile FilePath="test.txt" LineNumber="2" Text="3" />
<InsertIntoFile FilePath="test.txt" LineNumber="1" Text="1" />
<InsertIntoFile FilePath="test.txt" LineNumber="1" Text="Testing the 2MC" />
</Target>
<UsingTask
TaskName="InsertIntoFile"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<FilePath ParameterType="System.String" Required="true" />
<LineNumber ParameterType="System.Int32" Required="true" />
<Text ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Code Type="Fragment" Language="cs">
<![CDATA[
// By tradition, text file line numbering is 1-based
var lines = File.Exists(FilePath)
? File.ReadAllLines(FilePath).ToList()
: new List<String>(1);
lines.Insert(Math.Min(LineNumber - 1, lines.Count), Text);
File.WriteAllLines(FilePath, lines);
return true;
]]>
</Code>
</Task>
</UsingTask>
</Project>

MSbuild, How to access project property value in Inline Task code?

I have inline Task code written in csharp
I wonder if there is anyway to access propect property in inline Task code
For ex. I am trying to replace string match with project property value. is it possible?
<![CDATA[
MatchCollection matches = Regex.Matches(SourceStr, Pattern);
for (int i = 0; i < matches.Count; i++)
// replace the match value with project property... possible?
]]>
Pass it as a parameter, like you would with a compiled task?
<ParameterGroup>
<Foo ParameterType="System.Bar" Required="true" />
<ParameterGroup>
Edit: Looks like simple inline tokens work too.
<![CDATA[
Console.WriteLine("$(Foo)");
]]>
Too big to reply as comment.
Reason it's empty is as in your other question -- evaluation order. The code is evaluated and tokenized prior to hitting the target, I assume that's where you set the value, and setting the property.
If I understood what you're trying to achieve with the task, have a look below at an example, just pass them in as properties and out as output.
That said, I think you should look into Property Functions first, save youself a bunch of trouble.
http://msdn.microsoft.com/en-us/library/dd633440.aspx
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="Foo" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<SourceStr Required="true" Output="true" />
<Pattern Required="true" />
<Value Required="true" />
<Macros ParameterType="Microsoft.Build.Framework.ITaskItem[]" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.Linq" />
<Using Namespace="System.Text.RegularExpressions" />
<Code Type="Fragment" Language="cs">
<![CDATA[
var regex = new Regex(Pattern);
var matches = regex.Matches(SourceStr).Cast<Match>().Select(m => m.Value).ToList();
matches.ForEach(m => Log.LogMessage("{0} -> {1}", m, Value));
Macros = matches.Select(m => new TaskItem(m)).ToArray();
SourceStr = regex.Replace(SourceStr, Value);
]]>
</Code>
</Task>
</UsingTask>
<Target Name="Foo">
<PropertyGroup>
<SourceStr>Bar</SourceStr>
<Value>z</Value>
</PropertyGroup>
<Message Text="in $(SourceStr)" />
<Foo SourceStr="$(SourceStr)" Pattern="r$" Value="$(Value)">
<Output TaskParameter="SourceStr" PropertyName="SourceStr" />
<Output TaskParameter="Macros" ItemName="Macros" />
</Foo>
<Message Text="out $(SourceStr)" />
<Message Text="sans %(Macros.Identity)" />
</Target>
</Project>
There are examples in the MSDN documentation. For example:
<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' ToolsVersion="4.0">
<UsingTask TaskName="TokenReplace" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Path ParameterType="System.String" Required="true" />
<Token ParameterType="System.String" Required="true" />
<Replacement ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
<![CDATA[
string content = File.ReadAllText(Path);
content = content.Replace(Token, Replacement);
File.WriteAllText(Path, content);
]]>
</Code>
</Task>
</UsingTask>
<Target Name='Demo' >
<TokenReplace Path="C:\Project\Target.config" Token="$MyToken$" Replacement="MyValue"/>
</Target>
</Project>
If $(MyValue) was a project property you could do something like:
<TokenReplace Path="C:\Project\Target.config" Token="$MyToken$" Replacement="$(MyValue)"/>

get list of subdirectories in msbuild

Given a list of directories:
<ItemGroup>
<Dirs Include="Foo\Dir1" />
<Dirs Include="Foo\Dir2" />
</ItemGroup>
How can I get a list of all subdirectories.
Transforming this list with "$(Identity)\**" does not match anything and transforming with "$(Identity)\**\*" and then with RelativeDir yields only directories that contain files.
Currently I have to resort to C#:
<UsingTask TaskName="GetSubdirectories" TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<Directories ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<SubDirectories ParameterType="Microsoft.Build.Framework.ITaskItem[]" Output="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs"><![CDATA[
var result = new List<ITaskItem>();
foreach (var dirItem in Directories) {
foreach (var dir in Directory.GetDirectories(dirItem.ItemSpec, "*", SearchOption.AllDirectories)) {
if (dir.Contains(#"\.svn\") || dir.EndsWith(#"\.svn")) continue;
result.Add(new TaskItem(dir));
}
}
SubDirectories = result.ToArray();
]]></Code>
</Task>
</UsingTask>
<GetSubdirectories Directories="#(Dirs)">
<Output TaskParameter="SubDirectories" ItemName="SubDirs" />
</GetSubdirectories>
But I would like to know if there is an easier way.
Excerpted from the book "MSBuild Trickery":
<Import Project="EnableAllPropertyFunctions.tasks" />
<Target Name="GetSubdirectories">
<ItemGroup>
<Dirs Include="$([System.IO.Directory]::
EnumerateDirectories(
`.\Foo`,
`*`,
System.IO.SearchOption.AllDirectories))"
/>
</ItemGroup>
<Message Text="%(Dirs.Identity)" />
</Target>
You'll need to first enable the extended property function set by ensuring that the environment variable MSBuildEnableAllPropertyFunctions is set to the value 1 (that is what the imported .tasks file accomplishes, with an inline task).
Once #(Dirs) is set up, you can filter it with the Remove attribute to get rid of the Subversion folders.
<CreateItem Include="$(OutputFolder)\*\*.*">
<Output TaskParameter="Include" ItemName="FilesInSubFolders" />
</CreateItem>
<RemoveDuplicates Inputs="#(FilesInSubFolders->'%(RelativeDir)')">
<Output TaskParameter="Filtered" ItemName="SubDirs"/>
</RemoveDuplicates>
<Message Text="#(SubDirs)"/>
This will put all the immediate subfolder paths into #(SubDirs). If you change Include="$(OutputFolder)\*\*.*" to Include="$(OutputFolder)\**\*.*", it'll include all subfolders recursively.
To expand on Brian's answer with a fully self-contained example:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="GetSubdirectories">
<UsingTask TaskName="SetEnvironmentVariable"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v$(MSBuildToolsVersion).dll">
<ParameterGroup>
<Name ParameterType="System.String" Required="true" />
<Value ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Code Type="Fragment" Language="cs"><![CDATA[
Environment.SetEnvironmentVariable(Name, Value);
]]></Code>
</Task>
</UsingTask>
<Target Name="GetSubdirectories">
<SetEnvironmentVariable Name="MSBuildEnableAllPropertyFunctions" Value="1" />
<ItemGroup>
<Dirs Include="$([System.IO.Directory]::EnumerateFiles('.\Stuff', '*', System.IO.SearchOption.AllDirectories))"/>
</ItemGroup>
<Message Text="%(Dirs.Identity)" />
</Target>
</Project>
I got the UsingTask example from this answer.