Skip to content

Commit

Permalink
Added the function to request silent package upgrade (#41); Fixed doc…
Browse files Browse the repository at this point in the history
…umentation comments
  • Loading branch information
basicx-StrgV committed Sep 11, 2024
1 parent 96d8920 commit ffd3015
Show file tree
Hide file tree
Showing 2 changed files with 330 additions and 5 deletions.
200 changes: 195 additions & 5 deletions src/WGet.NET/Components/WinGetPackageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ public async Task<bool> UninstallPackageAsync(WinGetPackage package, Cancellatio
}
//---------------------------------------------------------------------------------------------

//---Upgrade-----------------------------------------------------------------------------------
//---List Upgrades-----------------------------------------------------------------------------
/// <summary>
/// Get all upgradeable packages.
/// </summary>
Expand Down Expand Up @@ -1001,11 +1001,13 @@ public async Task<List<WinGetPackage>> GetUpgradeablePackagesAsync(CancellationT

return ProcessOutputReader.ToPackageList(result.Output, PackageAction.UpgradeList);
}
//---------------------------------------------------------------------------------------------

//---Upgrade-----------------------------------------------------------------------------------
/// <summary>
/// Upgrades a package using winget.
/// </summary>
/// <param name="packageId">The id or name of the package for upgrade.</param>
/// <param name="packageId">The id or name of the package that should be upgraded.</param>
/// <returns>
/// <see langword="true"/> if the upgrade was successful or <see langword="false"/> if it failed.
/// </returns>
Expand All @@ -1032,7 +1034,40 @@ public bool UpgradePackage(string packageId)
/// <summary>
/// Upgrades a package using winget.
/// </summary>
/// <param name="package">The <see cref="WGetNET.WinGetPackage"/> that for the upgrade</param>
/// <param name="packageId">The id or name of the package that should be upgraded.</param>
/// <param name="silent">Request silent upgrade of packages.</param>
/// <returns>
/// <see langword="true"/> if the upgrade was successful or <see langword="false"/> if it failed.
/// </returns>
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
/// WinGet is not installed or not found on the system.
/// </exception>
/// <exception cref="System.ArgumentException">
/// A provided argument is empty.
/// </exception>
/// <exception cref="System.ArgumentNullException">
/// A provided argument is null.
/// </exception>
public bool UpgradePackage(string packageId, bool silent)
{
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(packageId, "packageId");

string cmd = AcceptSourceAgreements(string.Format(_upgradeCmd, packageId));

if (silent)
{
cmd = Silent(cmd);
}

ProcessResult result = Execute(cmd);

return result.Success;
}

/// <summary>
/// Upgrades a package using winget.
/// </summary>
/// <param name="package">The <see cref="WGetNET.WinGetPackage"/> that should be upgraded.</param>
/// <returns>
/// <see langword="true"/> if the upgrade was successful or <see langword="false"/> if it failed.
/// </returns>
Expand All @@ -1057,10 +1092,39 @@ public bool UpgradePackage(WinGetPackage package)
return UpgradePackage(package.Id);
}

/// <summary>
/// Upgrades a package using winget.
/// </summary>
/// <param name="package">The <see cref="WGetNET.WinGetPackage"/> that should be upgraded.</param>
/// <param name="silent">Request silent upgrade of packages.</param>
/// <returns>
/// <see langword="true"/> if the upgrade was successful or <see langword="false"/> if it failed.
/// </returns>
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
/// WinGet is not installed or not found on the system.
/// </exception>
/// <exception cref="System.ArgumentException">
/// A provided argument is empty.
/// </exception>
/// <exception cref="System.ArgumentNullException">
/// A provided argument is null.
/// </exception>
public bool UpgradePackage(WinGetPackage package, bool silent)
{
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");

if (package.HasShortenedId || package.HasNoId)
{
return UpgradePackage(package.Name, silent);
}

return UpgradePackage(package.Id, silent);
}

/// <summary>
/// Asynchronously upgrades a package using winget.
/// </summary>
/// <param name="packageId">The id or name of the package for upgrade.</param>
/// <param name="packageId">The id or name of the package that should be upgraded.</param>
/// <param name="cancellationToken">
/// The <see cref="System.Threading.CancellationToken"/> for the <see cref="System.Threading.Tasks.Task"/>.
/// </param>
Expand Down Expand Up @@ -1091,7 +1155,44 @@ public async Task<bool> UpgradePackageAsync(string packageId, CancellationToken
/// <summary>
/// Asynchronously upgrades a package using winget.
/// </summary>
/// <param name="package">The <see cref="WGetNET.WinGetPackage"/> that for the upgrade</param>
/// <param name="packageId">The id or name of the package that should be upgraded.</param>
/// <param name="silent">Request silent upgrade of packages.</param>
/// <param name="cancellationToken">
/// The <see cref="System.Threading.CancellationToken"/> for the <see cref="System.Threading.Tasks.Task"/>.
/// </param>
/// <returns>
/// A <see cref="System.Threading.Tasks.Task"/>, containing the result.
/// The result is <see langword="true"/> if the upgrade was successful or <see langword="false"/> if it failed.
/// </returns>
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
/// WinGet is not installed or not found on the system.
/// </exception>
/// <exception cref="System.ArgumentException">
/// A provided argument is empty.
/// </exception>
/// <exception cref="System.ArgumentNullException">
/// A provided argument is null.
/// </exception>
public async Task<bool> UpgradePackageAsync(string packageId, bool silent, CancellationToken cancellationToken = default)
{
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(packageId, "packageId");

string cmd = AcceptSourceAgreements(string.Format(_upgradeCmd, packageId));

if (silent)
{
cmd = Silent(cmd);
}

ProcessResult result = await ExecuteAsync(cmd, false, cancellationToken);

return result.Success;
}

/// <summary>
/// Asynchronously upgrades a package using winget.
/// </summary>
/// <param name="package">The <see cref="WGetNET.WinGetPackage"/> that should be upgraded.</param>
/// <param name="cancellationToken">
/// The <see cref="System.Threading.CancellationToken"/> for the <see cref="System.Threading.Tasks.Task"/>.
/// </param>
Expand Down Expand Up @@ -1120,6 +1221,39 @@ public async Task<bool> UpgradePackageAsync(WinGetPackage package, CancellationT
return await UpgradePackageAsync(package.Id, cancellationToken);
}

/// <summary>
/// Asynchronously upgrades a package using winget.
/// </summary>
/// <param name="package">The <see cref="WGetNET.WinGetPackage"/> that should be upgraded.</param>
/// <param name="silent">Request silent upgrade of packages.</param>
/// <param name="cancellationToken">
/// The <see cref="System.Threading.CancellationToken"/> for the <see cref="System.Threading.Tasks.Task"/>.
/// </param>
/// <returns>
/// A <see cref="System.Threading.Tasks.Task"/>, containing the result.
/// The result is <see langword="true"/> if the upgrade was successful or <see langword="false"/> if it failed.
/// </returns>
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
/// WinGet is not installed or not found on the system.
/// </exception>
/// <exception cref="System.ArgumentException">
/// A provided argument is empty.
/// </exception>
/// <exception cref="System.ArgumentNullException">
/// A provided argument is null.
/// </exception>
public async Task<bool> UpgradePackageAsync(WinGetPackage package, bool silent, CancellationToken cancellationToken = default)
{
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");

if (package.HasShortenedId || package.HasNoId)
{
return await UpgradePackageAsync(package.Name, silent, cancellationToken);
}

return await UpgradePackageAsync(package.Id, silent, cancellationToken);
}

/// <summary>
/// Tries to upgrade all packages using winget.
/// </summary>
Expand All @@ -1139,6 +1273,32 @@ public bool UpgradeAllPackages()
return result.Success;
}

/// <summary>
/// Tries to upgrade all packages using winget.
/// </summary>
/// <remarks>
/// The action might run succesfully without upgrading every or even any package.
/// </remarks>
/// <param name="silent">Request silent upgrade of packages.</param>
/// <returns>
/// <see langword="true"/> if the action run successfully or <see langword="false"/> if it failed.
/// </returns>
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
/// WinGet is not installed or not found on the system.
/// </exception>
public bool UpgradeAllPackages(bool silent)
{
string cmd = AcceptSourceAgreements(_upgradeAllCmd);
if (silent)
{
cmd = Silent(cmd);
}

ProcessResult result = Execute(cmd);

return result.Success;
}

/// <summary>
/// Asynchronously tries to upgrade all packages using winget.
/// </summary>
Expand All @@ -1161,6 +1321,36 @@ public async Task<bool> UpgradeAllPackagesAsync(CancellationToken cancellationTo

return result.Success;
}

/// <summary>
/// Asynchronously tries to upgrade all packages using winget.
/// </summary>
/// <param name="silent">Request silent upgrade of packages.</param>
/// <param name="cancellationToken">
/// The <see cref="System.Threading.CancellationToken"/> for the <see cref="System.Threading.Tasks.Task"/>.
/// </param>
/// <remarks>
/// The action might run succesfully without upgrading every or even any package.
/// </remarks>
/// <returns>
/// A <see cref="System.Threading.Tasks.Task"/>, containing the result.
/// The result is <see langword="true"/> if the action run successfully or <see langword="false"/> if it failed.
/// </returns>
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
/// WinGet is not installed or not found on the system.
/// </exception>
public async Task<bool> UpgradeAllPackagesAsync(bool silent, CancellationToken cancellationToken = default)
{
string cmd = AcceptSourceAgreements(_upgradeAllCmd);
if (silent)
{
cmd = Silent(cmd);
}

ProcessResult result = await ExecuteAsync(cmd, false, cancellationToken);

return result.Success;
}
//---------------------------------------------------------------------------------------------

//---Repair------------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit ffd3015

Please sign in to comment.