Skip to content

Commit 96d8920

Browse files
committed
Added the option to request a silent installation
1 parent 34305b7 commit 96d8920

1 file changed

Lines changed: 164 additions & 0 deletions

File tree

src/WGet.NET/Components/WinGetPackageManager.cs

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class WinGetPackageManager : WinGet
4646
// Parameters
4747
private const string _includeUnknown = "--include-unknown";
4848
private const string _acceptSourceAgreements = "--accept-source-agreements";
49+
private const string _silent = "--silent";
4950

5051
private readonly Version _downloadMinVersion = new(1, 6, 0);
5152
private readonly Version _repairMinVersion = new(1, 7, 0);
@@ -609,6 +610,39 @@ public bool InstallPackage(string packageId)
609610
return result.Success;
610611
}
611612

613+
/// <summary>
614+
/// Install a package using winget.
615+
/// </summary>
616+
/// <param name="packageId">The id or name of the package for the installation.</param>
617+
/// <param name="silent">Request silent installation of packages.</param>
618+
/// <returns>
619+
/// <see langword="true"/> if the installation was successful or <see langword="false"/> if it failed.
620+
/// </returns>
621+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
622+
/// WinGet is not installed or not found on the system.
623+
/// </exception>
624+
/// <exception cref="System.ArgumentException">
625+
/// A provided argument is empty.
626+
/// </exception>
627+
/// <exception cref="System.ArgumentNullException">
628+
/// A provided argument is null.
629+
/// </exception>
630+
public bool InstallPackage(string packageId, bool silent)
631+
{
632+
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(packageId, "packageId");
633+
634+
string cmd = string.Format(_installCmd, packageId);
635+
636+
if (silent)
637+
{
638+
cmd = Silent(cmd);
639+
}
640+
641+
ProcessResult result = Execute(cmd);
642+
643+
return result.Success;
644+
}
645+
612646
/// <summary>
613647
/// Install a package using winget.
614648
/// </summary>
@@ -637,6 +671,35 @@ public bool InstallPackage(WinGetPackage package)
637671
return InstallPackage(package.Id);
638672
}
639673

674+
/// <summary>
675+
/// Install a package using winget.
676+
/// </summary>
677+
/// <param name="package">The <see cref="WGetNET.WinGetPackage"/> for the installation.</param>
678+
/// <param name="silent">Request silent installation of packages.</param>
679+
/// <returns>
680+
/// <see langword="true"/> if the installation was successful or <see langword="false"/> if it failed.
681+
/// </returns>
682+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
683+
/// WinGet is not installed or not found on the system.
684+
/// </exception>
685+
/// <exception cref="System.ArgumentException">
686+
/// A provided argument is empty.
687+
/// </exception>
688+
/// <exception cref="System.ArgumentNullException">
689+
/// A provided argument is null.
690+
/// </exception>
691+
public bool InstallPackage(WinGetPackage package, bool silent)
692+
{
693+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
694+
695+
if (package.HasShortenedId || package.HasNoId)
696+
{
697+
return InstallPackage(package.Name, silent);
698+
}
699+
700+
return InstallPackage(package.Id, silent);
701+
}
702+
640703
/// <summary>
641704
/// Asynchronously install a package using winget.
642705
/// </summary>
@@ -668,6 +731,43 @@ public async Task<bool> InstallPackageAsync(string packageId, CancellationToken
668731
return result.Success;
669732
}
670733

734+
/// <summary>
735+
/// Asynchronously install a package using winget.
736+
/// </summary>
737+
/// <param name="packageId">The id or name of the package for the installation.</param>
738+
/// <param name="silent">Request silent installation of packages.</param>
739+
/// <param name="cancellationToken">
740+
/// The <see cref="System.Threading.CancellationToken"/> for the <see cref="System.Threading.Tasks.Task"/>.
741+
/// </param>
742+
/// <returns>
743+
/// A <see cref="System.Threading.Tasks.Task"/>, containing the result.
744+
/// The result is <see langword="true"/> if the installation was successful or <see langword="false"/> if it failed.
745+
/// </returns>
746+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
747+
/// WinGet is not installed or not found on the system.
748+
/// </exception>
749+
/// <exception cref="System.ArgumentException">
750+
/// A provided argument is empty.
751+
/// </exception>
752+
/// <exception cref="System.ArgumentNullException">
753+
/// A provided argument is null.
754+
/// </exception>
755+
public async Task<bool> InstallPackageAsync(string packageId, bool silent, CancellationToken cancellationToken = default)
756+
{
757+
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(packageId, "packageId");
758+
759+
string cmd = string.Format(_installCmd, packageId);
760+
761+
if (silent)
762+
{
763+
cmd = Silent(cmd);
764+
}
765+
766+
ProcessResult result = await ExecuteAsync(cmd, false, cancellationToken);
767+
768+
return result.Success;
769+
}
770+
671771
/// <summary>
672772
/// Asynchronously install a package using winget.
673773
/// </summary>
@@ -699,6 +799,39 @@ public async Task<bool> InstallPackageAsync(WinGetPackage package, CancellationT
699799

700800
return await InstallPackageAsync(package.Id, cancellationToken);
701801
}
802+
803+
/// <summary>
804+
/// Asynchronously install a package using winget.
805+
/// </summary>
806+
/// <param name="package">The <see cref="WGetNET.WinGetPackage"/> for the installation.</param>
807+
/// <param name="silent">Request silent installation of packages.</param>
808+
/// <param name="cancellationToken">
809+
/// The <see cref="System.Threading.CancellationToken"/> for the <see cref="System.Threading.Tasks.Task"/>.
810+
/// </param>
811+
/// <returns>
812+
/// A <see cref="System.Threading.Tasks.Task"/>, containing the result.
813+
/// The result is <see langword="true"/> if the installation was successful or <see langword="false"/> if it failed.
814+
/// </returns>
815+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
816+
/// WinGet is not installed or not found on the system.
817+
/// </exception>
818+
/// <exception cref="System.ArgumentException">
819+
/// A provided argument is empty.
820+
/// </exception>
821+
/// <exception cref="System.ArgumentNullException">
822+
/// A provided argument is null.
823+
/// </exception>
824+
public async Task<bool> InstallPackageAsync(WinGetPackage package, bool silent, CancellationToken cancellationToken = default)
825+
{
826+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
827+
828+
if (package.HasShortenedId || package.HasNoId)
829+
{
830+
return await InstallPackageAsync(package.Name, silent, cancellationToken);
831+
}
832+
833+
return await InstallPackageAsync(package.Id, silent, cancellationToken);
834+
}
702835
//---------------------------------------------------------------------------------------------
703836

704837
//---Uninstall---------------------------------------------------------------------------------
@@ -2803,6 +2936,11 @@ public async Task<bool> ResetPinsAsync(CancellationToken cancellationToken = def
28032936
/// </returns>
28042937
private string IncludeUnknownbyVersion(string argument)
28052938
{
2939+
if (string.IsNullOrWhiteSpace(argument))
2940+
{
2941+
return argument;
2942+
}
2943+
28062944
// Checking version to determine if "--include-unknown" is necessary.
28072945
if (CheckWinGetVersion(new Version(1, 4, 0)))
28082946
{
@@ -2823,11 +2961,37 @@ private string IncludeUnknownbyVersion(string argument)
28232961
/// </returns>
28242962
private string AcceptSourceAgreements(string argument)
28252963
{
2964+
if (string.IsNullOrWhiteSpace(argument))
2965+
{
2966+
return argument;
2967+
}
2968+
28262969
argument += $" {_acceptSourceAgreements}";
28272970

28282971
return argument;
28292972
}
28302973

2974+
/// <summary>
2975+
/// Adds the '--silent' argument to the given <see cref="System.String"/> of arguments.
2976+
/// </summary>
2977+
/// <param name="argument">
2978+
/// <see cref="System.String"/> containing the arguments that should be extended.
2979+
/// </param>
2980+
/// <returns>
2981+
/// A <see cref="System.String"/> containing the new process arguments.
2982+
/// </returns>
2983+
private string Silent(string argument)
2984+
{
2985+
if (string.IsNullOrWhiteSpace(argument))
2986+
{
2987+
return argument;
2988+
}
2989+
2990+
argument += $" {_silent}";
2991+
2992+
return argument;
2993+
}
2994+
28312995
/// <summary>
28322996
/// Tries to match a <see cref="WGetNET.WinGetPackage"/> to the provided search criteria.
28332997
/// </summary>

0 commit comments

Comments
 (0)