@@ -188,42 +188,109 @@ private static void UpdateProject(string project, IReadOnlyDictionary<string, Nu
188
188
return ;
189
189
}
190
190
191
- int changes = 0 ;
191
+ Console . WriteLine ( ) ;
192
+ Console . WriteLine ( $ "* { project } ") ;
193
+
194
+ bool sdkUpdated = TryUpdateSdk ( doc : doc , packages : packages , updates : updates ) ;
195
+ bool packageReferencesUpdated = TryUpdatePackageReferences ( doc : doc , packages : packages , updates : updates ) ;
196
+
197
+ if ( sdkUpdated || packageReferencesUpdated )
198
+ {
199
+ Console . WriteLine ( value : "=========== UPDATED ===========" ) ;
200
+
201
+ ProjectHelpers . SaveProject ( project : project , doc : doc ) ;
202
+ }
203
+ }
204
+
205
+ private static bool TryUpdatePackageReferences ( XmlDocument doc , IReadOnlyDictionary < string , NuGetVersion > packages , Dictionary < string , NuGetVersion > updates )
206
+ {
192
207
XmlNodeList ? nodes = doc . SelectNodes ( xpath : "/Project/ItemGroup/PackageReference" ) ;
193
208
194
- if ( nodes != null && nodes . Count ! = 0 )
209
+ if ( nodes == null || nodes . Count = = 0 )
195
210
{
196
- Console . WriteLine ( ) ;
197
- Console . WriteLine ( $ "* { project } ") ;
211
+ return false ;
212
+ }
213
+
214
+ bool packageReferencesUpdated = false ;
198
215
199
- foreach ( XmlElement node in nodes . OfType < XmlElement > ( ) )
216
+ foreach ( XmlElement node in nodes . OfType < XmlElement > ( ) )
217
+ {
218
+ string package = node . GetAttribute ( name : "Include" ) ;
219
+
220
+ foreach ( ( string nugetPackageId , NuGetVersion nugetVersion ) in packages )
200
221
{
201
- string package = node . GetAttribute ( name : "Include" ) ;
222
+ if ( ! PackageIdHelpers . IsExactMatch ( package : package , packageId : nugetPackageId ) )
223
+ {
224
+ continue ;
225
+ }
202
226
203
- foreach ( ( string nugetPackageId , NuGetVersion nugetVersion ) in packages )
227
+ bool updated = UpdateOnePackage ( node : node , installedPackageId : package , nugetPackageId : nugetPackageId , nugetVersion : nugetVersion ) ;
228
+
229
+ if ( updated )
204
230
{
205
- if ( PackageIdHelpers . IsExactMatch ( package : package , packageId : nugetPackageId ) )
206
- {
207
- bool updated = UpdateOnePackage ( node : node , installedPackageId : package , nugetPackageId : nugetPackageId , nugetVersion : nugetVersion ) ;
208
-
209
- if ( updated )
210
- {
211
- TrackUpdate ( updates : updates , nugetPackageId : nugetPackageId , nugetVersion : nugetVersion ) ;
212
- changes ++ ;
213
- }
214
-
215
- break ;
216
- }
231
+ TrackUpdate ( updates : updates , nugetPackageId : nugetPackageId , nugetVersion : nugetVersion ) ;
232
+ packageReferencesUpdated = true ;
217
233
}
234
+
235
+ break ;
236
+ }
237
+ }
238
+
239
+ return packageReferencesUpdated ;
240
+ }
241
+
242
+ private static bool TryUpdateSdk ( XmlDocument doc , IReadOnlyDictionary < string , NuGetVersion > packages , Dictionary < string , NuGetVersion > updates )
243
+ {
244
+ if ( doc . SelectSingleNode ( "/Project" ) is not XmlElement projectNode )
245
+ {
246
+ return false ;
247
+ }
248
+
249
+ string sdk = projectNode . GetAttribute ( "Sdk" ) ;
250
+
251
+ if ( string . IsNullOrWhiteSpace ( sdk ) )
252
+ {
253
+ return false ;
254
+ }
255
+
256
+ IReadOnlyList < string > parts = sdk . Split ( "/" ) ;
257
+
258
+ if ( parts . Count != 2 )
259
+ {
260
+ return false ;
261
+ }
262
+
263
+ string installedPackageId = parts [ 0 ] ;
264
+ string installedVersion = parts [ 1 ] ;
265
+
266
+ foreach ( ( string nugetPackageId , NuGetVersion nugetVersion ) in packages )
267
+ {
268
+ if ( ! PackageIdHelpers . IsExactMatch ( package : installedPackageId , packageId : nugetPackageId ) )
269
+ {
270
+ continue ;
218
271
}
219
272
220
- if ( changes != 0 )
273
+ bool upgrade = ShouldUpgrade ( installedVersion : installedVersion , nugetVersion : nugetVersion ) ;
274
+
275
+ if ( ! upgrade )
221
276
{
222
- Console . WriteLine ( value : "=========== UPDATED =========== ") ;
277
+ Console . WriteLine ( $ " >> { installedPackageId } Installed: { installedVersion } Upgrade: False. ") ;
223
278
224
- ProjectHelpers . SaveProject ( project : project , doc : doc ) ;
279
+ return false ;
225
280
}
281
+
282
+ Console . WriteLine ( $ " >> { installedPackageId } Installed: { installedVersion } Upgrade: True. New Version: { nugetVersion } .") ;
283
+ string newSdk = string . Join ( separator : "/" , nugetPackageId , nugetVersion ) ;
284
+ projectNode . SetAttribute ( name : "Sdk" , value : newSdk ) ;
285
+
286
+ TrackUpdate ( updates : updates , nugetPackageId : nugetPackageId , nugetVersion : nugetVersion ) ;
287
+
288
+ return true ;
289
+
290
+ // Package matched but was not upgraded.
226
291
}
292
+
293
+ return false ;
227
294
}
228
295
229
296
private static bool UpdateOnePackage ( XmlElement node , string installedPackageId , string nugetPackageId , NuGetVersion nugetVersion )
0 commit comments