Skip to content

Commit 3d6134f

Browse files
committed
Added improved args check for WinGet Objects
1 parent 36a7642 commit 3d6134f

10 files changed

Lines changed: 3656 additions & 3627 deletions

File tree

src/WGet.NET/Components/WinGetPackageManager.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ public bool InstallPackage(string packageId)
582582
/// </exception>
583583
public bool InstallPackage(WinGetPackage package)
584584
{
585-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
585+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
586586

587587
if (package.HasShortenedId)
588588
{
@@ -653,7 +653,7 @@ public async Task<bool> InstallPackageAsync(string packageId)
653653
/// </exception>
654654
public async Task<bool> InstallPackageAsync(WinGetPackage package)
655655
{
656-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
656+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
657657

658658
if (package.HasShortenedId)
659659
{
@@ -724,7 +724,7 @@ public bool UninstallPackage(string packageId)
724724
/// </exception>
725725
public bool UninstallPackage(WinGetPackage package)
726726
{
727-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
727+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
728728

729729
if (package.HasShortenedId)
730730
{
@@ -795,7 +795,7 @@ public async Task<bool> UninstallPackageAsync(string packageId)
795795
/// </exception>
796796
public async Task<bool> UninstallPackageAsync(WinGetPackage package)
797797
{
798-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
798+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
799799

800800
if (package.HasShortenedId)
801801
{
@@ -935,7 +935,7 @@ public bool UpgradePackage(string packageId)
935935
/// </exception>
936936
public bool UpgradePackage(WinGetPackage package)
937937
{
938-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
938+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
939939

940940
if (package.HasShortenedId)
941941
{
@@ -1006,7 +1006,7 @@ public async Task<bool> UpgradePackageAsync(string packageId)
10061006
/// </exception>
10071007
public async Task<bool> UpgradePackageAsync(WinGetPackage package)
10081008
{
1009-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
1009+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
10101010

10111011
if (package.HasShortenedId)
10121012
{
@@ -1623,7 +1623,7 @@ public bool Download(string packageId, DirectoryInfo directory)
16231623
/// </exception>
16241624
public bool Download(WinGetPackage package, string directory)
16251625
{
1626-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
1626+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
16271627

16281628
if (package.HasShortenedId)
16291629
{
@@ -1659,7 +1659,7 @@ public bool Download(WinGetPackage package, string directory)
16591659
/// </exception>
16601660
public bool Download(WinGetPackage package, DirectoryInfo directory)
16611661
{
1662-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
1662+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
16631663
ArgsHelper.ThrowIfObjectIsNull(directory, "directory");
16641664

16651665
if (package.HasShortenedId)
@@ -1774,7 +1774,7 @@ public async Task<bool> DownloadAsync(string packageId, DirectoryInfo directory)
17741774
/// </exception>
17751775
public async Task<bool> DownloadAsync(WinGetPackage package, string directory)
17761776
{
1777-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
1777+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
17781778

17791779
if (package.HasShortenedId)
17801780
{
@@ -1809,7 +1809,7 @@ public async Task<bool> DownloadAsync(WinGetPackage package, string directory)
18091809
/// </exception>
18101810
public async Task<bool> DownloadAsync(WinGetPackage package, DirectoryInfo directory)
18111811
{
1812-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
1812+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
18131813
ArgsHelper.ThrowIfObjectIsNull(directory, "directory");
18141814

18151815
if (package.HasShortenedId)
@@ -2034,7 +2034,7 @@ public bool PinAdd(string packageId, string version)
20342034
/// </exception>
20352035
public bool PinAdd(WinGetPackage package, bool blocking = false)
20362036
{
2037-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
2037+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
20382038

20392039
if (package.HasShortenedId)
20402040
{
@@ -2070,7 +2070,7 @@ public bool PinAdd(WinGetPackage package, bool blocking = false)
20702070
/// </exception>
20712071
public bool PinAdd(WinGetPackage package, string version)
20722072
{
2073-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
2073+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
20742074

20752075
if (package.HasShortenedId)
20762076
{
@@ -2210,7 +2210,7 @@ public async Task<bool> PinAddAsync(string packageId, string version)
22102210
/// </exception>
22112211
public async Task<bool> PinAddAsync(WinGetPackage package, bool blocking = false)
22122212
{
2213-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
2213+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
22142214

22152215
if (package.HasShortenedId)
22162216
{
@@ -2246,7 +2246,7 @@ public async Task<bool> PinAddAsync(WinGetPackage package, bool blocking = false
22462246
/// </exception>
22472247
public async Task<bool> PinAddAsync(WinGetPackage package, string version)
22482248
{
2249-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
2249+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
22502250

22512251
if (package.HasShortenedId)
22522252
{
@@ -2386,7 +2386,7 @@ public bool PinAddInstalled(string packageId, string version)
23862386
/// </exception>
23872387
public bool PinAddInstalled(WinGetPackage package, bool blocking = false)
23882388
{
2389-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
2389+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
23902390

23912391
if (package.HasShortenedId)
23922392
{
@@ -2422,7 +2422,7 @@ public bool PinAddInstalled(WinGetPackage package, bool blocking = false)
24222422
/// </exception>
24232423
public bool PinAddInstalled(WinGetPackage package, string version)
24242424
{
2425-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
2425+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
24262426

24272427
if (package.HasShortenedId)
24282428
{
@@ -2562,7 +2562,7 @@ public async Task<bool> PinAddInstalledAsync(string packageId, string version)
25622562
/// </exception>
25632563
public async Task<bool> PinAddInstalledAsync(WinGetPackage package, bool blocking = false)
25642564
{
2565-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
2565+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
25662566

25672567
if (package.HasShortenedId)
25682568
{
@@ -2598,7 +2598,7 @@ public async Task<bool> PinAddInstalledAsync(WinGetPackage package, bool blockin
25982598
/// </exception>
25992599
public async Task<bool> PinAddInstalledAsync(WinGetPackage package, string version)
26002600
{
2601-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
2601+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
26022602

26032603
if (package.HasShortenedId)
26042604
{
@@ -2680,7 +2680,7 @@ public bool PinRemove(string packageId)
26802680
/// </exception>
26812681
public bool PinRemove(WinGetPackage package)
26822682
{
2683-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
2683+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
26842684

26852685
if (package.HasShortenedId)
26862686
{
@@ -2760,7 +2760,7 @@ public async Task<bool> PinRemoveAsync(string packageId)
27602760
/// </exception>
27612761
public async Task<bool> PinRemoveAsync(WinGetPackage package)
27622762
{
2763-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
2763+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
27642764

27652765
if (package.HasShortenedId)
27662766
{
@@ -2840,7 +2840,7 @@ public bool PinRemoveInstalled(string packageId)
28402840
/// </exception>
28412841
public bool PinRemoveInstalled(WinGetPackage package)
28422842
{
2843-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
2843+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
28442844

28452845
if (package.HasShortenedId)
28462846
{
@@ -2920,7 +2920,7 @@ public async Task<bool> PinRemoveInstalledAsync(string packageId)
29202920
/// </exception>
29212921
public async Task<bool> PinRemoveInstalledAsync(WinGetPackage package)
29222922
{
2923-
ArgsHelper.ThrowIfObjectIsNull(package, "package");
2923+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
29242924

29252925
if (package.HasShortenedId)
29262926
{

src/WGet.NET/Components/WinGetSourceManager.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public bool AddSource(string name, string arg, string type)
240240
/// </exception>
241241
public bool AddSource(WinGetSource source)
242242
{
243-
ArgsHelper.ThrowIfObjectIsNull(source, "source");
243+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(source, "source");
244244

245245
if (string.IsNullOrWhiteSpace(source.Type))
246246
{
@@ -398,7 +398,7 @@ public async Task<bool> AddSourceAsync(string name, string arg, string type)
398398
/// </exception>
399399
public async Task<bool> AddSourceAsync(WinGetSource source)
400400
{
401-
ArgsHelper.ThrowIfObjectIsNull(source, "source");
401+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(source, "source");
402402

403403
if (string.IsNullOrWhiteSpace(source.Type))
404404
{
@@ -576,7 +576,7 @@ public string ExportSources(string sourceName)
576576
/// </exception>
577577
public string ExportSources(WinGetSource source)
578578
{
579-
ArgsHelper.ThrowIfObjectIsNull(source, "source");
579+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(source, "source");
580580

581581
return ExportSources(source.Name);
582582
}
@@ -677,7 +677,7 @@ public async Task<string> ExportSourcesAsync(string sourceName)
677677
/// </exception>
678678
public async Task<string> ExportSourcesAsync(WinGetSource source)
679679
{
680-
ArgsHelper.ThrowIfObjectIsNull(source, "source");
680+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(source, "source");
681681

682682
return await ExportSourcesAsync(source.Name);
683683
}
@@ -868,7 +868,7 @@ public bool ExportSourcesToFile(string file, string sourceName)
868868
/// </exception>
869869
public bool ExportSourcesToFile(string file, WinGetSource source)
870870
{
871-
ArgsHelper.ThrowIfObjectIsNull(source, "source");
871+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(source, "source");
872872

873873
return ExportSourcesToFile(file, source.Name);
874874
}
@@ -980,7 +980,7 @@ public async Task<bool> ExportSourcesToFileAsync(string file, string sourceName)
980980
/// </exception>
981981
public async Task<bool> ExportSourcesToFileAsync(string file, WinGetSource source)
982982
{
983-
ArgsHelper.ThrowIfObjectIsNull(source, "source");
983+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(source, "source");
984984

985985
return await ExportSourcesToFileAsync(file, source.Name);
986986
}
@@ -1464,7 +1464,7 @@ public bool RemoveSources(string name)
14641464
/// </exception>
14651465
public bool RemoveSources(WinGetSource source)
14661466
{
1467-
ArgsHelper.ThrowIfObjectIsNull(source, "source");
1467+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(source, "source");
14681468

14691469
return RemoveSources(source.Name);
14701470
}
@@ -1545,7 +1545,7 @@ public async Task<bool> RemoveSourcesAsync(string name)
15451545
/// </exception>
15461546
public async Task<bool> RemoveSourcesAsync(WinGetSource source)
15471547
{
1548-
ArgsHelper.ThrowIfObjectIsNull(source, "source");
1548+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(source, "source");
15491549

15501550
return await RemoveSourcesAsync(source.Name);
15511551
}

src/WGet.NET/HelperClasses/ArgsHelper.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,13 @@ public static void ThrowIfObjectIsNull(object arg, string name)
2323
throw new ArgumentNullException(name);
2424
}
2525
}
26+
27+
public static void ThrowIfWinGetObjectIsNullOrEmpty(IWinGetObject arg, string name)
28+
{
29+
if (arg == null || arg.IsEmpty)
30+
{
31+
throw new ArgumentNullException(name);
32+
}
33+
}
2634
}
2735
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//--------------------------------------------------//
2+
// Created by basicx-StrgV //
3+
// https://github.com/basicx-StrgV/ //
4+
//--------------------------------------------------//
5+
namespace WGetNET
6+
{
7+
/// <summary>
8+
/// Interface for all WinGet related objects.
9+
/// </summary>
10+
public interface IWinGetObject
11+
{
12+
/// <summary>
13+
/// Gets if the object is empty.
14+
/// </summary>
15+
public bool IsEmpty { get; }
16+
}
17+
}

src/WGet.NET/Models/WinGetData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace WGetNET
1010
/// <summary>
1111
/// Represents WinGet related data
1212
/// </summary>
13-
public class WinGetData
13+
public class WinGetData: IWinGetObject
1414
{
1515
/// <summary>
1616
/// Gets the version number of the winget installation as a <see cref="System.String"/>.

src/WGet.NET/Models/WinGetInfoEntry.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace WGetNET
77
/// <summary>
88
/// Represents a basic WinGet info entry
99
/// </summary>
10-
public class WinGetInfoEntry
10+
public class WinGetInfoEntry: IWinGetObject
1111
{
1212
/// <summary>
1313
/// Gets the name of this info entry.
@@ -42,6 +42,18 @@ public bool HasShortenedContent
4242
}
4343
}
4444

45+
public bool IsEmpty
46+
{
47+
get
48+
{
49+
if ((_name.Length + _content.Length) > 0)
50+
{
51+
return false;
52+
}
53+
return true;
54+
}
55+
}
56+
4557
private protected readonly string _name;
4658
private protected readonly string _content;
4759
private protected readonly bool _hasShortenedContent;

0 commit comments

Comments
 (0)