Skip to content

Commit 3086dfd

Browse files
committed
Implemented the functions that allow for the execution and generation of custom winget cmd's
1 parent 661a06e commit 3086dfd

6 files changed

Lines changed: 318 additions & 2 deletions

File tree

src/WGet.NET/Components/WinGet.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,53 @@ public async Task<WinGetInfo> GetInfoAsync(CancellationToken cancellationToken =
687687
}
688688
//---------------------------------------------------------------------------------------------
689689

690+
//---Custom execute----------------------------------------------------------------------------
691+
/// <summary>
692+
/// Exectutes WinGet with the provided arguments.
693+
/// </summary>
694+
/// <param name="args">
695+
/// A <see cref="WGetNET.WinGetArguments"/> object containing the arguments for the WinGet process.
696+
/// </param>
697+
/// <returns>
698+
/// A <see cref="WGetNET.WinGetResult"/> object.
699+
/// </returns>
700+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
701+
/// WinGet is not installed or not found on the system.
702+
/// </exception>
703+
public WinGetResult ExecuteCustom(WinGetArguments args)
704+
{
705+
ThrowIfNotInstalled();
706+
707+
WinGetResult result = new(Execute(args), args);
708+
709+
return result;
710+
}
711+
712+
/// <summary>
713+
/// Asynchronously exectutes WinGet with the provided arguments.
714+
/// </summary>
715+
/// <param name="args">
716+
/// A <see cref="WGetNET.WinGetArguments"/> object containing the arguments for the WinGet process.
717+
/// </param>
718+
/// <param name="cancellationToken">
719+
/// The <see cref="System.Threading.CancellationToken"/> for the <see cref="System.Threading.Tasks.Task"/>.
720+
/// </param>
721+
/// <returns>
722+
/// A <see cref="System.Threading.Tasks.Task"/> containing the <see cref="WGetNET.WinGetResult"/> object.
723+
/// </returns>
724+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
725+
/// WinGet is not installed or not found on the system.
726+
/// </exception>
727+
public async Task<WinGetResult> ExecuteCustomAsync(WinGetArguments args, CancellationToken cancellationToken = default)
728+
{
729+
ThrowIfNotInstalled();
730+
731+
WinGetResult result = new(await ExecuteAsync(args, false, cancellationToken), args);
732+
733+
return result;
734+
}
735+
//---------------------------------------------------------------------------------------------
736+
690737
//---Protected Functions-----------------------------------------------------------------------
691738
// \cond PRIVATE
692739
/// <summary>

src/WGet.NET/Data/WinGetArguments.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ namespace WGetNET
77
/// <summary>
88
/// Represents a winget arguments string for different winget actions.
99
/// </summary>
10-
internal class WinGetArguments : IWinGetObject
10+
/// <remarks>
11+
/// Used to easily generate arguments for winget execution.
12+
/// </remarks>
13+
public class WinGetArguments : IWinGetObject
1114
{
1215
/// <summary>
1316
/// The <see cref="WGetNET.WinGetArguments.WinGetAction"/> <see langword="enum"/> can be used to specify the winget action,
@@ -307,7 +310,36 @@ public static WinGetArguments SourceExport()
307310
return new WinGetArguments("source export");
308311
}
309312

313+
/// <summary>
314+
/// Creates a new winget arguments object with a custom base cmd.
315+
/// </summary>
316+
/// <param name="cmd">
317+
/// A <see cref="System.String"/> containing the custom cmd.
318+
/// </param>
319+
/// <returns>
320+
/// The created <see cref="WGetNET.WinGetArguments"/> object.
321+
/// </returns>
322+
public static WinGetArguments CustomCmd(string cmd)
323+
{
324+
return new WinGetArguments(cmd);
325+
}
326+
310327
//---Flags-------------------------------------------------------------------------------------
328+
/// <summary>
329+
/// Adds a custom flag/parameter to the arguments.
330+
/// </summary>
331+
/// <param name="custom">
332+
/// The <see cref="System.String"/> that should be added to the arguments.
333+
/// </param>
334+
/// <returns>
335+
/// The updated <see cref="WGetNET.WinGetArguments"/> object.
336+
/// </returns>
337+
public WinGetArguments Custom(string custom)
338+
{
339+
_arguments += custom;
340+
return this;
341+
}
342+
311343
/// <summary>
312344
/// Adds a query to the arguments.
313345
/// </summary>

src/WGet.NET/Data/WinGetResult.cs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//--------------------------------------------------//
2+
// Created by basicx-StrgV //
3+
// https://github.com/basicx-StrgV/ //
4+
//--------------------------------------------------//
5+
using System;
6+
using WGetNET.Models;
7+
8+
namespace WGetNET
9+
{
10+
/// <summary>
11+
/// Represents a winget execution result.
12+
/// </summary>
13+
public sealed class WinGetResult : IWinGetObject
14+
{
15+
/// <summary>
16+
/// Gets the exit code of the process.
17+
/// </summary>
18+
public int ExitCode
19+
{
20+
get
21+
{
22+
if (_processResult != null)
23+
{
24+
return _processResult.ExitCode;
25+
}
26+
else
27+
{
28+
return -1;
29+
}
30+
}
31+
}
32+
33+
/// <summary>
34+
/// Gets the output of the process as a <see cref="System.String"/> array.
35+
/// </summary>
36+
/// <remarks>
37+
/// Each entry in the array contains a line from the output stream.
38+
/// </remarks>
39+
public string[] Output
40+
{
41+
get
42+
{
43+
if (_processResult != null)
44+
{
45+
return _processResult.Output;
46+
}
47+
else
48+
{
49+
return Array.Empty<string>();
50+
}
51+
}
52+
}
53+
54+
/// <summary>
55+
/// Gets if the process finished successfully.
56+
/// </summary>
57+
public bool Success
58+
{
59+
get
60+
{
61+
if (_processResult != null)
62+
{
63+
return _processResult.Success;
64+
}
65+
else
66+
{
67+
return false;
68+
}
69+
}
70+
}
71+
72+
/// <summary>
73+
/// Gets the cmd that was executed.
74+
/// </summary>
75+
public string ExecutedCmd
76+
{
77+
get
78+
{
79+
if (_arguments != null)
80+
{
81+
return $"winget {_arguments.ToString()}";
82+
}
83+
else
84+
{
85+
return string.Empty;
86+
}
87+
}
88+
}
89+
90+
/// <inheritdoc/>
91+
public bool IsEmpty
92+
{
93+
get
94+
{
95+
return (_processResult == null);
96+
}
97+
}
98+
99+
private readonly ProcessResult? _processResult = null;
100+
private readonly WinGetArguments? _arguments = null;
101+
102+
/// <summary>
103+
/// Initializes a new instance of the <see cref="WGetNET.WinGetResult"/> class.
104+
/// </summary>
105+
/// <param name="processResult">
106+
/// The process result object generated by the <see cref="WGetNET.Components.Internal.ProcessManager"/>.
107+
/// </param>
108+
/// <param name="arguments">
109+
/// The <see cref="WGetNET.WinGetArguments"/> object containing the executed cmd.
110+
/// </param>
111+
internal WinGetResult(ProcessResult processResult, WinGetArguments? arguments)
112+
{
113+
_processResult = processResult;
114+
_arguments = arguments;
115+
}
116+
}
117+
}

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

Lines changed: 107 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/WGetTest/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ private void Run()
169169
PackageTests = PackageTests && !p2.SamePackage(p1);
170170
PackageTests = PackageTests && !p2.SamePackage(p3);
171171
Console.WriteLine($"Result: {PackageTests}");
172+
173+
// Custom Execute Test
174+
Console.WriteLine("\n=== Custom Execute Test ===");
175+
WinGetResult test6 = connector.ExecuteCustom(WinGetArguments.List().Query("git"));
176+
Console.WriteLine($"Executed cmd: {test6.ExecutedCmd}");
177+
Console.WriteLine($"Exit Code: {test6.ExitCode}");
178+
Console.WriteLine($"Success: {test6.Success}");
172179
}
173180
catch (Exception e)
174181
{

src/WGetTestLegacySupport/Program.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ private void Run()
169169
PackageTests = PackageTests && !p2.SamePackage(p1);
170170
PackageTests = PackageTests && !p2.SamePackage(p3);
171171
Console.WriteLine($"Result: {PackageTests}");
172+
173+
// Custom Execute Test
174+
Console.WriteLine("\n=== Custom Execute Test ===");
175+
WinGetResult test6 = connector.ExecuteCustom(WinGetArguments.List().Query("git"));
176+
Console.WriteLine($"Executed cmd: {test6.ExecutedCmd}");
177+
Console.WriteLine($"Exit Code: {test6.ExitCode}");
178+
Console.WriteLine($"Success: {test6.Success}");
172179
}
173180
catch (Exception e)
174181
{

0 commit comments

Comments
 (0)