Skip to content

Commit d718d94

Browse files
committed
Changed the ProcessOutputReader to a non statisc class with non static methods and moved it to the WGetNET.Components.Internal namespace; Removed redundant methods from the FileHelper class
1 parent 0420a24 commit d718d94

6 files changed

Lines changed: 311 additions & 447 deletions

File tree

src/WGet.NET/Helper/ProcessOutputReader.cs renamed to src/WGet.NET/Components/Internal/ProcessOutputReader.cs

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,26 @@
55
using System.Text;
66
using System.Collections.Generic;
77
using WGetNET.Models;
8+
using WGetNET.Helper;
89
using WGetNET.Builder;
910
using WGetNET.Extensions;
1011

11-
namespace WGetNET.Helper
12+
namespace WGetNET.Components.Internal
1213
{
1314
/// <summary>
14-
/// The <see langword="static"/> <see cref="WGetNET.Helper.ProcessOutputReader"/> class,
15-
/// provieds <see langword="static"/> methodes to process the output of the winget processes.
15+
/// The <see cref="WGetNET.Components.Internal.ProcessOutputReader"/> class,
16+
/// provieds methodes to read data from the output of the winget processes.
1617
/// </summary>
17-
internal static class ProcessOutputReader
18+
internal class ProcessOutputReader
1819
{
20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="WGetNET.Components.Internal.ProcessOutputReader"/> class.
22+
/// </summary>
23+
public ProcessOutputReader()
24+
{
25+
// Provide empty constructor
26+
}
27+
1928
//---To Package List------------------------------------------------------------------------------------------
2029
/// <summary>
2130
/// Converts a <see cref="System.Collections.Generic.List{T}"/>
@@ -33,7 +42,7 @@ internal static class ProcessOutputReader
3342
/// <returns>
3443
/// A <see cref="System.Collections.Generic.List{T}"/> of <see cref="WGetNET.WinGetPackage"/>'s.
3544
/// </returns>
36-
public static List<WinGetPackage> ToPackageList(string[] output, PackageAction action = PackageAction.Default, string? sourceName = null)
45+
public List<WinGetPackage> ToPackageList(string[] output, PackageAction action = PackageAction.Default, string? sourceName = null)
3746
{
3847
//Get top line index.
3948
//The array should always contain this line.
@@ -73,7 +82,7 @@ public static List<WinGetPackage> ToPackageList(string[] output, PackageAction a
7382
/// <returns>
7483
/// A <see cref="System.Collections.Generic.List{T}"/> of <see cref="WGetNET.WinGetPackage"/>'s.
7584
/// </returns>
76-
private static List<WinGetPackage> CreatePackageListFromOutput(string[] output, int[] columnList, PackageAction action = PackageAction.Default, string? sourceName = null)
85+
private List<WinGetPackage> CreatePackageListFromOutput(string[] output, int[] columnList, PackageAction action = PackageAction.Default, string? sourceName = null)
7786
{
7887
List<WinGetPackage> resultList = new();
7988

@@ -195,7 +204,7 @@ private static List<WinGetPackage> CreatePackageListFromOutput(string[] output,
195204
/// <returns>
196205
/// A <see cref="System.Collections.Generic.List{T}"/> of <see cref="WGetNET.WinGetPinnedPackage"/>'s.
197206
/// </returns>
198-
public static List<WinGetPinnedPackage> ToPinnedPackageList(string[] output)
207+
public List<WinGetPinnedPackage> ToPinnedPackageList(string[] output)
199208
{
200209
//Get top line index.
201210
//The array should always contain this line.
@@ -229,7 +238,7 @@ public static List<WinGetPinnedPackage> ToPinnedPackageList(string[] output)
229238
/// <returns>
230239
/// A <see cref="System.Collections.Generic.List{T}"/> of <see cref="WGetNET.WinGetPinnedPackage"/>'s.
231240
/// </returns>
232-
private static List<WinGetPinnedPackage> CreatePinnedPackageListFromOutput(string[] output, int[] columnList)
241+
private List<WinGetPinnedPackage> CreatePinnedPackageListFromOutput(string[] output, int[] columnList)
233242
{
234243
List<WinGetPinnedPackage> resultList = new();
235244

@@ -335,7 +344,7 @@ private static List<WinGetPinnedPackage> CreatePinnedPackageListFromOutput(strin
335344
/// <returns>
336345
/// A <see cref="System.Collections.Generic.List{T}"/> of <see cref="WGetNET.WinGetSource"/>'s.
337346
/// </returns>
338-
public static List<WinGetSource> ToSourceList(string[] output)
347+
public List<WinGetSource> ToSourceList(string[] output)
339348
{
340349
List<WinGetSource> sourceList = new();
341350

@@ -363,7 +372,7 @@ public static List<WinGetSource> ToSourceList(string[] output)
363372
/// <returns>
364373
/// The <see cref="WGetNET.WinGetInfo"/> object created from the output.
365374
/// </returns>
366-
public static WinGetInfo ToWingetInfo(string[] output, InfoActionVersionId actionVersionId)
375+
public WinGetInfo ToWingetInfo(string[] output, InfoActionVersionId actionVersionId)
367376
{
368377
if (output.Length <= 0)
369378
{
@@ -378,7 +387,7 @@ public static WinGetInfo ToWingetInfo(string[] output, InfoActionVersionId actio
378387
/// </summary>
379388
/// <param name="output"></param>
380389
/// <returns></returns>
381-
private static string ReadVersionFromData(string[] output)
390+
private string ReadVersionFromData(string[] output)
382391
{
383392
string version = string.Empty;
384393

@@ -411,7 +420,7 @@ private static string ReadVersionFromData(string[] output)
411420
/// <returns>
412421
/// The <see cref="WGetNET.WinGetInfo"/> object created from the output.
413422
/// </returns>
414-
private static WinGetInfo ReadDataByRange(string[] output, InfoActionVersionId actionVersionId)
423+
private WinGetInfo ReadDataByRange(string[] output, InfoActionVersionId actionVersionId)
415424
{
416425
return actionVersionId switch
417426
{
@@ -430,7 +439,7 @@ private static WinGetInfo ReadDataByRange(string[] output, InfoActionVersionId a
430439
/// <returns>
431440
/// The <see cref="WGetNET.WinGetInfo"/> object created from the output.
432441
/// </returns>
433-
private static WinGetInfo ReadDataForRange1(string[] output)
442+
private WinGetInfo ReadDataForRange1(string[] output)
434443
{
435444
InfoSetBuilder builder = new();
436445

@@ -462,7 +471,7 @@ private static WinGetInfo ReadDataForRange1(string[] output)
462471
/// <returns>
463472
/// The <see cref="WGetNET.WinGetInfo"/> object created from the output.
464473
/// </returns>
465-
private static WinGetInfo ReadDataForRange2(string[] output)
474+
private WinGetInfo ReadDataForRange2(string[] output)
466475
{
467476
InfoSetBuilder builder = new();
468477

@@ -497,7 +506,7 @@ private static WinGetInfo ReadDataForRange2(string[] output)
497506
/// <returns>
498507
/// The <see cref="WGetNET.WinGetInfo"/> object created from the output.
499508
/// </returns>
500-
private static WinGetInfo ReadDataForRange3(string[] output)
509+
private WinGetInfo ReadDataForRange3(string[] output)
501510
{
502511
InfoSetBuilder builder = new();
503512

@@ -537,7 +546,7 @@ private static WinGetInfo ReadDataForRange3(string[] output)
537546
/// <returns>
538547
/// The <see cref="WGetNET.WinGetInfo"/> object created from the output.
539548
/// </returns>
540-
private static WinGetInfo ReadDataForRange4(string[] output)
549+
private WinGetInfo ReadDataForRange4(string[] output)
541550
{
542551
InfoSetBuilder builder = new();
543552

@@ -577,7 +586,7 @@ private static WinGetInfo ReadDataForRange4(string[] output)
577586
/// <returns>
578587
/// A <see cref="WinGetDirectory"/> instance if the action was successful, or null if it failed.
579588
/// </returns>
580-
private static WinGetDirectory? ReadSingleDirectoryEntry(string[] output, int index)
589+
private WinGetDirectory? ReadSingleDirectoryEntry(string[] output, int index)
581590
{
582591
string[] entry = output[index].Split(':');
583592
if (entry.Length == 2)
@@ -602,7 +611,7 @@ private static WinGetInfo ReadDataForRange4(string[] output)
602611
/// <returns>
603612
/// A <see cref="System.Collections.Generic.List{T}"/> of <see cref="WGetNET.WinGetDirectory"/> objects.
604613
/// </returns>
605-
private static List<WinGetDirectory> ReadDirectories(string[] output)
614+
private List<WinGetDirectory> ReadDirectories(string[] output)
606615
{
607616
List<WinGetDirectory> directories = new();
608617

@@ -689,7 +698,7 @@ private static List<WinGetDirectory> ReadDirectories(string[] output)
689698
/// <returns>
690699
/// A <see cref="System.Collections.Generic.List{T}"/> of <see cref="WGetNET.WinGetLink"/> objects.
691700
/// </returns>
692-
private static List<WinGetLink> ReadLinks(string[] output)
701+
private List<WinGetLink> ReadLinks(string[] output)
693702
{
694703
List<WinGetLink> links = new();
695704

@@ -750,7 +759,7 @@ private static List<WinGetLink> ReadLinks(string[] output)
750759
/// <returns>
751760
/// A <see cref="System.Collections.Generic.List{T}"/> of <see cref="WGetNET.WinGetAdminOption"/> objects.
752761
/// </returns>
753-
private static List<WinGetAdminOption> ReadAdminSettings(string[] output)
762+
private List<WinGetAdminOption> ReadAdminSettings(string[] output)
754763
{
755764
List<WinGetAdminOption> adminSetting = new();
756765

@@ -794,7 +803,7 @@ private static List<WinGetAdminOption> ReadAdminSettings(string[] output)
794803
/// <returns>
795804
/// A <see cref="System.String"/> containing the hash value.
796805
/// </returns>
797-
public static string ResultToHash(ProcessResult result)
806+
public string ResultToHash(ProcessResult result)
798807
{
799808
string hash = "";
800809
if (result.Output.Length > 0 && result.Output[0].Contains(":"))
@@ -820,7 +829,7 @@ public static string ResultToHash(ProcessResult result)
820829
/// <returns>
821830
/// The <see cref="System.String"/> containing the export result.
822831
/// </returns>
823-
public static string ExportOutputToString(ProcessResult result)
832+
public string ExportOutputToString(ProcessResult result)
824833
{
825834
if (result.Success)
826835
{
@@ -850,7 +859,7 @@ public static string ExportOutputToString(ProcessResult result)
850859
/// <returns>
851860
/// A <see cref="System.Int32"/> <see langword="array"/> containing the column start indexes.
852861
/// </returns>
853-
private static int[] GetColumnList(string line, bool isPinnedPackageTable = false)
862+
private int[] GetColumnList(string line, bool isPinnedPackageTable = false)
854863
{
855864
int[] columns = new int[0];
856865

‎src/WGet.NET/Components/WinGet.cs‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ namespace WGetNET
2020
/// </summary>
2121
public class WinGet
2222
{
23+
/// <summary>
24+
/// Gets the current output reader instance.
25+
/// </summary>
26+
private protected ProcessOutputReader OutputReader
27+
{
28+
get
29+
{
30+
return _outputReader;
31+
}
32+
}
33+
2334
private const string _infoCmd = "--info";
2435
private const string _versionCmd = "--version";
2536
private const string _exportSettingsCmd = "settings export";
@@ -31,6 +42,7 @@ public class WinGet
3142
private Version _version;
3243

3344
private readonly bool _administratorPrivileges;
45+
private readonly ProcessOutputReader _outputReader;
3446

3547
/// <summary>
3648
/// Gets if winget is installed on the system.
@@ -103,6 +115,8 @@ public Version Version
103115
/// </summary>
104116
public WinGet()
105117
{
118+
_outputReader = new ProcessOutputReader();
119+
106120
// Check if the current process has administrator privileges
107121
_administratorPrivileges = CheckAdministratorPrivileges();
108122

@@ -129,7 +143,7 @@ public string ExportSettings()
129143
{
130144
ProcessResult result = Execute(_exportSettingsCmd);
131145

132-
return ProcessOutputReader.ExportOutputToString(result);
146+
return _outputReader.ExportOutputToString(result);
133147
}
134148

135149
/// <summary>
@@ -146,7 +160,7 @@ public async Task<string> ExportSettingsAsync()
146160
{
147161
ProcessResult result = await ExecuteAsync(_exportSettingsCmd);
148162

149-
return ProcessOutputReader.ExportOutputToString(result);
163+
return _outputReader.ExportOutputToString(result);
150164
}
151165

152166
/// <summary>
@@ -194,7 +208,7 @@ public void ExportSettingsToFile(string file)
194208

195209
ProcessResult result = Execute(_exportSettingsCmd);
196210

197-
FileHelper.ExportOutputToFile(file, result);
211+
FileHelper.WriteTextToFile(file, OutputReader.ExportOutputToString(result));
198212
}
199213

200214
/// <summary>
@@ -242,7 +256,7 @@ public async Task ExportSettingsToFileAsync(string file)
242256

243257
ProcessResult result = await ExecuteAsync(_exportSettingsCmd);
244258

245-
await FileHelper.ExportOutputToFileAsync(file, result);
259+
await FileHelper.WriteTextToFileAsync(file, OutputReader.ExportOutputToString(result));
246260
}
247261

248262
/// <summary>
@@ -272,7 +286,7 @@ public WinGetInfo GetInfo()
272286
actionVersionId = InfoActionVersionId.VersionRange4;
273287
}
274288

275-
return ProcessOutputReader.ToWingetInfo(result.Output, actionVersionId);
289+
return _outputReader.ToWingetInfo(result.Output, actionVersionId);
276290
}
277291

278292
/// <summary>
@@ -303,7 +317,7 @@ public async Task<WinGetInfo> GetInfoAsync()
303317
actionVersionId = InfoActionVersionId.VersionRange4;
304318
}
305319

306-
return ProcessOutputReader.ToWingetInfo(result.Output, actionVersionId);
320+
return _outputReader.ToWingetInfo(result.Output, actionVersionId);
307321
}
308322

309323
/// <summary>
@@ -447,7 +461,7 @@ private string CheckWinGetVersion()
447461
#elif NETSTANDARD2_0
448462
if (result.Output[i].StartsWith("v") && result.Output[i].Length >= 2)
449463
{
450-
// Return output without the 'v' at the start.
464+
// Return output without the 'v' at the start.
451465
return result.Output[i].Trim().Substring(1);
452466
}
453467
#endif

0 commit comments

Comments
 (0)