Skip to content

Commit 936d5d3

Browse files
committed
Updatyed build to set the project version in csproj file before compiling
1 parent b130650 commit 936d5d3

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

build/Program.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
using System.Diagnostics;
1212
using Cake.Common.Tools.DotNet.Test;
1313
using Cake.Common.Tools.DotNet.Build;
14+
using System.IO;
15+
using System.Xml.Linq;
16+
using System.Linq;
1417

1518
public static class Program
1619
{
@@ -41,7 +44,7 @@ public BuildContext(ICakeContext context)
4144
NugetVersion = LoadParameter(context, "nugetVersion");
4245
NuGetPushToken = LoadParameter(context, "nuGetPushToken");
4346
PushNuget = context.Argument<bool>("pushNuget", false);
44-
47+
4548
ProjectPaths = ProjectPaths.LoadFromContext(context, BuildConfiguration, SrcDirectoryPath, NugetVersion);
4649
}
4750

@@ -64,6 +67,29 @@ public override void Run(BuildContext context)
6467
}
6568

6669
[IsDependentOn(typeof(OutputParametersTask))]
70+
[TaskName(nameof(UpdateCsProjVersionTask))]
71+
public sealed class UpdateCsProjVersionTask : FrostingTask<BuildContext>
72+
{
73+
public override void Run(BuildContext context)
74+
{
75+
var filePath = context.ProjectPaths.CsprojFile;
76+
var xml = XElement.Load(filePath);
77+
var versionElements = xml
78+
.Elements("PropertyGroup")
79+
.SelectMany(x => x.Elements())
80+
.Where(x => string.Equals(x.Name.LocalName, "Version", StringComparison.OrdinalIgnoreCase));
81+
foreach (var versionElement in versionElements)
82+
{
83+
versionElement.Value = context.NugetVersion;
84+
}
85+
86+
var csprojString = xml.ToString();
87+
context.Log.Information($"Writing csproj file to '{filePath}' with content: {Environment.NewLine}{csprojString}");
88+
File.WriteAllText(filePath, csprojString);
89+
}
90+
}
91+
92+
[IsDependentOn(typeof(UpdateCsProjVersionTask))]
6793
[TaskName(nameof(BuildTask))]
6894
public sealed class BuildTask : FrostingTask<BuildContext>
6995
{
@@ -127,7 +153,7 @@ public override void Run(BuildContext context)
127153
}
128154

129155
context.DotNetNuGetPush(context.ProjectPaths.NuGetFilePath, new Cake.Common.Tools.DotNet.NuGet.Push.DotNetNuGetPushSettings
130-
{
156+
{
131157
Source = "https://api.nuget.org/v3/index.json",
132158
ApiKey = context.NuGetPushToken
133159
});

0 commit comments

Comments
 (0)