1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Collections . Immutable ;
4
+ using System . Diagnostics ;
4
5
using System . Linq ;
5
6
using System . Text ;
6
7
using System . Threading . Tasks ;
@@ -18,40 +19,83 @@ public CsProjUpdateResult UpdateCsProjPropertyValues(string csProjFilePath)
18
19
19
20
var propertyGroups = csProjXmlDoc . Descendants ( "PropertyGroup" ) . ToList ( ) ;
20
21
22
+ var projectUpdateGroups = DetermineProjectUpdateGroups ( ) ;
21
23
22
24
UpdateOrAddCsProjValues (
23
25
csProjXmlDoc ,
24
26
propertyGroups ,
25
- new CsprojUpdateGroupTracker ( CsprojUpdateGroupTracker . NotFoundActionType . DoNothing ,
26
- [
27
- targetFrameworkUpdates ,
28
- ] ) ,
29
- new CsprojUpdateGroupTracker ( CsprojUpdateGroupTracker . NotFoundActionType . AddElementToFirstPropertyGroup ,
30
- [
31
- langUpdates ,
32
- enableNETAnalyzersUpdates ,
33
- enforceCodeStyleInBuildUpdates ,
34
- ] ) ,
35
- new CsprojUpdateGroupTracker ( CsprojUpdateGroupTracker . NotFoundActionType . AddElementToNewPropertyGroup ,
36
- [
37
- nuGetAuditUpdates ,
38
- nugetAuditModeUpdates ,
39
- nugetAuditLevelUpdates
40
- ] ) ) ;
27
+ projectUpdateGroups ) ;
41
28
42
29
//Write the file back out
43
30
//Note: Use File.WriteAllText instead of Save() because calling XDocument.ToString() doesn't include the xml header
44
31
File . WriteAllText ( csProjFilePath , csProjXmlDoc . ToString ( ) , Encoding . UTF8 ) ;
45
32
46
- var langVersionUpdateType = langUpdates . GetFinalResult ( ) ;
47
- var targetFrameworkUpdate = targetFrameworkUpdates . GetFinalResult ( ) ;
33
+ var langUpdates = projectUpdateGroups . SelectMany ( x => x ) . SelectMany ( x => x . UpdateTrackers ) . FirstOrDefault ( x => x . ElementName == CsprojUpdateTracker . LangVersion ) ;
34
+ var targetFrameworkUpdates = projectUpdateGroups . SelectMany ( x => x ) . SelectMany ( x => x . UpdateTrackers ) . FirstOrDefault ( x => x . ElementName == CsprojUpdateTracker . TargetFramework ) ;
35
+
36
+ var langVersionUpdateType = langUpdates ? . GetFinalResult ( ) ;
37
+ var targetFrameworkUpdate = targetFrameworkUpdates ? . GetFinalResult ( ) ;
48
38
return new CsProjUpdateResult ( csProjFilePath , langVersionUpdateType , targetFrameworkUpdate ) ;
49
39
}
50
40
51
41
private ImmutableArray < ImmutableArray < CsprojUpdateGroupTracker > > DetermineProjectUpdateGroups ( )
52
42
{
53
- var buidler = ImmutableArray . CreateBuilder < ImmutableArray < CsprojUpdateGroupTracker > > ( ) ;
43
+ var builder = ImmutableArray . CreateBuilder < ImmutableArray < CsprojUpdateGroupTracker > > ( ) ;
44
+
45
+ var dotNetVersioningUpdates = GenerateUpdateGroupForDotNetVersioning ( ) ;
46
+ if ( dotNetVersioningUpdates . Any ( ) )
47
+ {
48
+ builder . Add ( dotNetVersioningUpdates ) ;
49
+ }
50
+
51
+ var dotNetAnalyzerUpdates = GenerateUpdateGroupForDotNetAnalyzers ( ) ;
52
+ if ( dotNetAnalyzerUpdates . Any ( ) )
53
+ {
54
+ builder . Add ( dotNetAnalyzerUpdates ) ;
55
+ }
56
+
57
+ var nugetAuditUpdates = GenerateUpdateGroupForNugetAudit ( ) ;
58
+
59
+ if ( nugetAuditUpdates . Any ( ) )
60
+ {
61
+ builder . Add ( nugetAuditUpdates ) ;
62
+ }
63
+
64
+ return builder . ToImmutableArray ( ) ;
65
+ }
66
+
67
+ private ImmutableArray < CsprojUpdateGroupTracker > GenerateUpdateGroupForNugetAudit ( )
68
+ {
69
+ if ( UpdateOptions . NugetAudit is object )
70
+ {
71
+ var nuGetAuditUpdates = new CsprojUpdateTracker (
72
+ CsprojUpdateTracker . NuGetAudit ,
73
+ UpdateOptions . NugetAudit . NuGetAudit . ToString ( ) . ToLower ( ) ,
74
+ addIfElementNotFound : true ) ;
75
+ var nugetAuditModeUpdates = new CsprojUpdateTracker (
76
+ CsprojUpdateTracker . NuGetAuditMode ,
77
+ UpdateOptions . NugetAudit . AuditMode . ToString ( ) . ToLower ( ) ,
78
+ addIfElementNotFound : true ) ;
79
+ var nugetAuditLevelUpdates = new CsprojUpdateTracker (
80
+ CsprojUpdateTracker . NuGetAuditLevel ,
81
+ UpdateOptions . NugetAudit . AuditLevel ,
82
+ addIfElementNotFound : true ) ;
54
83
84
+ var nugetUpdatesGroup = new CsprojUpdateGroupTracker ( CsprojUpdateGroupTracker . NotFoundActionType . AddElementToNewPropertyGroup ,
85
+ [
86
+ nuGetAuditUpdates ,
87
+ nugetAuditModeUpdates ,
88
+ nugetAuditLevelUpdates
89
+ ] ) ;
90
+
91
+ return [ nugetUpdatesGroup ] ;
92
+ }
93
+
94
+ return ImmutableArray < CsprojUpdateGroupTracker > . Empty ;
95
+ }
96
+
97
+ private ImmutableArray < CsprojUpdateGroupTracker > GenerateUpdateGroupForDotNetVersioning ( )
98
+ {
55
99
if ( UpdateOptions . DotNetVersioningOptions is object )
56
100
{
57
101
var targetFrameworkUpdates = new CsprojUpdateTracker (
@@ -70,24 +114,29 @@ private ImmutableArray<ImmutableArray<CsprojUpdateGroupTracker>> DetermineProjec
70
114
UpdateOptions . DotNetVersioningOptions . TreatWarningsAsErrors . ToString ( ) . ToLower ( ) ,
71
115
addIfElementNotFound : true ) ;
72
116
73
- var theseBuilder = ImmutableArray . CreateBuilder < CsprojUpdateGroupTracker > ( ) ;
117
+ var builder = ImmutableArray . CreateBuilder < CsprojUpdateGroupTracker > ( ) ;
74
118
75
- theseBuilder . Add (
119
+ builder . Add (
76
120
new CsprojUpdateGroupTracker ( CsprojUpdateGroupTracker . NotFoundActionType . DoNothing ,
77
121
[
78
122
targetFrameworkUpdates ,
79
123
] ) ) ;
80
124
81
- theseBuilder . Add (
125
+ builder . Add (
82
126
new CsprojUpdateGroupTracker ( CsprojUpdateGroupTracker . NotFoundActionType . AddElementToFirstPropertyGroup ,
83
127
[
84
128
langUpdates ,
85
129
warningsAsErrorsUpdates ,
86
130
] ) ) ;
87
131
88
- buidler . Add ( theseBuilder . ToImmutableArray ( ) ) ;
132
+ return builder . ToImmutableArray ( ) ;
89
133
}
90
-
134
+
135
+ return ImmutableArray < CsprojUpdateGroupTracker > . Empty ;
136
+ }
137
+
138
+ private ImmutableArray < CsprojUpdateGroupTracker > GenerateUpdateGroupForDotNetAnalyzers ( )
139
+ {
91
140
if ( UpdateOptions . DotNetAnalyzerOptions is object )
92
141
{
93
142
var enableNETAnalyzersUpdates = new CsprojUpdateTracker (
@@ -100,49 +149,47 @@ private ImmutableArray<ImmutableArray<CsprojUpdateGroupTracker>> DetermineProjec
100
149
UpdateOptions . DotNetAnalyzerOptions . EnforceCodeStyleInBuild . ToString ( ) . ToLower ( ) ,
101
150
addIfElementNotFound : true ) ;
102
151
103
- var theseBuilder = ImmutableArray . CreateBuilder < CsprojUpdateGroupTracker > ( ) ;
104
- }
152
+ var builder = ImmutableArray . CreateBuilder < CsprojUpdateGroupTracker > ( ) ;
105
153
106
- var nuGetAuditUpdates = new CsprojUpdateTracker (
107
- CsprojUpdateTracker . NuGetAudit ,
108
- UpdateOptions . NugetAudit . NuGetAudit . ToString ( ) . ToLower ( ) ,
109
- addIfElementNotFound : true ) ;
110
- var nugetAuditModeUpdates = new CsprojUpdateTracker (
111
- CsprojUpdateTracker . NuGetAuditMode ,
112
- UpdateOptions . NugetAudit . AuditMode . ToString ( ) . ToLower ( ) ,
113
- addIfElementNotFound : true ) ;
114
- var nugetAuditLevelUpdates = new CsprojUpdateTracker (
115
- CsprojUpdateTracker . NuGetAuditLevel ,
116
- UpdateOptions . NugetAudit . AuditLevel ,
117
- addIfElementNotFound : true ) ;
154
+ builder . Add (
155
+ new CsprojUpdateGroupTracker ( CsprojUpdateGroupTracker . NotFoundActionType . AddElementToNewPropertyGroup ,
156
+ [
157
+ enableNETAnalyzersUpdates ,
158
+ enforceCodeStyleInBuildUpdates
159
+ ] ) ) ;
118
160
161
+ return builder . ToImmutableArray ( ) ;
162
+ }
119
163
120
- return buidler . ToImmutableArray ( ) ;
164
+ return ImmutableArray < CsprojUpdateGroupTracker > . Empty ;
121
165
}
122
166
123
- private void UpdateOrAddCsProjValues ( XDocument csProjXmlDoc , List < XElement > propertyGroupsElements , params CsprojUpdateGroupTracker [ ] updateGroups )
167
+ private void UpdateOrAddCsProjValues ( XDocument csProjXmlDoc , List < XElement > propertyGroupsElements , ImmutableArray < ImmutableArray < CsprojUpdateGroupTracker > > updateGroups )
124
168
{
125
169
//Separate updates into groups
126
170
// This way, when the groups are added to the csproj, they are grouped together to the same PropetyGroup
127
171
// Not important for functional reasons, but it makes the csproj file easier to read
128
- foreach ( var group in updateGroups )
172
+ foreach ( var trackerGroup in updateGroups )
129
173
{
130
- foreach ( var propertyGroupElement in propertyGroupsElements )
174
+ foreach ( var tracker in trackerGroup )
131
175
{
132
- foreach ( var update in group . UpdateTrackers )
176
+ foreach ( var update in tracker . UpdateTrackers )
133
177
{
134
- if ( update . ElementName == CsprojUpdateTracker . TargetFramework )
135
- {
136
- UpdateTargetFrameworkValue ( propertyGroupElement , update ) ;
137
- }
138
- else
178
+ foreach ( var propertyGroupElement in propertyGroupsElements )
139
179
{
140
- UpdateCsprojValue ( propertyGroupElement , update ) ;
180
+ if ( update . ElementName == CsprojUpdateTracker . TargetFramework )
181
+ {
182
+ UpdateTargetFrameworkValue ( propertyGroupElement , update ) ;
183
+ }
184
+ else
185
+ {
186
+ UpdateCsprojValue ( propertyGroupElement , update ) ;
187
+ }
141
188
}
142
189
}
143
- }
144
190
145
- AddMissingElements ( csProjXmlDoc , group ) ;
191
+ AddMissingElements ( csProjXmlDoc , tracker ) ;
192
+ }
146
193
}
147
194
}
148
195
0 commit comments