Skip to content

Commit

Permalink
Added the option to request a silent installation
Browse files Browse the repository at this point in the history
  • Loading branch information
basicx-StrgV committed Sep 11, 2024
1 parent 34305b7 commit 96d8920
Showing 1 changed file with 164 additions and 0 deletions.
164 changes: 164 additions & 0 deletions src/WGet.NET/Components/WinGetPackageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class WinGetPackageManager : WinGet
// Parameters
private const string _includeUnknown = "--include-unknown";
private const string _acceptSourceAgreements = "--accept-source-agreements";
private const string _silent = "--silent";

private readonly Version _downloadMinVersion = new(1, 6, 0);
private readonly Version _repairMinVersion = new(1, 7, 0);
Expand Down Expand Up @@ -609,6 +610,39 @@ public bool InstallPackage(string packageId)
return result.Success;
}

/// <summary>
/// Install a package using winget.
/// </summary>
/// <param name="packageId">The id or name of the package for the installation.</param>
/// <param name="silent">Request silent installation of packages.</param>
/// <returns>
/// <see langword="true"/> if the installation 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 InstallPackage(string packageId, bool silent)
{
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(packageId, "packageId");

string cmd = string.Format(_installCmd, packageId);

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

ProcessResult result = Execute(cmd);

return result.Success;
}

/// <summary>
/// Install a package using winget.
/// </summary>
Expand Down Expand Up @@ -637,6 +671,35 @@ public bool InstallPackage(WinGetPackage package)
return InstallPackage(package.Id);
}

/// <summary>
/// Install a package using winget.
/// </summary>
/// <param name="package">The <see cref="WGetNET.WinGetPackage"/> for the installation.</param>
/// <param name="silent">Request silent installation of packages.</param>
/// <returns>
/// <see langword="true"/> if the installation 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 InstallPackage(WinGetPackage package, bool silent)
{
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");

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

return InstallPackage(package.Id, silent);
}

/// <summary>
/// Asynchronously install a package using winget.
/// </summary>
Expand Down Expand Up @@ -668,6 +731,43 @@ public async Task<bool> InstallPackageAsync(string packageId, CancellationToken
return result.Success;
}

/// <summary>
/// Asynchronously install a package using winget.
/// </summary>
/// <param name="packageId">The id or name of the package for the installation.</param>
/// <param name="silent">Request silent installation 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 installation 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> InstallPackageAsync(string packageId, bool silent, CancellationToken cancellationToken = default)
{
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(packageId, "packageId");

string cmd = string.Format(_installCmd, packageId);

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

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

return result.Success;
}

/// <summary>
/// Asynchronously install a package using winget.
/// </summary>
Expand Down Expand Up @@ -699,6 +799,39 @@ public async Task<bool> InstallPackageAsync(WinGetPackage package, CancellationT

return await InstallPackageAsync(package.Id, cancellationToken);
}

/// <summary>
/// Asynchronously install a package using winget.
/// </summary>
/// <param name="package">The <see cref="WGetNET.WinGetPackage"/> for the installation.</param>
/// <param name="silent">Request silent installation 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 installation 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> InstallPackageAsync(WinGetPackage package, bool silent, CancellationToken cancellationToken = default)
{
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");

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

return await InstallPackageAsync(package.Id, silent, cancellationToken);
}
//---------------------------------------------------------------------------------------------

//---Uninstall---------------------------------------------------------------------------------
Expand Down Expand Up @@ -2803,6 +2936,11 @@ public async Task<bool> ResetPinsAsync(CancellationToken cancellationToken = def
/// </returns>
private string IncludeUnknownbyVersion(string argument)
{
if (string.IsNullOrWhiteSpace(argument))
{
return argument;
}

// Checking version to determine if "--include-unknown" is necessary.
if (CheckWinGetVersion(new Version(1, 4, 0)))
{
Expand All @@ -2823,11 +2961,37 @@ private string IncludeUnknownbyVersion(string argument)
/// </returns>
private string AcceptSourceAgreements(string argument)
{
if (string.IsNullOrWhiteSpace(argument))
{
return argument;
}

argument += $" {_acceptSourceAgreements}";

return argument;
}

/// <summary>
/// Adds the '--silent' argument to the given <see cref="System.String"/> of arguments.
/// </summary>
/// <param name="argument">
/// <see cref="System.String"/> containing the arguments that should be extended.
/// </param>
/// <returns>
/// A <see cref="System.String"/> containing the new process arguments.
/// </returns>
private string Silent(string argument)
{
if (string.IsNullOrWhiteSpace(argument))
{
return argument;
}

argument += $" {_silent}";

return argument;
}

/// <summary>
/// Tries to match a <see cref="WGetNET.WinGetPackage"/> to the provided search criteria.
/// </summary>
Expand Down

0 comments on commit 96d8920

Please sign in to comment.