Skip to content

Commit 71758e7

Browse files
committed
Added asynchronous execution to the WinGetInfo and FileHandler class
1 parent ad29dae commit 71758e7

4 files changed

Lines changed: 155 additions & 4 deletions

File tree

src/WGet.NET/Components/WinGetInfo.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//--------------------------------------------------//
55
using System;
66
using System.ComponentModel;
7+
using System.Threading.Tasks;
78
using WGetNET.HelperClasses;
89

910
namespace WGetNET
@@ -90,6 +91,38 @@ public string ExportSettings()
9091
}
9192
}
9293

94+
/// <summary>
95+
/// Asynchronous exports the WinGet settings to a json string.
96+
/// </summary>
97+
/// <returns>
98+
/// A <see cref="System.String"/> containing the settings json.
99+
/// </returns>
100+
/// <exception cref="WGetNET.WinGetNotInstalledException">
101+
/// WinGet is not installed or not found on the system.
102+
/// </exception>
103+
/// <exception cref="WGetNET.WinGetActionFailedException">
104+
/// The current action failed for an unexpected reason.
105+
/// Please see inner exception.
106+
/// </exception>
107+
public async Task<string> ExportSettingsAsync()
108+
{
109+
try
110+
{
111+
ProcessResult result =
112+
await _processManager.ExecuteWingetProcessAsync(_exportSettingsCmd);
113+
114+
return ProcessOutputReader.ExportOutputToString(result);
115+
}
116+
catch (Win32Exception)
117+
{
118+
throw new WinGetNotInstalledException();
119+
}
120+
catch (Exception e)
121+
{
122+
throw new WinGetActionFailedException("Exporting sources failed.", e);
123+
}
124+
}
125+
93126
/// <summary>
94127
/// Exports the WinGet settings to a json and writes them to the given file.
95128
/// </summary>
@@ -130,6 +163,46 @@ public bool ExportSettingsToFile(string file)
130163
}
131164
}
132165

166+
/// <summary>
167+
/// Asynchronous exports the WinGet settings to a json and writes them to the given file.
168+
/// </summary>
169+
/// <param name="file">
170+
/// The file for the export.
171+
/// </param>
172+
/// <returns>
173+
/// <see langword="true"/> if the action was succesfull, and <see langword="false"/> if it failed.
174+
/// </returns>
175+
/// <exception cref="WGetNET.WinGetNotInstalledException">
176+
/// WinGet is not installed or not found on the system.
177+
/// </exception>
178+
/// <exception cref="WGetNET.WinGetActionFailedException">
179+
/// The current action failed for an unexpected reason.
180+
/// Please see inner exception.
181+
/// </exception>
182+
public async Task<bool> ExportSettingsToFileAsync(string file)
183+
{
184+
if (string.IsNullOrWhiteSpace(file))
185+
{
186+
return false;
187+
}
188+
189+
try
190+
{
191+
ProcessResult result =
192+
await _processManager.ExecuteWingetProcessAsync(_exportSettingsCmd);
193+
194+
return await FileHandler.ExportOutputToFileAsync(result, file);
195+
}
196+
catch (Win32Exception)
197+
{
198+
throw new WinGetNotInstalledException();
199+
}
200+
catch (Exception e)
201+
{
202+
throw new WinGetActionFailedException("Exporting sources failed.", e);
203+
}
204+
}
205+
133206
private string CheckWinGetVersion()
134207
{
135208
try

src/WGet.NET/HelperClasses/FileHandler.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// https://github.com/basicx-StrgV/ //
44
//--------------------------------------------------//
55
using System.IO;
6+
using System.Threading.Tasks;
67

78
namespace WGetNET.HelperClasses
89
{
@@ -26,9 +27,36 @@ public static bool ExportOutputToFile(ProcessResult result, string file)
2627
{
2728
string outputString = ProcessOutputReader.ExportOutputToString(result);
2829

29-
File.WriteAllText(
30-
file,
31-
outputString);
30+
File.WriteAllText(file, outputString);
31+
32+
return true;
33+
}
34+
else
35+
{
36+
return false;
37+
}
38+
}
39+
40+
/// <summary>
41+
/// Asynchronous writes the export result to a file.
42+
/// </summary>
43+
/// <param name="result">
44+
/// The <see cref="WGetNET.ProcessResult"/> object containing the export data.
45+
/// </param>
46+
/// <param name="file">
47+
/// A <see cref="System.String"/> containing the file path and name.
48+
/// </param>
49+
/// <returns>
50+
/// <see langword="true"/> if the action was successfull and <see langword="false"/> if it failed.
51+
/// </returns>
52+
public static async Task<bool> ExportOutputToFileAsync(ProcessResult result, string file)
53+
{
54+
if (result.Success)
55+
{
56+
string outputString = ProcessOutputReader.ExportOutputToString(result);
57+
58+
await File.WriteAllTextAsync(file, outputString);
59+
3260
return true;
3361
}
3462
else

src/WGet.NET/XmlDocumentation/WGet.NET.xml

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/WGetTest/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading.Tasks;
23
using System.Collections.Generic;
34
using WGetNET;
45

@@ -54,7 +55,9 @@ private void Run()
5455
string hash = connector.Hash("C:\\Test\\HashTest.txt");
5556
Console.WriteLine(hash);
5657

57-
string settings = connector.ExportSettings();
58+
Task<string> settingsTask = connector.ExportSettingsAsync();
59+
settingsTask.Wait();
60+
string settings = settingsTask.Result;
5861
bool settingExportStatus = connector.ExportSettingsToFile("C:\\Test\\Settings.json");
5962
}
6063
catch (Exception e)

0 commit comments

Comments
 (0)