WGet.NET 4.4.1
A WinGet wrapper library for .Net.
Loading...
Searching...
No Matches
WinGetSourceManager.cs
1//--------------------------------------------------//
2// Created by basicx-StrgV //
3// https://github.com/basicx-StrgV/ //
4//--------------------------------------------------//
5using System.Collections.Generic;
6using System.Threading;
7using System.Threading.Tasks;
10using WGetNET.Models;
11
12namespace WGetNET
13{
18 {
23 {
24 // Provide empty constructor for xlm docs
25 }
26
27 //---List--------------------------------------------------------------------------------------
37 public List<WinGetSource> GetInstalledSources()
38 {
39 ProcessResult result = Execute(WinGetArguments.SourceExport());
40
41 return ProcessOutputReader.ToSourceList(result.Output);
42 }
43
60 public List<WinGetSource> GetInstalledSources(string sourceName)
61 {
62 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(sourceName, "sourceName");
63
64 ProcessResult result = Execute(WinGetArguments.SourceExport().Name(sourceName));
65
66 return ProcessOutputReader.ToSourceList(result.Output);
67 }
68
82 public async Task<List<WinGetSource>> GetInstalledSourcesAsync(CancellationToken cancellationToken = default)
83 {
84 ProcessResult result = await ExecuteAsync(WinGetArguments.SourceExport(), false, cancellationToken);
85
86 // Return empty list if the task was cancled
87 if (cancellationToken.IsCancellationRequested)
88 {
89 return new List<WinGetSource>();
90 }
91
92 return ProcessOutputReader.ToSourceList(result.Output);
93 }
94
115 public async Task<List<WinGetSource>> GetInstalledSourcesAsync(string sourceName, CancellationToken cancellationToken = default)
116 {
117 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(sourceName, "sourceName");
118
119 ProcessResult result = await ExecuteAsync(WinGetArguments.SourceExport().Name(sourceName), false, cancellationToken);
120
121 // Return empty list if the task was cancled
122 if (cancellationToken.IsCancellationRequested)
123 {
124 return new List<WinGetSource>();
125 }
126
127 return ProcessOutputReader.ToSourceList(result.Output);
128 }
129 //---------------------------------------------------------------------------------------------
130
131 //---Add---------------------------------------------------------------------------------------
156 public bool AddSource(string name, string arg)
157 {
158 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(name, "name");
159 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(arg, "arg");
160
161 ProcessResult result =
162 Execute(
164 .SourceAdd()
165 .Name(name)
166 .Arg(arg)
167 .AcceptSourceAgreements(),
168 true);
169
170 return result.Success;
171 }
172
200 public bool AddSource(string name, string arg, string type)
201 {
202 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(name, "name");
203 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(arg, "arg");
204 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(type, "type");
205
206 ProcessResult result =
207 Execute(
209 .SourceAdd()
210 .Name(name)
211 .Arg(arg)
212 .Type(type)
213 .AcceptSourceAgreements(),
214 true);
215
216 return result.Success;
217 }
218
240 public bool AddSource(WinGetSource source)
241 {
242 ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(source, "source");
243
244 if (string.IsNullOrWhiteSpace(source.Type))
245 {
246 return AddSource(source.Name, source.Arg);
247 }
248
249 return AddSource(source.Name, source.Arg, source.Type);
250 }
251
270 public bool AddSource(IEnumerable<WinGetSource> sources)
271 {
272 ArgsHelper.ThrowIfObjectIsNull(sources, "sources");
273
274 bool succes = true;
275 foreach (WinGetSource source in sources)
276 {
277 if (!AddSource(source))
278 {
279 succes = false;
280 }
281 }
282
283 return succes;
284 }
285
314 public async Task<bool> AddSourceAsync(string name, string arg, CancellationToken cancellationToken = default)
315 {
316 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(name, "name");
317 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(arg, "arg");
318
319 ProcessResult result =
320 await ExecuteAsync(
322 .SourceAdd()
323 .Name(name)
324 .Arg(arg)
325 .AcceptSourceAgreements(),
326 true, cancellationToken);
327
328 return result.Success;
329 }
330
362 public async Task<bool> AddSourceAsync(string name, string arg, string type, CancellationToken cancellationToken = default)
363 {
364 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(name, "name");
365 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(arg, "arg");
366 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(type, "type");
367
368 ProcessResult result =
369 await ExecuteAsync(
371 .SourceAdd()
372 .Name(name)
373 .Arg(arg)
374 .Type(type)
375 .AcceptSourceAgreements(),
376 true, cancellationToken);
377
378 return result.Success;
379 }
380
406 public async Task<bool> AddSourceAsync(WinGetSource source, CancellationToken cancellationToken = default)
407 {
408 ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(source, "source");
409
410 if (string.IsNullOrWhiteSpace(source.Type))
411 {
412 return await AddSourceAsync(source.Name, source.Arg, cancellationToken);
413 }
414
415 return await AddSourceAsync(source.Name, source.Arg, source.Type, cancellationToken);
416 }
417
443 public async Task<bool> AddSourceAsync(IEnumerable<WinGetSource> sources, CancellationToken cancellationToken = default)
444 {
445 ArgsHelper.ThrowIfObjectIsNull(sources, "sources");
446
447 bool success = true;
448 foreach (WinGetSource source in sources)
449 {
450 if (cancellationToken.IsCancellationRequested)
451 {
452 return false;
453 }
454
455 if (!(await AddSourceAsync(source, cancellationToken)))
456 {
457 success = false;
458 }
459 }
460
461 return success;
462 }
463 //---------------------------------------------------------------------------------------------
464
465 //---Update------------------------------------------------------------------------------------
478 public bool UpdateSources()
479 {
480 ProcessResult result = Execute(WinGetArguments.SourceUpdate());
481
482 return result.Success;
483 }
484
501 public async Task<bool> UpdateSourcesAsync(CancellationToken cancellationToken = default)
502 {
503 ProcessResult result = await ExecuteAsync(WinGetArguments.SourceUpdate(), false, cancellationToken);
504
505 return result.Success;
506 }
507 //---------------------------------------------------------------------------------------------
508
509 //---Export------------------------------------------------------------------------------------
546 public void ExportSourcesToFile(string file)
547 {
548 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(file, "file");
549 ArgsHelper.ThrowIfPathIsInvalid(file);
550
551 FileHelper.WriteTextToFile(
552 file,
555 }
556
594 public void ExportSourcesToFile(string file, string sourceName)
595 {
596 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(file, "file");
597 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(sourceName, "sourceName");
598 ArgsHelper.ThrowIfPathIsInvalid(file);
599
600 FileHelper.WriteTextToFile(
601 file,
603 GetInstalledSources(sourceName)));
604 }
605
647 public void ExportSourcesToFile(string file, WinGetSource source)
648 {
649 ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(source, "source");
650 ArgsHelper.ThrowIfPathIsInvalid(file);
651
652 ExportSourcesToFile(file, source.Name);
653 }
654
697 public async Task ExportSourcesToFileAsync(string file, CancellationToken cancellationToken = default)
698 {
699 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(file, "file");
700 ArgsHelper.ThrowIfPathIsInvalid(file);
701
702 await FileHelper.WriteTextToFileAsync(
703 file, SourcesToJson(
704 await GetInstalledSourcesAsync(cancellationToken)),
705 cancellationToken);
706 }
707
751 public async Task ExportSourcesToFileAsync(string file, string sourceName, CancellationToken cancellationToken = default)
752 {
753 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(file, "file");
754 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(sourceName, "sourceName");
755 ArgsHelper.ThrowIfPathIsInvalid(file);
756
757 await FileHelper.WriteTextToFileAsync(
758 file,
760 await GetInstalledSourcesAsync(sourceName, cancellationToken)),
761 cancellationToken);
762 }
763
811 public async Task ExportSourcesToFileAsync(string file, WinGetSource source, CancellationToken cancellationToken = default)
812 {
813 ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(source, "source");
814 ArgsHelper.ThrowIfPathIsInvalid(file);
815
816 await ExportSourcesToFileAsync(file, source.Name, cancellationToken);
817 }
818 //---------------------------------------------------------------------------------------------
819
820 //---Import------------------------------------------------------------------------------------
845 public bool ImportSourcesFromJson(string jsonString)
846 {
847 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(jsonString, "jsonString");
848
849 List<SourceModel> sources = JsonHelper.StringToObject<List<SourceModel>>(jsonString);
850
851 bool success = true;
852 for (int i = 0; i < sources.Count; i++)
853 {
854 if (!AddSource(WinGetSource.FromSourceModel(sources[i])))
855 {
856 success = false;
857 }
858 }
859
860 return success;
861 }
862
891 public async Task<bool> ImportSourcesFromJsonAsync(string jsonString, CancellationToken cancellationToken = default)
892 {
893 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(jsonString, "jsonString");
894
895#if NETCOREAPP3_1_OR_GREATER
896 List<SourceModel> sources =
897 await JsonHelper.StringToObjectAsync<List<SourceModel>>(jsonString, cancellationToken);
898#elif NETSTANDARD2_0
899 List<SourceModel> sources =
900 JsonHelper.StringToObject<List<SourceModel>>(jsonString);
901#endif
902
903 bool success = true;
904 for (int i = 0; i < sources.Count; i++)
905 {
906 if (cancellationToken.IsCancellationRequested)
907 {
908 return false;
909 }
910
911 if (!(await AddSourceAsync(WinGetSource.FromSourceModel(sources[i]), cancellationToken)))
912 {
913 success = false;
914 }
915 }
916
917 return success;
918 }
919 //---------------------------------------------------------------------------------------------
920
921 //---Reset-------------------------------------------------------------------------------------
937 public bool ResetSources()
938 {
939 ProcessResult result = Execute(WinGetArguments.SourceReset().Force(), true);
940
941 return result.Success;
942 }
943
963 public async Task<bool> ResetSourcesAsync(CancellationToken cancellationToken = default)
964 {
965 ProcessResult result = await ExecuteAsync(WinGetArguments.SourceReset().Force(), true, cancellationToken);
966
967 return result.Success;
968 }
969 //---------------------------------------------------------------------------------------------
970
971 //---Remove------------------------------------------------------------------------------------
993 public bool RemoveSources(string name)
994 {
995 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(name, "name");
996
997 ProcessResult result = Execute(WinGetArguments.SourceRemove().Name(name), true);
998
999 return result.Success;
1000 }
1001
1023 public bool RemoveSources(WinGetSource source)
1024 {
1025 ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(source, "source");
1026
1027 return RemoveSources(source.Name);
1028 }
1029
1055 public async Task<bool> RemoveSourcesAsync(string name, CancellationToken cancellationToken = default)
1056 {
1057 ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(name, "name");
1058
1059 ProcessResult result = await ExecuteAsync(WinGetArguments.SourceRemove().Name(name), true, cancellationToken);
1060
1061 return result.Success;
1062 }
1063
1089 public async Task<bool> RemoveSourcesAsync(WinGetSource source, CancellationToken cancellationToken = default)
1090 {
1091 ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(source, "source");
1092
1093 return await RemoveSourcesAsync(source.Name, cancellationToken);
1094 }
1095 //---------------------------------------------------------------------------------------------
1096
1097 //---Other-------------------------------------------------------------------------------------
1110#pragma warning disable S2325 // Ignore warning for now because changing it will be an API braking change.
1111 public string SourcesToJson(IEnumerable<WinGetSource> sources)
1112#pragma warning restore S2325
1113 {
1114 ArgsHelper.ThrowIfObjectIsNull(sources, "sources");
1115
1116 // Create source models for json parsing
1117 List<SourceModel> models = new();
1118 foreach (WinGetSource source in sources)
1119 {
1120 models.Add(SourceModel.FromWinGetSource(source));
1121 }
1122
1123 return JsonHelper.GetJson(models);
1124 }
1125 //---------------------------------------------------------------------------------------------
1126 }
1127}
The WGetNET.WinGet class offers informations about the installed winget version.
Definition WinGet.cs:24
The WGetNET.WinGetSourceManager class offers methods to manage the sources used by winget.
async Task< bool > RemoveSourcesAsync(string name, CancellationToken cancellationToken=default)
Asynchronously removes a source from winget (Needs administrator rights).
bool AddSource(string name, string arg)
Adds a new source to winget (Needs administrator rights).
async Task< bool > AddSourceAsync(string name, string arg, CancellationToken cancellationToken=default)
Asynchronously adds a new source to winget (Needs administrator rights).
bool AddSource(string name, string arg, string type)
Adds a new source to winget (Needs administrator rights).
WinGetSourceManager()
Initializes a new instance of the WGetNET.WinGetSourceManager class.
bool AddSource(IEnumerable< WinGetSource > sources)
Adds multiple new sources to winget (Needs administrator rights).
bool ImportSourcesFromJson(string jsonString)
Imports sources into winget from a json string.
bool RemoveSources(WinGetSource source)
Removes a source from winget (Needs administrator rights).
async Task ExportSourcesToFileAsync(string file, CancellationToken cancellationToken=default)
Asynchronously exports the winget sources in json format to a file.
string SourcesToJson(IEnumerable< WinGetSource > sources)
Generates a valid json string from the provided sources.
async Task< bool > AddSourceAsync(WinGetSource source, CancellationToken cancellationToken=default)
Asynchronously adds a new source to winget (Needs administrator rights).
bool AddSource(WinGetSource source)
Adds a new source to winget (Needs administrator rights).
void ExportSourcesToFile(string file)
Exports the winget sources in json format to a file.
async Task ExportSourcesToFileAsync(string file, string sourceName, CancellationToken cancellationToken=default)
Asynchronously exports the winget sources in json format to a file.
bool RemoveSources(string name)
Removes a source from winget (Needs administrator rights).
async Task< bool > AddSourceAsync(string name, string arg, string type, CancellationToken cancellationToken=default)
Asynchronously adds a new source to winget (Needs administrator rights).
async Task< List< WinGetSource > > GetInstalledSourcesAsync(string sourceName, CancellationToken cancellationToken=default)
Asynchronously gets a list of installed sources that matches the provided name.
async Task< List< WinGetSource > > GetInstalledSourcesAsync(CancellationToken cancellationToken=default)
Asynchronously gets a list of all sources that are installed in winget.
List< WinGetSource > GetInstalledSources()
Gets a list of all sources that are installed in winget.
async Task ExportSourcesToFileAsync(string file, WinGetSource source, CancellationToken cancellationToken=default)
Asynchronously exports the winget sources in json format to a file.
async Task< bool > RemoveSourcesAsync(WinGetSource source, CancellationToken cancellationToken=default)
Asynchronously removes a source from winget (Needs administrator rights).
List< WinGetSource > GetInstalledSources(string sourceName)
Gets a list of installed sources that matches the provided name.
async Task< bool > ResetSourcesAsync(CancellationToken cancellationToken=default)
Asynchronously resets all sources that are installed in winget (Needs administrator rights).
async Task< bool > UpdateSourcesAsync(CancellationToken cancellationToken=default)
Asynchronously updates all sources that are installed in winget.
void ExportSourcesToFile(string file, WinGetSource source)
Exports the winget sources in json format to a file.
void ExportSourcesToFile(string file, string sourceName)
Exports the winget sources in json format to a file.
async Task< bool > ImportSourcesFromJsonAsync(string jsonString, CancellationToken cancellationToken=default)
Asynchronously imports sources into winget from a json string.
bool ResetSources()
Resets all sources that are installed in winget (Needs administrator rights).
async Task< bool > AddSourceAsync(IEnumerable< WinGetSource > sources, CancellationToken cancellationToken=default)
Asynchronously adds multiple new sources to winget (Needs administrator rights).
bool UpdateSources()
Updates all sources that are installed in winget.
Represents a winget arguments string for different winget actions.
static WinGetArguments SourceUpdate()
Creates a new winget arguments object with "source update" as the base cmd.
WinGetArguments Name(string name)
Adds name data to the arguments.
WinGetArguments Force()
Adds the '–force' flag to the arguments.
static WinGetArguments SourceReset()
Creates a new winget arguments object with "source reset" as the base cmd.
static WinGetArguments SourceExport()
Creates a new winget arguments object with "source export" as the base cmd.
static WinGetArguments SourceRemove()
Creates a new winget arguments object with "source remove" as the base cmd.
Represents a winget source.
string Name
Gets the name of the source.
string Type
Gets the type of the source.
string Arg
Gets the URL/UNC of the source.