Skip to content

Commit 294e5c0

Browse files
committed
Added action to list installed packages by source
1 parent ff5b956 commit 294e5c0

4 files changed

Lines changed: 127 additions & 5 deletions

File tree

src/WGet.NET/Components/WinGetPackageManager.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class WinGetPackageManager : WinGetInfo
1818
{
1919
private const string _listCmd = "list";
2020
private const string _searchInstalledCmd = "list {0}";
21+
private const string _searchInstalledBySourceCmd = "list {0} --source {1}";
2122
private const string _searchCmd = "search {0} --accept-source-agreements";
2223
private const string _searchBySourceCmd = "search {0} --source {1} --accept-source-agreements";
2324
private const string _installCmd = "install {0}";
@@ -260,6 +261,44 @@ public List<WinGetPackage> GetInstalledPackages(string packageName)
260261
}
261262
}
262263

264+
/// <summary>
265+
/// Gets a list of all installed packages. That match the provided name.
266+
/// </summary>
267+
/// <param name="packageName">
268+
/// The name of the package for the search.
269+
/// </param>
270+
/// <param name="sourceName">
271+
/// The name of the source for the search.
272+
/// </param>
273+
/// <returns>
274+
/// A <see cref="System.Collections.Generic.List{T}"/> of <see cref="WGetNET.WinGetPackage"/> instances.
275+
/// </returns>
276+
/// <exception cref="WGetNET.WinGetNotInstalledException">
277+
/// WinGet is not installed or not found on the system.
278+
/// </exception>
279+
/// <exception cref="WGetNET.WinGetActionFailedException">
280+
/// The current action failed for an unexpected reason.
281+
/// Please see inner exception.
282+
/// </exception>
283+
public List<WinGetPackage> GetInstalledPackages(string packageName, string sourceName)
284+
{
285+
try
286+
{
287+
ProcessResult result =
288+
_processManager.ExecuteWingetProcess(string.Format(_searchInstalledBySourceCmd, packageName, sourceName));
289+
290+
return ProcessOutputReader.ToPackageList(result.Output, PackageAction.InstalledListBySource, sourceName);
291+
}
292+
catch (Win32Exception)
293+
{
294+
throw new WinGetNotInstalledException();
295+
}
296+
catch (Exception e)
297+
{
298+
throw new WinGetActionFailedException("The search of installed packages failed.", e);
299+
}
300+
}
301+
263302
/// <summary>
264303
/// Asynchronously gets a list of all installed packages.
265304
/// </summary>
@@ -329,6 +368,45 @@ public async Task<List<WinGetPackage>> GetInstalledPackagesAsync(string packageN
329368
}
330369
}
331370

371+
/// <summary>
372+
/// Asynchronously gets a list of all installed packages. That match the provided name.
373+
/// </summary>
374+
/// <param name="packageName">
375+
/// The name of the package for the search.
376+
/// </param>
377+
/// <param name="sourceName">
378+
/// The name of the source for the search.
379+
/// </param>
380+
/// <returns>
381+
/// A <see cref="System.Threading.Tasks.Task"/>, containing the result.
382+
/// The result is a <see cref="System.Collections.Generic.List{T}"/> of <see cref="WGetNET.WinGetPackage"/> instances.
383+
/// </returns>
384+
/// <exception cref="WGetNET.WinGetNotInstalledException">
385+
/// WinGet is not installed or not found on the system.
386+
/// </exception>
387+
/// <exception cref="WGetNET.WinGetActionFailedException">
388+
/// The current action failed for an unexpected reason.
389+
/// Please see inner exception.
390+
/// </exception>
391+
public async Task<List<WinGetPackage>> GetInstalledPackagesAsync(string packageName, string sourceName)
392+
{
393+
try
394+
{
395+
ProcessResult result =
396+
await _processManager.ExecuteWingetProcessAsync(string.Format(_searchInstalledBySourceCmd, packageName, sourceName));
397+
398+
return ProcessOutputReader.ToPackageList(result.Output, PackageAction.InstalledListBySource, sourceName);
399+
}
400+
catch (Win32Exception)
401+
{
402+
throw new WinGetNotInstalledException();
403+
}
404+
catch (Exception e)
405+
{
406+
throw new WinGetActionFailedException("The search of installed packages failed.", e);
407+
}
408+
}
409+
332410
/// <summary>
333411
/// Install a package using winget.
334412
/// </summary>

src/WGet.NET/Enums/PackageAction.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ internal enum PackageAction
55
Default,
66
UpgradeList,
77
InstalledList,
8+
InstalledListBySource,
89
Search,
910
SearchBySource
1011
}

src/WGet.NET/HelperClasses/ProcessOutputReader.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal static class ProcessOutputReader
2424
/// Sets info about the action that is executet.
2525
/// </param>
2626
/// <param name="sourceName">
27-
/// Name of the source used in the search by source action.
27+
/// Name of the source used in the search or list by source action.
2828
/// </param>
2929
/// <returns>
3030
/// A <see cref="System.Collections.Generic.List{T}"/> of <see cref="WGetNET.WinGetPackage"/>'s.
@@ -58,7 +58,7 @@ public static List<WinGetPackage> ToPackageList(string[] output, PackageAction a
5858
/// Sets info about the action that is executet.
5959
/// </param>
6060
/// <param name="sourceName">
61-
/// Name of the source used in the search by source action.
61+
/// Name of the source used in the search or list by source action.
6262
/// </param>
6363
/// <returns>
6464
/// A <see cref="System.Collections.Generic.List{T}"/> of <see cref="WGetNET.WinGetPackage"/>'s.
@@ -115,7 +115,7 @@ private static List<WinGetPackage> CreatePackageListFromOutput(string[] output,
115115
{
116116
package.PackageSourceName = output[i][columnList[3]..].Trim();
117117
}
118-
else if (action == PackageAction.SearchBySource && !string.IsNullOrWhiteSpace(sourceName))
118+
else if ((action == PackageAction.SearchBySource || action == PackageAction.InstalledListBySource) && !string.IsNullOrWhiteSpace(sourceName))
119119
{
120120
package.PackageSourceName = sourceName;
121121
}

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

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

0 commit comments

Comments
 (0)