Skip to content

Commit 3e8952f

Browse files
committed
Added functions to get a installed package with an exact match, without using the exact functionality of winget (Workaround for #36)
1 parent 96671c0 commit 3e8952f

3 files changed

Lines changed: 309 additions & 26 deletions

File tree

src/WGet.NET/Components/WinGetPackageManager.cs

Lines changed: 173 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public async Task<List<WinGetPackage>> SearchPackageAsync(string packageId, stri
229229
}
230230
//---------------------------------------------------------------------------------------------
231231

232-
//---Install-----------------------------------------------------------------------------------
232+
//---List--------------------------------------------------------------------------------------
233233
/// <summary>
234234
/// Gets a list of all installed packages.
235235
/// </summary>
@@ -440,6 +440,148 @@ public async Task<List<WinGetPackage>> GetInstalledPackagesAsync(string packageI
440440
return ProcessOutputReader.ToPackageList(result.Output, PackageAction.InstalledListBySource, sourceName);
441441
}
442442

443+
/// <summary>
444+
/// Gets a installed package, that matchs the provided id/name. If there are multiple matches, the first match will be returned.
445+
/// </summary>
446+
/// <remarks>
447+
/// This method does an internal match and does not use the winget "exact" functionality.
448+
/// </remarks>
449+
/// <param name="packageId">
450+
/// The id or name of the package for the search.
451+
/// </param>
452+
/// <returns>
453+
/// A <see cref="WGetNET.WinGetPackage"/> instances or <see langword="null"/> if no match was found.
454+
/// </returns>
455+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
456+
/// WinGet is not installed or not found on the system.
457+
/// </exception>
458+
/// <exception cref="System.ArgumentException">
459+
/// A provided argument is empty.
460+
/// </exception>
461+
/// <exception cref="System.ArgumentNullException">
462+
/// A provided argument is null.
463+
/// </exception>
464+
public WinGetPackage? GetExactInstalledPackage(string packageId)
465+
{
466+
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(packageId, "packageId");
467+
468+
ProcessResult result = Execute(string.Format(AcceptSourceAgreements(_searchInstalledCmd), packageId));
469+
470+
return MatchExact(
471+
ProcessOutputReader.ToPackageList(result.Output, PackageAction.InstalledList),
472+
packageId.Trim()
473+
);
474+
}
475+
476+
/// <summary>
477+
/// Gets a installed package, that matchs the provided id/name. If there are multiple matches, the first match will be returned.
478+
/// </summary>
479+
/// <remarks>
480+
/// This method does an internal match and does not use the winget "exact" functionality.
481+
/// </remarks>
482+
/// <param name="packageId">
483+
/// The id or name of the package for the search.
484+
/// </param>
485+
/// <param name="sourceName">
486+
/// The name of the source for the search.
487+
/// </param>
488+
/// <returns>
489+
/// A <see cref="WGetNET.WinGetPackage"/> instances or <see langword="null"/> if no match was found.
490+
/// </returns>
491+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
492+
/// WinGet is not installed or not found on the system.
493+
/// </exception>
494+
/// <exception cref="System.ArgumentException">
495+
/// A provided argument is empty.
496+
/// </exception>
497+
/// <exception cref="System.ArgumentNullException">
498+
/// A provided argument is null.
499+
/// </exception>
500+
public WinGetPackage? GetExactInstalledPackage(string packageId, string sourceName)
501+
{
502+
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(packageId, "packageId");
503+
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(sourceName, "sourceName");
504+
505+
ProcessResult result = Execute(string.Format(AcceptSourceAgreements(_searchInstalledBySourceCmd), packageId, sourceName));
506+
507+
return MatchExact(
508+
ProcessOutputReader.ToPackageList(result.Output, PackageAction.InstalledList),
509+
packageId.Trim()
510+
);
511+
}
512+
513+
/// <summary>
514+
/// Asynchronously gets a installed package, that matchs the provided id/name. If there are multiple matches, the first match will be returned.
515+
/// </summary>
516+
/// <remarks>
517+
/// This method does an internal match and does not use the winget "exact" functionality.
518+
/// </remarks>
519+
/// <param name="packageId">
520+
/// The id or name of the package for the search.
521+
/// </param>
522+
/// <returns>
523+
/// A <see cref="WGetNET.WinGetPackage"/> instances or <see langword="null"/> if no match was found.
524+
/// </returns>
525+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
526+
/// WinGet is not installed or not found on the system.
527+
/// </exception>
528+
/// <exception cref="System.ArgumentException">
529+
/// A provided argument is empty.
530+
/// </exception>
531+
/// <exception cref="System.ArgumentNullException">
532+
/// A provided argument is null.
533+
/// </exception>
534+
public async Task<WinGetPackage?> GetExactInstalledPackageAsync(string packageId)
535+
{
536+
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(packageId, "packageId");
537+
538+
ProcessResult result = await ExecuteAsync(string.Format(AcceptSourceAgreements(_searchInstalledCmd), packageId));
539+
540+
return MatchExact(
541+
ProcessOutputReader.ToPackageList(result.Output, PackageAction.InstalledList),
542+
packageId.Trim()
543+
);
544+
}
545+
546+
/// <summary>
547+
/// Asynchronously gets a installed package, that matchs the provided id/name. If there are multiple matches, the first match will be returned.
548+
/// </summary>
549+
/// <remarks>
550+
/// This method does an internal match and does not use the winget "exact" functionality.
551+
/// </remarks>
552+
/// <param name="packageId">
553+
/// The id or name of the package for the search.
554+
/// </param>
555+
/// <param name="sourceName">
556+
/// The name of the source for the search.
557+
/// </param>
558+
/// <returns>
559+
/// A <see cref="WGetNET.WinGetPackage"/> instances or <see langword="null"/> if no match was found.
560+
/// </returns>
561+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
562+
/// WinGet is not installed or not found on the system.
563+
/// </exception>
564+
/// <exception cref="System.ArgumentException">
565+
/// A provided argument is empty.
566+
/// </exception>
567+
/// <exception cref="System.ArgumentNullException">
568+
/// A provided argument is null.
569+
/// </exception>
570+
public async Task<WinGetPackage?> GetExactInstalledPackageAsync(string packageId, string sourceName)
571+
{
572+
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(packageId, "packageId");
573+
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(sourceName, "sourceName");
574+
575+
ProcessResult result = await ExecuteAsync(string.Format(AcceptSourceAgreements(_searchInstalledBySourceCmd), packageId, sourceName));
576+
577+
return MatchExact(
578+
ProcessOutputReader.ToPackageList(result.Output, PackageAction.InstalledList),
579+
packageId.Trim()
580+
);
581+
}
582+
//---------------------------------------------------------------------------------------------
583+
584+
//---Install-----------------------------------------------------------------------------------
443585
/// <summary>
444586
/// Install a package using winget.
445587
/// </summary>
@@ -2685,6 +2827,36 @@ private string AcceptSourceAgreements(string argument)
26852827

26862828
return argument;
26872829
}
2830+
2831+
/// <summary>
2832+
/// Tries to match a <see cref="WGetNET.WinGetPackage"/> to the provided search criteria.
2833+
/// </summary>
2834+
/// <param name="matchList">
2835+
/// The list to try and finde match in.
2836+
/// </param>
2837+
/// <param name="matchString">
2838+
/// The search criteria, that will be tried to be matched againts the package id and/or name.
2839+
/// </param>
2840+
/// <returns>
2841+
/// The <see cref="WGetNET.WinGetPackage"/> that matches the search criteria, or <see langword="null"/> if no package matches.
2842+
/// </returns>
2843+
private WinGetPackage? MatchExact(List<WinGetPackage> matchList, string matchString)
2844+
{
2845+
if (matchList == null || matchList.Count <= 0 || string.IsNullOrWhiteSpace(matchString))
2846+
{
2847+
return null;
2848+
}
2849+
2850+
for (int i = 0; i < matchList.Count; i++)
2851+
{
2852+
if (string.Equals(matchList[i].Id, matchString) || string.Equals(matchList[i].Name, matchString))
2853+
{
2854+
return matchList[i];
2855+
}
2856+
}
2857+
2858+
return null;
2859+
}
26882860
//---------------------------------------------------------------------------------------------
26892861
}
26902862
}

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

Lines changed: 133 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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.Threading.Tasks;
4-
using System.Collections.Generic;
55
using WGetNET;
66

77
namespace WGetTest
@@ -53,6 +53,8 @@ private void Run()
5353
Console.WriteLine(test3[0].Name);
5454
Console.WriteLine(test3[0].Id);
5555

56+
WinGetPackage? test4 = connector.GetExactInstalledPackage("Microsoft Edge");
57+
5658
/*bool repairResult = connector.RepairPackage("7zip.7zip");
5759
Console.WriteLine("Repair Test result: ", repairResult);*/
5860

0 commit comments

Comments
 (0)