Skip to content

Commit dbccb19

Browse files
committed
Added the function to get the data from the winget info action, with support for multiple versions of winget #25; Fixed a problem with the version check function
1 parent adc4784 commit dbccb19

8 files changed

Lines changed: 999 additions & 82 deletions

File tree

src/WGet.NET/Components/WinGetInfo.cs

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace WGetNET
1414
/// </summary>
1515
public class WinGetInfo
1616
{
17+
private const string _infoCmd = "--info";
1718
private const string _versionCmd = "--version";
1819
private const string _exportSettingsCmd = "settings export";
1920

@@ -219,6 +220,98 @@ public async Task<bool> ExportSettingsToFileAsync(string file)
219220
}
220221
}
221222

223+
/// <summary>
224+
/// Gets all WinGet related data provided by the WinGet info action.
225+
/// </summary>
226+
/// <returns>
227+
/// A <see cref="WGetNET.WinGetData"/> containing winget related information.
228+
/// </returns>
229+
/// <exception cref="WGetNET.WinGetNotInstalledException">
230+
/// WinGet is not installed or not found on the system.
231+
/// </exception>
232+
/// <exception cref="WGetNET.WinGetActionFailedException">
233+
/// The current action failed for an unexpected reason.
234+
/// Please see inner exception.
235+
/// </exception>
236+
public WinGetData GetWinGetData()
237+
{
238+
try
239+
{
240+
ProcessResult result =
241+
_processManager.ExecuteWingetProcess(_infoCmd);
242+
243+
InfoActionVersionId actionVersionId = InfoActionVersionId.VersionRange1;
244+
if (CheckWinGetVersion(new Version(1, 4, 3531), new Version(1, 5, 101)))
245+
{
246+
actionVersionId = InfoActionVersionId.VersionRange2;
247+
}
248+
else if (CheckWinGetVersion(new Version(1, 5, 441), new Version(1, 5, 441)))
249+
{
250+
actionVersionId= InfoActionVersionId.VersionRange3;
251+
}
252+
else if (CheckWinGetVersion(new Version(1, 5, 1081)))
253+
{
254+
actionVersionId = InfoActionVersionId.VersionRange4;
255+
}
256+
257+
return ProcessOutputReader.ToWingetData(result.Output, actionVersionId);
258+
}
259+
catch (Win32Exception)
260+
{
261+
throw new WinGetNotInstalledException();
262+
}
263+
catch (Exception e)
264+
{
265+
throw new WinGetActionFailedException("Getting data failed.", e);
266+
}
267+
}
268+
269+
/// <summary>
270+
/// Asynchronous gets all WinGet related data provided by the WinGet info action.
271+
/// </summary>
272+
/// <returns>
273+
/// A <see cref="WGetNET.WinGetData"/> containing winget related information.
274+
/// </returns>
275+
/// <exception cref="WGetNET.WinGetNotInstalledException">
276+
/// WinGet is not installed or not found on the system.
277+
/// </exception>
278+
/// <exception cref="WGetNET.WinGetActionFailedException">
279+
/// The current action failed for an unexpected reason.
280+
/// Please see inner exception.
281+
/// </exception>
282+
public async Task<WinGetData> GetWinGetDataAsync()
283+
{
284+
try
285+
{
286+
ProcessResult result =
287+
await _processManager.ExecuteWingetProcessAsync(_infoCmd);
288+
289+
InfoActionVersionId actionVersionId = InfoActionVersionId.VersionRange1;
290+
if (CheckWinGetVersion(new Version(1, 4, 3531), new Version(1, 5, 101)))
291+
{
292+
actionVersionId = InfoActionVersionId.VersionRange2;
293+
}
294+
else if (CheckWinGetVersion(new Version(1, 5, 441), new Version(1, 5, 441)))
295+
{
296+
actionVersionId = InfoActionVersionId.VersionRange3;
297+
}
298+
else if (CheckWinGetVersion(new Version(1, 5, 1081)))
299+
{
300+
actionVersionId = InfoActionVersionId.VersionRange4;
301+
}
302+
303+
return ProcessOutputReader.ToWingetData(result.Output, actionVersionId);
304+
}
305+
catch (Win32Exception)
306+
{
307+
throw new WinGetNotInstalledException();
308+
}
309+
catch (Exception e)
310+
{
311+
throw new WinGetActionFailedException("Getting data failed.", e);
312+
}
313+
}
314+
222315
/// <summary>
223316
/// Checks if the installed WinGet version is between the given versions or the same.
224317
/// </summary>
@@ -233,8 +326,10 @@ public async Task<bool> ExportSettingsToFileAsync(string file)
233326
protected bool CheckWinGetVersion(Version minVersion, Version? maxVersion = null)
234327
{
235328
Version winGetVersion = WinGetVersionObject;
236-
if ((winGetVersion.Major >= minVersion.Major && winGetVersion.Minor >= minVersion.Minor && winGetVersion.Build >= minVersion.Build) &&
237-
((maxVersion == null) || (winGetVersion.Major <= maxVersion.Major && winGetVersion.Minor <= maxVersion.Minor && winGetVersion.Build <= maxVersion.Build)))
329+
if ((winGetVersion.Major >= minVersion.Major && winGetVersion.Minor >= minVersion.Minor &&
330+
((winGetVersion.Minor != minVersion.Minor) || (winGetVersion.Minor == minVersion.Minor && winGetVersion.Build >= minVersion.Build))) &&
331+
((maxVersion == null) || (winGetVersion.Major <= maxVersion.Major && winGetVersion.Minor <= maxVersion.Minor &&
332+
((winGetVersion.Minor != maxVersion.Minor) || (winGetVersion.Minor == maxVersion.Minor && winGetVersion.Build <= maxVersion.Build)))))
238333
{
239334
return true;
240335
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//--------------------------------------------------//
2+
// Created by basicx-StrgV //
3+
// https://github.com/basicx-StrgV/ //
4+
//--------------------------------------------------//
5+
namespace WGetNET
6+
{
7+
/// <summary>
8+
/// Defines identifiers for all version ranges that generate a different output when using "winget --info".
9+
/// </summary>
10+
internal enum InfoActionVersionId
11+
{
12+
/// <summary>
13+
/// First WinGet version to version 1.4.3132
14+
/// </summary>
15+
VersionRange1,
16+
/// <summary>
17+
/// Version 1.4.3531 to version 1.5.101
18+
/// </summary>
19+
VersionRange2,
20+
/// <summary>
21+
/// Version 1.5.441
22+
/// </summary>
23+
VersionRange3,
24+
/// <summary>
25+
/// Version 1.5.1081 to newest version
26+
/// </summary>
27+
VersionRange4
28+
}
29+
}

src/WGet.NET/HelperClasses/ArrayManager.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,28 @@ public static int GetEntryContains(string[] inputArray, string value)
9999
return index;
100100
}
101101

102+
/// <summary>
103+
/// Removes empty entries from a <see langword="array"/> of <see cref="System.String"/>'s.
104+
/// </summary>
105+
/// <param name="inputArray">The input <see langword="array"/>.</param>
106+
/// <returns>
107+
/// The <see langword="array"/> with empty entries removed.
108+
/// </returns>
109+
public static string[] RemoveEmptyEntries(string[] inputArray)
110+
{
111+
string[] newArray = new string[0];
112+
113+
for (int i = 0; i < inputArray.Length; i++)
114+
{
115+
if (!string.IsNullOrWhiteSpace(inputArray[i]))
116+
{
117+
newArray = Add(newArray, inputArray[i]);
118+
}
119+
}
120+
121+
return newArray;
122+
}
123+
102124
/// <summary>
103125
/// Copys a <see langword="array"/> to a new one.
104126
/// </summary>

0 commit comments

Comments
 (0)