Skip to content

Commit 1fcc24c

Browse files
committed
Added the function to run an upgrade on all packages
1 parent 6ed4c47 commit 1fcc24c

3 files changed

Lines changed: 109 additions & 0 deletions

File tree

src/WGet.NET/Components/WinGetPackageManager.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class WinGetPackageManager : WinGetInfo
2121
private const string _searchCmd = "search {0} --accept-source-agreements";
2222
private const string _installCmd = "install {0}";
2323
private const string _upgradeCmd = "upgrade {0}";
24+
private const string _upgradeAllCmd = "upgrade --all";
2425
private const string _getUpgradeableCmd = "upgrade";
2526
private const string _includeUnknown = "--include-unknown";
2627
private const string _uninstallCmd = "uninstall {0}";
@@ -735,6 +736,76 @@ public async Task<bool> UpgradePackageAsync(WinGetPackage package)
735736
return await UpgradePackageAsync(package.PackageId);
736737
}
737738

739+
/// <summary>
740+
/// Tries to upgrade all packages using winget.
741+
/// </summary>
742+
/// <remarks>
743+
/// The action might run succesfully without upgrading every or even any package.
744+
/// </remarks>
745+
/// <returns>
746+
/// <see langword="true"/> if the action run successfully or <see langword="false"/> if it failed.
747+
/// </returns>
748+
/// <exception cref="WGetNET.WinGetNotInstalledException">
749+
/// WinGet is not installed or not found on the system.
750+
/// </exception>
751+
/// <exception cref="WGetNET.WinGetActionFailedException">
752+
/// The current action failed for an unexpected reason.
753+
/// Please see inner exception.
754+
/// </exception>
755+
public bool UpgradeAllPackages()
756+
{
757+
try
758+
{
759+
ProcessResult result =
760+
_processManager.ExecuteWingetProcess(_upgradeAllCmd);
761+
762+
return result.Success;
763+
}
764+
catch (Win32Exception)
765+
{
766+
throw new WinGetNotInstalledException();
767+
}
768+
catch (Exception e)
769+
{
770+
throw new WinGetActionFailedException("Upgrading all packages failed.", e);
771+
}
772+
}
773+
774+
/// <summary>
775+
/// Asynchronously tries to upgrade all packages using winget.
776+
/// </summary>
777+
/// <remarks>
778+
/// The action might run succesfully without upgrading every or even any package.
779+
/// </remarks>
780+
/// <returns>
781+
/// <see langword="true"/> if the action run successfully or <see langword="false"/> if it failed.
782+
/// </returns>
783+
/// <exception cref="WGetNET.WinGetNotInstalledException">
784+
/// WinGet is not installed or not found on the system.
785+
/// </exception>
786+
/// <exception cref="WGetNET.WinGetActionFailedException">
787+
/// The current action failed for an unexpected reason.
788+
/// Please see inner exception.
789+
/// </exception>
790+
public async Task<bool> UpgradeAllPackagesAsync()
791+
{
792+
try
793+
{
794+
ProcessResult result =
795+
await _processManager.ExecuteWingetProcessAsync(_upgradeAllCmd);
796+
797+
return result.Success;
798+
}
799+
catch (Win32Exception)
800+
{
801+
throw new WinGetNotInstalledException();
802+
}
803+
catch (Exception e)
804+
{
805+
throw new WinGetActionFailedException("Upgrading all packages failed.", e);
806+
}
807+
}
808+
738809
private string AddArgumentByVersion(string argument)
739810
{
740811
// Checking version to determine if "--include-unknown" is necessary

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

Lines changed: 36 additions & 0 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ private void Run()
5959
settingsTask.Wait();
6060
string settings = settingsTask.Result;
6161
bool settingExportStatus = connector.ExportSettingsToFile("C:\\Test\\Settings.json");
62+
63+
//bool upAllresult = connector.UpgradeAllPackages();
6264
}
6365
catch (Exception e)
6466
{

0 commit comments

Comments
 (0)