Skip to content

Commit b2f4204

Browse files
authored
Merge pull request #168 from credfeto/feat/support-packages-as-product-sdk
Added support for updating SDK packages
2 parents 5b69fdb + 4c04879 commit b2f4204

File tree

3 files changed

+120
-27
lines changed

3 files changed

+120
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Please ADD ALL Changes to the UNRELEASED SECTION and not a specific release
77

88
## [Unreleased]
99
### Added
10+
- Support for packages in Product SDK
1011
### Fixed
1112
### Changed
1213
- FF-1429 - Updated SonarAnalyzer.CSharp to 8.30.0.37606

src/Credfeto.Package.Update/Helpers/ProjectHelpers.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal static class ProjectHelpers
2222
CloseOutput = true
2323
};
2424

25-
public static string[] FindProjects(string folder)
25+
public static IReadOnlyList<string> FindProjects(string folder)
2626
{
2727
return Directory.EnumerateFiles(path: folder, searchPattern: "*.csproj", searchOption: SearchOption.AllDirectories)
2828
.ToArray();
@@ -32,10 +32,35 @@ public static IEnumerable<string> GetPackageIds(IReadOnlyList<string> projects)
3232
{
3333
return projects.Select(TryLoadDocument)
3434
.Where(doc => doc != null)
35-
.Select(doc => doc!.SelectNodes(xpath: "/Project/ItemGroup/PackageReference"))
36-
.Where(nodes => nodes != null)
37-
.SelectMany(nodes => nodes!.OfType<XmlElement>())
38-
.Select(node => node.GetAttribute(name: "Include"));
35+
.Select(doc => doc!)
36+
.SelectMany(doc => GetPackagesFromReferences(doc)
37+
.Concat(GetPackagesFromSdk(doc)))
38+
.Select(item => item.ToLowerInvariant())
39+
.Distinct();
40+
}
41+
42+
private static IEnumerable<string> GetPackagesFromSdk(XmlDocument doc)
43+
{
44+
if (doc.SelectSingleNode("/Project") is not XmlElement project)
45+
{
46+
yield break;
47+
}
48+
49+
IReadOnlyList<string> sdk = project.GetAttribute("Sdk")
50+
.Split("/");
51+
52+
if (sdk.Count == 2)
53+
{
54+
yield return sdk[0];
55+
}
56+
}
57+
58+
private static IEnumerable<string> GetPackagesFromReferences(XmlDocument doc)
59+
{
60+
return doc.SelectNodes(xpath: "/Project/ItemGroup/PackageReference")
61+
?.OfType<XmlElement>()
62+
.Select(node => node!.GetAttribute(name: "Include"))
63+
.Where(include => !string.IsNullOrWhiteSpace(include)) ?? Array.Empty<string>();
3964
}
4065

4166
public static XmlDocument? TryLoadDocument(string project)

src/Credfeto.Package.Update/Program.cs

Lines changed: 89 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -188,42 +188,109 @@ private static void UpdateProject(string project, IReadOnlyDictionary<string, Nu
188188
return;
189189
}
190190

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+
{
192207
XmlNodeList? nodes = doc.SelectNodes(xpath: "/Project/ItemGroup/PackageReference");
193208

194-
if (nodes != null && nodes.Count != 0)
209+
if (nodes == null || nodes.Count == 0)
195210
{
196-
Console.WriteLine();
197-
Console.WriteLine($"* {project}");
211+
return false;
212+
}
213+
214+
bool packageReferencesUpdated = false;
198215

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)
200221
{
201-
string package = node.GetAttribute(name: "Include");
222+
if (!PackageIdHelpers.IsExactMatch(package: package, packageId: nugetPackageId))
223+
{
224+
continue;
225+
}
202226

203-
foreach ((string nugetPackageId, NuGetVersion nugetVersion) in packages)
227+
bool updated = UpdateOnePackage(node: node, installedPackageId: package, nugetPackageId: nugetPackageId, nugetVersion: nugetVersion);
228+
229+
if (updated)
204230
{
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;
217233
}
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;
218271
}
219272

220-
if (changes != 0)
273+
bool upgrade = ShouldUpgrade(installedVersion: installedVersion, nugetVersion: nugetVersion);
274+
275+
if (!upgrade)
221276
{
222-
Console.WriteLine(value: "=========== UPDATED ===========");
277+
Console.WriteLine($" >> {installedPackageId} Installed: {installedVersion} Upgrade: False.");
223278

224-
ProjectHelpers.SaveProject(project: project, doc: doc);
279+
return false;
225280
}
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.
226291
}
292+
293+
return false;
227294
}
228295

229296
private static bool UpdateOnePackage(XmlElement node, string installedPackageId, string nugetPackageId, NuGetVersion nugetVersion)

0 commit comments

Comments
 (0)