Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSbuild for updating the assemblyinfo file

I am writing a batch file to automate a series of tasks. One of these tasks is to update the version of the dlls in my solution, by editing the assemblyinfo.cs file in the various projects in my solution; and then finally calling msbuild.exe to compile the solution.

In this regard, is it possible to write a command line script to update the various assemblyinfo.cs files in my .net solution. I would prefer to call msbuild from the commandline itself and not create another msbuild scripted file.

How does one do this using MSBuild? Is there any other way to do it?

Thanks for your time...

like image 456
user20358 Avatar asked Jan 05 '12 08:01

user20358


2 Answers

Editing files with dos cmd batch files is pretty hairy without the help of other tools. You would need to use something like the for /f command to step through the lines, and then process each line. For example look for the line that starts: "[assembly: AssemblyVersion" and replace it with something else.

However, if you don't have much in your AssemblyInfo.cs (and remember you can split the AssemblyInfo.cs into multiple cs files if you want) I'd suggest creating the file from scratch with a couple of echo statements.

If you have other tools available like sed.exe the edits can be done easily.

My preference these days would be to go for a trivial powershell script, that could eat this for breakfast and gives you access to the .Net libraries if you need it.

Here's a templating form of it:

(Get-Content AssemblyInfo.template.cs) -replace "{version}","1.2.3.4" > AssemblyInfo.cs

Here's a form that uses regular expressions to replace whatever version number is there:

$x = 'Version("{0}")' -f "1.2.3.4"
$content = Get-Content AssemblyInfo.cs
$content -replace 'Version\(".*"\)',$x > AssemblyInfo.cs
like image 136
StephenD Avatar answered Sep 23 '22 13:09

StephenD


This is the MSBuild target code where I update all my assemblyinfo.cs files (You have to init AssemblyInfoFilesToUpdate items collections before using this target):

  <!-- Update all the assembly info files with generated version info -->
  <Target Name="AfterGet" Condition="'$(ServerBuild)'=='true' And '$(BuildVersion)'!=''">
    <Message Text="Modifying AssemblyInfo files..." />
    <!-- Clear read-only attributes -->
    <Attrib Files="@(AssemblyInfoFilesToUpdate)" Normal="true" />
    <!-- Update AssemblyVersion -->
    <FileUpdate Files="@(AssemblyInfoFilesToUpdate)"
            Regex="AssemblyVersion\(&quot;.*&quot;\)\]"
            ReplacementText="AssemblyVersion(&quot;$(BuildVersion)&quot;)]" />
    <!-- Update AssemblyFileVersion -->
    <FileUpdate Files="@(AssemblyInfoFilesToUpdate)"
            Regex="AssemblyFileVersion\(&quot;.*&quot;\)\]"
            ReplacementText="AssemblyFileVersion(&quot;$(BuildVersion)&quot;)]" />
    <Message Text="AssemblyInfo files updated to version &quot;$(BuildVersion)&quot;" />
  </Target>

I'm using FileUpdate task from MSBuildCommunityTasks.

like image 23
Ludwo Avatar answered Sep 25 '22 13:09

Ludwo