-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the VM's "platform-const" feature to achieve tree-shaking. (dart-…
…archive/os_detect#31) * Use the VM's "platform-const" feature to achieve tree-shaking. By using the VM's `@pragma("vm:platform-const")` annotation, and `Platform` members annotated with it, ensure that the `isLinux` and `operatingSystem` values are also known at compile-time, even if they cannot be annotated as platform-constants. This is done by having a class per known operating system, and making sure to only create instances of those classes guarded by platform-constants (or real constants), so that if nobody goes out of their way to create a custom instance, which they should only do for testing, all but one of those platform-specific classes can be tree-shaken. At that point, checks like `is MacOS` (one of the types) can be statically determined to be false if `MacOS` was tree-shaken, and `.id` can be statically resolved to a single final variable with a known value. * Update test matrix to start at 3.0.0 * Add example discussing tree-shaking. * Format * Address comments. * Don't refer to unimported name in docs.
- Loading branch information
Showing
15 changed files
with
500 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
## 2.0.2-dev | ||
|
||
- Require Dart 2.18 | ||
- Require Dart 3.0 | ||
- Make work with VM's platform-constants. | ||
|
||
## 2.0.1 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// Prints the operating system detected by the current compilation environment. | ||
library pkg.os_detect.run; | ||
|
||
import 'package:os_detect/os_detect.dart' as os_detect; | ||
|
||
void main() { | ||
final knownName = knownOSName(); | ||
print('OS name : ${os_detect.operatingSystem} ' | ||
'${knownName != null ? '($knownName)' : ''}'); | ||
print('OS version : ${os_detect.operatingSystemVersion}'); | ||
} | ||
|
||
String? knownOSName() { | ||
if (os_detect.isAndroid) { | ||
return 'Android'; | ||
} | ||
if (os_detect.isBrowser) { | ||
return 'Browser'; | ||
} | ||
if (os_detect.isFuchsia) { | ||
return 'Fuchsia'; | ||
} | ||
if (os_detect.isIOS) { | ||
return 'iOS'; | ||
} | ||
if (os_detect.isLinux) { | ||
return 'Linux'; | ||
} | ||
if (os_detect.isMacOS) { | ||
return 'MacOS'; | ||
} | ||
if (os_detect.isWindows) { | ||
return 'Windows'; | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
// Try compiling this example with (if on Linux): | ||
// | ||
// dart compile exe --target-os=linux tree_shaking.dart | ||
// | ||
// then check that "SOMETHING ELSE" does not occur in the | ||
// output `tree_shaking.exe` program, e.g.: | ||
// | ||
// strings tree_shaking.exe | grep SOMETHING | ||
// | ||
// which shows no matches. | ||
|
||
import 'package:os_detect/os_detect.dart' as platform; | ||
|
||
void main() { | ||
if (platform.isLinux) { | ||
print('Is Linux'); | ||
} else { | ||
print('SOMETHING ELSE'); | ||
} | ||
if (platform.operatingSystem == 'linux') { | ||
print('Is Linux'); | ||
} else { | ||
print('SOMETHING ELSE'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,8 @@ | ||
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
/// Functionality to override information about the current platform. | ||
library pkg.os_detect.override; | ||
library; | ||
|
||
import 'dart:async' show Zone, runZoned; | ||
|
||
import 'src/osid_unknown.dart' | ||
if (dart.library.io) 'src/osid_io.dart' | ||
if (dart.library.html) 'src/osid_html.dart'; | ||
|
||
/// The name and version of an operating system. | ||
class OperatingSystem { | ||
/// The current operating system ID. | ||
/// | ||
/// Defaults to what information is available | ||
/// from known platform specific libraries, | ||
/// but can be overridden using functionality from the | ||
/// `osid_override.dart` library. | ||
static OperatingSystem get current => | ||
Zone.current[#_os] as OperatingSystem? ?? platformOS; | ||
|
||
/// A string representing the operating system or platform. | ||
final String id; | ||
|
||
/// A string representing the version of the operating system or platform. | ||
/// | ||
/// May be empty if no version is known or available. | ||
final String version; | ||
|
||
/// Creates an operating system ID with the provided values. | ||
const OperatingSystem(this.id, this.version); | ||
} | ||
|
||
/// Convenience operations on [OperatingSystem]. | ||
/// | ||
/// Implemented as extensions to allow users to *implement* [OperatingSystem] | ||
/// without having to implement all of these getters. | ||
extension OperatingSystemGetters on OperatingSystem { | ||
/// Whether the operating system is a version of | ||
/// [Linux](https://en.wikipedia.org/wiki/Linux). | ||
/// | ||
/// Identified by [id] being the string `linux`. | ||
/// | ||
/// This value is `false` if the operating system is a specialized | ||
/// version of Linux that identifies itself by a different name, | ||
/// for example Android (see [isAndroid]). | ||
bool get isLinux => 'linux' == id; | ||
|
||
/// Whether the operating system is a version of | ||
/// [macOS](https://en.wikipedia.org/wiki/MacOS). | ||
/// | ||
/// Identified by [id] being the string `macos`. | ||
bool get isMacOS => 'macos' == id; | ||
|
||
/// Whether the operating system is a version of | ||
/// [Microsoft Windows](https://en.wikipedia.org/wiki/Microsoft_Windows). | ||
/// | ||
/// Identified by [id] being the string `windows`. | ||
bool get isWindows => 'windows' == id; | ||
|
||
/// Whether the operating system is a version of | ||
/// [Android](https://en.wikipedia.org/wiki/Android_%28operating_system%29). | ||
/// | ||
/// Identified by [id] being the string `android`. | ||
bool get isAndroid => 'android' == id; | ||
|
||
/// Whether the operating system is a version of | ||
/// [iOS](https://en.wikipedia.org/wiki/IOS). | ||
/// | ||
/// Identified by [id] being the string `ios`. | ||
bool get isIOS => 'ios' == id; | ||
|
||
/// Whether the operating system is a version of | ||
/// [Fuchsia](https://en.wikipedia.org/wiki/Google_Fuchsia). | ||
/// | ||
/// Identified by [id] being the string `fuchsia`. | ||
bool get isFuchsia => 'fuchsia' == id; | ||
|
||
/// Whether running in a web browser. | ||
/// | ||
/// Identified by [id] being the string `browser`. | ||
/// | ||
/// If so, the [version] is the string made available | ||
/// through `window.navigator.appVersion`. | ||
bool get isBrowser => 'browser' == id; | ||
} | ||
|
||
/// Run [body] in a zone with platform overrides. | ||
/// | ||
/// Overrides [OperatingSystem.current] with the supplied [operatingSystem] | ||
/// value while running in a new zone, and then runs [body] in that zone. | ||
/// | ||
/// This override affects the `operatingSystem` and `version` | ||
/// exported by `package:osid/osid.dart`. | ||
R overrideOperatingSystem<R>( | ||
OperatingSystem operatingSystem, R Function() body) => | ||
runZoned(body, zoneValues: {#_os: operatingSystem}); | ||
export 'src/os_override.dart' show OperatingSystem, overrideOperatingSystem; |
Oops, something went wrong.