Skip to content

Commit a47ebb6

Browse files
committed
Moved WinGet base class cmd's to the WinGetArguments class
1 parent 1e2a3d2 commit a47ebb6

3 files changed

Lines changed: 246 additions & 27 deletions

File tree

src/WGet.NET/Components/WinGet.cs

Lines changed: 69 additions & 25 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,11 @@ public WinGet()
154148
/// </exception>
155149
public string ExportSettings()
156150
{
157-
ProcessResult result = Execute(_exportSettingsCmd);
151+
ProcessResult result =
152+
Execute(
153+
WinGetArguments
154+
.SettingsExport()
155+
.ToString());
158156

159157
return ProcessOutputReader.ExportOutputToString(result);
160158
}
@@ -174,7 +172,12 @@ public string ExportSettings()
174172
/// </exception>
175173
public async Task<string> ExportSettingsAsync(CancellationToken cancellationToken = default)
176174
{
177-
ProcessResult result = await ExecuteAsync(_exportSettingsCmd, false, cancellationToken);
175+
ProcessResult result =
176+
await ExecuteAsync(
177+
WinGetArguments
178+
.SettingsExport()
179+
.ToString(),
180+
false, cancellationToken);
178181

179182
return ProcessOutputReader.ExportOutputToString(result);
180183
}
@@ -222,7 +225,11 @@ public void ExportSettingsToFile(string file)
222225
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(file, "file");
223226
ArgsHelper.ThrowIfPathIsInvalid(file);
224227

225-
ProcessResult result = Execute(_exportSettingsCmd);
228+
ProcessResult result =
229+
Execute(
230+
WinGetArguments
231+
.SettingsExport()
232+
.ToString());
226233

227234
FileHelper.WriteTextToFile(file, ProcessOutputReader.ExportOutputToString(result));
228235
}
@@ -273,7 +280,12 @@ public async Task ExportSettingsToFileAsync(string file, CancellationToken cance
273280
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(file, "file");
274281
ArgsHelper.ThrowIfPathIsInvalid(file);
275282

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

278290
await FileHelper.WriteTextToFileAsync(file, ProcessOutputReader.ExportOutputToString(result), cancellationToken);
279291
}
@@ -396,9 +408,13 @@ public bool EnableAdminSetting(string settingName)
396408
{
397409
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(settingName, "settingName");
398410

399-
string cmd = string.Format(_settingsEnableCmd, settingName);
400-
401-
ProcessResult result = Execute(cmd, true);
411+
ProcessResult result =
412+
Execute(
413+
WinGetArguments
414+
.Settings()
415+
.Enable(settingName)
416+
.ToString(),
417+
true);
402418

403419
return result.Success;
404420
}
@@ -460,9 +476,13 @@ public async Task<bool> EnableAdminSettingAsync(string settingName, Cancellation
460476
{
461477
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(settingName, "settingName");
462478

463-
string cmd = string.Format(_settingsEnableCmd, settingName);
464-
465-
ProcessResult result = await ExecuteAsync(cmd, true, cancellationToken);
479+
ProcessResult result =
480+
await ExecuteAsync(
481+
WinGetArguments
482+
.Settings()
483+
.Enable(settingName)
484+
.ToString(),
485+
true, cancellationToken);
466486

467487
return result.Success;
468488
}
@@ -524,9 +544,13 @@ public bool DisableAdminSetting(string settingName)
524544
{
525545
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(settingName, "settingName");
526546

527-
string cmd = string.Format(_settingsDisableCmd, settingName);
528-
529-
ProcessResult result = Execute(cmd, true);
547+
ProcessResult result =
548+
Execute(
549+
WinGetArguments
550+
.Settings()
551+
.Disable(settingName)
552+
.ToString(),
553+
true);
530554

531555
return result.Success;
532556
}
@@ -588,9 +612,13 @@ public async Task<bool> DisableAdminSettingAsync(string settingName, Cancellatio
588612
{
589613
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(settingName, "settingName");
590614

591-
string cmd = string.Format(_settingsDisableCmd, settingName);
592-
593-
ProcessResult result = await ExecuteAsync(cmd, true, cancellationToken);
615+
ProcessResult result =
616+
await ExecuteAsync(
617+
WinGetArguments
618+
.Settings()
619+
.Disable(settingName)
620+
.ToString(),
621+
true, cancellationToken);
594622

595623
return result.Success;
596624
}
@@ -640,7 +668,12 @@ public async Task<bool> DisableAdminSettingAsync(WinGetAdminSetting setting, Can
640668
/// </exception>
641669
public WinGetInfo GetInfo()
642670
{
643-
ProcessResult result = Execute(_infoCmd);
671+
ProcessResult result =
672+
Execute(
673+
WinGetArguments
674+
.WinGet()
675+
.Info()
676+
.ToString());
644677

645678
InfoActionVersionId actionVersionId = InfoActionVersionId.VersionRange1;
646679
if (CheckWinGetVersion(new Version(1, 4, 3531), new Version(1, 5, 101)))
@@ -674,7 +707,13 @@ public WinGetInfo GetInfo()
674707
/// </exception>
675708
public async Task<WinGetInfo> GetInfoAsync(CancellationToken cancellationToken = default)
676709
{
677-
ProcessResult result = await ExecuteAsync(_infoCmd, false, cancellationToken);
710+
ProcessResult result =
711+
await ExecuteAsync(
712+
WinGetArguments
713+
.WinGet()
714+
.Info()
715+
.ToString(),
716+
false, cancellationToken);
678717

679718
// Check the version range the action should be performed for
680719
InfoActionVersionId actionVersionId = InfoActionVersionId.VersionRange1;
@@ -837,7 +876,12 @@ private string CheckWinGetVersion()
837876
return string.Empty;
838877
}
839878

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

842886
for (int i = 0; i < result.Output.Length; i++)
843887
{

src/WGet.NET/Data/WinGetArguments.cs

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ internal class WinGetArguments : IWinGetObject
1111
{
1212
internal enum WinGetAction
1313
{
14+
None,
15+
Settings,
16+
SettingsExport,
1417
List,
1518
Search,
1619
Install,
@@ -34,7 +37,7 @@ public string Arguments
3437
{
3538
get
3639
{
37-
return _arguments;
40+
return _arguments.Trim();
3841
}
3942
}
4043

@@ -67,6 +70,40 @@ internal WinGetArguments(string baseCmd, WinGetAction action)
6770
}
6871

6972
//---Base Cmd's--------------------------------------------------------------------------------
73+
/// <summary>
74+
/// Creates a new winget arguments object with no base cmd.
75+
/// Used for direct callin of the winget cmd with flags.
76+
/// </summary>
77+
/// <returns>
78+
/// The created <see cref="WGetNET.WinGetArguments"/> object.
79+
/// </returns>
80+
public static WinGetArguments WinGet()
81+
{
82+
return new WinGetArguments("", WinGetAction.None);
83+
}
84+
85+
/// <summary>
86+
/// Creates a new winget arguments object with "settings" as the base cmd.
87+
/// </summary>
88+
/// <returns>
89+
/// The created <see cref="WGetNET.WinGetArguments"/> object.
90+
/// </returns>
91+
public static WinGetArguments Settings()
92+
{
93+
return new WinGetArguments("settings", WinGetAction.Settings);
94+
}
95+
96+
/// <summary>
97+
/// Creates a new winget arguments object with "settings export" as the base cmd.
98+
/// </summary>
99+
/// <returns>
100+
/// The created <see cref="WGetNET.WinGetArguments"/> object.
101+
/// </returns>
102+
public static WinGetArguments SettingsExport()
103+
{
104+
return new WinGetArguments("settings export", WinGetAction.SettingsExport);
105+
}
106+
70107
/// <summary>
71108
/// Creates a new winget arguments object with "list" as the base cmd.
72109
/// </summary>
@@ -321,6 +358,36 @@ public WinGetArguments Directory(string directory)
321358
return this;
322359
}
323360

361+
/// <summary>
362+
/// Adds a enable action to the arguments.
363+
/// </summary>
364+
/// <param name="query">
365+
/// A <see cref="System.String"/> containing the option to enable.
366+
/// </param>
367+
/// <returns>
368+
/// The updated <see cref="WGetNET.WinGetArguments"/> object.
369+
/// </returns>
370+
public WinGetArguments Enable(string query)
371+
{
372+
_arguments += $" --enable \"{query}\"";
373+
return this;
374+
}
375+
376+
/// <summary>
377+
/// Adds a disable action to the arguments.
378+
/// </summary>
379+
/// <param name="query">
380+
/// A <see cref="System.String"/> containing the option to disable.
381+
/// </param>
382+
/// <returns>
383+
/// The updated <see cref="WGetNET.WinGetArguments"/> object.
384+
/// </returns>
385+
public WinGetArguments Disable(string query)
386+
{
387+
_arguments += $" --disable \"{query}\"";
388+
return this;
389+
}
390+
324391
/// <summary>
325392
/// Adds a version query to the arguments.
326393
/// </summary>
@@ -336,6 +403,18 @@ public WinGetArguments Version(string version)
336403
return this;
337404
}
338405

406+
/// <summary>
407+
/// Adds a version query to the arguments.
408+
/// </summary>
409+
/// <returns>
410+
/// The updated <see cref="WGetNET.WinGetArguments"/> object.
411+
/// </returns>
412+
public WinGetArguments Version()
413+
{
414+
_arguments += $" --version";
415+
return this;
416+
}
417+
339418
/// <summary>
340419
/// Adds the '--exact' flag to the arguments.
341420
/// </summary>
@@ -456,11 +535,23 @@ public WinGetArguments Force()
456535
return this;
457536
}
458537

538+
/// <summary>
539+
/// Adds the '--info' flag to the arguments.
540+
/// </summary>
541+
/// <returns>
542+
/// The updated <see cref="WGetNET.WinGetArguments"/> object.
543+
/// </returns>
544+
public WinGetArguments Info()
545+
{
546+
_arguments += " --info";
547+
return this;
548+
}
549+
459550
//---Others------------------------------------------------------------------------------------
460551
/// <inheritdoc/>
461552
public override string ToString()
462553
{
463-
return _arguments;
554+
return _arguments.Trim();
464555
}
465556
}
466557
}

0 commit comments

Comments
 (0)