Skip to content

Commit 9090a32

Browse files
committed
Improved WinGetArguments integration + Cleanup
1 parent c489108 commit 9090a32

5 files changed

Lines changed: 143 additions & 513 deletions

File tree

src/WGet.NET/Components/WinGet.cs

Lines changed: 17 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,7 @@ public WinGet()
148148
/// </exception>
149149
public string ExportSettings()
150150
{
151-
ProcessResult result =
152-
Execute(
153-
WinGetArguments
154-
.SettingsExport()
155-
.ToString());
151+
ProcessResult result = Execute(WinGetArguments.SettingsExport());
156152

157153
return ProcessOutputReader.ExportOutputToString(result);
158154
}
@@ -172,12 +168,7 @@ public string ExportSettings()
172168
/// </exception>
173169
public async Task<string> ExportSettingsAsync(CancellationToken cancellationToken = default)
174170
{
175-
ProcessResult result =
176-
await ExecuteAsync(
177-
WinGetArguments
178-
.SettingsExport()
179-
.ToString(),
180-
false, cancellationToken);
171+
ProcessResult result = await ExecuteAsync(WinGetArguments.SettingsExport(), false, cancellationToken);
181172

182173
return ProcessOutputReader.ExportOutputToString(result);
183174
}
@@ -225,11 +216,7 @@ public void ExportSettingsToFile(string file)
225216
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(file, "file");
226217
ArgsHelper.ThrowIfPathIsInvalid(file);
227218

228-
ProcessResult result =
229-
Execute(
230-
WinGetArguments
231-
.SettingsExport()
232-
.ToString());
219+
ProcessResult result = Execute(WinGetArguments.SettingsExport());
233220

234221
FileHelper.WriteTextToFile(file, ProcessOutputReader.ExportOutputToString(result));
235222
}
@@ -280,12 +267,7 @@ public async Task ExportSettingsToFileAsync(string file, CancellationToken cance
280267
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(file, "file");
281268
ArgsHelper.ThrowIfPathIsInvalid(file);
282269

283-
ProcessResult result =
284-
await ExecuteAsync(
285-
WinGetArguments
286-
.SettingsExport()
287-
.ToString(),
288-
false, cancellationToken);
270+
ProcessResult result = await ExecuteAsync(WinGetArguments.SettingsExport(), false, cancellationToken);
289271

290272
await FileHelper.WriteTextToFileAsync(file, ProcessOutputReader.ExportOutputToString(result), cancellationToken);
291273
}
@@ -408,13 +390,7 @@ public bool EnableAdminSetting(string settingName)
408390
{
409391
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(settingName, "settingName");
410392

411-
ProcessResult result =
412-
Execute(
413-
WinGetArguments
414-
.Settings()
415-
.Enable(settingName)
416-
.ToString(),
417-
true);
393+
ProcessResult result = Execute(WinGetArguments.Settings().Enable(settingName), true);
418394

419395
return result.Success;
420396
}
@@ -476,13 +452,7 @@ public async Task<bool> EnableAdminSettingAsync(string settingName, Cancellation
476452
{
477453
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(settingName, "settingName");
478454

479-
ProcessResult result =
480-
await ExecuteAsync(
481-
WinGetArguments
482-
.Settings()
483-
.Enable(settingName)
484-
.ToString(),
485-
true, cancellationToken);
455+
ProcessResult result = await ExecuteAsync(WinGetArguments.Settings().Enable(settingName), true, cancellationToken);
486456

487457
return result.Success;
488458
}
@@ -544,13 +514,7 @@ public bool DisableAdminSetting(string settingName)
544514
{
545515
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(settingName, "settingName");
546516

547-
ProcessResult result =
548-
Execute(
549-
WinGetArguments
550-
.Settings()
551-
.Disable(settingName)
552-
.ToString(),
553-
true);
517+
ProcessResult result = Execute(WinGetArguments.Settings().Disable(settingName), true);
554518

555519
return result.Success;
556520
}
@@ -612,13 +576,7 @@ public async Task<bool> DisableAdminSettingAsync(string settingName, Cancellatio
612576
{
613577
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(settingName, "settingName");
614578

615-
ProcessResult result =
616-
await ExecuteAsync(
617-
WinGetArguments
618-
.Settings()
619-
.Disable(settingName)
620-
.ToString(),
621-
true, cancellationToken);
579+
ProcessResult result = await ExecuteAsync(WinGetArguments.Settings().Disable(settingName), true, cancellationToken);
622580

623581
return result.Success;
624582
}
@@ -668,12 +626,7 @@ public async Task<bool> DisableAdminSettingAsync(WinGetAdminSetting setting, Can
668626
/// </exception>
669627
public WinGetInfo GetInfo()
670628
{
671-
ProcessResult result =
672-
Execute(
673-
WinGetArguments
674-
.WinGet()
675-
.Info()
676-
.ToString());
629+
ProcessResult result = Execute(WinGetArguments.WinGet().Info());
677630

678631
InfoActionVersionId actionVersionId = InfoActionVersionId.VersionRange1;
679632
if (CheckWinGetVersion(new Version(1, 4, 3531), new Version(1, 5, 101)))
@@ -707,13 +660,7 @@ public WinGetInfo GetInfo()
707660
/// </exception>
708661
public async Task<WinGetInfo> GetInfoAsync(CancellationToken cancellationToken = default)
709662
{
710-
ProcessResult result =
711-
await ExecuteAsync(
712-
WinGetArguments
713-
.WinGet()
714-
.Info()
715-
.ToString(),
716-
false, cancellationToken);
663+
ProcessResult result = await ExecuteAsync(WinGetArguments.WinGet().Info(), false, cancellationToken);
717664

718665
// Check the version range the action should be performed for
719666
InfoActionVersionId actionVersionId = InfoActionVersionId.VersionRange1;
@@ -770,7 +717,7 @@ private protected bool CheckWinGetVersion(Version minVersion, Version? maxVersio
770717
/// Exectutes a WinGet action from the given cmd.
771718
/// </summary>
772719
/// <param name="args">
773-
/// A <see cref="System.String"/> containing the arguments for the WinGet process.
720+
/// A <see cref="WGetNET.WinGetArguments"/> object containing the arguments for the WinGet process.
774721
/// </param>
775722
/// <param name="needsAdminRights">
776723
/// Sets if the process that should be executed needs administrator privileges.
@@ -785,7 +732,7 @@ private protected bool CheckWinGetVersion(Version minVersion, Version? maxVersio
785732
/// The current process does not have administrator privileges.
786733
/// (Only if <paramref name="needsAdminRights"/> is set to <see langword="true"/>)
787734
/// </exception>
788-
private protected ProcessResult Execute(string args, bool needsAdminRights = false)
735+
private protected ProcessResult Execute(WinGetArguments args, bool needsAdminRights = false)
789736
{
790737
ThrowIfNotInstalled();
791738

@@ -794,14 +741,14 @@ private protected ProcessResult Execute(string args, bool needsAdminRights = fal
794741
ThrowIfNotAdmin();
795742
}
796743

797-
return _processManager.ExecuteWingetProcess(args);
744+
return _processManager.ExecuteWingetProcess(args.Arguments);
798745
}
799746

800747
/// <summary>
801748
/// Asynchronously exectutes a WinGet action from the given cmd.
802749
/// </summary>
803750
/// <param name="args">
804-
/// A <see cref="System.String"/> containing the arguments for the WinGet process.
751+
/// A <see cref="WGetNET.WinGetArguments"/> object containing the arguments for the WinGet process.
805752
/// </param>
806753
/// <param name="needsAdminRights">
807754
/// Sets if the process that should be executed needs administrator privileges.
@@ -820,7 +767,7 @@ private protected ProcessResult Execute(string args, bool needsAdminRights = fal
820767
/// The current process does not have administrator privileges.
821768
/// (Only if <paramref name="needsAdminRights"/> is set to <see langword="true"/>)
822769
/// </exception>
823-
private protected async Task<ProcessResult> ExecuteAsync(string args, bool needsAdminRights = false, CancellationToken cancellationToken = default)
770+
private protected async Task<ProcessResult> ExecuteAsync(WinGetArguments args, bool needsAdminRights = false, CancellationToken cancellationToken = default)
824771
{
825772
ThrowIfNotInstalled();
826773

@@ -829,7 +776,7 @@ private protected async Task<ProcessResult> ExecuteAsync(string args, bool needs
829776
ThrowIfNotAdmin();
830777
}
831778

832-
return await _processManager.ExecuteWingetProcessAsync(args, cancellationToken);
779+
return await _processManager.ExecuteWingetProcessAsync(args.Arguments, cancellationToken);
833780
}
834781
// \endcond
835782
//---------------------------------------------------------------------------------------------
@@ -876,12 +823,7 @@ private string CheckWinGetVersion()
876823
return string.Empty;
877824
}
878825

879-
ProcessResult result =
880-
Execute(
881-
WinGetArguments
882-
.WinGet()
883-
.Version()
884-
.ToString());
826+
ProcessResult result = Execute(WinGetArguments.WinGet().Version());
885827

886828
for (int i = 0; i < result.Output.Length; i++)
887829
{

0 commit comments

Comments
 (0)