fix: tighten Unix platform handling

Adds Linux and macOS platform guards so the analyzer can narrow calls through Utils.IsLinux() and Utils.IsMacOS().

Marks the Linux/macOS autostart and system proxy helpers with explicit platform attributes.

Updates Utils.GetSystemHosts() to read /etc/hosts on Linux and macOS while keeping the existing Windows hosts and hosts.ics merge behavior.
This commit is contained in:
Miheichev Aleksandr Sergeevich 2026-05-13 15:41:52 +03:00
parent 06796e474a
commit 25c7c69886
4 changed files with 27 additions and 6 deletions

View file

@ -863,6 +863,8 @@ public class Utils
}
public static Dictionary<string, string> GetSystemHosts()
{
if (IsWindows())
{
var hosts = GetSystemHosts(@"C:\Windows\System32\drivers\etc\hosts");
var hostsIcs = GetSystemHosts(@"C:\Windows\System32\drivers\etc\hosts.ics");
@ -875,6 +877,14 @@ public class Utils
return hosts;
}
if (IsLinux() || IsMacOS())
{
return GetSystemHosts("/etc/hosts");
}
return new Dictionary<string, string>();
}
public static async Task<string?> GetCliWrapOutput(string filePath, string? arg)
{
return await GetCliWrapOutput(filePath, arg != null ? new List<string>() { arg } : null);
@ -1117,8 +1127,10 @@ public class Utils
[SupportedOSPlatformGuard("windows")]
public static bool IsWindows() => OperatingSystem.IsWindows();
[SupportedOSPlatformGuard("linux")]
public static bool IsLinux() => OperatingSystem.IsLinux();
[SupportedOSPlatformGuard("macos")]
public static bool IsMacOS() => OperatingSystem.IsMacOS();
[UnsupportedOSPlatformGuard("windows")]

View file

@ -127,6 +127,7 @@ public static class AutoStartupHandler
#region Linux
[SupportedOSPlatform("linux")]
private static async Task ClearTaskLinux()
{
try
@ -140,6 +141,7 @@ public static class AutoStartupHandler
await Task.CompletedTask;
}
[SupportedOSPlatform("linux")]
private static async Task SetTaskLinux()
{
try
@ -160,6 +162,7 @@ public static class AutoStartupHandler
}
}
[SupportedOSPlatform("linux")]
private static string GetHomePathLinux()
{
var homePath = Path.Combine(Utils.GetHomePath(), ".config", "autostart", $"{Global.AppName}.desktop");
@ -171,6 +174,7 @@ public static class AutoStartupHandler
#region macOS
[SupportedOSPlatform("macos")]
private static async Task ClearTaskOSX()
{
try
@ -190,6 +194,7 @@ public static class AutoStartupHandler
}
}
[SupportedOSPlatform("macos")]
private static async Task SetTaskOSX()
{
try
@ -207,6 +212,7 @@ public static class AutoStartupHandler
}
}
[SupportedOSPlatform("macos")]
private static string GetLaunchAgentPathMacOS()
{
var homePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
@ -215,6 +221,7 @@ public static class AutoStartupHandler
return launchAgentPath;
}
[SupportedOSPlatform("macos")]
private static string GenerateLaunchAgentPlist()
{
var exePath = Utils.GetExePath();

View file

@ -1,5 +1,6 @@
namespace ServiceLib.Handler.SysProxy;
[SupportedOSPlatform("linux")]
public static class ProxySettingLinux
{
private static readonly string _proxySetFileName = $"{Global.ProxySetLinuxShellFileName.Replace(Global.NamespaceSample, "")}.sh";

View file

@ -1,5 +1,6 @@
namespace ServiceLib.Handler.SysProxy;
[SupportedOSPlatform("macos")]
public static class ProxySettingOSX
{
private static readonly string _proxySetFileName = $"{Global.ProxySetOSXShellFileName.Replace(Global.NamespaceSample, "")}.sh";