11
11
using System . Diagnostics ;
12
12
using Cake . Common . Tools . DotNet . Test ;
13
13
using Cake . Common . Tools . DotNet . Build ;
14
+ using System . IO ;
15
+ using System . Xml . Linq ;
16
+ using System . Linq ;
14
17
15
18
public static class Program
16
19
{
@@ -41,7 +44,7 @@ public BuildContext(ICakeContext context)
41
44
NugetVersion = LoadParameter ( context , "nugetVersion" ) ;
42
45
NuGetPushToken = LoadParameter ( context , "nuGetPushToken" ) ;
43
46
PushNuget = context . Argument < bool > ( "pushNuget" , false ) ;
44
-
47
+
45
48
ProjectPaths = ProjectPaths . LoadFromContext ( context , BuildConfiguration , SrcDirectoryPath , NugetVersion ) ;
46
49
}
47
50
@@ -64,6 +67,29 @@ public override void Run(BuildContext context)
64
67
}
65
68
66
69
[ 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 ) ) ]
67
93
[ TaskName ( nameof ( BuildTask ) ) ]
68
94
public sealed class BuildTask : FrostingTask < BuildContext >
69
95
{
@@ -127,7 +153,7 @@ public override void Run(BuildContext context)
127
153
}
128
154
129
155
context . DotNetNuGetPush ( context . ProjectPaths . NuGetFilePath , new Cake . Common . Tools . DotNet . NuGet . Push . DotNetNuGetPushSettings
130
- {
156
+ {
131
157
Source = "https://api.nuget.org/v3/index.json" ,
132
158
ApiKey = context . NuGetPushToken
133
159
} ) ;
0 commit comments