Skip to content

Commit d48458a

Browse files
committed
Added normal pinning and unpinning for #18
1 parent 531150a commit d48458a

3 files changed

Lines changed: 388 additions & 43 deletions

File tree

src/WGet.NET/Components/WinGetPackageManager.cs

Lines changed: 223 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,6 +1622,7 @@ public async Task<bool> DownloadAsync(WinGetPackage package, DirectoryInfo direc
16221622
/// Adds a pinned package to winget.
16231623
/// </summary>
16241624
/// <param name="packageId">The id or name of the package to pin.</param>
1625+
/// <param name="blocking">Set to <see langword="true"/> if updating of pinned package should be fully blocked.</param>
16251626
/// <returns>
16261627
/// <see langword="true"/> if the pin was added successful or <see langword="false"/> if it failed.
16271628
/// </returns>
@@ -1635,7 +1636,7 @@ public async Task<bool> DownloadAsync(WinGetPackage package, DirectoryInfo direc
16351636
/// <exception cref="WGetNET.WinGetFeatureNotSupportedException">
16361637
/// This feature is not supported in the installed WinGet version.
16371638
/// </exception>
1638-
public bool PinAdd(string packageId)
1639+
public bool PinAdd(string packageId, bool blocking = false)
16391640
{
16401641
if (!WinGetVersionIsMatchOrAbove(1, 5))
16411642
{
@@ -1644,9 +1645,16 @@ public bool PinAdd(string packageId)
16441645

16451646
try
16461647
{
1648+
string cmd = string.Format(_pinAddCmd, packageId);
1649+
1650+
if (blocking)
1651+
{
1652+
cmd += " --blocking";
1653+
}
1654+
1655+
16471656
ProcessResult result =
1648-
_processManager.ExecuteWingetProcess(
1649-
string.Format(_pinAddCmd, packageId));
1657+
_processManager.ExecuteWingetProcess(cmd);
16501658

16511659
if (!result.Success)
16521660
{
@@ -1665,6 +1673,117 @@ public bool PinAdd(string packageId)
16651673
}
16661674
}
16671675

1676+
/// <summary>
1677+
/// Adds a pinned package to winget.
1678+
/// </summary>
1679+
/// <param name="package">The package to pin.</param>
1680+
/// <param name="blocking">Set to <see langword="true"/> if updating of pinned package should be fully blocked.</param>
1681+
/// <returns>
1682+
/// <see langword="true"/> if the pin was added successful or <see langword="false"/> if it failed.
1683+
/// </returns>
1684+
/// <exception cref="WGetNET.WinGetNotInstalledException">
1685+
/// WinGet is not installed or not found on the system.
1686+
/// </exception>
1687+
/// <exception cref="WGetNET.WinGetActionFailedException">
1688+
/// The current action failed for an unexpected reason.
1689+
/// Please see inner exception.
1690+
/// </exception>
1691+
/// <exception cref="WGetNET.WinGetFeatureNotSupportedException">
1692+
/// This feature is not supported in the installed WinGet version.
1693+
/// </exception>
1694+
public bool PinAdd(WinGetPackage package, bool blocking = false)
1695+
{
1696+
if (package.HasShortenedId)
1697+
{
1698+
return PinAdd(package.Name, blocking);
1699+
}
1700+
1701+
return PinAdd(package.Id, blocking);
1702+
}
1703+
1704+
/// <summary>
1705+
/// Asynchronously adds a pinned package to winget.
1706+
/// </summary>
1707+
/// <param name="packageId">The id or name of the package to pin.</param>
1708+
/// <param name="blocking">Set to <see langword="true"/> if updating of pinned package should be fully blocked.</param>
1709+
/// <returns>
1710+
/// <see langword="true"/> if the pin was added successful or <see langword="false"/> if it failed.
1711+
/// </returns>
1712+
/// <exception cref="WGetNET.WinGetNotInstalledException">
1713+
/// WinGet is not installed or not found on the system.
1714+
/// </exception>
1715+
/// <exception cref="WGetNET.WinGetActionFailedException">
1716+
/// The current action failed for an unexpected reason.
1717+
/// Please see inner exception.
1718+
/// </exception>
1719+
/// <exception cref="WGetNET.WinGetFeatureNotSupportedException">
1720+
/// This feature is not supported in the installed WinGet version.
1721+
/// </exception>
1722+
public async Task<bool> PinAddAsync(string packageId, bool blocking = false)
1723+
{
1724+
if (!WinGetVersionIsMatchOrAbove(1, 5))
1725+
{
1726+
throw new WinGetFeatureNotSupportedException("1.5");
1727+
}
1728+
1729+
try
1730+
{
1731+
string cmd = string.Format(_pinAddCmd, packageId);
1732+
1733+
if (blocking)
1734+
{
1735+
cmd += " --blocking";
1736+
}
1737+
1738+
1739+
ProcessResult result =
1740+
await _processManager.ExecuteWingetProcessAsync(cmd);
1741+
1742+
if (!result.Success)
1743+
{
1744+
return false;
1745+
}
1746+
1747+
return true;
1748+
}
1749+
catch (Win32Exception)
1750+
{
1751+
throw new WinGetNotInstalledException();
1752+
}
1753+
catch (Exception e)
1754+
{
1755+
throw new WinGetActionFailedException("Pinning the package failed.", e);
1756+
}
1757+
}
1758+
1759+
/// <summary>
1760+
/// Asynchronously adds a pinned package to winget.
1761+
/// </summary>
1762+
/// <param name="package">The package to pin.</param>
1763+
/// <param name="blocking">Set to <see langword="true"/> if updating of pinned package should be fully blocked.</param>
1764+
/// <returns>
1765+
/// <see langword="true"/> if the pin was added successful or <see langword="false"/> if it failed.
1766+
/// </returns>
1767+
/// <exception cref="WGetNET.WinGetNotInstalledException">
1768+
/// WinGet is not installed or not found on the system.
1769+
/// </exception>
1770+
/// <exception cref="WGetNET.WinGetActionFailedException">
1771+
/// The current action failed for an unexpected reason.
1772+
/// Please see inner exception.
1773+
/// </exception>
1774+
/// <exception cref="WGetNET.WinGetFeatureNotSupportedException">
1775+
/// This feature is not supported in the installed WinGet version.
1776+
/// </exception>
1777+
public async Task<bool> PinAddAsync(WinGetPackage package, bool blocking = false)
1778+
{
1779+
if (package.HasShortenedId)
1780+
{
1781+
return await PinAddAsync(package.Name, blocking);
1782+
}
1783+
1784+
return await PinAddAsync(package.Id, blocking);
1785+
}
1786+
16681787
/// <summary>
16691788
/// Removes a pinned package from winget.
16701789
/// </summary>
@@ -1711,6 +1830,107 @@ public bool PinRemove(string packageId)
17111830
throw new WinGetActionFailedException("Unpinning the package failed.", e);
17121831
}
17131832
}
1833+
1834+
/// <summary>
1835+
/// Removes a pinned package from winget.
1836+
/// </summary>
1837+
/// <param name="package">The package to unpin.</param>
1838+
/// <returns>
1839+
/// <see langword="true"/> if the removal of the pin was successful or <see langword="false"/> if it failed.
1840+
/// </returns>
1841+
/// <exception cref="WGetNET.WinGetNotInstalledException">
1842+
/// WinGet is not installed or not found on the system.
1843+
/// </exception>
1844+
/// <exception cref="WGetNET.WinGetActionFailedException">
1845+
/// The current action failed for an unexpected reason.
1846+
/// Please see inner exception.
1847+
/// </exception>
1848+
/// <exception cref="WGetNET.WinGetFeatureNotSupportedException">
1849+
/// This feature is not supported in the installed WinGet version.
1850+
/// </exception>
1851+
public bool PinRemove(WinGetPackage package)
1852+
{
1853+
if (package.HasShortenedId)
1854+
{
1855+
return PinRemove(package.Name);
1856+
}
1857+
1858+
return PinRemove(package.Id);
1859+
}
1860+
1861+
/// <summary>
1862+
/// Asynchronously removes a pinned package from winget.
1863+
/// </summary>
1864+
/// <param name="packageId">The id or name of the package to unpin.</param>
1865+
/// <returns>
1866+
/// <see langword="true"/> if the removal of the pin was successful or <see langword="false"/> if it failed.
1867+
/// </returns>
1868+
/// <exception cref="WGetNET.WinGetNotInstalledException">
1869+
/// WinGet is not installed or not found on the system.
1870+
/// </exception>
1871+
/// <exception cref="WGetNET.WinGetActionFailedException">
1872+
/// The current action failed for an unexpected reason.
1873+
/// Please see inner exception.
1874+
/// </exception>
1875+
/// <exception cref="WGetNET.WinGetFeatureNotSupportedException">
1876+
/// This feature is not supported in the installed WinGet version.
1877+
/// </exception>
1878+
public async Task<bool> PinRemoveAsync(string packageId)
1879+
{
1880+
if (!WinGetVersionIsMatchOrAbove(1, 5))
1881+
{
1882+
throw new WinGetFeatureNotSupportedException("1.5");
1883+
}
1884+
1885+
try
1886+
{
1887+
ProcessResult result =
1888+
await _processManager.ExecuteWingetProcessAsync(
1889+
string.Format(_pinRemoveCmd, packageId));
1890+
1891+
if (!result.Success)
1892+
{
1893+
return false;
1894+
}
1895+
1896+
return true;
1897+
}
1898+
catch (Win32Exception)
1899+
{
1900+
throw new WinGetNotInstalledException();
1901+
}
1902+
catch (Exception e)
1903+
{
1904+
throw new WinGetActionFailedException("Unpinning the package failed.", e);
1905+
}
1906+
}
1907+
1908+
/// <summary>
1909+
/// Asynchronously removes a pinned package from winget.
1910+
/// </summary>
1911+
/// <param name="package">The package to unpin.</param>
1912+
/// <returns>
1913+
/// <see langword="true"/> if the removal of the pin was successful or <see langword="false"/> if it failed.
1914+
/// </returns>
1915+
/// <exception cref="WGetNET.WinGetNotInstalledException">
1916+
/// WinGet is not installed or not found on the system.
1917+
/// </exception>
1918+
/// <exception cref="WGetNET.WinGetActionFailedException">
1919+
/// The current action failed for an unexpected reason.
1920+
/// Please see inner exception.
1921+
/// </exception>
1922+
/// <exception cref="WGetNET.WinGetFeatureNotSupportedException">
1923+
/// This feature is not supported in the installed WinGet version.
1924+
/// </exception>
1925+
public async Task<bool> PinRemoveAsync(WinGetPackage package)
1926+
{
1927+
if (package.HasShortenedId)
1928+
{
1929+
return await PinRemoveAsync(package.Name);
1930+
}
1931+
1932+
return await PinRemoveAsync(package.Id);
1933+
}
17141934
//---------------------------------------------------------------------------------------------
17151935
}
17161936
}

0 commit comments

Comments
 (0)