@@ -19,21 +19,21 @@ namespace WGetNET
1919 /// </summary>
2020 public class WinGetPackageManager : WinGet
2121 {
22+ // Commands
2223 private const string _listCmd = "list" ;
2324 private const string _searchInstalledCmd = "list \" {0}\" " ;
2425 private const string _searchInstalledBySourceCmd = "list \" {0}\" --source {1}" ;
25- private const string _searchCmd = "search \" {0}\" --accept-source-agreements " ;
26- private const string _searchBySourceCmd = "search \" {0}\" --source {1} --accept-source-agreements " ;
26+ private const string _searchCmd = "search \" {0}\" " ;
27+ private const string _searchBySourceCmd = "search \" {0}\" --source {1}" ;
2728 private const string _installCmd = "install \" {0}\" " ;
28- private const string _upgradeCmd = "upgrade \" {0}\" --accept-source-agreements " ;
29- private const string _upgradeAllCmd = "upgrade --al --accept-source-agreements " ;
29+ private const string _upgradeCmd = "upgrade \" {0}\" " ;
30+ private const string _upgradeAllCmd = "upgrade --all " ;
3031 private const string _getUpgradeableCmd = "upgrade" ;
31- private const string _includeUnknown = "--include-unknown" ;
3232 private const string _uninstallCmd = "uninstall \" {0}\" " ;
3333 private const string _exportCmd = "export -o {0}" ;
3434 private const string _importCmd = "import -i {0} --ignore-unavailable" ;
3535 private const string _hashCmd = "hash {0}" ;
36- private const string _downloadCmd = "download {0} --download-directory {1} --accept-source-agreements " ;
36+ private const string _downloadCmd = "download {0} --download-directory {1}" ;
3737 private const string _pinListCmd = "pin list" ;
3838 private const string _pinAddCmd = "pin add \" {0}\" " ;
3939 private const string _pinAddByVersionCmd = "pin add \" {0}\" --version \" {1}\" " ;
@@ -43,6 +43,9 @@ public class WinGetPackageManager : WinGet
4343 private const string _pinRemoveInstalledCmd = "pin remove \" {0}\" --installed" ;
4444 private const string _pinResetCmd = "pin reset --force" ;
4545 private const string _repairCmd = "repair \" {0}\" " ;
46+ // Parameters
47+ private const string _includeUnknown = "--include-unknown" ;
48+ private const string _acceptSourceAgreements = "--accept-source-agreements" ;
4649
4750 private readonly Version _downloadMinVersion = new ( 1 , 6 , 0 ) ;
4851 private readonly Version _pinMinVersion = new ( 1 , 5 , 0 ) ;
@@ -79,7 +82,7 @@ public List<WinGetPackage> SearchPackage(string packageId, bool exact = false)
7982 {
8083 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( packageId , "packageId" ) ;
8184
82- string cmd = string . Format ( _searchCmd , packageId ) ;
85+ string cmd = AcceptSourceAgreements ( string . Format ( _searchCmd , packageId ) ) ;
8386
8487 if ( exact )
8588 {
@@ -118,7 +121,7 @@ public List<WinGetPackage> SearchPackage(string packageId, string sourceName, bo
118121 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( packageId , "packageId" ) ;
119122 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( sourceName , "sourceName" ) ;
120123
121- string cmd = string . Format ( _searchBySourceCmd , packageId , sourceName ) ;
124+ string cmd = AcceptSourceAgreements ( string . Format ( _searchBySourceCmd , packageId , sourceName ) ) ;
122125
123126 if ( exact )
124127 {
@@ -157,7 +160,7 @@ public async Task<List<WinGetPackage>> SearchPackageAsync(string packageId, bool
157160 {
158161 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( packageId , "packageId" ) ;
159162
160- string cmd = string . Format ( _searchCmd , packageId ) ;
163+ string cmd = AcceptSourceAgreements ( string . Format ( _searchCmd , packageId ) ) ;
161164
162165 if ( exact )
163166 {
@@ -206,7 +209,7 @@ public async Task<List<WinGetPackage>> SearchPackageAsync(string packageId, stri
206209 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( packageId , "packageId" ) ;
207210 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( sourceName , "sourceName" ) ;
208211
209- string cmd = string . Format ( _searchBySourceCmd , packageId , sourceName ) ;
212+ string cmd = AcceptSourceAgreements ( string . Format ( _searchBySourceCmd , packageId , sourceName ) ) ;
210213
211214 if ( exact )
212215 {
@@ -688,7 +691,7 @@ public async Task<bool> UninstallPackageAsync(WinGetPackage package, Cancellatio
688691 /// </exception>
689692 public List < WinGetPackage > GetUpgradeablePackages ( )
690693 {
691- string cmd = AddArgumentByVersion ( _getUpgradeableCmd ) ;
694+ string cmd = IncludeUnknownbyVersion ( _getUpgradeableCmd ) ;
692695
693696 ProcessResult result = Execute ( cmd ) ;
694697
@@ -710,7 +713,7 @@ public List<WinGetPackage> GetUpgradeablePackages()
710713 /// </exception>
711714 public async Task < List < WinGetPackage > > GetUpgradeablePackagesAsync ( CancellationToken cancellationToken = default )
712715 {
713- string cmd = AddArgumentByVersion ( _getUpgradeableCmd ) ;
716+ string cmd = IncludeUnknownbyVersion ( _getUpgradeableCmd ) ;
714717
715718 ProcessResult result = await ExecuteAsync ( cmd , false , cancellationToken ) ;
716719
@@ -743,7 +746,7 @@ public bool UpgradePackage(string packageId)
743746 {
744747 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( packageId , "packageId" ) ;
745748
746- string cmd = string . Format ( _upgradeCmd , packageId ) ;
749+ string cmd = AcceptSourceAgreements ( string . Format ( _upgradeCmd , packageId ) ) ;
747750
748751 ProcessResult result = Execute ( cmd ) ;
749752
@@ -802,7 +805,7 @@ public async Task<bool> UpgradePackageAsync(string packageId, CancellationToken
802805 {
803806 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( packageId , "packageId" ) ;
804807
805- string cmd = string . Format ( _upgradeCmd , packageId ) ;
808+ string cmd = AcceptSourceAgreements ( string . Format ( _upgradeCmd , packageId ) ) ;
806809
807810 ProcessResult result = await ExecuteAsync ( cmd , false , cancellationToken ) ;
808811
@@ -855,7 +858,7 @@ public async Task<bool> UpgradePackageAsync(WinGetPackage package, CancellationT
855858 /// </exception>
856859 public bool UpgradeAllPackages ( )
857860 {
858- ProcessResult result = Execute ( _upgradeAllCmd ) ;
861+ ProcessResult result = Execute ( AcceptSourceAgreements ( _upgradeAllCmd ) ) ;
859862
860863 return result . Success ;
861864 }
@@ -878,31 +881,10 @@ public bool UpgradeAllPackages()
878881 /// </exception>
879882 public async Task < bool > UpgradeAllPackagesAsync ( CancellationToken cancellationToken = default )
880883 {
881- ProcessResult result = await ExecuteAsync ( _upgradeAllCmd , false , cancellationToken ) ;
884+ ProcessResult result = await ExecuteAsync ( AcceptSourceAgreements ( _upgradeAllCmd ) , false , cancellationToken ) ;
882885
883886 return result . Success ;
884887 }
885-
886- /// <summary>
887- /// Adds the '--include-unknown' argument to the given <see cref="System.String"/> of aruments
888- /// when the winget version is higher then 1.4.0.
889- /// </summary>
890- /// <param name="argument">
891- /// <see cref="System.String"/> containing the arguments that should be extended.
892- /// </param>
893- /// <returns>
894- /// A <see cref="System.String"/> containing the new process arguments.
895- /// </returns>
896- private string AddArgumentByVersion ( string argument )
897- {
898- // Checking version to determine if "--include-unknown" is necessary.
899- if ( CheckWinGetVersion ( new Version ( 1 , 4 , 0 ) ) )
900- {
901- // Winget version supports new argument, add "--include-unknown" to arguments
902- argument += $ " { _includeUnknown } ";
903- }
904- return argument ;
905- }
906888 //---------------------------------------------------------------------------------------------
907889
908890 //---Repair------------------------------------------------------------------------------------
@@ -1358,7 +1340,7 @@ public bool Download(string packageId, string directory)
13581340 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( packageId , "packageId" ) ;
13591341 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( directory , "directory" ) ;
13601342
1361- string cmd = string . Format ( _downloadCmd , packageId , directory ) ;
1343+ string cmd = string . Format ( AcceptSourceAgreements ( _downloadCmd ) , packageId , directory ) ;
13621344
13631345 ProcessResult result = Execute ( cmd ) ;
13641346
@@ -1497,7 +1479,7 @@ public async Task<bool> DownloadAsync(string packageId, string directory, Cancel
14971479 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( packageId , "packageId" ) ;
14981480 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( directory , "directory" ) ;
14991481
1500- string cmd = string . Format ( _downloadCmd , packageId , directory ) ;
1482+ string cmd = string . Format ( AcceptSourceAgreements ( _downloadCmd ) , packageId , directory ) ;
15011483
15021484 ProcessResult result = await ExecuteAsync ( cmd , false , cancellationToken ) ;
15031485
@@ -2642,5 +2624,44 @@ public async Task<bool> ResetPinsAsync(CancellationToken cancellationToken = def
26422624 return result . Success ;
26432625 }
26442626 //---------------------------------------------------------------------------------------------
2627+
2628+ //---Helper Functions--------------------------------------------------------------------------
2629+ /// <summary>
2630+ /// Adds the '--include-unknown' argument to the given <see cref="System.String"/> of arguments
2631+ /// when the winget version is higher then 1.4.0.
2632+ /// </summary>
2633+ /// <param name="argument">
2634+ /// <see cref="System.String"/> containing the arguments that should be extended.
2635+ /// </param>
2636+ /// <returns>
2637+ /// A <see cref="System.String"/> containing the new process arguments.
2638+ /// </returns>
2639+ private string IncludeUnknownbyVersion ( string argument )
2640+ {
2641+ // Checking version to determine if "--include-unknown" is necessary.
2642+ if ( CheckWinGetVersion ( new Version ( 1 , 4 , 0 ) ) )
2643+ {
2644+ // Winget version supports new argument, add "--include-unknown" to arguments
2645+ argument += $ " { _includeUnknown } ";
2646+ }
2647+ return argument ;
2648+ }
2649+
2650+ /// <summary>
2651+ /// Adds the '--accept-source-agreements' argument to the given <see cref="System.String"/> of arguments.
2652+ /// </summary>
2653+ /// <param name="argument">
2654+ /// <see cref="System.String"/> containing the arguments that should be extended.
2655+ /// </param>
2656+ /// <returns>
2657+ /// A <see cref="System.String"/> containing the new process arguments.
2658+ /// </returns>
2659+ private string AcceptSourceAgreements ( string argument )
2660+ {
2661+ argument += $ " { _acceptSourceAgreements } ";
2662+
2663+ return argument ;
2664+ }
2665+ //---------------------------------------------------------------------------------------------
26452666 }
26462667}
0 commit comments