Skip to content

Commit 8bb9521

Browse files
authored
Merge pull request #169 from nils-a/feature/GH-167
(#167) add a check of the registry
2 parents 17df6f3 + 1f9dc01 commit 8bb9521

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

src/JavaVersionSwitcher/Adapters/IRegistryAdapter.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,14 @@ namespace JavaVersionSwitcher.Adapters;
44

55
public interface IRegistryAdapter
66
{
7-
IAsyncEnumerable<string> GetInstallationPaths();
7+
IAsyncEnumerable<IJavaRegistryRegistration> GetInstallations();
8+
}
9+
10+
public interface IJavaRegistryRegistration
11+
{
12+
string RegKey { get; }
13+
14+
string InstallationPath { get; }
15+
16+
string Version { get; }
817
}

src/JavaVersionSwitcher/Adapters/RegistryAdapter.cs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Runtime.InteropServices;
34
using System.Threading.Tasks;
@@ -22,7 +23,7 @@ private static Task<AsyncRegistryKey> GetHklmAsync()
2223
return AsyncRegistryKey.Hklm;
2324
}
2425

25-
internal class AsyncRegistryKey
26+
internal class AsyncRegistryKey : IDisposable
2627
{
2728
#pragma warning disable CA1416
2829
private readonly RegistryKey _key;
@@ -70,19 +71,24 @@ public override string ToString()
7071
{
7172
return _key.ToString();
7273
}
74+
75+
public void Dispose()
76+
{
77+
_key?.Dispose();
78+
}
7379
#pragma warning restore CA1416
7480
}
7581

76-
public async IAsyncEnumerable<string> GetInstallationPaths()
82+
public async IAsyncEnumerable<IJavaRegistryRegistration> GetInstallations()
7783
{
78-
var localMachine = await GetHklmAsync();
84+
using var localMachine = await GetHklmAsync();
7985
if (localMachine == null)
8086
{
8187
_logger.LogVerbose($"Not Checking the registry, since we're running on {RuntimeInformation.RuntimeIdentifier}");
8288
yield break;
8389
}
8490

85-
var javaSoft = await localMachine.OpenSubKey("SOFTWARE\\JavaSoft");
91+
using var javaSoft = await localMachine.OpenSubKey("SOFTWARE\\JavaSoft");
8692
if (javaSoft == null)
8793
{
8894
_logger.LogWarning(@"RegKey 'HKLM\Software\JavaSoft' does not exist.");
@@ -99,20 +105,20 @@ public async IAsyncEnumerable<string> GetInstallationPaths()
99105

100106
foreach (var root in roots)
101107
{
102-
var rootKey = await javaSoft.OpenSubKey(root);
108+
using var rootKey = await javaSoft.OpenSubKey(root);
103109
if (rootKey == null)
104110
{
105111
continue;
106112
}
107-
108-
var keyNames = await rootKey.GetSubKeyNames();
109-
foreach (var name in keyNames)
113+
114+
var versions = await rootKey.GetSubKeyNames();
115+
foreach (var javaVersion in versions)
110116
{
111-
_logger.LogVerbose($"Checking SubKey: {rootKey}\\{name}");
112-
var key = await rootKey.OpenSubKey(name);
117+
_logger.LogVerbose($"Checking SubKey: {rootKey}\\{javaVersion}");
118+
using var key = await rootKey.OpenSubKey(javaVersion);
113119
if (key == null)
114120
{
115-
_logger.LogWarning($"SubKey '{rootKey}\\{name}' was reported to exist, but it does not.");
121+
_logger.LogWarning($"SubKey '{rootKey}\\{javaVersion}' was reported to exist, but it does not.");
116122
continue;
117123
}
118124

@@ -121,8 +127,20 @@ public async IAsyncEnumerable<string> GetInstallationPaths()
121127
continue;
122128
}
123129

124-
yield return javaHome;
130+
yield return new JavaRegistryRegistration
131+
{
132+
RegKey = key.ToString(),
133+
InstallationPath = javaHome,
134+
Version = javaVersion,
135+
};
125136
}
126137
}
127138
}
139+
140+
private class JavaRegistryRegistration : IJavaRegistryRegistration
141+
{
142+
public string RegKey { get; init; }
143+
public string InstallationPath { get; init; }
144+
public string Version { get; init; }
145+
}
128146
}

src/JavaVersionSwitcher/Commands/CheckSettingsCommand.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,19 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
9494
}
9595
}
9696

97-
await foreach (var path in _registryAdapter.GetInstallationPaths())
97+
await foreach (var installation in _registryAdapter.GetInstallations())
9898
{
99-
var binPath = Path.Combine(path, "bin", "java.exe");
99+
var binPath = Path.Combine(installation.InstallationPath, "bin", "java.exe");
100100
if (File.Exists(binPath))
101101
{
102102
continue;
103103
}
104104

105105
var warn =
106-
$"Path for java installation '{path}' set in Windows Registry. But does not contain a java executable!";
106+
$@"Path for java installation '{installation.InstallationPath}' set in Windows Registry. But does not contain a java executable!
107+
You should probably remove the RegKey {installation.RegKey}";
107108
_console.MarkupLine($"[red]{warn}[/]");
109+
errors = true;
108110
}
109111

110112
if (errors)

src/JavaVersionSwitcher/JavaVersionSwitcher.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
</PropertyGroup>
3737

3838
<ItemGroup>
39+
<PackageReference Include="IDisposableAnalyzers" Version="4.0.7">
40+
<PrivateAssets>all</PrivateAssets>
41+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
42+
</PackageReference>
3943
<PackageReference Include="JetBrains.Annotations" Version="2023.2.0" />
4044
<PackageReference Include="Microsoft.Win32.Registry" Version="6.0.0-preview.5.21301.5" />
4145
<PackageReference Include="Microsoft.Windows.Compatibility" Version="7.0.5" />

0 commit comments

Comments
 (0)