From 1dc4b79aa9a6d464a564366b27176e847c15801f Mon Sep 17 00:00:00 2001 From: Greg Spencer Date: Tue, 30 Mar 2021 13:46:45 -0700 Subject: [PATCH 1/3] Add OS constants for use in switch statements --- lib/src/interface/platform.dart | 60 +++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/lib/src/interface/platform.dart b/lib/src/interface/platform.dart index 82c8bfe..a69ac1a 100644 --- a/lib/src/interface/platform.dart +++ b/lib/src/interface/platform.dart @@ -11,6 +11,54 @@ abstract class Platform { /// Creates a new [Platform]. const Platform(); + /// A string constant to compare with [operatingSystem] to see if the platform + /// is Linux. + /// + /// Useful in case statements when switching on [operatingSystem]. + /// + /// To just check if the platform is Linux, use [isLinux]. + static const String linux = 'linux'; + + /// A string constant to compare with [operatingSystem] to see if the platform + /// is Windows. + /// + /// Useful in case statements when switching on [operatingSystem]. + /// + /// To just check if the platform is Windows, use [isWindows]. + static const String windows = 'windows'; + + /// A string constant to compare with [operatingSystem] to see if the platform + /// is macOS. + /// + /// Useful in case statements when switching on [operatingSystem]. + /// + /// To just check if the platform is macOS, use [isMacOS]. + static const String macOS = 'macos'; + + /// A string constant to compare with [operatingSystem] to see if the platform + /// is Android. + /// + /// Useful in case statements when switching on [operatingSystem]. + /// + /// To just check if the platform is Android, use [isAndroid]. + static const String android = 'android'; + + /// A string constant to compare with [operatingSystem] to see if the platform + /// is iOS. + /// + /// Useful in case statements when switching on [operatingSystem]. + /// + /// To just check if the platform is iOS, use [isIOS]. + static const String iOS = 'ios'; + + /// A string constant to compare with [operatingSystem] to see if the platform + /// is Fuchsia. + /// + /// Useful in case statements when switching on [operatingSystem]. + /// + /// To just check if the platform is Fuchsia, use [isFuchsia]. + static const String fuchsia = 'fuchsia'; + /// The number of processors of the machine. int get numberOfProcessors; @@ -29,22 +77,22 @@ abstract class Platform { String get localHostname; /// True if the operating system is Linux. - bool get isLinux => (operatingSystem == "linux"); + bool get isLinux => operatingSystem == linux; /// True if the operating system is OS X. - bool get isMacOS => (operatingSystem == "macos"); + bool get isMacOS => operatingSystem == macOS; /// True if the operating system is Windows. - bool get isWindows => (operatingSystem == "windows"); + bool get isWindows => operatingSystem == windows; /// True if the operating system is Android. - bool get isAndroid => (operatingSystem == "android"); + bool get isAndroid => operatingSystem == android; /// True if the operating system is iOS. - bool get isIOS => (operatingSystem == "ios"); + bool get isIOS => operatingSystem == iOS; /// True if the operating system is Fuchsia - bool get isFuchsia => (operatingSystem == "fuchsia"); + bool get isFuchsia => operatingSystem == fuchsia; /// The environment for this process. /// From ce2d1203a3f61b2aee3407981536baabe1c3864d Mon Sep 17 00:00:00 2001 From: Greg Spencer Date: Tue, 30 Mar 2021 13:48:36 -0700 Subject: [PATCH 2/3] Bumped version and added to change log --- CHANGELOG.md | 5 +++++ pubspec.yaml | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 558aeb1..664defd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +### 3.0.1 + +* Added string constants for each of the supported platforms for use in switch + statements. + ### 3.0.0 * First stable null safe release. diff --git a/pubspec.yaml b/pubspec.yaml index 5480397..480f98e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: platform -version: 3.0.0 +version: 3.0.1 description: A pluggable, mockable platform abstraction for Dart. homepage: https://github.com/google/platform.dart @@ -7,4 +7,4 @@ environment: sdk: '>=2.12.0-0 <3.0.0' dev_dependencies: - test: ^1.16.0-nullsafety.1 + test: ^1.16.8 From c0c9caa9051f064e98d136d45137871f753cb854 Mon Sep 17 00:00:00 2001 From: Greg Spencer Date: Tue, 30 Mar 2021 13:55:53 -0700 Subject: [PATCH 3/3] Add operatingSystemValues and more comments --- lib/src/interface/platform.dart | 35 +++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/src/interface/platform.dart b/lib/src/interface/platform.dart index a69ac1a..7f9262f 100644 --- a/lib/src/interface/platform.dart +++ b/lib/src/interface/platform.dart @@ -13,33 +13,33 @@ abstract class Platform { /// A string constant to compare with [operatingSystem] to see if the platform /// is Linux. - /// + /// /// Useful in case statements when switching on [operatingSystem]. - /// + /// /// To just check if the platform is Linux, use [isLinux]. static const String linux = 'linux'; /// A string constant to compare with [operatingSystem] to see if the platform /// is Windows. - /// + /// /// Useful in case statements when switching on [operatingSystem]. - /// + /// /// To just check if the platform is Windows, use [isWindows]. static const String windows = 'windows'; /// A string constant to compare with [operatingSystem] to see if the platform /// is macOS. - /// + /// /// Useful in case statements when switching on [operatingSystem]. - /// + /// /// To just check if the platform is macOS, use [isMacOS]. static const String macOS = 'macos'; /// A string constant to compare with [operatingSystem] to see if the platform /// is Android. - /// + /// /// Useful in case statements when switching on [operatingSystem]. - /// + /// /// To just check if the platform is Android, use [isAndroid]. static const String android = 'android'; @@ -53,12 +53,22 @@ abstract class Platform { /// A string constant to compare with [operatingSystem] to see if the platform /// is Fuchsia. - /// + /// /// Useful in case statements when switching on [operatingSystem]. - /// + /// /// To just check if the platform is Fuchsia, use [isFuchsia]. static const String fuchsia = 'fuchsia'; + /// A list of the possible values that [operatingSystem] can return. + static const List operatingSystemValues = [ + linux, + macOS, + windows, + android, + iOS, + fuchsia, + ]; + /// The number of processors of the machine. int get numberOfProcessors; @@ -68,6 +78,11 @@ abstract class Platform { /// A string (`linux`, `macos`, `windows`, `android`, `ios`, or `fuchsia`) /// representing the operating system. + /// + /// The possible return values are available from [operatingSystemValues], and + /// there are constants for each of the platforms to use in switch statements + /// or conditionals (See [linux], [macOS], [windows], [android], [iOS], and + /// [fuchsia]). String get operatingSystem; /// A string representing the version of the operating system or platform.