@@ -35,14 +35,38 @@ public CsProjUpdateResult UpdateCsProjPropertyValues(string csProjFilePath)
35
35
CsprojUpdateTracker . EnforceCodeStyleInBuild ,
36
36
UpdateOptions . EnforceCodeStyleInBuild . ToString ( ) . ToLower ( ) ,
37
37
addIfElementNotFound : true ) ;
38
+ var nuGetAuditUpdates = new CsprojUpdateTracker (
39
+ CsprojUpdateTracker . NuGetAudit ,
40
+ UpdateOptions . NugetAudit . NuGetAudit . ToString ( ) . ToLower ( ) ,
41
+ addIfElementNotFound : true ) ;
42
+ var nugetAuditModeUpdates = new CsprojUpdateTracker (
43
+ CsprojUpdateTracker . NuGetAuditMode ,
44
+ UpdateOptions . NugetAudit . AuditMode . ToString ( ) . ToLower ( ) ,
45
+ addIfElementNotFound : true ) ;
46
+ var nugetAuditLevelUpdates = new CsprojUpdateTracker (
47
+ CsprojUpdateTracker . NuGetAuditLevel ,
48
+ UpdateOptions . NugetAudit . AuditLevel ,
49
+ addIfElementNotFound : true ) ;
38
50
39
51
UpdateOrAddCsProjValues (
40
52
csProjXmlDoc ,
41
53
propertyGroups ,
42
- targetFrameworkUpdates ,
43
- langUpdates ,
44
- enableNETAnalyzersUpdates ,
45
- enforceCodeStyleInBuildUpdates ) ;
54
+ new CsprojUpdateGroupTracker ( CsprojUpdateGroupTracker . NotFoundActionType . DoNothing ,
55
+ [
56
+ targetFrameworkUpdates ,
57
+ ] ) ,
58
+ new CsprojUpdateGroupTracker ( CsprojUpdateGroupTracker . NotFoundActionType . AddElementToFirstPropertyGroup ,
59
+ [
60
+ langUpdates ,
61
+ enableNETAnalyzersUpdates ,
62
+ enforceCodeStyleInBuildUpdates ,
63
+ ] ) ,
64
+ new CsprojUpdateGroupTracker ( CsprojUpdateGroupTracker . NotFoundActionType . AddElementToNewPropertyGroup ,
65
+ [
66
+ nuGetAuditUpdates ,
67
+ nugetAuditModeUpdates ,
68
+ nugetAuditLevelUpdates
69
+ ] ) ) ;
46
70
47
71
//Write the file back out
48
72
//Note: Use File.WriteAllText instead of Save() because calling XDocument.ToString() doesn't include the xml header
@@ -53,56 +77,60 @@ public CsProjUpdateResult UpdateCsProjPropertyValues(string csProjFilePath)
53
77
return new CsProjUpdateResult ( csProjFilePath , langVersionUpdateType , targetFrameworkUpdate ) ;
54
78
}
55
79
56
- private void UpdateOrAddCsProjValues ( XDocument csProjXmlDoc , List < XElement > propertyGroupsElements , params CsprojUpdateTracker [ ] updates )
80
+ private void UpdateOrAddCsProjValues ( XDocument csProjXmlDoc , List < XElement > propertyGroupsElements , params CsprojUpdateGroupTracker [ ] updateGroups )
57
81
{
58
- foreach ( var propertyGroupElement in propertyGroupsElements )
82
+ //Separate updates into groups
83
+ // This way, when the groups are added to the csproj, they are grouped together to the same PropetyGroup
84
+ // Not important for functional reasons, but it makes the csproj file easier to read
85
+ foreach ( var group in updateGroups )
59
86
{
60
- foreach ( var update in updates )
87
+ foreach ( var propertyGroupElement in propertyGroupsElements )
61
88
{
62
- if ( update . ElementName == CsprojUpdateTracker . TargetFramework )
63
- {
64
- UpdateTargetFrameworkValue ( propertyGroupElement , update ) ;
65
- }
66
- else
89
+ foreach ( var update in group . UpdateTrackers )
67
90
{
68
- UpdateCsprojValue ( propertyGroupElement , update ) ;
91
+ if ( update . ElementName == CsprojUpdateTracker . TargetFramework )
92
+ {
93
+ UpdateTargetFrameworkValue ( propertyGroupElement , update ) ;
94
+ }
95
+ else
96
+ {
97
+ UpdateCsprojValue ( propertyGroupElement , update ) ;
98
+ }
69
99
}
70
100
}
71
- }
72
101
73
- AddMissingElements ( csProjXmlDoc , updates ) ;
102
+ AddMissingElements ( csProjXmlDoc , group ) ;
103
+ }
74
104
}
75
105
76
- private void AddMissingElements ( XDocument csProjXmlDoc , params CsprojUpdateTracker [ ] updates )
106
+ private void AddMissingElements ( XDocument csProjXmlDoc , CsprojUpdateGroupTracker updateGroup )
77
107
{
78
- var updatesToMake = new List < CsprojUpdateTracker > ( ) ;
79
- foreach ( var update in updates )
108
+ XElement ? propertyGroupToAddTo = null ;
109
+ if ( updateGroup . NotFoundAction == CsprojUpdateGroupTracker . NotFoundActionType . AddElementToFirstPropertyGroup )
80
110
{
81
- if ( update . ShouldAddElement ( ) )
82
- {
83
- updatesToMake . Add ( update ) ;
84
- }
111
+ propertyGroupToAddTo = csProjXmlDoc . Descendants ( "PropertyGroup" ) . FirstOrDefault ( ) ;
85
112
}
86
-
87
- if ( updatesToMake . Any ( ) )
113
+ else if ( updateGroup . NotFoundAction == CsprojUpdateGroupTracker . NotFoundActionType . AddElementToNewPropertyGroup )
88
114
{
89
- var propertyGroup = csProjXmlDoc . Descendants ( "PropertyGroup" ) . FirstOrDefault ( ) ;
90
- if ( propertyGroup is null )
91
- {
92
- Logger . Information ( "Adding new PropertyGroup element for other required elements" ) ;
93
- var newPropertyGroup = new XElement ( "PropertyGroup" ) ;
94
- csProjXmlDoc . Add ( newPropertyGroup ) ;
115
+ Logger . Information ( "Adding new PropertyGroup element for other required elements" ) ;
116
+ var newPropertyGroup = new XElement ( "PropertyGroup" ) ;
117
+ csProjXmlDoc . Root ! . Add ( newPropertyGroup ) ;
95
118
96
- propertyGroup = newPropertyGroup ;
97
- }
119
+ propertyGroupToAddTo = newPropertyGroup ;
120
+ }
98
121
99
- foreach ( var elementToAdd in updatesToMake )
122
+ //Add the trackers that haven't already set the final value
123
+ var toUpdate = updateGroup . UpdateTrackers . Where ( x => ! x . HasMadeRequiredUpdate ( ) ) . ToImmutableArray ( ) ;
124
+ if ( propertyGroupToAddTo is not null
125
+ && toUpdate . Any ( ) )
126
+ {
127
+ foreach ( var trackers in toUpdate )
100
128
{
101
- Logger . Information ( $ "Adding { elementToAdd . ElementName } element to csproj") ;
102
- var newElement = new XElement ( elementToAdd . ElementName , elementToAdd . NewValue ) ;
103
- propertyGroup . Add ( newElement ) ;
129
+ Logger . Information ( $ "Adding { trackers . ElementName } element to csproj") ;
130
+ var newElement = new XElement ( trackers . ElementName , trackers . NewValue ) ;
131
+ propertyGroupToAddTo . Add ( newElement ) ;
104
132
105
- elementToAdd . SetResults . Add ( CsprojValueUpdateResultType . AddedElement ) ;
133
+ trackers . SetResults . Add ( CsprojValueUpdateResultType . AddedElement ) ;
106
134
}
107
135
}
108
136
}
@@ -197,13 +225,28 @@ private void UpdateTargetFrameworksValue(XElement childElm, CsprojUpdateTracker
197
225
}
198
226
}
199
227
228
+ private record CsprojUpdateGroupTracker (
229
+ CsprojUpdateGroupTracker . NotFoundActionType NotFoundAction ,
230
+ IEnumerable < CsprojUpdateTracker > UpdateTrackers )
231
+ {
232
+ public enum NotFoundActionType
233
+ {
234
+ DoNothing ,
235
+ AddElementToFirstPropertyGroup ,
236
+ AddElementToNewPropertyGroup
237
+ }
238
+ }
239
+
200
240
private class CsprojUpdateTracker
201
241
{
202
242
public const string TargetFramework = "TargetFramework" ;
203
243
public const string TargetFrameworks = "TargetFrameworks" ;
204
244
public const string LangVersion = "LangVersion" ;
205
245
public const string EnableNETAnalyzers = "EnableNETAnalyzers" ;
206
246
public const string EnforceCodeStyleInBuild = "EnforceCodeStyleInBuild" ;
247
+ public const string NuGetAudit = "NuGetAudit" ;
248
+ public const string NuGetAuditMode = "NuGetAuditMode" ;
249
+ public const string NuGetAuditLevel = "NuGetAuditLevel" ;
207
250
208
251
public CsprojUpdateTracker ( string elementName , string newValue , bool addIfElementNotFound )
209
252
: this ( elementName , newValue , addIfElementNotFound , ImmutableArray < string > . Empty )
@@ -242,9 +285,10 @@ public CsprojValueUpdateResultType GetFinalResult()
242
285
return CsprojValueUpdateResultType . NotFound ;
243
286
}
244
287
245
- public bool ShouldAddElement ( )
288
+ public bool HasMadeRequiredUpdate ( )
246
289
{
247
- return AddIfElementNotFound && SetResults . All ( x => x == CsprojValueUpdateResultType . NotFound ) ;
290
+ return SetResults . Any ( x => x != CsprojValueUpdateResultType . NotFound
291
+ && x != CsprojValueUpdateResultType . Unknown ) ;
248
292
}
249
293
}
250
294
}
0 commit comments