Skip to content

Commit e5db5fb

Browse files
committed
Added a function that checks if winget is installed on the system, instead of using the check version result; Removed exception catching from the process flow when checking the winget version
1 parent 3d19aa5 commit e5db5fb

2 files changed

Lines changed: 80 additions & 41 deletions

File tree

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

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using WGetNET.Models;
99
using WGetNET.Exceptions;
1010
using WGetNET.HelperClasses;
11+
using System.IO;
1112

1213
namespace WGetNET
1314
{
@@ -32,11 +33,7 @@ public bool IsInstalled
3233
{
3334
get
3435
{
35-
if (CheckWinGetVersion() != string.Empty)
36-
{
37-
return true;
38-
}
39-
return false;
36+
return CheckInstallation();
4037
}
4138
}
4239

@@ -396,33 +393,59 @@ protected bool CheckWinGetVersion(Version minVersion, Version? maxVersion = null
396393
/// </returns>
397394
private string CheckWinGetVersion()
398395
{
399-
try
396+
if (!IsInstalled)
400397
{
401-
ProcessResult result =
402-
_processManager.ExecuteWingetProcess(_versionCmd);
398+
return string.Empty;
399+
}
403400

404-
for (int i = 0; i < result.Output.Length; i++)
405-
{
401+
ProcessResult result =
402+
_processManager.ExecuteWingetProcess(_versionCmd);
403+
404+
for (int i = 0; i < result.Output.Length; i++)
405+
{
406406
#if NETCOREAPP3_1_OR_GREATER
407-
if (result.Output[i].StartsWith('v') && result.Output[i].Length >= 2)
408-
{
409-
// Return output without the 'v' at the start.
410-
return result.Output[i].Trim()[1..];
411-
}
407+
if (result.Output[i].StartsWith('v') && result.Output[i].Length >= 2)
408+
{
409+
// Return output without the 'v' at the start.
410+
return result.Output[i].Trim()[1..];
411+
}
412412
#elif NETSTANDARD2_0
413-
if (result.Output[i].StartsWith("v") && result.Output[i].Length >= 2)
414-
{
415-
return result.Output[i].Trim().Substring(1);
416-
}
417-
#endif
413+
if (result.Output[i].StartsWith("v") && result.Output[i].Length >= 2)
414+
{
415+
// Return output without the 'v' at the start.
416+
return result.Output[i].Trim().Substring(1);
418417
}
418+
#endif
419+
}
420+
421+
return string.Empty;
422+
}
423+
424+
/// <summary>
425+
/// Checks if winget is installed on the system.
426+
/// </summary>
427+
/// <returns>
428+
/// <see langword="true"/> if the winget installation could be found and <see langword="false"/> if not.
429+
/// </returns>
430+
private bool CheckInstallation()
431+
{
432+
string? pathEnvVar = Environment.GetEnvironmentVariable("Path");
433+
if (string.IsNullOrWhiteSpace(pathEnvVar))
434+
{
435+
return false;
419436
}
420-
catch
437+
438+
string[] paths = pathEnvVar.Split(';');
439+
440+
for (int i = 0; i < paths.Length; i++)
421441
{
422-
return string.Empty;
442+
if (File.Exists(Path.Combine(paths[i], "winget.exe")))
443+
{
444+
return true;
445+
}
423446
}
424447

425-
return string.Empty;
448+
return false;
426449
}
427450
}
428451
}

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

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

0 commit comments

Comments
 (0)