Skip to content

Commit 0eb57e4

Browse files
committed
Added the functionality to search a package by source
1 parent 42bdab3 commit 0eb57e4

5 files changed

Lines changed: 162 additions & 11 deletions

File tree

src/WGet.NET/Components/WinGetPackageManager.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class WinGetPackageManager : WinGetInfo
1919
private const string _listCmd = "list";
2020
private const string _searchInstalledCmd = "list {0}";
2121
private const string _searchCmd = "search {0} --accept-source-agreements";
22+
private const string _searchBySourceCmd = "search {0} --source {1} --accept-source-agreements";
2223
private const string _installCmd = "install {0}";
2324
private const string _upgradeCmd = "upgrade {0}";
2425
private const string _upgradeAllCmd = "upgrade --all";
@@ -74,6 +75,45 @@ public List<WinGetPackage> SearchPackage(string packageName)
7475
}
7576
}
7677

78+
/// <summary>
79+
/// Uses the winget search function to search for a package that maches the given name.
80+
/// </summary>
81+
/// <param name="packageName">
82+
/// The name of the package for the search.
83+
/// </param>
84+
/// <param name="sourceName">
85+
/// The name of the source for the search.
86+
/// </param>
87+
/// <returns>
88+
/// A <see cref="System.Collections.Generic.List{T}"/> of <see cref="WGetNET.WinGetPackage"/> instances.
89+
/// </returns>
90+
/// <exception cref="WGetNET.WinGetNotInstalledException">
91+
/// WinGet is not installed or not found on the system.
92+
/// </exception>
93+
/// <exception cref="WGetNET.WinGetActionFailedException">
94+
/// The current action failed for an unexpected reason.
95+
/// Please see inner exception.
96+
/// </exception>
97+
public List<WinGetPackage> SearchPackage(string packageName, string sourceName)
98+
{
99+
try
100+
{
101+
ProcessResult result =
102+
_processManager.ExecuteWingetProcess(
103+
string.Format(_searchBySourceCmd, packageName, sourceName));
104+
105+
return ProcessOutputReader.ToPackageList(result.Output, PackageAction.SearchBySource, sourceName);
106+
}
107+
catch (Win32Exception)
108+
{
109+
throw new WinGetNotInstalledException();
110+
}
111+
catch (Exception e)
112+
{
113+
throw new WinGetActionFailedException("The package search failed.", e);
114+
}
115+
}
116+
77117
/// <summary>
78118
/// Uses the winget search function to asynchronously search for a package that maches the given name.
79119
/// </summary>
@@ -110,6 +150,46 @@ await _processManager.ExecuteWingetProcessAsync(
110150
throw new WinGetActionFailedException("The package search failed.", e);
111151
}
112152
}
153+
154+
/// <summary>
155+
/// Uses the winget search function to asynchronously search for a package that maches the given name.
156+
/// </summary>
157+
/// <param name="packageName">
158+
/// The name of the package for the search.
159+
/// </param>
160+
/// <param name="sourceName">
161+
/// The name of the source for the search.
162+
/// </param>
163+
/// <returns>
164+
/// A <see cref="System.Threading.Tasks.Task"/>, containing the result.
165+
/// The result is a <see cref="System.Collections.Generic.List{T}"/> of <see cref="WGetNET.WinGetPackage"/> instances.
166+
/// </returns>
167+
/// <exception cref="WGetNET.WinGetNotInstalledException">
168+
/// WinGet is not installed or not found on the system.
169+
/// </exception>
170+
/// <exception cref="WGetNET.WinGetActionFailedException">
171+
/// The current action failed for an unexpected reason.
172+
/// Please see inner exception.
173+
/// </exception>
174+
public async Task<List<WinGetPackage>> SearchPackageAsync(string packageName, string sourceName)
175+
{
176+
try
177+
{
178+
ProcessResult result =
179+
await _processManager.ExecuteWingetProcessAsync(
180+
string.Format(_searchBySourceCmd, packageName, sourceName));
181+
182+
return ProcessOutputReader.ToPackageList(result.Output, PackageAction.SearchBySource, sourceName);
183+
}
184+
catch (Win32Exception)
185+
{
186+
throw new WinGetNotInstalledException();
187+
}
188+
catch (Exception e)
189+
{
190+
throw new WinGetActionFailedException("The package search failed.", e);
191+
}
192+
}
113193
//---------------------------------------------------------------------------------------------
114194

115195
//---Install-----------------------------------------------------------------------------------

src/WGet.NET/Enums/PackageAction.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ internal enum PackageAction
55
Default,
66
UpgradeList,
77
InstalledList,
8-
Search
8+
Search,
9+
SearchBySource
910
}
1011
}

src/WGet.NET/HelperClasses/ProcessOutputReader.cs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ internal static class ProcessOutputReader
2323
/// <param name="action">
2424
/// Sets info about the action that is executet.
2525
/// </param>
26+
/// <param name="sourceName">
27+
/// Name of the source used in the search by source action.
28+
/// </param>
2629
/// <returns>
2730
/// A <see cref="System.Collections.Generic.List{T}"/> of <see cref="WGetNET.WinGetPackage"/>'s.
2831
/// </returns>
29-
public static List<WinGetPackage> ToPackageList(string[] output, PackageAction action = PackageAction.Default)
32+
public static List<WinGetPackage> ToPackageList(string[] output, PackageAction action = PackageAction.Default, string? sourceName = null)
3033
{
3134
//Get top line index.
3235
//The array should always contain this line.
@@ -39,7 +42,7 @@ public static List<WinGetPackage> ToPackageList(string[] output, PackageAction a
3942
//Remove unneeded output Lines
4043
output = ArrayManager.RemoveRange(output, 0, labelLine + 2);
4144

42-
return CreatePackageListFromOutput(output, columnList, action);
45+
return CreatePackageListFromOutput(output, columnList, action, sourceName);
4346
}
4447

4548
/// <summary>
@@ -54,14 +57,17 @@ public static List<WinGetPackage> ToPackageList(string[] output, PackageAction a
5457
/// <param name="action">
5558
/// Sets info about the action that is executet.
5659
/// </param>
60+
/// <param name="sourceName">
61+
/// Name of the source used in the search by source action.
62+
/// </param>
5763
/// <returns>
5864
/// A <see cref="System.Collections.Generic.List{T}"/> of <see cref="WGetNET.WinGetPackage"/>'s.
5965
/// </returns>
60-
private static List<WinGetPackage> CreatePackageListFromOutput(string[] output, int[] columnList, PackageAction action = PackageAction.Default)
66+
private static List<WinGetPackage> CreatePackageListFromOutput(string[] output, int[] columnList, PackageAction action = PackageAction.Default, string? sourceName = null)
6167
{
6268
List<WinGetPackage> resultList = new List<WinGetPackage>();
6369

64-
if (columnList.Length < 4)
70+
if (columnList.Length < 3)
6571
{
6672
return resultList;
6773
}
@@ -79,11 +85,22 @@ private static List<WinGetPackage> CreatePackageListFromOutput(string[] output,
7985
WinGetPackage package = new WinGetPackage()
8086
{
8187
PackageName = output[i][columnList[0]..columnList[1]].Trim(),
82-
PackageId = output[i][columnList[1]..columnList[2]].Trim(),
83-
PackageVersion = output[i][columnList[2]..columnList[3]].Trim(),
84-
PackageAvailableVersion = output[i][columnList[2]..columnList[3]].Trim()
88+
PackageId = output[i][columnList[1]..columnList[2]].Trim()
8589
};
8690

91+
//Set version info depending on the column count.
92+
if (columnList.Length > 3)
93+
{
94+
package.PackageVersion = output[i][columnList[2]..columnList[3]].Trim();
95+
package.PackageAvailableVersion = output[i][columnList[2]..columnList[3]].Trim();
96+
}
97+
else
98+
{
99+
package.PackageVersion = output[i][columnList[2]..].Trim();
100+
package.PackageAvailableVersion = output[i][columnList[2]..].Trim();
101+
}
102+
103+
//Set information, depending on the action and the column count.
87104
if ((action == PackageAction.UpgradeList || action == PackageAction.InstalledList || action == PackageAction.Search) && columnList.Length >= 5)
88105
{
89106
string availableVersion = output[i][columnList[3]..columnList[4]].Trim();
@@ -98,6 +115,10 @@ private static List<WinGetPackage> CreatePackageListFromOutput(string[] output,
98115
{
99116
package.PackageSourceName = output[i][columnList[3]..].Trim();
100117
}
118+
else if (action == PackageAction.SearchBySource && !string.IsNullOrWhiteSpace(sourceName))
119+
{
120+
package.PackageSourceName = sourceName;
121+
}
101122

102123
resultList.Add(package);
103124
}

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

Lines changed: 51 additions & 2 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private void Run()
3030
Version winGetVersionObject = connector.WinGetVersionObject;
3131

3232
//---Tests-----------------------------------------------------------------------------
33-
List<WinGetPackage> test = connector.SearchPackage("visualstudio");
33+
List<WinGetPackage> test = connector.SearchPackage("git", "winget");
3434
Console.WriteLine(test[3].PackageName);
3535
Console.WriteLine(test[3].PackageId);
3636

0 commit comments

Comments
 (0)