5
5
using System . Linq ;
6
6
using System . Text ;
7
7
using System . Threading . Tasks ;
8
+ using System . Xml ;
8
9
using System . Xml . Linq ;
9
10
10
11
using Serilog ;
@@ -15,20 +16,20 @@ public class CsProjUpdater(ILogger Logger)
15
16
{
16
17
public CsProjUpdateResult UpdateCsProjPropertyValues ( string csProjFilePath , CSharpOptions cSharpOptions )
17
18
{
18
- var csProjXmlDoc = XDocument . Load ( csProjFilePath , LoadOptions . PreserveWhitespace ) ;
19
-
19
+ var csProjXmlDoc = XDocument . Load ( csProjFilePath ) ;
20
+ var rootElm = csProjXmlDoc . Root ?? throw new Exception ( $ "*.csproj file has no root xml element: { csProjFilePath } " ) ;
20
21
var propertyGroups = csProjXmlDoc . Descendants ( "PropertyGroup" ) . ToList ( ) ;
21
22
22
23
var projectUpdateGroups = DetermineProjectUpdateGroups ( cSharpOptions ) ;
23
24
24
25
UpdateOrAddCsProjValues (
25
- csProjXmlDoc ,
26
+ rootElm ,
26
27
propertyGroups ,
27
28
projectUpdateGroups ) ;
28
29
29
30
//Write the file back out
30
31
//Note: Use File.WriteAllText instead of Save() because calling XDocument.ToString() doesn't include the xml header
31
- File . WriteAllText ( csProjFilePath , csProjXmlDoc . ToString ( ) , Encoding . UTF8 ) ;
32
+ File . WriteAllText ( csProjFilePath , rootElm . ToString ( ) , Encoding . UTF8 ) ;
32
33
33
34
var langUpdates = projectUpdateGroups . SelectMany ( x => x ) . SelectMany ( x => x . UpdateTrackers ) . FirstOrDefault ( x => x . ElementName == CsprojUpdateTracker . LangVersion ) ;
34
35
var targetFrameworkUpdates = projectUpdateGroups . SelectMany ( x => x ) . SelectMany ( x => x . UpdateTrackers ) . FirstOrDefault ( x => x . ElementName == CsprojUpdateTracker . TargetFramework ) ;
@@ -158,7 +159,7 @@ private ImmutableArray<CsprojUpdateGroupTracker> GenerateUpdateGroupForDotNetAna
158
159
return builder . ToImmutableArray ( ) ;
159
160
}
160
161
161
- private void UpdateOrAddCsProjValues ( XDocument csProjXmlDoc , List < XElement > propertyGroupsElements , ImmutableArray < ImmutableArray < CsprojUpdateGroupTracker > > updateGroups )
162
+ private void UpdateOrAddCsProjValues ( XElement csProjXml , List < XElement > propertyGroupsElements , ImmutableArray < ImmutableArray < CsprojUpdateGroupTracker > > updateGroups )
162
163
{
163
164
//Separate updates into groups
164
165
// This way, when the groups are added to the csproj, they are grouped together to the same PropetyGroup
@@ -182,12 +183,12 @@ private void UpdateOrAddCsProjValues(XDocument csProjXmlDoc, List<XElement> prop
182
183
}
183
184
}
184
185
185
- AddMissingElements ( csProjXmlDoc , tracker ) ;
186
+ AddMissingElements ( csProjXml , tracker ) ;
186
187
}
187
188
}
188
189
}
189
190
190
- private void AddMissingElements ( XDocument csProjXmlDoc , CsprojUpdateGroupTracker updateGroup )
191
+ private void AddMissingElements ( XElement csProjXml , CsprojUpdateGroupTracker updateGroup )
191
192
{
192
193
var toUpdate = updateGroup . UpdateTrackers . Where ( x => ! x . HasMadeRequiredUpdate ( ) ) . ToImmutableArray ( ) ;
193
194
@@ -199,13 +200,14 @@ private void AddMissingElements(XDocument csProjXmlDoc, CsprojUpdateGroupTracker
199
200
XElement ? propertyGroupToAddTo = null ;
200
201
if ( updateGroup . NotFoundAction == CsprojUpdateGroupTracker . NotFoundActionType . AddElementToFirstPropertyGroup )
201
202
{
202
- propertyGroupToAddTo = csProjXmlDoc . Descendants ( "PropertyGroup" ) . FirstOrDefault ( ) ;
203
+ propertyGroupToAddTo = csProjXml . Descendants ( "PropertyGroup" ) . FirstOrDefault ( ) ;
203
204
}
204
205
else if ( updateGroup . NotFoundAction == CsprojUpdateGroupTracker . NotFoundActionType . AddElementToNewPropertyGroup )
205
206
{
206
207
Logger . Information ( "Adding new PropertyGroup element for other required elements" ) ;
207
208
propertyGroupToAddTo = new XElement ( "PropertyGroup" ) ;
208
- csProjXmlDoc . Root ! . Add ( propertyGroupToAddTo ) ;
209
+
210
+ csProjXml . Add ( propertyGroupToAddTo ) ;
209
211
}
210
212
211
213
//Add the trackers that haven't already set the final value
@@ -216,7 +218,6 @@ private void AddMissingElements(XDocument csProjXmlDoc, CsprojUpdateGroupTracker
216
218
Logger . Information ( $ "Adding { trackers . ElementName } element to csproj") ;
217
219
var newElement = new XElement ( trackers . ElementName , trackers . NewValue ) ;
218
220
propertyGroupToAddTo . Add ( newElement ) ;
219
-
220
221
trackers . SetResults . Add ( CsprojValueUpdateResultType . AddedElement ) ;
221
222
}
222
223
}
0 commit comments