Skip to content

Commit

Permalink
Merge pull request #402 from dart-lang/merge-os_detect-package
Browse files Browse the repository at this point in the history
Merge `package:os_detect`
  • Loading branch information
mosuem authored Oct 17, 2024
2 parents 8151b9f + 64a2a5d commit 1a3e04e
Show file tree
Hide file tree
Showing 22 changed files with 855 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .github/ISSUE_TEMPLATE/os_detect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
name: "package:os_detect"
about: "Create a bug or file a feature request against package:os_detect."
labels: "package:os_detect"
---
4 changes: 4 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@
"package:logging":
- changed-files:
- any-glob-to-any-file: 'pkgs/logging/**'

"package:os_detect":
- changed-files:
- any-glob-to-any-file: 'pkgs/os_detect/**'
73 changes: 73 additions & 0 deletions .github/workflows/os_detect.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: package:os_detect

on:
# Run CI on pushes to the main branch, and on PRs against main.
push:
branches: [ main ]
paths:
- '.github/workflows/os_detect.yaml'
- 'pkgs/os_detect/**'
pull_request:
branches: [ main ]
paths:
- '.github/workflows/os_detect.yaml'
- 'pkgs/os_detect/**'
schedule:
- cron: "0 0 * * 0"
env:
PUB_ENVIRONMENT: bot.github

defaults:
run:
working-directory: pkgs/os_detect/

jobs:
# Check code formatting and static analysis on a single OS (linux)
# against Dart dev.
analyze:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
sdk: [dev]
steps:
- uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938
- uses: dart-lang/setup-dart@0a8a0fc875eb934c15d08629302413c671d3f672
with:
sdk: ${{ matrix.sdk }}
- id: install
name: Install dependencies
run: dart pub get
- name: Check formatting
run: dart format --output=none --set-exit-if-changed .
if: always() && steps.install.outcome == 'success'
- name: Analyze code
run: dart analyze --fatal-infos
if: always() && steps.install.outcome == 'success'

# Run tests on a matrix consisting of two dimensions:
# 1. OS: ubuntu-latest, (macos-latest, windows-latest)
# 2. release channel: dev
test:
needs: analyze
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
# Add macos-latest and/or windows-latest if relevant for this package.
os: [ubuntu-latest, windows-latest, macos-latest]
sdk: [3.0.0, dev]
steps:
- uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938
- uses: dart-lang/setup-dart@0a8a0fc875eb934c15d08629302413c671d3f672
with:
sdk: ${{ matrix.sdk }}
- id: install
name: Install dependencies
run: dart pub get
- name: Run VM tests
run: dart test --platform vm
if: always() && steps.install.outcome == 'success'
- name: Run Chrome tests
run: dart test --platform chrome
if: always() && steps.install.outcome == 'success'
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This repository is home to various Dart packages under the [dart.dev](https://pu
| [crypto](pkgs/crypto/) | Implementations of SHA, MD5, and HMAC cryptographic functions. | [![pub package](https://img.shields.io/pub/v/crypto.svg)](https://pub.dev/packages/crypto) |
| [fixnum](pkgs/fixnum/) | Library for 32- and 64-bit signed fixed-width integers. | [![pub package](https://img.shields.io/pub/v/fixnum.svg)](https://pub.dev/packages/fixnum) |
| [logging](pkgs/logging/) | Provides APIs for debugging and error logging. | [![pub package](https://img.shields.io/pub/v/logging.svg)](https://pub.dev/packages/logging) |
| [os_detect](pkgs/os_detect/) | Platform independent OS detection. | [![pub package](https://img.shields.io/pub/v/os_detect.svg)](https://pub.dev/packages/os_detect) |

## Publishing automation

Expand Down
3 changes: 3 additions & 0 deletions pkgs/os_detect/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.dart_tool/
.packages
pubspec.lock
6 changes: 6 additions & 0 deletions pkgs/os_detect/AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Below is a list of people and organizations that have contributed
# to the Dart project. Names should be added to the list like so:
#
# Name/Organization <email address>

Google LLC
17 changes: 17 additions & 0 deletions pkgs/os_detect/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## 2.0.2

- Require Dart 3.0
- Make work with VM's platform-constants.
- Move to `dart-lang/core` monorepo.

## 2.0.1

- Populate the pubspec `repository` field.

## 2.0.0

- Stable null safety release.

## 1.0.0

- Initial release
27 changes: 27 additions & 0 deletions pkgs/os_detect/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright 2020, the Dart project authors.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Google LLC nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 changes: 42 additions & 0 deletions pkgs/os_detect/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[![Dart CI](https://github.com/dart-lang/core/actions/workflows/os_detect.yaml/badge.svg)](https://github.com/dart-lang/core/actions/workflows/os_detect.yaml)
[![pub package](https://img.shields.io/pub/v/os_detect.svg)](https://pub.dev/packages/os_detect)
[![package publisher](https://img.shields.io/pub/publisher/os_detect.svg)](https://pub.dev/packages/os_detect/publisher)

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:

```dart
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.

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`.
27 changes: 27 additions & 0 deletions pkgs/os_detect/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# https://dart.dev/tools/analysis#the-analysis-options-file
include: package:dart_flutter_team_lints/analysis_options.yaml

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

linter:
rules:
- avoid_bool_literals_in_conditional_expressions
- avoid_classes_with_only_static_members
- avoid_private_typedef_functions
- avoid_redundant_argument_values
- avoid_returning_this
- avoid_unused_constructor_parameters
- avoid_void_async
- literal_only_boolean_expressions
- missing_whitespace_between_adjacent_strings
- no_adjacent_strings_in_list
- no_runtimeType_toString
- package_api_docs
- prefer_const_declarations
- use_raw_strings


40 changes: 40 additions & 0 deletions pkgs/os_detect/bin/os_detect.dart
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;
}
26 changes: 26 additions & 0 deletions pkgs/os_detect/example/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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 ID: ${os_detect.operatingSystem}
OS Version: ${os_detect.operatingSystemVersion}''');
if (os_detect.isAndroid) {
print(' OS Type: Android');
} else if (os_detect.isBrowser) {
print(' OS Type: Browser');
} else if (os_detect.isFuchsia) {
print(' OS Type: Fuchsia');
} else if (os_detect.isIOS) {
print(' OS Type: iOS');
} else if (os_detect.isLinux) {
print(' OS Type: Linux');
} else if (os_detect.isMacOS) {
print(' OS Type: MacOS');
} else if (os_detect.isWindows) {
print(' OS Type: Windows');
}
}
29 changes: 29 additions & 0 deletions pkgs/os_detect/example/tree_shaking.dart
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');
}
}
Loading

0 comments on commit 1a3e04e

Please sign in to comment.