Skip to content

Commit 5245505

Browse files
committed
Changed checking of the installed status and the winget version to be done in the constructor of the WinGet class so it does not need to be constantly recheck
1 parent e5db5fb commit 5245505

3 files changed

Lines changed: 53 additions & 42 deletions

File tree

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

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public class WinGet
2222
private const string _exportSettingsCmd = "settings export";
2323

2424
private protected readonly ProcessManager _processManager;
25+
private protected readonly bool _isInstalled;
26+
27+
private readonly string _wingetExePath;
28+
private readonly string _versionString;
29+
private readonly Version _version;
2530

2631
/// <summary>
2732
/// Gets if winget is installed on the system.
@@ -33,7 +38,7 @@ public bool IsInstalled
3338
{
3439
get
3540
{
36-
return CheckInstallation();
41+
return _isInstalled;
3742
}
3843
}
3944

@@ -47,7 +52,7 @@ public string VersionString
4752
{
4853
get
4954
{
50-
return CheckWinGetVersion();
55+
return _versionString;
5156
}
5257
}
5358

@@ -61,7 +66,7 @@ public Version Version
6166
{
6267
get
6368
{
64-
return VersionParser.Parse(CheckWinGetVersion());
69+
return _version;
6570
}
6671
}
6772

@@ -70,7 +75,21 @@ public Version Version
7075
/// </summary>
7176
public WinGet()
7277
{
73-
_processManager = new ProcessManager("winget");
78+
_wingetExePath = CheckInstallation();
79+
80+
if (string.IsNullOrWhiteSpace(_wingetExePath))
81+
{
82+
_isInstalled = false;
83+
_processManager = new ProcessManager("winget");
84+
}
85+
else
86+
{
87+
_isInstalled = true;
88+
_processManager = new ProcessManager(_wingetExePath);
89+
}
90+
91+
_versionString = CheckWinGetVersion();
92+
_version = VersionParser.Parse(_versionString);
7493
}
7594

7695
/// <summary>
@@ -393,7 +412,7 @@ protected bool CheckWinGetVersion(Version minVersion, Version? maxVersion = null
393412
/// </returns>
394413
private string CheckWinGetVersion()
395414
{
396-
if (!IsInstalled)
415+
if (!_isInstalled)
397416
{
398417
return string.Empty;
399418
}
@@ -422,30 +441,32 @@ private string CheckWinGetVersion()
422441
}
423442

424443
/// <summary>
425-
/// Checks if winget is installed on the system.
444+
/// Checks if winget is installed on the system and returns the path to the executable.
426445
/// </summary>
427446
/// <returns>
428-
/// <see langword="true"/> if the winget installation could be found and <see langword="false"/> if not.
447+
/// <see cref="System.String"/> containing the executable path if it was found or <see cref="System.String.Empty"/> if not.
429448
/// </returns>
430-
private bool CheckInstallation()
449+
private string CheckInstallation()
431450
{
432451
string? pathEnvVar = Environment.GetEnvironmentVariable("Path");
433452
if (string.IsNullOrWhiteSpace(pathEnvVar))
434453
{
435-
return false;
454+
return string.Empty;
436455
}
437456

438457
string[] paths = pathEnvVar.Split(';');
439458

459+
string exePath;
440460
for (int i = 0; i < paths.Length; i++)
441461
{
442-
if (File.Exists(Path.Combine(paths[i], "winget.exe")))
462+
exePath = Path.Combine(paths[i], "winget.exe");
463+
if (File.Exists(exePath))
443464
{
444-
return true;
465+
return exePath;
445466
}
446467
}
447468

448-
return false;
469+
return string.Empty;
449470
}
450471
}
451472
}

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

Lines changed: 20 additions & 28 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: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ private void Run()
2929

3030
Version winGetVersionObject = connector.Version;
3131

32-
bool uriTest = Uri.TryCreate("", UriKind.Absolute, out Uri? uri);
33-
3432
WinGetInfo info = winget.GetInfo();
3533
Console.WriteLine(info.Version);
3634

0 commit comments

Comments
 (0)