Skip to content

Commit 421c0e0

Browse files
authored
Merge pull request #13 from meokullu/new_version_0001
New version 0001
2 parents 37ef614 + 272a8ec commit 421c0e0

6 files changed

Lines changed: 127 additions & 74 deletions

File tree

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
#### Security
1717
-->
1818

19+
### [1.3.0] (2025-11-24)
20+
#### Added
21+
* Null check for return package data in `CheckVersionAsync(string path, bool includePrerelease)` method. If it is null, return empty list of Packages.
22+
* Null check for return package data in `CheckVersionAsync(List<string> pathList, bool includePrerelease)` method. If it is null, return empty list of Packages.
23+
* Network availability check before making NuGet API calls in `GetPackageFromNuGetAsync(string packageName, bool includePrerelease)` method. If network is unavailable, it return empty Package.
24+
* Network availability check before making NuGet API calls in `GetPackagesFromNuGetAsync(List<string> packageNameList, bool includePrerelease)` method. If network is unavailable, it return empty list of Packages.
25+
26+
#### Changed
27+
* Updated NuGet.Protocol package to the 6.14.0 version.
28+
1929
### [1.2.0] (2024-07-07)
2030
#### Added
2131
* `PackageEqualityComparer` class is added into `Package`. It has `Equal(Package x, Package y)` and `GetHashCode(Package obj)` interface methods of `IEqualityComparer<Package>`.

NuGetVersionChecker/NuGetVersionChecker.csproj

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,25 @@
1111
<RepositoryType>git</RepositoryType>
1212
<PackageTags>version; version-checker; version-compare; NuGet; Nuget-Package; version-check; Dependency</PackageTags>
1313
<PackageReleaseNotes>
14-
v1.2.0
15-
PackageEqualityComparer added which is derived from IEqualityComparer.
16-
CheckVersionAsync() method is added to compare versions as a list of Package.
17-
GetPackageFromNuGetAsync() and GetPackagesFromNuGetAsync() method have now includePrerelease option parameter which is set false default to determine if prerelease packages are included during search from NuGet API.
14+
v1.3.0
15+
* Null check for return package data in `CheckVersionAsync(string path, bool includePrerelease)` method. If it is null, return empty list of Packages.
16+
* Null check for return package data in `CheckVersionAsync(List&lt;string&gt;
17+
pathList, bool includePrerelease)` method. If it is null, return empty list of Packages.
18+
* Network availability check before making NuGet API calls in `GetPackageFromNuGetAsync(string packageName, bool includePrerelease)` method. If network is unavailable, it return empty Package.
19+
* Network availability check before making NuGet API calls in `GetPackagesFromNuGetAsync(List&lt;string&gt; packageNameList, bool includePrerelease)` method. If network is unavailable, it return empty list of Packages.
20+
1821
See changelog (https://github.com/meokullu/NuGetVersionChecker/blob/master/CHANGELOG.md)
1922
</PackageReleaseNotes>
20-
<AssemblyVersion>1.2.0</AssemblyVersion>
21-
<FileVersion>1.2.0</FileVersion>
23+
<AssemblyVersion>1.3.0</AssemblyVersion>
24+
<FileVersion>1.3.0</FileVersion>
2225
<Title>Nuget Version Checker</Title>
2326
<PackageReadmeFile>README.md</PackageReadmeFile>
2427
<PackageProjectUrl>https://meokullu.github.io/NuGetVersionChecker/</PackageProjectUrl>
2528
<RepositoryUrl>https://github.com/meokullu/NuGetVersionChecker</RepositoryUrl>
2629
<ApplicationIcon>Resources\icon256.ico</ApplicationIcon>
2730
<Copyright>Enes Okullu</Copyright>
2831
<PackageIcon>icon128.png</PackageIcon>
29-
<Version>1.2.0</Version>
32+
<Version>1.3.0</Version>
3033
<PackageLicenseFile>LICENSE</PackageLicenseFile>
3134
</PropertyGroup>
3235

@@ -63,7 +66,7 @@
6366
</ItemGroup>
6467

6568
<ItemGroup>
66-
<PackageReference Include="NuGet.Protocol" Version="6.10.0">
69+
<PackageReference Include="NuGet.Protocol" Version="6.14.0">
6770
</PackageReference>
6871
</ItemGroup>
6972

Lines changed: 91 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using NuGet.Common;
22
using NuGet.Protocol;
33
using NuGet.Protocol.Core.Types;
4-
using System;
54
using System.Collections.Generic;
65
using System.Linq;
76
using System.Threading;
87
using System.Threading.Tasks;
8+
using System.Net.NetworkInformation;
99

1010
namespace NuGetVersionChecker
1111
{
@@ -30,61 +30,18 @@ public partial class NuGetVersionChecker
3030
/// <returns>Returns a package.</returns>
3131
public static async Task<Package> GetPackageFromNuGetAsync(string packageName, bool includePrerelease = true)
3232
{
33-
PackageSearchResource resource = await s_repository.GetResourceAsync<PackageSearchResource>();
34-
SearchFilter searchFilter = new SearchFilter(includePrerelease: includePrerelease);
35-
36-
// Making a search with taking one package.
37-
IEnumerable<IPackageSearchMetadata> searchResults = await resource.SearchAsync(
38-
packageName,
39-
searchFilter,
40-
skip: 0,
41-
take: 1,
42-
s_logger,
43-
s_cancellationToken);
44-
45-
// Getting only one value from the list. Since it takes only 1, it is "Single" instead of "First".
46-
IPackageSearchMetadata package = searchResults.SingleOrDefault();
47-
48-
// Checking if package is null.
49-
if (package == null)
33+
// Quick check for network availability to avoid unnecessary network calls when offline.
34+
if (NetworkInterface.GetIsNetworkAvailable() == false)
5035
{
5136
return new Package();
5237
}
5338

54-
// Get version info of list of the package.
55-
IEnumerable<VersionInfo> versionInfoList = await package.GetVersionsAsync();
56-
57-
// Checking if versionInfoList is null or empty.
58-
if (versionInfoList == null || versionInfoList.Any() == false)
39+
try
5940
{
60-
return new Package();
61-
}
62-
63-
// Get latets version of the list of versions.
64-
VersionInfo versionInfo = versionInfoList.OrderByDescending(p => p.Version).First();
65-
66-
// Returning a Package via creating with its constructor.
67-
return new Package(name: package.Identity.Id, version: versionInfo.Version);
68-
}
41+
PackageSearchResource resource = await s_repository.GetResourceAsync<PackageSearchResource>();
42+
SearchFilter searchFilter = new SearchFilter(includePrerelease: includePrerelease);
6943

70-
/// <summary>
71-
/// Returns list of packages by making search on NuGet.
72-
/// </summary>
73-
/// <param name="packageNameList">List of package names.</param>
74-
/// <param name="includePrerelease">Include Prerelease version to compare versions.</param>
75-
/// <returns>List of packages.</returns>
76-
public static async Task<List<Package>> GetPackagesFromNuGetAsync(List<string> packageNameList, bool includePrerelease = true)
77-
{
78-
PackageSearchResource resource = await s_repository.GetResourceAsync<PackageSearchResource>();
79-
SearchFilter searchFilter = new SearchFilter(includePrerelease: includePrerelease);
80-
81-
// Return value.
82-
List<Package> packageList = new List<Package>();
83-
84-
// Loop for given list of package name.
85-
foreach (string packageName in packageNameList)
86-
{
87-
//
44+
// Making a search with taking one package.
8845
IEnumerable<IPackageSearchMetadata> searchResults = await resource.SearchAsync(
8946
packageName,
9047
searchFilter,
@@ -99,33 +56,105 @@ public static async Task<List<Package>> GetPackagesFromNuGetAsync(List<string> p
9956
// Checking if package is null.
10057
if (package == null)
10158
{
102-
continue;
59+
return new Package();
10360
}
10461

105-
// Get latest version info of newest version of the package.
62+
// Get version info of list of the package.
10663
IEnumerable<VersionInfo> versionInfoList = await package.GetVersionsAsync();
10764

10865
// Checking if versionInfoList is null or empty.
10966
if (versionInfoList == null || versionInfoList.Any() == false)
11067
{
111-
continue;
68+
return new Package();
11269
}
11370

114-
// Get latest version of the list of versions.
71+
// Get latets version of the list of versions.
11572
VersionInfo versionInfo = versionInfoList.OrderByDescending(p => p.Version).First();
11673

117-
// Checking if versionInfo is null.
118-
if (versionInfo == null)
74+
// Returning a Package via creating with its constructor.
75+
return new Package(name: package.Identity.Id, version: versionInfo.Version);
76+
}
77+
catch
78+
{
79+
// If any network or runtime error occurs, return empty package.
80+
return new Package();
81+
}
82+
}
83+
84+
/// <summary>
85+
/// Returns list of packages by making search on NuGet.
86+
/// </summary>
87+
/// <param name="packageNameList">List of package names.</param>
88+
/// <param name="includePrerelease">Include Prerelease version to compare versions.</param>
89+
/// <returns>List of packages.</returns>
90+
public static async Task<List<Package>> GetPackagesFromNuGetAsync(List<string> packageNameList, bool includePrerelease = true)
91+
{
92+
// Return value.
93+
List<Package> packageList = new List<Package>();
94+
95+
// Quick check for network availability to avoid unnecessary network calls when offline.
96+
if (NetworkInterface.GetIsNetworkAvailable() == false)
97+
{
98+
return packageList;
99+
}
100+
101+
try
102+
{
103+
PackageSearchResource resource = await s_repository.GetResourceAsync<PackageSearchResource>();
104+
SearchFilter searchFilter = new SearchFilter(includePrerelease: includePrerelease);
105+
106+
107+
// Loop for given list of package name.
108+
foreach (string packageName in packageNameList)
119109
{
120-
continue;
110+
//
111+
IEnumerable<IPackageSearchMetadata> searchResults = await resource.SearchAsync(
112+
packageName,
113+
searchFilter,
114+
skip: 0,
115+
take: 1,
116+
s_logger,
117+
s_cancellationToken);
118+
119+
// Getting only one value from the list. Since it takes only 1, it is "Single" instead of "First".
120+
IPackageSearchMetadata package = searchResults.SingleOrDefault();
121+
122+
// Checking if package is null.
123+
if (package == null)
124+
{
125+
continue;
126+
}
127+
128+
// Get latest version info of newest version of the package.
129+
IEnumerable<VersionInfo> versionInfoList = await package.GetVersionsAsync();
130+
131+
// Checking if versionInfoList is null or empty.
132+
if (versionInfoList == null || versionInfoList.Any() == false)
133+
{
134+
continue;
135+
}
136+
137+
// Get latest version of the list of versions.
138+
VersionInfo versionInfo = versionInfoList.OrderByDescending(p => p.Version).First();
139+
140+
// Checking if versionInfo is null.
141+
if (versionInfo == null)
142+
{
143+
continue;
144+
}
145+
146+
// Adding the Package via creating with its constructor.
147+
packageList.Add(new Package(name: package.Identity.Id, version: versionInfo.Version));
121148
}
122149

123-
// Adding the Package via creating with its constructor.
124-
packageList.Add(new Package(name: package.Identity.Id, version: versionInfo.Version));
150+
// Retuning list of packages as return data.
151+
return packageList;
152+
}
153+
catch
154+
{
155+
// If any network or runtime error occurs, return list of empty packages.
156+
return packageList;
125157
}
126-
127-
// Retuning list of packages as return data.
128-
return packageList;
129158
}
130159
}
131160
}

NuGetVersionChecker/src/Package.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public bool Equals(Package x, Package y)
8787
if (x == null || y == null)
8888
{
8989
// Returning false to indicate at least one of packages is null.
90-
return false;
90+
return false;
9191
}
9292
// Checking if two given package's property values are same.
9393
else if (x.Name == y.Name && x.Version == y.Version)
@@ -113,5 +113,5 @@ public int GetHashCode(Package obj)
113113
}
114114
}
115115
}
116-
}
116+
}
117117
}

NuGetVersionChecker/src/ParseFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static List<Package> GetPackages(string path)
5050
{
5151
// Checking if node is null.
5252
if (xmlNode == null)
53-
{
53+
{
5454
continue;
5555
}
5656

NuGetVersionChecker/src/VersionChecker.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Diagnostics;
32
using System.Linq;
43
using System.Threading.Tasks;
54
using static NuGetVersionChecker.NuGetVersionChecker.Package;
@@ -33,6 +32,12 @@ public static async Task<List<Package>> CheckVersionAsync(string path, bool incl
3332
packageNameList: usedPackages.Select(p => p.Name).ToList(),
3433
includePrerelease: includePrerelease);
3534

35+
// Check if nuget packages is null or empty.
36+
if (nugetPackages == null || nugetPackages.Count == 0)
37+
{
38+
return packages;
39+
}
40+
3641
// Loop for packages
3742
foreach (Package package in usedPackages)
3843
{
@@ -89,6 +94,12 @@ public static async Task<List<Package>> CheckVersionAsync(List<string> pathList,
8994
packageNameList: disctinctUsedPackage.Select(p => p.Name).ToList(),
9095
includePrerelease: includePrerelease);
9196

97+
// Check if nuget packages is null or empty.
98+
if (nugetPackages == null || nugetPackages.Count == 0)
99+
{
100+
return packages;
101+
}
102+
92103
// Loop for packages
93104
foreach (Package package in usedPackages)
94105
{

0 commit comments

Comments
 (0)