Skip to content

Commit 978f956

Browse files
committed
Added additional functions to execute custom winget cmd's that only take strings as parameters; Data processing improvements
1 parent 32ba30a commit 978f956

5 files changed

Lines changed: 172 additions & 25 deletions

File tree

src/WGet.NET/Components/WinGet.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,15 +700,51 @@ public async Task<WinGetInfo> GetInfoAsync(CancellationToken cancellationToken =
700700
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
701701
/// WinGet is not installed or not found on the system.
702702
/// </exception>
703+
/// <exception cref="System.ArgumentNullException">
704+
/// A provided argument is null.
705+
/// </exception>
703706
public WinGetResult ExecuteCustom(WinGetArguments args)
704707
{
708+
ArgsHelper.ThrowIfObjectIsNull(args, "args");
709+
705710
ThrowIfNotInstalled();
706711

707712
WinGetResult result = new(Execute(args), args);
708713

709714
return result;
710715
}
711716

717+
/// <summary>
718+
/// Exectutes WinGet with the provided arguments.
719+
/// </summary>
720+
/// <param name="args">
721+
/// A <see cref="System.String"/> containing the arguments for the WinGet process.
722+
/// </param>
723+
/// <returns>
724+
/// A <see cref="WGetNET.WinGetResult"/> object.
725+
/// </returns>
726+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
727+
/// WinGet is not installed or not found on the system.
728+
/// </exception>
729+
/// <exception cref="System.ArgumentNullException">
730+
/// A provided argument is null.
731+
/// </exception>
732+
/// <exception cref="System.ArgumentException">
733+
/// A provided argument is empty.
734+
/// </exception>
735+
public WinGetResult ExecuteCustom(string args)
736+
{
737+
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(args, "args");
738+
739+
ThrowIfNotInstalled();
740+
741+
WinGetArguments argsObj = WinGetArguments.CustomCmd(args);
742+
743+
WinGetResult result = new(Execute(argsObj), argsObj);
744+
745+
return result;
746+
}
747+
712748
/// <summary>
713749
/// Asynchronously exectutes WinGet with the provided arguments.
714750
/// </summary>
@@ -724,14 +760,53 @@ public WinGetResult ExecuteCustom(WinGetArguments args)
724760
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
725761
/// WinGet is not installed or not found on the system.
726762
/// </exception>
763+
/// <exception cref="System.ArgumentNullException">
764+
/// A provided argument is null.
765+
/// </exception>
727766
public async Task<WinGetResult> ExecuteCustomAsync(WinGetArguments args, CancellationToken cancellationToken = default)
728767
{
768+
ArgsHelper.ThrowIfObjectIsNull(args, "args");
769+
729770
ThrowIfNotInstalled();
730771

731772
WinGetResult result = new(await ExecuteAsync(args, false, cancellationToken), args);
732773

733774
return result;
734775
}
776+
777+
/// <summary>
778+
/// Asynchronously exectutes WinGet with the provided arguments.
779+
/// </summary>
780+
/// <param name="args">
781+
/// A <see cref="System.String"/> containing the arguments for the WinGet process.
782+
/// </param>
783+
/// <param name="cancellationToken">
784+
/// The <see cref="System.Threading.CancellationToken"/> for the <see cref="System.Threading.Tasks.Task"/>.
785+
/// </param>
786+
/// <returns>
787+
/// A <see cref="System.Threading.Tasks.Task"/> containing the <see cref="WGetNET.WinGetResult"/> object.
788+
/// </returns>
789+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
790+
/// WinGet is not installed or not found on the system.
791+
/// </exception>
792+
/// <exception cref="System.ArgumentNullException">
793+
/// A provided argument is null.
794+
/// </exception>
795+
/// <exception cref="System.ArgumentException">
796+
/// A provided argument is empty.
797+
/// </exception>
798+
public async Task<WinGetResult> ExecuteCustomAsync(string args, CancellationToken cancellationToken = default)
799+
{
800+
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(args, "args");
801+
802+
ThrowIfNotInstalled();
803+
804+
WinGetArguments argsObj = WinGetArguments.CustomCmd(args);
805+
806+
WinGetResult result = new(await ExecuteAsync(argsObj, false, cancellationToken), argsObj);
807+
808+
return result;
809+
}
735810
//---------------------------------------------------------------------------------------------
736811

737812
//---Protected Functions-----------------------------------------------------------------------

src/WGet.NET/Data/WinGetArguments.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,16 @@ public static WinGetArguments SourceExport()
321321
/// </returns>
322322
public static WinGetArguments CustomCmd(string cmd)
323323
{
324+
cmd = cmd.ToLower().Trim();
325+
if (cmd.StartsWith("winget"))
326+
{
327+
#if NETCOREAPP3_1_OR_GREATER
328+
cmd = cmd[6..];
329+
#elif NETSTANDARD2_0
330+
cmd = cmd.Substring(6);
331+
#endif
332+
}
333+
324334
return new WinGetArguments(cmd);
325335
}
326336

@@ -336,7 +346,7 @@ public static WinGetArguments CustomCmd(string cmd)
336346
/// </returns>
337347
public WinGetArguments Custom(string custom)
338348
{
339-
_arguments += custom;
349+
_arguments += $" {custom.ToLower().Trim()}";
340350
return this;
341351
}
342352

src/WGet.NET/XmlDocumentation/WGet.NET.xml

Lines changed: 70 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/WGetTest/Program.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ private void Run()
2222
Stopwatch sw = Stopwatch.StartNew();
2323
try
2424
{
25+
Console.WriteLine("TEST APP NETCORE 3.1\n\n");
26+
2527
Console.WriteLine("=== Winget Information ===");
2628
WinGetPackageManager connector = new WinGetPackageManager();
2729
WinGetSourceManager sourceManager = new WinGetSourceManager();
@@ -172,10 +174,16 @@ private void Run()
172174

173175
// Custom Execute Test
174176
Console.WriteLine("\n=== Custom Execute Test ===");
177+
Console.WriteLine("TEST 1:");
175178
WinGetResult test6 = connector.ExecuteCustom(WinGetArguments.List().Query("git"));
176179
Console.WriteLine($"Executed cmd: {test6.ExecutedCmd}");
177180
Console.WriteLine($"Exit Code: {test6.ExitCode}");
178181
Console.WriteLine($"Success: {test6.Success}");
182+
Console.WriteLine("\nTEST 2:");
183+
WinGetResult test7 = connector.ExecuteCustom(WinGetArguments.CustomCmd("list").Custom("-q git"));
184+
Console.WriteLine($"Executed cmd: {test7.ExecutedCmd}");
185+
Console.WriteLine($"Exit Code: {test7.ExitCode}");
186+
Console.WriteLine($"Success: {test7.Success}");
179187
}
180188
catch (Exception e)
181189
{

src/WGetTestLegacySupport/Program.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ private void Run()
2222
Stopwatch sw = Stopwatch.StartNew();
2323
try
2424
{
25+
Console.WriteLine("TEST APP NETSTANDARD 2.0\n\n");
26+
2527
Console.WriteLine("=== Winget Information ===");
2628
WinGetPackageManager connector = new WinGetPackageManager();
2729
WinGetSourceManager sourceManager = new WinGetSourceManager();
@@ -172,10 +174,16 @@ private void Run()
172174

173175
// Custom Execute Test
174176
Console.WriteLine("\n=== Custom Execute Test ===");
177+
Console.WriteLine("TEST 1:");
175178
WinGetResult test6 = connector.ExecuteCustom(WinGetArguments.List().Query("git"));
176179
Console.WriteLine($"Executed cmd: {test6.ExecutedCmd}");
177180
Console.WriteLine($"Exit Code: {test6.ExitCode}");
178181
Console.WriteLine($"Success: {test6.Success}");
182+
Console.WriteLine("\nTEST 2:");
183+
WinGetResult test7 = connector.ExecuteCustom(WinGetArguments.CustomCmd("list").Custom("-q git"));
184+
Console.WriteLine($"Executed cmd: {test7.ExecutedCmd}");
185+
Console.WriteLine($"Exit Code: {test7.ExitCode}");
186+
Console.WriteLine($"Success: {test7.Success}");
179187
}
180188
catch (Exception e)
181189
{

0 commit comments

Comments
 (0)