11

I am very new to cakebuild. I want to update the version info of assemblyinfo.cs using cakebuild.

public static void CreateAssemblyInfo() method overwrites the entire content of the assemblyinfo file. But I need just version info to be updated.

How can I achieve this.?

Regards, Aradhya

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
shobha aradhya
  • 121
  • 1
  • 3

2 Answers2

14

If you do not want to have separate files you can also use a regex replace:

#addin "Cake.FileHelpers"
var yourVersion = "1.0.0.0";

Task("SetVersion")
   .Does(() => {
       ReplaceRegexInFiles("./your/AssemblyInfo.cs", 
                           "(?<=AssemblyVersion\\(\")(.+?)(?=\"\\))", 
                           yourVersion);
   });

Depending on your AssemblyInfo file you may want to also replace the values of AssemblyFileVersion or AssemblyInformationalVersion

Philipp Grathwohl
  • 2,726
  • 3
  • 27
  • 38
10

Have 2 files, one for static stuff and one for the auto generated bits.

The pattern I usually apply is to have an SolutionInfo.cs that's shared between projects and a AssemblyInfo.cs per project which are unique per project.

An example folder structure could be

src
|    Solution.sln
|    SolutionInfo.cs
|    
\--- Project
    |   Project.csproj
    |
    \---Properties
            AssemblyInfo.cs

And basically your csproj file would instead of:

<Compile Include="Properties\AssemblyInfo.cs" />

Be something like:

<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="..\SolutionInfo.cs">
  <Link>Properties\SolutionInfo.cs</Link>
</Compile>

This way you keep any manual edits to your AssemblyInfo.cs and can safely auto generate without risk of overwriting that info.

This also lets you share things like version / copyright / company between projects in a solution.

The Cake build script part of this would look something like this:

Task("SolutionInfo")
    .IsDependentOn("Clean")
    .IsDependentOn("Restore")
    .Does(() =>
{
    var file = "./src/SolutionInfo.cs";
    CreateAssemblyInfo(file, assemblyInfo);
});
devlead
  • 4,935
  • 15
  • 36
  • Would you include the "SolutionInfo.cs"-file in source control or would you ignore it? Trying to figure out how to setup the build in the best way to be able to set the version based on GitVersion-info. Using .NET7 in my case, maybe there is a better approach? – Markus Knappen Johansson May 25 '23 at 21:34
  • Update: Never mind, just found the "DotNetMSBuildSettings" on the DotNetBuildSettings-class =D – Markus Knappen Johansson May 25 '23 at 21:43