Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.
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
2 changes: 1 addition & 1 deletion .github/workflows/test-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
matrix:
# Add macos-latest and/or windows-latest if relevant for this package.
os: [ubuntu-latest, windows-latest, macos-latest]
sdk: [2.12.0, dev]
sdk: [2.18.0, dev]
steps:
- uses: actions/checkout@755da8c3cf115ac066823e79a1e1788f8940201b
- uses: dart-lang/setup-dart@6a218f2413a3e78e9087f638a238f6b40893203d
Expand Down
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.2-dev

- Require Dart 2.18

## 2.0.1

- Populate the pubspec `repository` field.
Expand All @@ -6,10 +10,6 @@

- Stable null safety release.

## 2.0.0-nullsafety.0

- Migrate to null safety.

## 1.0.0

- Initial release
56 changes: 27 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,37 @@ Platform independent access to information about the current operating system.

## Querying the current OS

Exposes `operatingSystem` and `operatingSystemVersion` strings
similar to those of the `Platform` class in `dart:io`,
but also works on the web.
The `operatingSystem` of a browser is the string "browser".
Also exposes convenience getters like `isLinux`, `isAndroid` and `isBrowser`
based on the `operatingSystem` string.

To use this package instead of `dart:io`, replace
the import of `dart:io` with:
Exposes `operatingSystem` and `operatingSystemVersion` strings similar to those
of the `Platform` class in `dart:io`, but also works on the web. The
`operatingSystem` of a browser is the string "browser". Also exposes convenience
getters like `isLinux`, `isAndroid` and `isBrowser` based on the
`operatingSystem` string.

To use this package instead of `dart:io`, replace the import of `dart:io` with:

```dart
import "package:os_detect/os_detect.dart" as Platform;
import 'package:os_detect/os_detect.dart' as os_detect;
```

That should keep the code working if the only functionality used from `dart:io`
is operating system detection.
You should then use your IDE to rename the import prefix from `Platform`
to something lower-cased which follows the style guide for import prefixes.
is operating system detection. You should then use your IDE to rename the import
prefix from `Platform` to something lower-cased which follows the style guide
for import prefixes.

Any new platform which supports neither `dart:io` nor `dart:html`
can make itself recognizable by configuring
the `dart.os.name` and `dart.os.version` environment settings,
so that `const String.fromEnvironment` can access them.
Any new platform which supports neither `dart:io` nor `dart:html` can make
itself recognizable by configuring the `dart.os.name` and `dart.os.version`
environment settings, so that `const String.fromEnvironment` can access them.

## Overriding the current OS string

It's possible to override the current operating system string,
as exposed by `operatingSystem` and `operatingSystemVersion`
in `package:os_detect/os_detect.dart`.
To do so, import the `package:os_detect/override.dart` library
and use the `overrideOperatingSystem` function to run code in a zone
where the operating system and version values are set
to whatever values are desired.

The class `OperatingSystemID` can also be used directly to
abstract over the operating system name and version.
The `OperatingSystemID.current` defaults to the values provided by the platform
when not overridden using `overrideOperatingSystem`.
It's possible to override the current operating system string, as exposed by
`operatingSystem` and `operatingSystemVersion` in
`package:os_detect/os_detect.dart`. To do so, import the
`package:os_detect/override.dart` library and use the `overrideOperatingSystem`
function to run code in a zone where the operating system and version values are
set to whatever values are desired.

The class `OperatingSystemID` can also be used directly to abstract over the
operating system name and version. The `OperatingSystemID.current` defaults to
the values provided by the platform when not overridden using
`overrideOperatingSystem`.
53 changes: 50 additions & 3 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,55 @@
# Copyright (c) 2020, 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.
# https://dart.dev/guides/language/analysis-options
include: package:lints/recommended.yaml

analyzer:
language:
strict-casts: true
strict-inference: true
strict-raw-types: true

linter:
rules:
- always_declare_return_types
- avoid_bool_literals_in_conditional_expressions
- avoid_catching_errors
- avoid_classes_with_only_static_members
- avoid_dynamic_calls
- avoid_private_typedef_functions
- avoid_redundant_argument_values
- avoid_returning_null_for_future
- avoid_returning_this
- avoid_unused_constructor_parameters
- avoid_void_async
- cancel_subscriptions
- comment_references
- directives_ordering
- join_return_with_assignment
- lines_longer_than_80_chars
- literal_only_boolean_expressions
- missing_whitespace_between_adjacent_strings
- no_adjacent_strings_in_list
- no_runtimeType_toString
- omit_local_variable_types
- only_throw_errors
- package_api_docs
- prefer_asserts_in_initializer_lists
- prefer_const_constructors
- prefer_const_declarations
- prefer_expression_function_bodies
- prefer_final_locals
- prefer_relative_imports
- prefer_single_quotes
- sort_pub_dependencies
- test_types_in_equals
- throw_in_finally
- type_annotate_public_apis
- unawaited_futures
- unnecessary_await_in_return
- unnecessary_lambdas
- unnecessary_parenthesis
- unnecessary_raw_strings
- unnecessary_statements
- use_if_null_to_convert_nulls_to_bools
- use_raw_strings
- use_string_buffers
- use_super_parameters
12 changes: 12 additions & 0 deletions example/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 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.

import 'package:os_detect/os_detect.dart' as os_detect;

void main() {
print('''
OS: ${os_detect.operatingSystem}
OS Version: ${os_detect.operatingSystemVersion}
''');
}
27 changes: 13 additions & 14 deletions lib/override.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
/// Functionality to override information about the current platform.
library pkg.os_detect.override;

import "dart:async" show Zone, runZoned;
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";
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 {
Expand Down Expand Up @@ -47,55 +47,54 @@ extension OperatingSystemGetters on OperatingSystem {
/// 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);
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);
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);
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);
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);
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);
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);
bool get isBrowser => 'browser' == id;
}

/// Run [body] in a zone with platform overrides.
///
/// Overrides [OperatingSystem.current] with the supplied [osId]
/// 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) {
return runZoned(body, zoneValues: {#_os: operatingSystem});
}
OperatingSystem operatingSystem, R Function() body) =>
runZoned(body, zoneValues: {#_os: operatingSystem});
4 changes: 2 additions & 2 deletions lib/src/osid_html.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// 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.

import "dart:html";
import 'dart:html';

import '../override.dart';

const String _os = "browser";
const String _os = 'browser';
String get _osVersion => window.navigator.appVersion;

final OperatingSystem platformOS = OperatingSystem(_os, _osVersion);
2 changes: 1 addition & 1 deletion lib/src/osid_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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.

import "dart:io";
import 'dart:io';

import '../override.dart';

Expand Down
4 changes: 2 additions & 2 deletions lib/src/osid_unknown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import '../override.dart';

const String _os =
String.fromEnvironment("dart.os.name", defaultValue: "unknown");
const String _osVersion = String.fromEnvironment("dart.os.version");
String.fromEnvironment('dart.os.name', defaultValue: 'unknown');
const String _osVersion = String.fromEnvironment('dart.os.version');

const OperatingSystem platformOS = OperatingSystem(_os, _osVersion);
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: os_detect
version: 2.0.1
version: 2.0.2-dev
description: Platform independent OS detection.
repository: https://github.com/dart-lang/os_detect

environment:
sdk: '>=2.12.0 <3.0.0'
sdk: '>=2.18.0 <3.0.0'

dev_dependencies:
lints: ^1.0.0
lints: ^2.0.0
test: ^1.16.8
36 changes: 18 additions & 18 deletions test/osid_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,36 @@
// 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.

import "dart:async";
import 'dart:async';

import "package:os_detect/os_detect.dart";
import 'package:os_detect/os_detect.dart';
import 'package:os_detect/override.dart';
import "package:test/test.dart";
import 'package:test/test.dart';

void main() {
test("Exists and is consistent", () {
test('Exists and is consistent', () {
expect(operatingSystem, isNotNull);
expect(operatingSystemVersion, isNotNull);

expect(isLinux, operatingSystem == "linux");
expect(isAndroid, operatingSystem == "android");
expect(isMacOS, operatingSystem == "macos");
expect(isWindows, operatingSystem == "windows");
expect(isIOS, operatingSystem == "ios");
expect(isFuchsia, operatingSystem == "fuchsia");
expect(isBrowser, operatingSystem == "browser");
expect(isLinux, operatingSystem == 'linux');
expect(isAndroid, operatingSystem == 'android');
expect(isMacOS, operatingSystem == 'macos');
expect(isWindows, operatingSystem == 'windows');
expect(isIOS, operatingSystem == 'ios');
expect(isFuchsia, operatingSystem == 'fuchsia');
expect(isBrowser, operatingSystem == 'browser');
});

test("Override", () {
const overrideName = "argle-bargle";
const overrideVersion = "glop-glyf";
test('Override', () {
const overrideName = 'argle-bargle';
const overrideVersion = 'glop-glyf';
const overrideOS = OperatingSystem(overrideName, overrideVersion);
Zone? overrideZone;

var originalName = operatingSystem;
var originalVersion = operatingSystemVersion;
var originalID = OperatingSystem.current;
var originalZone = Zone.current;
final originalName = operatingSystem;
final originalVersion = operatingSystemVersion;
final originalID = OperatingSystem.current;
final originalZone = Zone.current;
expect(originalName, isNot(overrideName));
expect(originalVersion, isNot(overrideVersion));

Expand Down