Skip to content

Commit 63f89d9

Browse files
committed
Added the function to pin and unpin packages with the "--installed" flag for #18
1 parent d48458a commit 63f89d9

3 files changed

Lines changed: 517 additions & 39 deletions

File tree

src/WGet.NET/Components/WinGetPackageManager.cs

Lines changed: 317 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public class WinGetPackageManager : WinGetInfo
3333
private const string _downloadCmd = "download {0} --download-directory {1}";
3434
private const string _pinAddCmd = "pin add \"{0}\"";
3535
private const string _pinRemoveCmd = "pin remove \"{0}\"";
36+
private const string _pinAddInstalledCmd = "pin add \"{0}\" --installed";
37+
private const string _pinRemoveInstalledCmd = "pin remove \"{0}\" --installed";
3638

3739
/// <summary>
3840
/// Initializes a new instance of the <see cref="WGetNET.WinGetPackageManager"/> class.
@@ -1931,6 +1933,321 @@ public async Task<bool> PinRemoveAsync(WinGetPackage package)
19311933

19321934
return await PinRemoveAsync(package.Id);
19331935
}
1936+
1937+
1938+
/// <summary>
1939+
/// Adds a pinned installed package to winget.
1940+
/// </summary>
1941+
/// <param name="packageId">The id or name of the package to pin.</param>
1942+
/// <param name="blocking">Set to <see langword="true"/> if updating of pinned package should be fully blocked.</param>
1943+
/// <returns>
1944+
/// <see langword="true"/> if the pin was added successful or <see langword="false"/> if it failed.
1945+
/// </returns>
1946+
/// <exception cref="WGetNET.WinGetNotInstalledException">
1947+
/// WinGet is not installed or not found on the system.
1948+
/// </exception>
1949+
/// <exception cref="WGetNET.WinGetActionFailedException">
1950+
/// The current action failed for an unexpected reason.
1951+
/// Please see inner exception.
1952+
/// </exception>
1953+
/// <exception cref="WGetNET.WinGetFeatureNotSupportedException">
1954+
/// This feature is not supported in the installed WinGet version.
1955+
/// </exception>
1956+
public bool PinAddInstalled(string packageId, bool blocking = false)
1957+
{
1958+
if (!WinGetVersionIsMatchOrAbove(1, 5))
1959+
{
1960+
throw new WinGetFeatureNotSupportedException("1.5");
1961+
}
1962+
1963+
try
1964+
{
1965+
string cmd = string.Format(_pinAddInstalledCmd, packageId);
1966+
1967+
if (blocking)
1968+
{
1969+
cmd += " --blocking";
1970+
}
1971+
1972+
1973+
ProcessResult result =
1974+
_processManager.ExecuteWingetProcess(cmd);
1975+
1976+
if (!result.Success)
1977+
{
1978+
return false;
1979+
}
1980+
1981+
return true;
1982+
}
1983+
catch (Win32Exception)
1984+
{
1985+
throw new WinGetNotInstalledException();
1986+
}
1987+
catch (Exception e)
1988+
{
1989+
throw new WinGetActionFailedException("Pinning the package failed.", e);
1990+
}
1991+
}
1992+
1993+
/// <summary>
1994+
/// Adds a pinned installed package to winget.
1995+
/// </summary>
1996+
/// <param name="package">The package to pin.</param>
1997+
/// <param name="blocking">Set to <see langword="true"/> if updating of pinned package should be fully blocked.</param>
1998+
/// <returns>
1999+
/// <see langword="true"/> if the pin was added successful or <see langword="false"/> if it failed.
2000+
/// </returns>
2001+
/// <exception cref="WGetNET.WinGetNotInstalledException">
2002+
/// WinGet is not installed or not found on the system.
2003+
/// </exception>
2004+
/// <exception cref="WGetNET.WinGetActionFailedException">
2005+
/// The current action failed for an unexpected reason.
2006+
/// Please see inner exception.
2007+
/// </exception>
2008+
/// <exception cref="WGetNET.WinGetFeatureNotSupportedException">
2009+
/// This feature is not supported in the installed WinGet version.
2010+
/// </exception>
2011+
public bool PinAddInstalled(WinGetPackage package, bool blocking = false)
2012+
{
2013+
if (package.HasShortenedId)
2014+
{
2015+
return PinAddInstalled(package.Name, blocking);
2016+
}
2017+
2018+
return PinAddInstalled(package.Id, blocking);
2019+
}
2020+
2021+
/// <summary>
2022+
/// Asynchronously adds a pinned installed package to winget.
2023+
/// </summary>
2024+
/// <param name="packageId">The id or name of the package to pin.</param>
2025+
/// <param name="blocking">Set to <see langword="true"/> if updating of pinned package should be fully blocked.</param>
2026+
/// <returns>
2027+
/// <see langword="true"/> if the pin was added successful or <see langword="false"/> if it failed.
2028+
/// </returns>
2029+
/// <exception cref="WGetNET.WinGetNotInstalledException">
2030+
/// WinGet is not installed or not found on the system.
2031+
/// </exception>
2032+
/// <exception cref="WGetNET.WinGetActionFailedException">
2033+
/// The current action failed for an unexpected reason.
2034+
/// Please see inner exception.
2035+
/// </exception>
2036+
/// <exception cref="WGetNET.WinGetFeatureNotSupportedException">
2037+
/// This feature is not supported in the installed WinGet version.
2038+
/// </exception>
2039+
public async Task<bool> PinAddInstalledAsync(string packageId, bool blocking = false)
2040+
{
2041+
if (!WinGetVersionIsMatchOrAbove(1, 5))
2042+
{
2043+
throw new WinGetFeatureNotSupportedException("1.5");
2044+
}
2045+
2046+
try
2047+
{
2048+
string cmd = string.Format(_pinAddInstalledCmd, packageId);
2049+
2050+
if (blocking)
2051+
{
2052+
cmd += " --blocking";
2053+
}
2054+
2055+
2056+
ProcessResult result =
2057+
await _processManager.ExecuteWingetProcessAsync(cmd);
2058+
2059+
if (!result.Success)
2060+
{
2061+
return false;
2062+
}
2063+
2064+
return true;
2065+
}
2066+
catch (Win32Exception)
2067+
{
2068+
throw new WinGetNotInstalledException();
2069+
}
2070+
catch (Exception e)
2071+
{
2072+
throw new WinGetActionFailedException("Pinning the package failed.", e);
2073+
}
2074+
}
2075+
2076+
/// <summary>
2077+
/// Asynchronously adds a pinned installed package to winget.
2078+
/// </summary>
2079+
/// <param name="package">The package to pin.</param>
2080+
/// <param name="blocking">Set to <see langword="true"/> if updating of pinned package should be fully blocked.</param>
2081+
/// <returns>
2082+
/// <see langword="true"/> if the pin was added successful or <see langword="false"/> if it failed.
2083+
/// </returns>
2084+
/// <exception cref="WGetNET.WinGetNotInstalledException">
2085+
/// WinGet is not installed or not found on the system.
2086+
/// </exception>
2087+
/// <exception cref="WGetNET.WinGetActionFailedException">
2088+
/// The current action failed for an unexpected reason.
2089+
/// Please see inner exception.
2090+
/// </exception>
2091+
/// <exception cref="WGetNET.WinGetFeatureNotSupportedException">
2092+
/// This feature is not supported in the installed WinGet version.
2093+
/// </exception>
2094+
public async Task<bool> PinAddInstalledAsync(WinGetPackage package, bool blocking = false)
2095+
{
2096+
if (package.HasShortenedId)
2097+
{
2098+
return await PinAddInstalledAsync(package.Name, blocking);
2099+
}
2100+
2101+
return await PinAddInstalledAsync(package.Id, blocking);
2102+
}
2103+
2104+
/// <summary>
2105+
/// Removes a pinned package from winget.
2106+
/// </summary>
2107+
/// <param name="packageId">The id or name of the package to unpin.</param>
2108+
/// <returns>
2109+
/// <see langword="true"/> if the removal of the pin was successful or <see langword="false"/> if it failed.
2110+
/// </returns>
2111+
/// <exception cref="WGetNET.WinGetNotInstalledException">
2112+
/// WinGet is not installed or not found on the system.
2113+
/// </exception>
2114+
/// <exception cref="WGetNET.WinGetActionFailedException">
2115+
/// The current action failed for an unexpected reason.
2116+
/// Please see inner exception.
2117+
/// </exception>
2118+
/// <exception cref="WGetNET.WinGetFeatureNotSupportedException">
2119+
/// This feature is not supported in the installed WinGet version.
2120+
/// </exception>
2121+
public bool PinRemoveInstalled(string packageId)
2122+
{
2123+
if (!WinGetVersionIsMatchOrAbove(1, 5))
2124+
{
2125+
throw new WinGetFeatureNotSupportedException("1.5");
2126+
}
2127+
2128+
try
2129+
{
2130+
ProcessResult result =
2131+
_processManager.ExecuteWingetProcess(
2132+
string.Format(_pinRemoveInstalledCmd, packageId));
2133+
2134+
if (!result.Success)
2135+
{
2136+
return false;
2137+
}
2138+
2139+
return true;
2140+
}
2141+
catch (Win32Exception)
2142+
{
2143+
throw new WinGetNotInstalledException();
2144+
}
2145+
catch (Exception e)
2146+
{
2147+
throw new WinGetActionFailedException("Unpinning the package failed.", e);
2148+
}
2149+
}
2150+
2151+
/// <summary>
2152+
/// Removes a pinned package from winget.
2153+
/// </summary>
2154+
/// <param name="package">The package to unpin.</param>
2155+
/// <returns>
2156+
/// <see langword="true"/> if the removal of the pin was successful or <see langword="false"/> if it failed.
2157+
/// </returns>
2158+
/// <exception cref="WGetNET.WinGetNotInstalledException">
2159+
/// WinGet is not installed or not found on the system.
2160+
/// </exception>
2161+
/// <exception cref="WGetNET.WinGetActionFailedException">
2162+
/// The current action failed for an unexpected reason.
2163+
/// Please see inner exception.
2164+
/// </exception>
2165+
/// <exception cref="WGetNET.WinGetFeatureNotSupportedException">
2166+
/// This feature is not supported in the installed WinGet version.
2167+
/// </exception>
2168+
public bool PinRemoveInstalled(WinGetPackage package)
2169+
{
2170+
if (package.HasShortenedId)
2171+
{
2172+
return PinRemoveInstalled(package.Name);
2173+
}
2174+
2175+
return PinRemoveInstalled(package.Id);
2176+
}
2177+
2178+
/// <summary>
2179+
/// Asynchronously removes a pinned package from winget.
2180+
/// </summary>
2181+
/// <param name="packageId">The id or name of the package to unpin.</param>
2182+
/// <returns>
2183+
/// <see langword="true"/> if the removal of the pin was successful or <see langword="false"/> if it failed.
2184+
/// </returns>
2185+
/// <exception cref="WGetNET.WinGetNotInstalledException">
2186+
/// WinGet is not installed or not found on the system.
2187+
/// </exception>
2188+
/// <exception cref="WGetNET.WinGetActionFailedException">
2189+
/// The current action failed for an unexpected reason.
2190+
/// Please see inner exception.
2191+
/// </exception>
2192+
/// <exception cref="WGetNET.WinGetFeatureNotSupportedException">
2193+
/// This feature is not supported in the installed WinGet version.
2194+
/// </exception>
2195+
public async Task<bool> PinRemoveInstalledAsync(string packageId)
2196+
{
2197+
if (!WinGetVersionIsMatchOrAbove(1, 5))
2198+
{
2199+
throw new WinGetFeatureNotSupportedException("1.5");
2200+
}
2201+
2202+
try
2203+
{
2204+
ProcessResult result =
2205+
await _processManager.ExecuteWingetProcessAsync(
2206+
string.Format(_pinRemoveInstalledCmd, packageId));
2207+
2208+
if (!result.Success)
2209+
{
2210+
return false;
2211+
}
2212+
2213+
return true;
2214+
}
2215+
catch (Win32Exception)
2216+
{
2217+
throw new WinGetNotInstalledException();
2218+
}
2219+
catch (Exception e)
2220+
{
2221+
throw new WinGetActionFailedException("Unpinning the package failed.", e);
2222+
}
2223+
}
2224+
2225+
/// <summary>
2226+
/// Asynchronously removes a pinned package from winget.
2227+
/// </summary>
2228+
/// <param name="package">The package to unpin.</param>
2229+
/// <returns>
2230+
/// <see langword="true"/> if the removal of the pin was successful or <see langword="false"/> if it failed.
2231+
/// </returns>
2232+
/// <exception cref="WGetNET.WinGetNotInstalledException">
2233+
/// WinGet is not installed or not found on the system.
2234+
/// </exception>
2235+
/// <exception cref="WGetNET.WinGetActionFailedException">
2236+
/// The current action failed for an unexpected reason.
2237+
/// Please see inner exception.
2238+
/// </exception>
2239+
/// <exception cref="WGetNET.WinGetFeatureNotSupportedException">
2240+
/// This feature is not supported in the installed WinGet version.
2241+
/// </exception>
2242+
public async Task<bool> PinRemoveInstalledAsync(WinGetPackage package)
2243+
{
2244+
if (package.HasShortenedId)
2245+
{
2246+
return await PinRemoveInstalledAsync(package.Name);
2247+
}
2248+
2249+
return await PinRemoveInstalledAsync(package.Id);
2250+
}
19342251
//---------------------------------------------------------------------------------------------
19352252
}
19362253
}

0 commit comments

Comments
 (0)