5

I'm using the ParseReleaseNotes alias in Cake to manage my versioning, worked fine with a project where I patch the assembly info with CreateAssemblyInfo alias.

Now with project not using csproj but project.json I want to achieve the same and assembly info isn't an real option with project.json.

Checking out the DotNetCoreBuild(string, ​DotNetCoreBuildSettings)​ and it's DotNetCoreBuildSettings there only seems to be a way to set parts of the version via it's VersionSuffix property.

Is there an Cake alias / setting to achieve this or is it possible to patch the project.json from Cake?

2 Answers2

5

There is no built in Cake Alias to provide this functionality, but you can make use of a 3rd Party Addin for the MagicChunks project. You can add this into your Cake script by simply doing:

#addin "MagicChunks"

And from there, you can do something like:

var projectToPackagePackageJson = $"{projectToPackage}/project.json";
Information("Updating {0} version -> {1}", projectToPackagePackageJson, nugetVersion);

TransformConfig(projectToPackagePackageJson, projectToPackagePackageJson, new TransformationCollection {
    { "version", nugetVersion }
});

Where TransformConfig is the Method Alias that is added by the MagicChunks addin.

NOTE: This sample was taken from the following project.

Gary Ewan Park
  • 17,610
  • 5
  • 42
  • 60
2

There's no built in alias to patch project.json version or parameter for dotnet build to set full version that I know of.

That said as project.json is just "JSON" it's fully possible to patch project.json using a JSON serializer i.e. JSON.Net.

Below I've created an example that references JSON.Net as an addin and then created an UpdateProjectJsonVersion utility function that I can use to patch my project.json using parsed the ReleaseNotes (in this case I've hard coded it for simplicity).

#addin "Newtonsoft.Json"

// fake a release note
ReleaseNotes releaseNotes = new ReleaseNotes(
    new Version("3.0.0"),
    new [] {"3rd release"},
    "3.0.-beta"
    );

// project.json to patch
FilePath filePaths = File("./project.json");

// patch project.json
UpdateProjectJsonVersion(releaseNotes.RawVersionLine, filePaths);

// utility function that patches project.json using json.net
public static void UpdateProjectJsonVersion(string version, FilePath projectPath)
{
    var project = Newtonsoft.Json.Linq.JObject.Parse(
        System.IO.File.ReadAllText(projectPath.FullPath, Encoding.UTF8));

    project["version"].Replace(version);

    System.IO.File.WriteAllText(projectPath.FullPath, project.ToString(), Encoding.UTF8);
}

So basically just call UpdateProjectJsonVersion before you call the DotNetCoreBuild alias and it'll result in same version as your release notes.

devlead
  • 4,935
  • 15
  • 36