22// Created by basicx-StrgV //
33// https://github.com/basicx-StrgV/ //
44//--------------------------------------------------//
5+ using System . Collections . Generic ;
56using System . Threading ;
67using System . Threading . Tasks ;
7- using System . Collections . Generic ;
8- using WGetNET . Models ;
9- using WGetNET . Helper ;
108using WGetNET . Components . Internal ;
9+ using WGetNET . Helper ;
10+ using WGetNET . Models ;
1111
1212namespace WGetNET
1313{
@@ -16,13 +16,6 @@ namespace WGetNET
1616 /// </summary>
1717 public class WinGetSourceManager : WinGet
1818 {
19- private const string _sourceAddCmd = "source add -n {0} -a {1} --accept-source-agreements" ;
20- private const string _sourceAddWithTypeCmd = "source add -n {0} -a {1} -t {2} --accept-source-agreements" ;
21- private const string _sourceUpdateCmd = "source update" ;
22- private const string _sourceExportCmd = "source export" ;
23- private const string _sourceResetCmd = "source reset --force" ;
24- private const string _sourceRemoveCmd = "source remove -n {0}" ;
25-
2619 /// <summary>
2720 /// Initializes a new instance of the <see cref="WGetNET.WinGetSourceManager"/> class.
2821 /// </summary>
@@ -43,7 +36,11 @@ public WinGetSourceManager()
4336 /// </exception>
4437 public List < WinGetSource > GetInstalledSources ( )
4538 {
46- ProcessResult result = Execute ( _sourceExportCmd ) ;
39+ ProcessResult result =
40+ Execute (
41+ WinGetArguments
42+ . SourceExport ( )
43+ . ToString ( ) ) ;
4744
4845 return ProcessOutputReader . ToSourceList ( result . Output ) ;
4946 }
@@ -68,9 +65,12 @@ public List<WinGetSource> GetInstalledSources(string sourceName)
6865 {
6966 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( sourceName , "sourceName" ) ;
7067
71- string cmd = $ "{ _sourceExportCmd } -n { sourceName } ";
72-
73- ProcessResult result = Execute ( cmd ) ;
68+ ProcessResult result =
69+ Execute (
70+ WinGetArguments
71+ . SourceExport ( )
72+ . Name ( sourceName )
73+ . ToString ( ) ) ;
7474
7575 return ProcessOutputReader . ToSourceList ( result . Output ) ;
7676 }
@@ -90,7 +90,12 @@ public List<WinGetSource> GetInstalledSources(string sourceName)
9090 /// </exception>
9191 public async Task < List < WinGetSource > > GetInstalledSourcesAsync ( CancellationToken cancellationToken = default )
9292 {
93- ProcessResult result = await ExecuteAsync ( _sourceExportCmd , false , cancellationToken ) ;
93+ ProcessResult result =
94+ await ExecuteAsync (
95+ WinGetArguments
96+ . SourceExport ( )
97+ . ToString ( ) ,
98+ false , cancellationToken ) ;
9499
95100 // Return empty list if the task was cancled
96101 if ( cancellationToken . IsCancellationRequested )
@@ -125,9 +130,13 @@ public async Task<List<WinGetSource>> GetInstalledSourcesAsync(string sourceName
125130 {
126131 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( sourceName , "sourceName" ) ;
127132
128- string cmd = $ "{ _sourceExportCmd } -n { sourceName } ";
129-
130- ProcessResult result = await ExecuteAsync ( cmd , false , cancellationToken ) ;
133+ ProcessResult result =
134+ await ExecuteAsync (
135+ WinGetArguments
136+ . SourceExport ( )
137+ . Name ( sourceName )
138+ . ToString ( ) ,
139+ false , cancellationToken ) ;
131140
132141 // Return empty list if the task was cancled
133142 if ( cancellationToken . IsCancellationRequested )
@@ -169,9 +178,15 @@ public bool AddSource(string name, string arg)
169178 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( name , "name" ) ;
170179 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( arg , "arg" ) ;
171180
172- string cmd = string . Format ( _sourceAddCmd , name , arg ) ;
173-
174- ProcessResult result = Execute ( cmd , true ) ;
181+ ProcessResult result =
182+ Execute (
183+ WinGetArguments
184+ . SourceAdd ( )
185+ . Name ( name )
186+ . Arg ( arg )
187+ . AcceptSourceAgreements ( )
188+ . ToString ( ) ,
189+ true ) ;
175190
176191 return result . Success ;
177192 }
@@ -209,9 +224,16 @@ public bool AddSource(string name, string arg, string type)
209224 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( arg , "arg" ) ;
210225 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( type , "type" ) ;
211226
212- string cmd = string . Format ( _sourceAddWithTypeCmd , name , arg , type ) ;
213-
214- ProcessResult result = Execute ( cmd , true ) ;
227+ ProcessResult result =
228+ Execute (
229+ WinGetArguments
230+ . SourceAdd ( )
231+ . Name ( name )
232+ . Arg ( arg )
233+ . Type ( type )
234+ . AcceptSourceAgreements ( )
235+ . ToString ( ) ,
236+ true ) ;
215237
216238 return result . Success ;
217239 }
@@ -316,9 +338,15 @@ public async Task<bool> AddSourceAsync(string name, string arg, CancellationToke
316338 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( name , "name" ) ;
317339 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( arg , "arg" ) ;
318340
319- string cmd = string . Format ( _sourceAddCmd , name , arg ) ;
320-
321- ProcessResult result = await ExecuteAsync ( cmd , true , cancellationToken ) ;
341+ ProcessResult result =
342+ await ExecuteAsync (
343+ WinGetArguments
344+ . SourceAdd ( )
345+ . Name ( name )
346+ . Arg ( arg )
347+ . AcceptSourceAgreements ( )
348+ . ToString ( ) ,
349+ true , cancellationToken ) ;
322350
323351 return result . Success ;
324352 }
@@ -360,9 +388,16 @@ public async Task<bool> AddSourceAsync(string name, string arg, string type, Can
360388 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( arg , "arg" ) ;
361389 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( type , "type" ) ;
362390
363- string cmd = string . Format ( _sourceAddWithTypeCmd , name , arg , type ) ;
364-
365- ProcessResult result = await ExecuteAsync ( cmd , true , cancellationToken ) ;
391+ ProcessResult result =
392+ await ExecuteAsync (
393+ WinGetArguments
394+ . SourceAdd ( )
395+ . Name ( name )
396+ . Arg ( arg )
397+ . Type ( type )
398+ . AcceptSourceAgreements ( )
399+ . ToString ( ) ,
400+ true , cancellationToken ) ;
366401
367402 return result . Success ;
368403 }
@@ -466,7 +501,11 @@ public async Task<bool> AddSourceAsync(IEnumerable<WinGetSource> sources, Cancel
466501 /// </exception>
467502 public bool UpdateSources ( )
468503 {
469- ProcessResult result = Execute ( _sourceUpdateCmd ) ;
504+ ProcessResult result =
505+ Execute (
506+ WinGetArguments
507+ . SourceUpdate ( )
508+ . ToString ( ) ) ;
470509
471510 return result . Success ;
472511 }
@@ -489,7 +528,12 @@ public bool UpdateSources()
489528 /// </exception>
490529 public async Task < bool > UpdateSourcesAsync ( CancellationToken cancellationToken = default )
491530 {
492- ProcessResult result = await ExecuteAsync ( _sourceUpdateCmd , false , cancellationToken ) ;
531+ ProcessResult result =
532+ await ExecuteAsync (
533+ WinGetArguments
534+ . SourceUpdate ( )
535+ . ToString ( ) ,
536+ false , cancellationToken ) ;
493537
494538 return result . Success ;
495539 }
@@ -925,7 +969,13 @@ public async Task<bool> ImportSourcesFromJsonAsync(string jsonString, Cancellati
925969 /// </exception>
926970 public bool ResetSources ( )
927971 {
928- ProcessResult result = Execute ( _sourceResetCmd , true ) ;
972+ ProcessResult result =
973+ Execute (
974+ WinGetArguments
975+ . SourceReset ( )
976+ . Force ( )
977+ . ToString ( ) ,
978+ true ) ;
929979
930980 return result . Success ;
931981 }
@@ -951,7 +1001,13 @@ public bool ResetSources()
9511001 /// </exception>
9521002 public async Task < bool > ResetSourcesAsync ( CancellationToken cancellationToken = default )
9531003 {
954- ProcessResult result = await ExecuteAsync ( _sourceResetCmd , true , cancellationToken ) ;
1004+ ProcessResult result =
1005+ await ExecuteAsync (
1006+ WinGetArguments
1007+ . SourceReset ( )
1008+ . Force ( )
1009+ . ToString ( ) ,
1010+ true , cancellationToken ) ;
9551011
9561012 return result . Success ;
9571013 }
@@ -983,9 +1039,13 @@ public bool RemoveSources(string name)
9831039 {
9841040 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( name , "name" ) ;
9851041
986- string cmd = string . Format ( _sourceRemoveCmd , name ) ;
987-
988- ProcessResult result = Execute ( cmd , true ) ;
1042+ ProcessResult result =
1043+ Execute (
1044+ WinGetArguments
1045+ . SourceRemove ( )
1046+ . Name ( name )
1047+ . ToString ( ) ,
1048+ true ) ;
9891049
9901050 return result . Success ;
9911051 }
@@ -1047,9 +1107,13 @@ public async Task<bool> RemoveSourcesAsync(string name, CancellationToken cancel
10471107 {
10481108 ArgsHelper . ThrowIfStringIsNullOrWhiteSpace ( name , "name" ) ;
10491109
1050- string cmd = string . Format ( _sourceRemoveCmd , name ) ;
1051-
1052- ProcessResult result = await ExecuteAsync ( cmd , true , cancellationToken ) ;
1110+ ProcessResult result =
1111+ await ExecuteAsync (
1112+ WinGetArguments
1113+ . SourceRemove ( )
1114+ . Name ( name )
1115+ . ToString ( ) ,
1116+ true , cancellationToken ) ;
10531117
10541118 return result . Success ;
10551119 }
0 commit comments