Skip to content

Implement OS checks #40358

Closed
Closed

Description

@terrajobst

We decided in #40111 (comment) to expose OS checks as static helpers on OperatingSystem. We need to implement those:

namespace System
{
    public partial class OperatingSystem
    {
        // Primary
        public static bool IsOSPlatform(string platform);
        public static bool IsOSPlatformVersionAtLeast(string platform, int major, int minor = 0, int build = 0, int revision = 0);

        // Accelerators for platforms where versions don't make sense
        public static bool IsBrowser();
        public static bool IsLinux();

        // Accelerators with version checks

        public static bool IsFreeBSD();
        public static bool IsFreeBSDVersionAtLeast(int major, int minor = 0, int build = 0, int revision = 0);

        public static bool IsAndroid();
        public static bool IsAndroidVersionAtLeast(int major, int minor = 0, int build = 0, int revision = 0);

        public static bool IsIOS();
        public static bool IsIOSVersionAtLeast(int major, int minor = 0, int build = 0, int revision = 0);

        public static bool IsMacOS();
        public static bool IsMacOSVersionAtLeast(int major, int minor = 0, int build = 0, int revision = 0);

        public static bool IsTvOS();
        public static bool IsTvOSVersionAtLeast(int major, int minor = 0, int build = 0, int revision = 0);

        public static bool IsWatchOS();
        public static bool IsWatchOSVersionAtLeast(int major, int minor = 0, int build = 0, int revision = 0);

        public static bool IsWindows();
        public static bool IsWindowsVersionAtLeast(int major, int minor = 0, int build = 0, int revision = 0);
    }
}

/cc @jeffhandley @buyaa-n

Activity

  1. added this to the 5.0.0 milestone on Aug 5, 2020
  2. removed
    untriagedNew issue has not been triaged by the area owner
    on Aug 5, 2020
  3. akoeplinger commented on Aug 5, 2020

    @akoeplinger
    Member

    Do we want to implement them using the same underlying RuntimeInformation architecture?
    So running RuntimeInformation.IsOSPlatform(OSPlatform.Create("iOS")) would return true because it just forwards to OperatingSystem.IsOSPlatform()?

  4. terrajobst commented on Aug 6, 2020

    @terrajobst
    ContributorAuthor

    I consider that an implementation detail. In my head, the impl looked something like this:

            public static bool IsOSPlatform(string platform)
                => RuntimeInformation.IsOSPlatform(new OSPlatform(platform));
            public static bool IsOSPlatformVersionAtLeast(string platform, int major, int minor = 0, int build = 0, int revision = 0)
                => IsOSPlatform(platform) && Environment.OSVersion.Version >= new Version(major, minor, build, revision);
    
            public static bool IsFreeBSD()
                => ISPlatform("FreeBSD");
            public static bool IsFreeBSDVersionAtLeast(int major, int minor = 0, int build = 0, int revision = 0);
                => ISPlatform("FreeBSD", major, minor, build, revision);
    
            ...
  5. akoeplinger commented on Aug 6, 2020

    @akoeplinger
    Member

    Ok yeah I agree it is an implementation detail but ideally the two APIs should align like in your example.

    It'd be bad if in the future when we add a new platform we'd need to add it in two places or worse if RuntimeInformation.IsOSPlatform(OSPlatform.Create("AppleWashingMachine") returned a different value than OperatingSystem.IsOSPlatform("AppleWashingMachine")

  6. ghost locked as resolved and limited conversation to collaborators on Dec 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions