Skip to content

Commit 9ac83b2

Browse files
committed
(maint) removed warning CA1416
by disabling the parent process detection in non-windows.
1 parent c1ac063 commit 9ac83b2

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/JavaVersionSwitcher/Adapters/ShellAdapter.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics;
3+
using System.Runtime.InteropServices;
34
using JavaVersionSwitcher.Logging;
45

56
namespace JavaVersionSwitcher.Adapters
@@ -14,8 +15,14 @@ public ShellAdapter(ILogger logger)
1415
_logger = logger;
1516
}
1617

18+
1719
public ShellType GetShellType()
1820
{
21+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
22+
{
23+
return ShellType.Unknown;
24+
}
25+
1926
try
2027
{
2128
var proc = GetParentProcess(Process.GetCurrentProcess());
@@ -45,11 +52,20 @@ public ShellType GetShellType()
4552
}
4653
}
4754

48-
private static Process GetParentProcess(Process process) {
55+
private static Process GetParentProcess(Process process)
56+
{
4957
return FindPidFromIndexedProcessName(FindIndexedProcessName(process.Id));
5058
}
5159

52-
private static string FindIndexedProcessName(int pid) {
60+
#pragma warning disable CA1416
61+
private static string FindIndexedProcessName(int pid)
62+
{
63+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
64+
{
65+
throw new NotImplementedException(
66+
"Accessing parent process is currently only available on windows.");
67+
}
68+
5369
var processName = Process.GetProcessById(pid).ProcessName;
5470
var processesByName = Process.GetProcessesByName(processName);
5571
string processIndexedName = null;
@@ -66,8 +82,15 @@ private static string FindIndexedProcessName(int pid) {
6682
}
6783

6884
private static Process FindPidFromIndexedProcessName(string indexedProcessName) {
85+
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
86+
{
87+
throw new NotImplementedException(
88+
"Accessing parent process is currently only available on windows.");
89+
}
90+
6991
var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName);
7092
return Process.GetProcessById((int) parentId.NextValue());
7193
}
94+
#pragma warning restore CA1416
7295
}
7396
}

src/JavaVersionSwitcher/JavaVersionSwitcher.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
66
<PackAsTool>true</PackAsTool>
77
</PropertyGroup>
8-
8+
99
<ItemGroup>
1010
<None Include="$(MSBuildProjectDirectory)/../../README.md" PackagePath="" Pack="true" />
1111
<None Include="$(MSBuildProjectDirectory)/../../res/logo/logo-128.png" PackagePath="" Pack="true" />
@@ -37,9 +37,9 @@
3737

3838
<ItemGroup>
3939
<PackageReference Include="JetBrains.Annotations" Version="2021.1.0" />
40+
<PackageReference Include="Microsoft.Windows.Compatibility" Version="5.0.2" />
4041
<PackageReference Include="SimpleInjector" Version="5.3.1" />
4142
<PackageReference Include="Spectre.Console" Version="0.40.0" />
42-
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="5.0.1" />
4343
</ItemGroup>
4444

4545
</Project>

0 commit comments

Comments
 (0)