-
Notifications
You must be signed in to change notification settings - Fork 9
Use the VM's "platform-const" feature to achieve tree-shaking. #31
Changes from all commits
870e614
49043ca
ba441bd
a4afc4e
55fe08a
9cfc627
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
|
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; | ||
} |
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'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,19 @@ | |
/// Information about the current operating system. | ||
library pkg.os_detect; | ||
|
||
import 'override.dart'; | ||
import 'src/os_override.dart'; | ||
|
||
/// Identification of the current operating system or platform. | ||
/// | ||
/// Specific known operating systems are reported by a unique string. | ||
/// Specific known operating systems are reported by a unique known string, | ||
/// and all the `is<Name>` values are computed by comparing the | ||
/// [operatingSystem] string against those known strings. | ||
/// That means that *at most* one of those value can be `true`, | ||
/// and usually precisely one will be `true`. | ||
/// | ||
/// **Notice:** Programs running in a browser will report their | ||
/// operating system as `"browser"`, not the operating system | ||
/// that browser is running on. See [isBrowser]. | ||
String get operatingSystem => OperatingSystem.current.id; | ||
|
||
/// Representation of the version of the current operating system or platform. | ||
|
@@ -24,37 +32,59 @@ String get operatingSystemVersion => OperatingSystem.current.version; | |
/// | ||
/// 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]). | ||
/// for example Android (see [isAndroid]), | ||
/// or if the code is running inside a browser (see [isBrowser]). | ||
@pragma('vm:prefer-inline') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Notice that this annotation will not make guarantees, the new Even if the method is inlined, it may happen in the C++ compiler backend. That has also a tree-shaker, but we want to ensure it's tree shaken before our kernel-level global analysis pass. We have this new platform-const annotation for that purpose. If there's subsets of expression/statement language missing to make it work, @sstrickl can implement that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not expecting this getter to be platform-const, that's going to be impossible when we also have the ability to do runtime overrides. However, if inlined twice, and all but one |
||
bool get isLinux => OperatingSystem.current.isLinux; | ||
|
||
/// Whether the current operating system is a version of | ||
/// [macOS](https://en.wikipedia.org/wiki/MacOS). | ||
/// | ||
/// Identified by [operatingSystem] being the string `macos`. | ||
/// | ||
/// The value is `false` if the code is running inside a browser, | ||
/// even if that browser is running on MacOS (see [isBrowser]). | ||
@pragma('vm:prefer-inline') | ||
bool get isMacOS => OperatingSystem.current.isMacOS; | ||
|
||
/// Whether the current operating system is a version of | ||
/// [Microsoft Windows](https://en.wikipedia.org/wiki/Microsoft_Windows). | ||
/// | ||
/// Identified by [operatingSystem] being the string `windows`. | ||
/// | ||
/// The value is `false` if the code is running inside a browser, | ||
/// even if that browser is running on Windows (see [isBrowser]). | ||
@pragma('vm:prefer-inline') | ||
bool get isWindows => OperatingSystem.current.isWindows; | ||
|
||
/// Whether the current operating system is a version of | ||
/// [Android](https://en.wikipedia.org/wiki/Android_%28operating_system%29). | ||
/// | ||
/// Identified by [operatingSystem] being the string `android`. | ||
/// | ||
/// The value is `false` if the code is running inside a browser, | ||
/// even if that browser is running on Android (see [isBrowser]). | ||
@pragma('vm:prefer-inline') | ||
bool get isAndroid => OperatingSystem.current.isAndroid; | ||
|
||
/// Whether the current operating system is a version of | ||
/// [iOS](https://en.wikipedia.org/wiki/IOS). | ||
/// | ||
/// Identified by [operatingSystem] being the string `ios`. | ||
/// | ||
/// The value is `false` if the code is running inside a browser, | ||
/// even if that browser is running on iOS (see [isBrowser]). | ||
@pragma('vm:prefer-inline') | ||
bool get isIOS => OperatingSystem.current.isIOS; | ||
|
||
/// Whether the current operating system is a version of | ||
/// [Fuchsia](https://en.wikipedia.org/wiki/Google_Fuchsia). | ||
/// | ||
/// Identified by [operatingSystem] being the string `fuchsia`. | ||
/// | ||
/// The value is `false` if the code is running inside a browser, | ||
/// even if that browser is running on Fuchsia (see [isBrowser]). | ||
@pragma('vm:prefer-inline') | ||
bool get isFuchsia => OperatingSystem.current.isFuchsia; | ||
|
||
/// Whether running in a web browser. | ||
|
@@ -63,4 +93,12 @@ bool get isFuchsia => OperatingSystem.current.isFuchsia; | |
/// | ||
/// If so, the [operatingSystemVersion] is the string made available | ||
/// through `window.navigator.appVersion`. | ||
/// | ||
/// The value is `true` when the code is running inside a browser, | ||
/// no matter which operating system the browser is itself running on. | ||
/// No attempt is made to detect the underlying operating system. | ||
/// That information *may* be derived from [operatingSystemVersion], | ||
/// but browsers are able to lie in the app-version/user-agent | ||
/// string. | ||
@pragma('vm:prefer-inline') | ||
bool get isBrowser => OperatingSystem.current.isBrowser; |
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; |
Uh oh!
There was an error while loading. Please reload this page.