Skip to content

Commit 1a3e04e

Browse files
authored
Merge pull request #402 from dart-lang/merge-os_detect-package
Merge `package:os_detect`
2 parents 8151b9f + 64a2a5d commit 1a3e04e

22 files changed

+855
-0
lines changed

.github/ISSUE_TEMPLATE/os_detect.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
name: "package:os_detect"
3+
about: "Create a bug or file a feature request against package:os_detect."
4+
labels: "package:os_detect"
5+
---

.github/labeler.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@
2727
"package:logging":
2828
- changed-files:
2929
- any-glob-to-any-file: 'pkgs/logging/**'
30+
31+
"package:os_detect":
32+
- changed-files:
33+
- any-glob-to-any-file: 'pkgs/os_detect/**'

.github/workflows/os_detect.yaml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
name: package:os_detect
2+
3+
on:
4+
# Run CI on pushes to the main branch, and on PRs against main.
5+
push:
6+
branches: [ main ]
7+
paths:
8+
- '.github/workflows/os_detect.yaml'
9+
- 'pkgs/os_detect/**'
10+
pull_request:
11+
branches: [ main ]
12+
paths:
13+
- '.github/workflows/os_detect.yaml'
14+
- 'pkgs/os_detect/**'
15+
schedule:
16+
- cron: "0 0 * * 0"
17+
env:
18+
PUB_ENVIRONMENT: bot.github
19+
20+
defaults:
21+
run:
22+
working-directory: pkgs/os_detect/
23+
24+
jobs:
25+
# Check code formatting and static analysis on a single OS (linux)
26+
# against Dart dev.
27+
analyze:
28+
runs-on: ubuntu-latest
29+
strategy:
30+
fail-fast: false
31+
matrix:
32+
sdk: [dev]
33+
steps:
34+
- uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938
35+
- uses: dart-lang/setup-dart@0a8a0fc875eb934c15d08629302413c671d3f672
36+
with:
37+
sdk: ${{ matrix.sdk }}
38+
- id: install
39+
name: Install dependencies
40+
run: dart pub get
41+
- name: Check formatting
42+
run: dart format --output=none --set-exit-if-changed .
43+
if: always() && steps.install.outcome == 'success'
44+
- name: Analyze code
45+
run: dart analyze --fatal-infos
46+
if: always() && steps.install.outcome == 'success'
47+
48+
# Run tests on a matrix consisting of two dimensions:
49+
# 1. OS: ubuntu-latest, (macos-latest, windows-latest)
50+
# 2. release channel: dev
51+
test:
52+
needs: analyze
53+
runs-on: ${{ matrix.os }}
54+
strategy:
55+
fail-fast: false
56+
matrix:
57+
# Add macos-latest and/or windows-latest if relevant for this package.
58+
os: [ubuntu-latest, windows-latest, macos-latest]
59+
sdk: [3.0.0, dev]
60+
steps:
61+
- uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938
62+
- uses: dart-lang/setup-dart@0a8a0fc875eb934c15d08629302413c671d3f672
63+
with:
64+
sdk: ${{ matrix.sdk }}
65+
- id: install
66+
name: Install dependencies
67+
run: dart pub get
68+
- name: Run VM tests
69+
run: dart test --platform vm
70+
if: always() && steps.install.outcome == 'success'
71+
- name: Run Chrome tests
72+
run: dart test --platform chrome
73+
if: always() && steps.install.outcome == 'success'

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This repository is home to various Dart packages under the [dart.dev](https://pu
1515
| [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) |
1616
| [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) |
1717
| [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) |
18+
| [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) |
1819

1920
## Publishing automation
2021

pkgs/os_detect/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.dart_tool/
2+
.packages
3+
pubspec.lock

pkgs/os_detect/AUTHORS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Below is a list of people and organizations that have contributed
2+
# to the Dart project. Names should be added to the list like so:
3+
#
4+
# Name/Organization <email address>
5+
6+
Google LLC

pkgs/os_detect/CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## 2.0.2
2+
3+
- Require Dart 3.0
4+
- Make work with VM's platform-constants.
5+
- Move to `dart-lang/core` monorepo.
6+
7+
## 2.0.1
8+
9+
- Populate the pubspec `repository` field.
10+
11+
## 2.0.0
12+
13+
- Stable null safety release.
14+
15+
## 1.0.0
16+
17+
- Initial release

pkgs/os_detect/LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright 2020, the Dart project authors.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following
11+
disclaimer in the documentation and/or other materials provided
12+
with the distribution.
13+
* Neither the name of Google LLC nor the names of its
14+
contributors may be used to endorse or promote products derived
15+
from this software without specific prior written permission.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

pkgs/os_detect/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[![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)
2+
[![pub package](https://img.shields.io/pub/v/os_detect.svg)](https://pub.dev/packages/os_detect)
3+
[![package publisher](https://img.shields.io/pub/publisher/os_detect.svg)](https://pub.dev/packages/os_detect/publisher)
4+
5+
Platform independent access to information about the current operating system.
6+
7+
## Querying the current OS
8+
9+
Exposes `operatingSystem` and `operatingSystemVersion` strings similar to those
10+
of the `Platform` class in `dart:io`, but also works on the web. The
11+
`operatingSystem` of a browser is the string "browser". Also exposes convenience
12+
getters like `isLinux`, `isAndroid` and `isBrowser` based on the
13+
`operatingSystem` string.
14+
15+
To use this package instead of `dart:io`, replace the import of `dart:io` with:
16+
17+
```dart
18+
import 'package:os_detect/os_detect.dart' as os_detect;
19+
```
20+
21+
That should keep the code working if the only functionality used from `dart:io`
22+
is operating system detection. You should then use your IDE to rename the import
23+
prefix from `Platform` to something lower-cased which follows the style guide
24+
for import prefixes.
25+
26+
Any new platform which supports neither `dart:io` nor `dart:html` can make
27+
itself recognizable by configuring the `dart.os.name` and `dart.os.version`
28+
environment settings, so that `const String.fromEnvironment` can access them.
29+
30+
## Overriding the current OS string
31+
32+
It's possible to override the current operating system string, as exposed by
33+
`operatingSystem` and `operatingSystemVersion` in
34+
`package:os_detect/os_detect.dart`. To do so, import the
35+
`package:os_detect/override.dart` library and use the `overrideOperatingSystem`
36+
function to run code in a zone where the operating system and version values are
37+
set to whatever values are desired.
38+
39+
The class `OperatingSystemID` can also be used directly to abstract over the
40+
operating system name and version. The `OperatingSystemID.current` defaults to
41+
the values provided by the platform when not overridden using
42+
`overrideOperatingSystem`.

pkgs/os_detect/analysis_options.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# https://dart.dev/tools/analysis#the-analysis-options-file
2+
include: package:dart_flutter_team_lints/analysis_options.yaml
3+
4+
analyzer:
5+
language:
6+
strict-casts: true
7+
strict-inference: true
8+
strict-raw-types: true
9+
10+
linter:
11+
rules:
12+
- avoid_bool_literals_in_conditional_expressions
13+
- avoid_classes_with_only_static_members
14+
- avoid_private_typedef_functions
15+
- avoid_redundant_argument_values
16+
- avoid_returning_this
17+
- avoid_unused_constructor_parameters
18+
- avoid_void_async
19+
- literal_only_boolean_expressions
20+
- missing_whitespace_between_adjacent_strings
21+
- no_adjacent_strings_in_list
22+
- no_runtimeType_toString
23+
- package_api_docs
24+
- prefer_const_declarations
25+
- use_raw_strings
26+
27+

pkgs/os_detect/bin/os_detect.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// Prints the operating system detected by the current compilation environment.
6+
library pkg.os_detect.run;
7+
8+
import 'package:os_detect/os_detect.dart' as os_detect;
9+
10+
void main() {
11+
final knownName = knownOSName();
12+
print('OS name : ${os_detect.operatingSystem} '
13+
'${knownName != null ? '($knownName)' : ''}');
14+
print('OS version : ${os_detect.operatingSystemVersion}');
15+
}
16+
17+
String? knownOSName() {
18+
if (os_detect.isAndroid) {
19+
return 'Android';
20+
}
21+
if (os_detect.isBrowser) {
22+
return 'Browser';
23+
}
24+
if (os_detect.isFuchsia) {
25+
return 'Fuchsia';
26+
}
27+
if (os_detect.isIOS) {
28+
return 'iOS';
29+
}
30+
if (os_detect.isLinux) {
31+
return 'Linux';
32+
}
33+
if (os_detect.isMacOS) {
34+
return 'MacOS';
35+
}
36+
if (os_detect.isWindows) {
37+
return 'Windows';
38+
}
39+
return null;
40+
}

pkgs/os_detect/example/example.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:os_detect/os_detect.dart' as os_detect;
6+
7+
void main() {
8+
print('''
9+
OS ID: ${os_detect.operatingSystem}
10+
OS Version: ${os_detect.operatingSystemVersion}''');
11+
if (os_detect.isAndroid) {
12+
print(' OS Type: Android');
13+
} else if (os_detect.isBrowser) {
14+
print(' OS Type: Browser');
15+
} else if (os_detect.isFuchsia) {
16+
print(' OS Type: Fuchsia');
17+
} else if (os_detect.isIOS) {
18+
print(' OS Type: iOS');
19+
} else if (os_detect.isLinux) {
20+
print(' OS Type: Linux');
21+
} else if (os_detect.isMacOS) {
22+
print(' OS Type: MacOS');
23+
} else if (os_detect.isWindows) {
24+
print(' OS Type: Windows');
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Try compiling this example with (if on Linux):
6+
//
7+
// dart compile exe --target-os=linux tree_shaking.dart
8+
//
9+
// then check that "SOMETHING ELSE" does not occur in the
10+
// output `tree_shaking.exe` program, e.g.:
11+
//
12+
// strings tree_shaking.exe | grep SOMETHING
13+
//
14+
// which shows no matches.
15+
16+
import 'package:os_detect/os_detect.dart' as platform;
17+
18+
void main() {
19+
if (platform.isLinux) {
20+
print('Is Linux');
21+
} else {
22+
print('SOMETHING ELSE');
23+
}
24+
if (platform.operatingSystem == 'linux') {
25+
print('Is Linux');
26+
} else {
27+
print('SOMETHING ELSE');
28+
}
29+
}

0 commit comments

Comments
 (0)