Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 9e27954

Browse files
committed
Expand Env with more OS detection
1 parent 8ee3d5c commit 9e27954

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

src/ServiceStack.Text/Env.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ static Env()
2424

2525
IsAndroid = AssemblyUtils.FindType("Android.Manifest") != null;
2626

27+
try
28+
{
29+
IsOSX = AssemblyUtils.FindType("Mono.AppKit") != null;
30+
#if NET45
31+
IsWindows = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("windir"));
32+
if (File.Exists(@"/System/Library/CoreServices/SystemVersion.plist"))
33+
IsOSX = true;
34+
string osType = File.Exists(@"/proc/sys/kernel/ostype") ? File.ReadAllText(@"/proc/sys/kernel/ostype") : null;
35+
IsLinux = osType?.IndexOf("Linux", StringComparison.OrdinalIgnoreCase) >= 0;
36+
#endif
37+
}
38+
catch (Exception ignore) {}
39+
2740
//Throws unhandled exception if not called from the main thread
2841
//IsWinRT = AssemblyUtils.FindType("Windows.ApplicationModel") != null;
2942

@@ -36,13 +49,20 @@ static Env()
3649
IsWindowsStore = true;
3750
}
3851

39-
#if PCL || NETSTANDARD1_1
40-
IsUnix = IsMono;
41-
IsWin = !IsUnix;
52+
#if PCL
53+
IsUnix = IsMono || IsOSX || IsLinux;
54+
IsWindows = !IsUnix;
55+
#elif NETSTANDARD1_1
56+
IsLinux = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux);
57+
IsWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
58+
IsOSX = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX);
59+
IsUnix = IsOSX || IsLinux;
4260
#else
4361
var platform = (int)Environment.OSVersion.Platform;
44-
IsUnix = (platform == 4) || (platform == 6) || (platform == 128);
45-
IsWin = Environment.GetEnvironmentVariable("OS")?.IndexOf("Windows", StringComparison.OrdinalIgnoreCase) >= 0;
62+
IsUnix = platform == 4 || platform == 6 || platform == 128;
63+
IsLinux = IsUnix;
64+
if (Environment.GetEnvironmentVariable("OS")?.IndexOf("Windows", StringComparison.OrdinalIgnoreCase) >= 0)
65+
IsWindows = true;
4666
#endif
4767

4868
ServerUserAgent = "ServiceStack/" +
@@ -59,9 +79,13 @@ static Env()
5979

6080
public static decimal ServiceStackVersion = 4.00m;
6181

82+
public static bool IsLinux { get; set; }
83+
84+
public static bool IsOSX { get; set; }
85+
6286
public static bool IsUnix { get; set; }
6387

64-
public static bool IsWin { get; set; }
88+
public static bool IsWindows { get; set; }
6589

6690
public static bool IsMono { get; set; }
6791

0 commit comments

Comments
 (0)