Skip to content

Commit 661a06e

Browse files
authored
Merge pull request #44 from basicx-StrgV/feature-winget-query-rework
Feature winget query rework into dev
2 parents 7c88205 + 7372090 commit 661a06e

8 files changed

Lines changed: 1368 additions & 451 deletions

File tree

src/WGet.NET/Components/WinGet.cs

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ namespace WGetNET
2222
/// </summary>
2323
public class WinGet
2424
{
25-
private const string _infoCmd = "--info";
26-
private const string _versionCmd = "--version";
27-
private const string _exportSettingsCmd = "settings export";
28-
private const string _settingsEnableCmd = "settings --enable \"{0}\"";
29-
private const string _settingsDisableCmd = "settings --disable \"{0}\"";
30-
3125
private ProcessManager _processManager;
3226
private string _wingetExePath;
3327
private DateTime _wingetExeModificationDate;
@@ -154,7 +148,7 @@ public WinGet()
154148
/// </exception>
155149
public string ExportSettings()
156150
{
157-
ProcessResult result = Execute(_exportSettingsCmd);
151+
ProcessResult result = Execute(WinGetArguments.SettingsExport());
158152

159153
return ProcessOutputReader.ExportOutputToString(result);
160154
}
@@ -174,7 +168,7 @@ public string ExportSettings()
174168
/// </exception>
175169
public async Task<string> ExportSettingsAsync(CancellationToken cancellationToken = default)
176170
{
177-
ProcessResult result = await ExecuteAsync(_exportSettingsCmd, false, cancellationToken);
171+
ProcessResult result = await ExecuteAsync(WinGetArguments.SettingsExport(), false, cancellationToken);
178172

179173
return ProcessOutputReader.ExportOutputToString(result);
180174
}
@@ -222,7 +216,7 @@ public void ExportSettingsToFile(string file)
222216
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(file, "file");
223217
ArgsHelper.ThrowIfPathIsInvalid(file);
224218

225-
ProcessResult result = Execute(_exportSettingsCmd);
219+
ProcessResult result = Execute(WinGetArguments.SettingsExport());
226220

227221
FileHelper.WriteTextToFile(file, ProcessOutputReader.ExportOutputToString(result));
228222
}
@@ -273,7 +267,7 @@ public async Task ExportSettingsToFileAsync(string file, CancellationToken cance
273267
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(file, "file");
274268
ArgsHelper.ThrowIfPathIsInvalid(file);
275269

276-
ProcessResult result = await ExecuteAsync(_exportSettingsCmd, false, cancellationToken);
270+
ProcessResult result = await ExecuteAsync(WinGetArguments.SettingsExport(), false, cancellationToken);
277271

278272
await FileHelper.WriteTextToFileAsync(file, ProcessOutputReader.ExportOutputToString(result), cancellationToken);
279273
}
@@ -396,9 +390,7 @@ public bool EnableAdminSetting(string settingName)
396390
{
397391
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(settingName, "settingName");
398392

399-
string cmd = string.Format(_settingsEnableCmd, settingName);
400-
401-
ProcessResult result = Execute(cmd, true);
393+
ProcessResult result = Execute(WinGetArguments.Settings().Enable(settingName), true);
402394

403395
return result.Success;
404396
}
@@ -460,9 +452,7 @@ public async Task<bool> EnableAdminSettingAsync(string settingName, Cancellation
460452
{
461453
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(settingName, "settingName");
462454

463-
string cmd = string.Format(_settingsEnableCmd, settingName);
464-
465-
ProcessResult result = await ExecuteAsync(cmd, true, cancellationToken);
455+
ProcessResult result = await ExecuteAsync(WinGetArguments.Settings().Enable(settingName), true, cancellationToken);
466456

467457
return result.Success;
468458
}
@@ -524,9 +514,7 @@ public bool DisableAdminSetting(string settingName)
524514
{
525515
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(settingName, "settingName");
526516

527-
string cmd = string.Format(_settingsDisableCmd, settingName);
528-
529-
ProcessResult result = Execute(cmd, true);
517+
ProcessResult result = Execute(WinGetArguments.Settings().Disable(settingName), true);
530518

531519
return result.Success;
532520
}
@@ -588,9 +576,7 @@ public async Task<bool> DisableAdminSettingAsync(string settingName, Cancellatio
588576
{
589577
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(settingName, "settingName");
590578

591-
string cmd = string.Format(_settingsDisableCmd, settingName);
592-
593-
ProcessResult result = await ExecuteAsync(cmd, true, cancellationToken);
579+
ProcessResult result = await ExecuteAsync(WinGetArguments.Settings().Disable(settingName), true, cancellationToken);
594580

595581
return result.Success;
596582
}
@@ -640,7 +626,7 @@ public async Task<bool> DisableAdminSettingAsync(WinGetAdminSetting setting, Can
640626
/// </exception>
641627
public WinGetInfo GetInfo()
642628
{
643-
ProcessResult result = Execute(_infoCmd);
629+
ProcessResult result = Execute(WinGetArguments.WinGet().Info());
644630

645631
InfoActionVersionId actionVersionId = InfoActionVersionId.VersionRange1;
646632
if (CheckWinGetVersion(new Version(1, 4, 3531), new Version(1, 5, 101)))
@@ -674,7 +660,7 @@ public WinGetInfo GetInfo()
674660
/// </exception>
675661
public async Task<WinGetInfo> GetInfoAsync(CancellationToken cancellationToken = default)
676662
{
677-
ProcessResult result = await ExecuteAsync(_infoCmd, false, cancellationToken);
663+
ProcessResult result = await ExecuteAsync(WinGetArguments.WinGet().Info(), false, cancellationToken);
678664

679665
// Check the version range the action should be performed for
680666
InfoActionVersionId actionVersionId = InfoActionVersionId.VersionRange1;
@@ -731,7 +717,7 @@ private protected bool CheckWinGetVersion(Version minVersion, Version? maxVersio
731717
/// Exectutes a WinGet action from the given cmd.
732718
/// </summary>
733719
/// <param name="args">
734-
/// 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.
735721
/// </param>
736722
/// <param name="needsAdminRights">
737723
/// Sets if the process that should be executed needs administrator privileges.
@@ -746,7 +732,7 @@ private protected bool CheckWinGetVersion(Version minVersion, Version? maxVersio
746732
/// The current process does not have administrator privileges.
747733
/// (Only if <paramref name="needsAdminRights"/> is set to <see langword="true"/>)
748734
/// </exception>
749-
private protected ProcessResult Execute(string args, bool needsAdminRights = false)
735+
private protected ProcessResult Execute(WinGetArguments args, bool needsAdminRights = false)
750736
{
751737
ThrowIfNotInstalled();
752738

@@ -755,14 +741,14 @@ private protected ProcessResult Execute(string args, bool needsAdminRights = fal
755741
ThrowIfNotAdmin();
756742
}
757743

758-
return _processManager.ExecuteWingetProcess(args);
744+
return _processManager.ExecuteWingetProcess(args.Arguments);
759745
}
760746

761747
/// <summary>
762748
/// Asynchronously exectutes a WinGet action from the given cmd.
763749
/// </summary>
764750
/// <param name="args">
765-
/// 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.
766752
/// </param>
767753
/// <param name="needsAdminRights">
768754
/// Sets if the process that should be executed needs administrator privileges.
@@ -781,7 +767,7 @@ private protected ProcessResult Execute(string args, bool needsAdminRights = fal
781767
/// The current process does not have administrator privileges.
782768
/// (Only if <paramref name="needsAdminRights"/> is set to <see langword="true"/>)
783769
/// </exception>
784-
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)
785771
{
786772
ThrowIfNotInstalled();
787773

@@ -790,7 +776,7 @@ private protected async Task<ProcessResult> ExecuteAsync(string args, bool needs
790776
ThrowIfNotAdmin();
791777
}
792778

793-
return await _processManager.ExecuteWingetProcessAsync(args, cancellationToken);
779+
return await _processManager.ExecuteWingetProcessAsync(args.Arguments, cancellationToken);
794780
}
795781
// \endcond
796782
//---------------------------------------------------------------------------------------------
@@ -837,7 +823,7 @@ private string CheckWinGetVersion()
837823
return string.Empty;
838824
}
839825

840-
ProcessResult result = Execute(_versionCmd);
826+
ProcessResult result = Execute(WinGetArguments.WinGet().Version());
841827

842828
for (int i = 0; i < result.Output.Length; i++)
843829
{

0 commit comments

Comments
 (0)