Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions src/Polyfill/OperatingSystemCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;

Expand Down Expand Up @@ -49,16 +50,10 @@ public static bool IsMacOSVersionAtLeast(int major, int minor = 0, int build = 0
return false;
}

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

macOSVersion = Version.Parse(versionString.Split(Environment.NewLine.ToCharArray())[0]);
}
// prints only the version, e.g. "14.5" or "10.15.7"
macOSVersion ??= ParseVersion(RunProcess("/usr/bin/sw_vers", "-productVersion"));

return macOSVersion >= new Version(major, minor, build);
return macOSVersion >= new Version(major, minor, build, 0);
}

public static bool IsFreeBSDVersionAtLeast(int major, int minor, int build = 0, int revision = 0)
Expand Down Expand Up @@ -117,15 +112,38 @@ public static bool IsAndroidVersionAtLeast(int major, int minor = 0, int build =
return false;
}

if (androidVersion == null)
// e.g. "14" or "8.1.0"
androidVersion ??= ParseVersion(RunProcess("getprop", "ro.build.version.release"));

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

/// <summary>
/// Parses the first version number found in <paramref name="input"/>, ignoring any surrounding text.
/// Components that are not present are set to 0, so the result can be compared to a four part version.
/// </summary>
internal static Version ParseVersion(string input)
{
var start = 0;
while (start < input.Length && !char.IsDigit(input[start]))
{
start++;
}

var end = start;
while (end < input.Length && (char.IsDigit(input[end]) || input[end] == '.'))
{
var result = RunProcess("getprop", "ro.build.version.release")
.Replace(" ", string.Empty);
end++;
}

androidVersion = Version.Parse(result);
var parts = input.Substring(start, end - start).Split(new[] {'.'}, StringSplitOptions.RemoveEmptyEntries);
var components = new int[4];
for (var index = 0; index < parts.Length && index < components.Length; index++)
{
components[index] = int.Parse(parts[index], CultureInfo.InvariantCulture);
}

return androidVersion >= new Version(major, minor, build, revision);
return new(components[0], components[1], components[2], components[3]);
}
}
#endif
40 changes: 27 additions & 13 deletions src/Split/net461/OperatingSystemCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Polyfills;
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
[ExcludeFromCodeCoverage]
Expand Down Expand Up @@ -44,14 +45,8 @@ public static bool IsMacOSVersionAtLeast(int major, int minor = 0, int build = 0
{
return false;
}
if (macOSVersion == null)
{
var versionString = RunProcess("/usr/bin/sw_vers", "")
.Replace("ProductVersion:", string.Empty)
.Replace(" ", string.Empty);
macOSVersion = Version.Parse(versionString.Split(Environment.NewLine.ToCharArray())[0]);
}
return macOSVersion >= new Version(major, minor, build);
macOSVersion ??= ParseVersion(RunProcess("/usr/bin/sw_vers", "-productVersion"));
return macOSVersion >= new Version(major, minor, build, 0);
}
public static bool IsFreeBSDVersionAtLeast(int major, int minor, int build = 0, int revision = 0)
{
Expand Down Expand Up @@ -99,13 +94,32 @@ public static bool IsAndroidVersionAtLeast(int major, int minor = 0, int build =
{
return false;
}
if (androidVersion == null)
androidVersion ??= ParseVersion(RunProcess("getprop", "ro.build.version.release"));
return androidVersion >= new Version(major, minor, build, revision);
}
/// <summary>
/// Parses the first version number found in <paramref name="input"/>, ignoring any surrounding text.
/// Components that are not present are set to 0, so the result can be compared to a four part version.
/// </summary>
internal static Version ParseVersion(string input)
{
var start = 0;
while (start < input.Length && !char.IsDigit(input[start]))
{
start++;
}
var end = start;
while (end < input.Length && (char.IsDigit(input[end]) || input[end] == '.'))
{
var result = RunProcess("getprop", "ro.build.version.release")
.Replace(" ", string.Empty);
androidVersion = Version.Parse(result);
end++;
}
return androidVersion >= new Version(major, minor, build, revision);
var parts = input.Substring(start, end - start).Split(new[] {'.'}, StringSplitOptions.RemoveEmptyEntries);
var components = new int[4];
for (var index = 0; index < parts.Length && index < components.Length; index++)
{
components[index] = int.Parse(parts[index], CultureInfo.InvariantCulture);
}
return new(components[0], components[1], components[2], components[3]);
}
}
#endif
40 changes: 27 additions & 13 deletions src/Split/net462/OperatingSystemCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Polyfills;
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
[ExcludeFromCodeCoverage]
Expand Down Expand Up @@ -44,14 +45,8 @@ public static bool IsMacOSVersionAtLeast(int major, int minor = 0, int build = 0
{
return false;
}
if (macOSVersion == null)
{
var versionString = RunProcess("/usr/bin/sw_vers", "")
.Replace("ProductVersion:", string.Empty)
.Replace(" ", string.Empty);
macOSVersion = Version.Parse(versionString.Split(Environment.NewLine.ToCharArray())[0]);
}
return macOSVersion >= new Version(major, minor, build);
macOSVersion ??= ParseVersion(RunProcess("/usr/bin/sw_vers", "-productVersion"));
return macOSVersion >= new Version(major, minor, build, 0);
}
public static bool IsFreeBSDVersionAtLeast(int major, int minor, int build = 0, int revision = 0)
{
Expand Down Expand Up @@ -99,13 +94,32 @@ public static bool IsAndroidVersionAtLeast(int major, int minor = 0, int build =
{
return false;
}
if (androidVersion == null)
androidVersion ??= ParseVersion(RunProcess("getprop", "ro.build.version.release"));
return androidVersion >= new Version(major, minor, build, revision);
}
/// <summary>
/// Parses the first version number found in <paramref name="input"/>, ignoring any surrounding text.
/// Components that are not present are set to 0, so the result can be compared to a four part version.
/// </summary>
internal static Version ParseVersion(string input)
{
var start = 0;
while (start < input.Length && !char.IsDigit(input[start]))
{
start++;
}
var end = start;
while (end < input.Length && (char.IsDigit(input[end]) || input[end] == '.'))
{
var result = RunProcess("getprop", "ro.build.version.release")
.Replace(" ", string.Empty);
androidVersion = Version.Parse(result);
end++;
}
return androidVersion >= new Version(major, minor, build, revision);
var parts = input.Substring(start, end - start).Split(new[] {'.'}, StringSplitOptions.RemoveEmptyEntries);
var components = new int[4];
for (var index = 0; index < parts.Length && index < components.Length; index++)
{
components[index] = int.Parse(parts[index], CultureInfo.InvariantCulture);
}
return new(components[0], components[1], components[2], components[3]);
}
}
#endif
40 changes: 27 additions & 13 deletions src/Split/net47/OperatingSystemCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Polyfills;
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
[ExcludeFromCodeCoverage]
Expand Down Expand Up @@ -44,14 +45,8 @@ public static bool IsMacOSVersionAtLeast(int major, int minor = 0, int build = 0
{
return false;
}
if (macOSVersion == null)
{
var versionString = RunProcess("/usr/bin/sw_vers", "")
.Replace("ProductVersion:", string.Empty)
.Replace(" ", string.Empty);
macOSVersion = Version.Parse(versionString.Split(Environment.NewLine.ToCharArray())[0]);
}
return macOSVersion >= new Version(major, minor, build);
macOSVersion ??= ParseVersion(RunProcess("/usr/bin/sw_vers", "-productVersion"));
return macOSVersion >= new Version(major, minor, build, 0);
}
public static bool IsFreeBSDVersionAtLeast(int major, int minor, int build = 0, int revision = 0)
{
Expand Down Expand Up @@ -99,13 +94,32 @@ public static bool IsAndroidVersionAtLeast(int major, int minor = 0, int build =
{
return false;
}
if (androidVersion == null)
androidVersion ??= ParseVersion(RunProcess("getprop", "ro.build.version.release"));
return androidVersion >= new Version(major, minor, build, revision);
}
/// <summary>
/// Parses the first version number found in <paramref name="input"/>, ignoring any surrounding text.
/// Components that are not present are set to 0, so the result can be compared to a four part version.
/// </summary>
internal static Version ParseVersion(string input)
{
var start = 0;
while (start < input.Length && !char.IsDigit(input[start]))
{
start++;
}
var end = start;
while (end < input.Length && (char.IsDigit(input[end]) || input[end] == '.'))
{
var result = RunProcess("getprop", "ro.build.version.release")
.Replace(" ", string.Empty);
androidVersion = Version.Parse(result);
end++;
}
return androidVersion >= new Version(major, minor, build, revision);
var parts = input.Substring(start, end - start).Split(new[] {'.'}, StringSplitOptions.RemoveEmptyEntries);
var components = new int[4];
for (var index = 0; index < parts.Length && index < components.Length; index++)
{
components[index] = int.Parse(parts[index], CultureInfo.InvariantCulture);
}
return new(components[0], components[1], components[2], components[3]);
}
}
#endif
40 changes: 27 additions & 13 deletions src/Split/net471/OperatingSystemCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Polyfills;
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
[ExcludeFromCodeCoverage]
Expand Down Expand Up @@ -44,14 +45,8 @@ public static bool IsMacOSVersionAtLeast(int major, int minor = 0, int build = 0
{
return false;
}
if (macOSVersion == null)
{
var versionString = RunProcess("/usr/bin/sw_vers", "")
.Replace("ProductVersion:", string.Empty)
.Replace(" ", string.Empty);
macOSVersion = Version.Parse(versionString.Split(Environment.NewLine.ToCharArray())[0]);
}
return macOSVersion >= new Version(major, minor, build);
macOSVersion ??= ParseVersion(RunProcess("/usr/bin/sw_vers", "-productVersion"));
return macOSVersion >= new Version(major, minor, build, 0);
}
public static bool IsFreeBSDVersionAtLeast(int major, int minor, int build = 0, int revision = 0)
{
Expand Down Expand Up @@ -99,13 +94,32 @@ public static bool IsAndroidVersionAtLeast(int major, int minor = 0, int build =
{
return false;
}
if (androidVersion == null)
androidVersion ??= ParseVersion(RunProcess("getprop", "ro.build.version.release"));
return androidVersion >= new Version(major, minor, build, revision);
}
/// <summary>
/// Parses the first version number found in <paramref name="input"/>, ignoring any surrounding text.
/// Components that are not present are set to 0, so the result can be compared to a four part version.
/// </summary>
internal static Version ParseVersion(string input)
{
var start = 0;
while (start < input.Length && !char.IsDigit(input[start]))
{
start++;
}
var end = start;
while (end < input.Length && (char.IsDigit(input[end]) || input[end] == '.'))
{
var result = RunProcess("getprop", "ro.build.version.release")
.Replace(" ", string.Empty);
androidVersion = Version.Parse(result);
end++;
}
return androidVersion >= new Version(major, minor, build, revision);
var parts = input.Substring(start, end - start).Split(new[] {'.'}, StringSplitOptions.RemoveEmptyEntries);
var components = new int[4];
for (var index = 0; index < parts.Length && index < components.Length; index++)
{
components[index] = int.Parse(parts[index], CultureInfo.InvariantCulture);
}
return new(components[0], components[1], components[2], components[3]);
}
}
#endif
40 changes: 27 additions & 13 deletions src/Split/net472/OperatingSystemCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Polyfills;
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
[ExcludeFromCodeCoverage]
Expand Down Expand Up @@ -44,14 +45,8 @@ public static bool IsMacOSVersionAtLeast(int major, int minor = 0, int build = 0
{
return false;
}
if (macOSVersion == null)
{
var versionString = RunProcess("/usr/bin/sw_vers", "")
.Replace("ProductVersion:", string.Empty)
.Replace(" ", string.Empty);
macOSVersion = Version.Parse(versionString.Split(Environment.NewLine.ToCharArray())[0]);
}
return macOSVersion >= new Version(major, minor, build);
macOSVersion ??= ParseVersion(RunProcess("/usr/bin/sw_vers", "-productVersion"));
return macOSVersion >= new Version(major, minor, build, 0);
}
public static bool IsFreeBSDVersionAtLeast(int major, int minor, int build = 0, int revision = 0)
{
Expand Down Expand Up @@ -99,13 +94,32 @@ public static bool IsAndroidVersionAtLeast(int major, int minor = 0, int build =
{
return false;
}
if (androidVersion == null)
androidVersion ??= ParseVersion(RunProcess("getprop", "ro.build.version.release"));
return androidVersion >= new Version(major, minor, build, revision);
}
/// <summary>
/// Parses the first version number found in <paramref name="input"/>, ignoring any surrounding text.
/// Components that are not present are set to 0, so the result can be compared to a four part version.
/// </summary>
internal static Version ParseVersion(string input)
{
var start = 0;
while (start < input.Length && !char.IsDigit(input[start]))
{
start++;
}
var end = start;
while (end < input.Length && (char.IsDigit(input[end]) || input[end] == '.'))
{
var result = RunProcess("getprop", "ro.build.version.release")
.Replace(" ", string.Empty);
androidVersion = Version.Parse(result);
end++;
}
return androidVersion >= new Version(major, minor, build, revision);
var parts = input.Substring(start, end - start).Split(new[] {'.'}, StringSplitOptions.RemoveEmptyEntries);
var components = new int[4];
for (var index = 0; index < parts.Length && index < components.Length; index++)
{
components[index] = int.Parse(parts[index], CultureInfo.InvariantCulture);
}
return new(components[0], components[1], components[2], components[3]);
}
}
#endif
Loading
Loading