Skip to content

Commit adc4784

Browse files
committed
Improved version checking
1 parent 3c6f358 commit adc4784

3 files changed

Lines changed: 54 additions & 47 deletions

File tree

src/WGet.NET/Components/WinGetInfo.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,21 @@ public async Task<bool> ExportSettingsToFileAsync(string file)
220220
}
221221

222222
/// <summary>
223-
/// Checks if the installed WinGet version is the same or higher as the given version.
223+
/// Checks if the installed WinGet version is between the given versions or the same.
224224
/// </summary>
225-
/// <param name="major">The major version.</param>
226-
/// <param name="minor">The minor version.</param>
225+
/// <remarks>
226+
/// If no max version is provided, no upper limit will be set.
227+
/// </remarks>
228+
/// <param name="minVersion">The min version for the check.</param>
229+
/// <param name="maxVersion">The max version for the check.</param>
227230
/// <returns>
228-
/// <see langword="true"/> if the installed WinGet version is the same or higher as the given version, or <see langword="false"/> if not.
231+
/// <see langword="true"/> if the installed WinGet version matches the check, or <see langword="false"/> if not.
229232
/// </returns>
230-
protected bool WinGetVersionIsMatchOrAbove(int major, int minor = 0)
233+
protected bool CheckWinGetVersion(Version minVersion, Version? maxVersion = null)
231234
{
232235
Version winGetVersion = WinGetVersionObject;
233-
if (winGetVersion.Major >= major && winGetVersion.Minor >= minor)
236+
if ((winGetVersion.Major >= minVersion.Major && winGetVersion.Minor >= minVersion.Minor && winGetVersion.Build >= minVersion.Build) &&
237+
((maxVersion == null) || (winGetVersion.Major <= maxVersion.Major && winGetVersion.Minor <= maxVersion.Minor && winGetVersion.Build <= maxVersion.Build)))
234238
{
235239
return true;
236240
}

src/WGet.NET/Components/WinGetPackageManager.cs

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public class WinGetPackageManager : WinGetInfo
4040
private const string _pinRemoveInstalledCmd = "pin remove \"{0}\" --installed";
4141
private const string _pinResetCmd = "pin reset --force";
4242

43+
private readonly Version _downloadMinVersion = new(1, 6, 0);
44+
private readonly Version _pinMinVersion = new(1, 5, 0);
45+
4346
/// <summary>
4447
/// Initializes a new instance of the <see cref="WGetNET.WinGetPackageManager"/> class.
4548
/// </summary>
@@ -1066,7 +1069,7 @@ public async Task<bool> UpgradeAllPackagesAsync()
10661069
private string AddArgumentByVersion(string argument)
10671070
{
10681071
// Checking version to determine if "--include-unknown" is necessary.
1069-
if (WinGetVersionIsMatchOrAbove(1, 4))
1072+
if (CheckWinGetVersion(new Version(1, 4, 0)))
10701073
{
10711074
// Winget version supports new argument, add "--include-unknown" to arguments
10721075
argument += $" {_includeUnknown}";
@@ -1448,9 +1451,9 @@ private string HashResultToHash(ProcessResult result)
14481451
/// </exception>
14491452
public bool Download(string packageId, string directory)
14501453
{
1451-
if (!WinGetVersionIsMatchOrAbove(1, 6))
1454+
if (!CheckWinGetVersion(_downloadMinVersion))
14521455
{
1453-
throw new WinGetFeatureNotSupportedException("1.6");
1456+
throw new WinGetFeatureNotSupportedException(_downloadMinVersion);
14541457
}
14551458

14561459
try
@@ -1570,9 +1573,9 @@ public bool Download(WinGetPackage package, DirectoryInfo directory)
15701573
/// </exception>
15711574
public async Task<bool> DownloadAsync(string packageId, string directory)
15721575
{
1573-
if (!WinGetVersionIsMatchOrAbove(1, 6))
1576+
if (!CheckWinGetVersion(_downloadMinVersion))
15741577
{
1575-
throw new WinGetFeatureNotSupportedException("1.6");
1578+
throw new WinGetFeatureNotSupportedException(_downloadMinVersion);
15761579
}
15771580

15781581
try
@@ -1692,9 +1695,9 @@ public async Task<bool> DownloadAsync(WinGetPackage package, DirectoryInfo direc
16921695
/// </exception>
16931696
public List<WinGetPinnedPackage> GetPinnedPackages()
16941697
{
1695-
if (!WinGetVersionIsMatchOrAbove(1, 5))
1698+
if (!CheckWinGetVersion(_pinMinVersion))
16961699
{
1697-
throw new WinGetFeatureNotSupportedException("1.5");
1700+
throw new WinGetFeatureNotSupportedException(_pinMinVersion);
16981701
}
16991702

17001703
try
@@ -1732,9 +1735,9 @@ public List<WinGetPinnedPackage> GetPinnedPackages()
17321735
/// </exception>
17331736
public async Task<List<WinGetPinnedPackage>> GetPinnedPackagesAsync()
17341737
{
1735-
if (!WinGetVersionIsMatchOrAbove(1, 5))
1738+
if (!CheckWinGetVersion(_pinMinVersion))
17361739
{
1737-
throw new WinGetFeatureNotSupportedException("1.5");
1740+
throw new WinGetFeatureNotSupportedException(_pinMinVersion);
17381741
}
17391742

17401743
try
@@ -1776,9 +1779,9 @@ public async Task<List<WinGetPinnedPackage>> GetPinnedPackagesAsync()
17761779
/// </exception>
17771780
public bool PinAdd(string packageId, bool blocking = false)
17781781
{
1779-
if (!WinGetVersionIsMatchOrAbove(1, 5))
1782+
if (!CheckWinGetVersion(_pinMinVersion))
17801783
{
1781-
throw new WinGetFeatureNotSupportedException("1.5");
1784+
throw new WinGetFeatureNotSupportedException(_pinMinVersion);
17821785
}
17831786

17841787
try
@@ -1828,9 +1831,9 @@ public bool PinAdd(string packageId, bool blocking = false)
18281831
/// </exception>
18291832
public bool PinAdd(string packageId, string version)
18301833
{
1831-
if (!WinGetVersionIsMatchOrAbove(1, 5))
1834+
if (!CheckWinGetVersion(_pinMinVersion))
18321835
{
1833-
throw new WinGetFeatureNotSupportedException("1.5");
1836+
throw new WinGetFeatureNotSupportedException(_pinMinVersion);
18341837
}
18351838

18361839
try
@@ -1930,9 +1933,9 @@ public bool PinAdd(WinGetPackage package, string version)
19301933
/// </exception>
19311934
public async Task<bool> PinAddAsync(string packageId, bool blocking = false)
19321935
{
1933-
if (!WinGetVersionIsMatchOrAbove(1, 5))
1936+
if (!CheckWinGetVersion(_pinMinVersion))
19341937
{
1935-
throw new WinGetFeatureNotSupportedException("1.5");
1938+
throw new WinGetFeatureNotSupportedException(_pinMinVersion);
19361939
}
19371940

19381941
try
@@ -1983,9 +1986,9 @@ public async Task<bool> PinAddAsync(string packageId, bool blocking = false)
19831986
/// </exception>
19841987
public async Task<bool> PinAddAsync(string packageId, string version)
19851988
{
1986-
if (!WinGetVersionIsMatchOrAbove(1, 5))
1989+
if (!CheckWinGetVersion(_pinMinVersion))
19871990
{
1988-
throw new WinGetFeatureNotSupportedException("1.5");
1991+
throw new WinGetFeatureNotSupportedException(_pinMinVersion);
19891992
}
19901993

19911994
try
@@ -2085,9 +2088,9 @@ public async Task<bool> PinAddAsync(WinGetPackage package, string version)
20852088
/// </exception>
20862089
public bool PinAddInstalled(string packageId, bool blocking = false)
20872090
{
2088-
if (!WinGetVersionIsMatchOrAbove(1, 5))
2091+
if (!CheckWinGetVersion(_pinMinVersion))
20892092
{
2090-
throw new WinGetFeatureNotSupportedException("1.5");
2093+
throw new WinGetFeatureNotSupportedException(_pinMinVersion);
20912094
}
20922095

20932096
try
@@ -2138,9 +2141,9 @@ public bool PinAddInstalled(string packageId, bool blocking = false)
21382141
/// </exception>
21392142
public bool PinAddInstalled(string packageId, string version)
21402143
{
2141-
if (!WinGetVersionIsMatchOrAbove(1, 5))
2144+
if (!CheckWinGetVersion(_pinMinVersion))
21422145
{
2143-
throw new WinGetFeatureNotSupportedException("1.5");
2146+
throw new WinGetFeatureNotSupportedException(_pinMinVersion);
21442147
}
21452148

21462149
try
@@ -2240,9 +2243,9 @@ public bool PinAddInstalled(WinGetPackage package, string version)
22402243
/// </exception>
22412244
public async Task<bool> PinAddInstalledAsync(string packageId, bool blocking = false)
22422245
{
2243-
if (!WinGetVersionIsMatchOrAbove(1, 5))
2246+
if (!CheckWinGetVersion(_pinMinVersion))
22442247
{
2245-
throw new WinGetFeatureNotSupportedException("1.5");
2248+
throw new WinGetFeatureNotSupportedException(_pinMinVersion);
22462249
}
22472250

22482251
try
@@ -2293,9 +2296,9 @@ public async Task<bool> PinAddInstalledAsync(string packageId, bool blocking = f
22932296
/// </exception>
22942297
public async Task<bool> PinAddInstalledAsync(string packageId, string version)
22952298
{
2296-
if (!WinGetVersionIsMatchOrAbove(1, 5))
2299+
if (!CheckWinGetVersion(_pinMinVersion))
22972300
{
2298-
throw new WinGetFeatureNotSupportedException("1.5");
2301+
throw new WinGetFeatureNotSupportedException(_pinMinVersion);
22992302
}
23002303

23012304
try
@@ -2396,9 +2399,9 @@ public async Task<bool> PinAddInstalledAsync(WinGetPackage package, string versi
23962399
/// </exception>
23972400
public bool PinRemove(string packageId)
23982401
{
2399-
if (!WinGetVersionIsMatchOrAbove(1, 5))
2402+
if (!CheckWinGetVersion(_pinMinVersion))
24002403
{
2401-
throw new WinGetFeatureNotSupportedException("1.5");
2404+
throw new WinGetFeatureNotSupportedException(_pinMinVersion);
24022405
}
24032406

24042407
try
@@ -2466,9 +2469,9 @@ public bool PinRemove(WinGetPackage package)
24662469
/// </exception>
24672470
public async Task<bool> PinRemoveAsync(string packageId)
24682471
{
2469-
if (!WinGetVersionIsMatchOrAbove(1, 5))
2472+
if (!CheckWinGetVersion(_pinMinVersion))
24702473
{
2471-
throw new WinGetFeatureNotSupportedException("1.5");
2474+
throw new WinGetFeatureNotSupportedException(_pinMinVersion);
24722475
}
24732476

24742477
try
@@ -2535,9 +2538,9 @@ public async Task<bool> PinRemoveAsync(WinGetPackage package)
25352538
/// </exception>
25362539
public bool PinRemoveInstalled(string packageId)
25372540
{
2538-
if (!WinGetVersionIsMatchOrAbove(1, 5))
2541+
if (!CheckWinGetVersion(_pinMinVersion))
25392542
{
2540-
throw new WinGetFeatureNotSupportedException("1.5");
2543+
throw new WinGetFeatureNotSupportedException(_pinMinVersion);
25412544
}
25422545

25432546
try
@@ -2604,9 +2607,9 @@ public bool PinRemoveInstalled(WinGetPackage package)
26042607
/// </exception>
26052608
public async Task<bool> PinRemoveInstalledAsync(string packageId)
26062609
{
2607-
if (!WinGetVersionIsMatchOrAbove(1, 5))
2610+
if (!CheckWinGetVersion(_pinMinVersion))
26082611
{
2609-
throw new WinGetFeatureNotSupportedException("1.5");
2612+
throw new WinGetFeatureNotSupportedException(_pinMinVersion);
26102613
}
26112614

26122615
try
@@ -2677,9 +2680,9 @@ public async Task<bool> PinRemoveInstalledAsync(WinGetPackage package)
26772680
/// </exception>
26782681
public bool ResetPins()
26792682
{
2680-
if (!WinGetVersionIsMatchOrAbove(1, 5))
2683+
if (!CheckWinGetVersion(_pinMinVersion))
26812684
{
2682-
throw new WinGetFeatureNotSupportedException("1.5");
2685+
throw new WinGetFeatureNotSupportedException(_pinMinVersion);
26832686
}
26842687

26852688
try
@@ -2720,9 +2723,9 @@ public bool ResetPins()
27202723
/// </exception>
27212724
public async Task<bool> ResetPinsAsync()
27222725
{
2723-
if (!WinGetVersionIsMatchOrAbove(1, 5))
2726+
if (!CheckWinGetVersion(_pinMinVersion))
27242727
{
2725-
throw new WinGetFeatureNotSupportedException("1.5");
2728+
throw new WinGetFeatureNotSupportedException(_pinMinVersion);
27262729
}
27272730

27282731
try

src/WGet.NET/Exceptions/WinGetFeatureNotSupportedException.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public override string Message
2020
{
2121
get
2222
{
23-
if (string.IsNullOrWhiteSpace(_minVersion))
23+
if (_minVersion == null)
2424
{
2525
return "This feature is not supported in the installed WinGet version.";
2626
}
@@ -30,13 +30,13 @@ public override string Message
3030

3131
}
3232

33-
private readonly string _minVersion = "";
33+
private readonly Version? _minVersion;
3434

3535
/// <summary>
3636
/// Initializes a new instance of the <see cref="WGetNET.WinGetFeatureNotSupportedException"/> class.
3737
/// </summary>
3838
/// <param name="minVersion">Min WinGet version needed for the feature</param>
39-
public WinGetFeatureNotSupportedException(string minVersion)
39+
public WinGetFeatureNotSupportedException(Version minVersion)
4040
{
4141
_minVersion = minVersion;
4242
}
@@ -46,7 +46,7 @@ public WinGetFeatureNotSupportedException(string minVersion)
4646
/// </summary>
4747
/// <param name="minVersion">Min WinGet version needed for the feature</param>
4848
/// <param name="innerException">The inner exception</param>
49-
public WinGetFeatureNotSupportedException(string minVersion, Exception innerException) : base(null, innerException)
49+
public WinGetFeatureNotSupportedException(Version minVersion, Exception innerException) : base(null, innerException)
5050
{
5151
_minVersion = minVersion;
5252
}

0 commit comments

Comments
 (0)