Skip to content

Commit

Permalink
Update OperatingSystemPolyfill.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Jan 28, 2025
1 parent 45c3227 commit 2eaec3a
Showing 1 changed file with 65 additions and 28 deletions.
93 changes: 65 additions & 28 deletions src/Polyfill/OperatingSystemPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,22 @@ public static bool IsWindowsVersionAtLeast(int major, int minor = 0, int build =
return false;
}

var input = RuntimeInformation.OSDescription
if (windowsVersion == null)
{
var input = RuntimeInformation.OSDescription
.Replace("Microsoft Windows", string.Empty)
.Replace(" ", string.Empty);
var version = Version.Parse(input);
windowsVersion = Version.Parse(input);
}

return version >= new Version(major, minor, build, revision);
return windowsVersion >= new Version(major, minor, build, revision);
#endif
}

#if !NET5_0_OR_GREATER
static Version? windowsVersion;
#endif

/// <summary>
/// Indicates whether the current application is running on macOS.
/// </summary>
Expand Down Expand Up @@ -157,16 +164,23 @@ public static bool IsMacOSVersionAtLeast(int major, int minor = 0, int build = 0
return false;
}

var versionString = RunProcess("/usr/bin/sw_vers", "")
.Replace("ProductVersion:", string.Empty)
.Replace(" ", string.Empty);
if (macOSVersion == null)
{
var versionString = RunProcess("/usr/bin/sw_vers", "")
.Replace("ProductVersion:", string.Empty)
.Replace(" ", string.Empty);

var version = Version.Parse(versionString.Split(Environment.NewLine.ToCharArray())[0]);
macOSVersion = Version.Parse(versionString.Split(Environment.NewLine.ToCharArray())[0]);
}

return version >= new Version(major, minor, build);
return macOSVersion >= new Version(major, minor, build);
#endif
}

#if !NET5_0_OR_GREATER
static Version? macOSVersion;
#endif

/// <summary>
/// Check for the Mac Catalyst version (iOS version as presented in Apple documentation) with a ≤ version comparison. Used to guard APIs that were added in the given Mac Catalyst release.
/// </summary>
Expand Down Expand Up @@ -227,18 +241,25 @@ public static bool IsFreeBSDVersionAtLeast(int major, int minor, int build = 0,
return false;
}

var versionString = Environment.OSVersion.VersionString
.Replace("Unix", string.Empty)
.Replace("FreeBSD", string.Empty)
.Replace("-release", string.Empty)
.Replace(" ", string.Empty);
if (freeBsdVersion == null)
{
var versionString = Environment.OSVersion.VersionString
.Replace("Unix", string.Empty)
.Replace("FreeBSD", string.Empty)
.Replace("-release", string.Empty)
.Replace(" ", string.Empty);

var version = Version.Parse(versionString);
freeBsdVersion = Version.Parse(versionString);
}

return version >= new Version(major, minor, build, revision);
return freeBsdVersion >= new Version(major, minor, build, revision);
#endif
}

#if !NET5_0_OR_GREATER
static Version? freeBsdVersion;
#endif

/// <summary>
/// Indicates whether the current application is running on iOS or MacCatalyst.
/// </summary>
Expand Down Expand Up @@ -311,20 +332,29 @@ public static bool IsAndroid()
#if NET5_0_OR_GREATER
return OperatingSystem.IsAndroid();
#else
try
if (!isAndroid.HasValue)
{
return RunProcess("uname", "-o")
.Replace(" ", string.Empty)
.ToLower()
.Equals("android");
}
catch
{
return false;
try
{
isAndroid = RunProcess("uname", "-o")
.Replace(" ", string.Empty)
.ToLower()
.Equals("android");
}
catch
{
isAndroid = false;
}
}

return isAndroid.Value;
#endif
}

#if !NET5_0_OR_GREATER
private static bool? isAndroid;
#endif

/// <summary>
/// Checks if the Android version (returned by the Linux command uname) is greater than or equal to the specified version. This method can be used to guard APIs that were added in the specified version.
/// </summary>
Expand All @@ -344,15 +374,22 @@ public static bool IsAndroidVersionAtLeast(int major, int minor = 0, int build =
return false;
}

var result = RunProcess("getprop", "ro.build.version.release")
.Replace(" ", string.Empty);
if (androidVersion == null)
{
var result = RunProcess("getprop", "ro.build.version.release")
.Replace(" ", string.Empty);

var version = Version.Parse(result);
androidVersion = Version.Parse(result);
}

return version >= new Version(major, minor, build, revision);
return androidVersion >= new Version(major, minor, build, revision);
#endif
}

#if !NET5_0_OR_GREATER
private static Version? androidVersion;
#endif

/// <summary>
/// Indicates whether the current application is running on watchOS.
/// </summary>
Expand Down

0 comments on commit 2eaec3a

Please sign in to comment.