Skip to content

Commit 531150a

Browse files
committed
Added basic pinning and unpinning functions
1 parent 80d7a4f commit 531150a

3 files changed

Lines changed: 180 additions & 29 deletions

File tree

src/WGet.NET/Components/WinGetPackageManager.cs

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class WinGetPackageManager : WinGetInfo
3131
private const string _importCmd = "import -i {0} --ignore-unavailable";
3232
private const string _hashCmd = "hash {0}";
3333
private const string _downloadCmd = "download {0} --download-directory {1}";
34+
private const string _pinAddCmd = "pin add \"{0}\"";
35+
private const string _pinRemoveCmd = "pin remove \"{0}\"";
3436

3537
/// <summary>
3638
/// Initializes a new instance of the <see cref="WGetNET.WinGetPackageManager"/> class.
@@ -1359,7 +1361,7 @@ private string HashResultToHash(ProcessResult result)
13591361
}
13601362
//---------------------------------------------------------------------------------------------
13611363

1362-
//---Download--------------------------------------------------------------------------------------
1364+
//---Download----------------------------------------------------------------------------------
13631365
/// <summary>
13641366
/// Downloads the installer of a package using winget.
13651367
/// </summary>
@@ -1614,5 +1616,101 @@ public async Task<bool> DownloadAsync(WinGetPackage package, DirectoryInfo direc
16141616
return await DownloadAsync(package.Id, directory.FullName);
16151617
}
16161618
//---------------------------------------------------------------------------------------------
1619+
1620+
//---Pin---------------------------------------------------------------------------------------
1621+
/// <summary>
1622+
/// Adds a pinned package to winget.
1623+
/// </summary>
1624+
/// <param name="packageId">The id or name of the package to pin.</param>
1625+
/// <returns>
1626+
/// <see langword="true"/> if the pin was added successful or <see langword="false"/> if it failed.
1627+
/// </returns>
1628+
/// <exception cref="WGetNET.WinGetNotInstalledException">
1629+
/// WinGet is not installed or not found on the system.
1630+
/// </exception>
1631+
/// <exception cref="WGetNET.WinGetActionFailedException">
1632+
/// The current action failed for an unexpected reason.
1633+
/// Please see inner exception.
1634+
/// </exception>
1635+
/// <exception cref="WGetNET.WinGetFeatureNotSupportedException">
1636+
/// This feature is not supported in the installed WinGet version.
1637+
/// </exception>
1638+
public bool PinAdd(string packageId)
1639+
{
1640+
if (!WinGetVersionIsMatchOrAbove(1, 5))
1641+
{
1642+
throw new WinGetFeatureNotSupportedException("1.5");
1643+
}
1644+
1645+
try
1646+
{
1647+
ProcessResult result =
1648+
_processManager.ExecuteWingetProcess(
1649+
string.Format(_pinAddCmd, packageId));
1650+
1651+
if (!result.Success)
1652+
{
1653+
return false;
1654+
}
1655+
1656+
return true;
1657+
}
1658+
catch (Win32Exception)
1659+
{
1660+
throw new WinGetNotInstalledException();
1661+
}
1662+
catch (Exception e)
1663+
{
1664+
throw new WinGetActionFailedException("Pinning the package failed.", e);
1665+
}
1666+
}
1667+
1668+
/// <summary>
1669+
/// Removes a pinned package from winget.
1670+
/// </summary>
1671+
/// <param name="packageId">The id or name of the package to unpin.</param>
1672+
/// <returns>
1673+
/// <see langword="true"/> if the removal of the pin was successful or <see langword="false"/> if it failed.
1674+
/// </returns>
1675+
/// <exception cref="WGetNET.WinGetNotInstalledException">
1676+
/// WinGet is not installed or not found on the system.
1677+
/// </exception>
1678+
/// <exception cref="WGetNET.WinGetActionFailedException">
1679+
/// The current action failed for an unexpected reason.
1680+
/// Please see inner exception.
1681+
/// </exception>
1682+
/// <exception cref="WGetNET.WinGetFeatureNotSupportedException">
1683+
/// This feature is not supported in the installed WinGet version.
1684+
/// </exception>
1685+
public bool PinRemove(string packageId)
1686+
{
1687+
if (!WinGetVersionIsMatchOrAbove(1, 5))
1688+
{
1689+
throw new WinGetFeatureNotSupportedException("1.5");
1690+
}
1691+
1692+
try
1693+
{
1694+
ProcessResult result =
1695+
_processManager.ExecuteWingetProcess(
1696+
string.Format(_pinRemoveCmd, packageId));
1697+
1698+
if (!result.Success)
1699+
{
1700+
return false;
1701+
}
1702+
1703+
return true;
1704+
}
1705+
catch (Win32Exception)
1706+
{
1707+
throw new WinGetNotInstalledException();
1708+
}
1709+
catch (Exception e)
1710+
{
1711+
throw new WinGetActionFailedException("Unpinning the package failed.", e);
1712+
}
1713+
}
1714+
//---------------------------------------------------------------------------------------------
16171715
}
16181716
}

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

Lines changed: 78 additions & 28 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ private void Run()
6565
//bool upAllresult = connector.UpgradeAllPackages();
6666

6767
bool downloadResult = connector.Download("7zip.7zip", "C:\\Test");
68+
69+
connector.PinAdd("7zip.7zip");
70+
connector.PinRemove("7zip.7zip");
6871
}
6972
catch (Exception e)
7073
{

0 commit comments

Comments
 (0)