Skip to content

Commit

Permalink
Merge pull request #6 from PlugFox/feature/null_safety
Browse files Browse the repository at this point in the history
Feature/null safety
  • Loading branch information
PlugFox authored Feb 18, 2021
2 parents 826c30a + 42cd3e4 commit b1d962f
Show file tree
Hide file tree
Showing 13 changed files with 270 additions and 296 deletions.
26 changes: 14 additions & 12 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
name: platform_info

on:
push:
branches: [ master, dev ]
branches: [ master, dev, feature/null_safety ]
pull_request:
branches: [ master, dev ]
branches: [ master, dev, feature/null_safety ]

jobs:
build:
runs-on: ubuntu-latest
container:
image: google/dart:latest
image: google/dart:2.12-dev
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 2
- name: Install Dependencies
run: pub get
- name: Format
run: dartfmt --dry-run --set-exit-if-changed lib
- name: Run analyzer
run: dartanalyzer --fatal-infos --fatal-warnings lib example test
run: dart pub get
- name: Check format
run: dart format --set-exit-if-changed none
- name: Check analyzer
run: dart analyze --fatal-infos --fatal-warnings .
- name: Run tests
run: pub run test_coverage
run: dart test --coverage=coverage && dart run coverage:format_coverage --lcov --in=coverage --out=coverage/lcov.info --packages=.packages --report-on=lib
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
- name: Check Code Coverage
uses: ChicagoFlutter/lcov-cop@v1.0.2
uses: VeryGoodOpenSource/very_good_coverage@v1.1.1
with:
path: coverage/lcov.info
min_coverage: 100
exclude: "**/*.g.dart"
2 changes: 2 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ analyzer:
- 'assets/**'
# Mocks for test
- 'test/data/**'
# Coverage
- 'test/.test_coverage.dart'

strong-mode:
implicit-casts: false
Expand Down
31 changes: 16 additions & 15 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ void main(List<String> args) {
print(platform.numberOfProcessors.gcd(1));

final string = platform.when(
io: () => platform.when(
fuchsia: () => 'io fuchsia',
windows: () => 'io windows',
android: () => 'io android',
iOS: () => 'io iOS',
macOS: () => 'io macOS',
linux: () => 'io linux',
unknown: () => 'io unknown',
),
web: () => platform.when(
material: () => 'web Android or Fuchsia',
cupertino: () => 'web macOS or iOS',
orElse: () => 'web Windows or Linux or unknown',
),
);
io: () => platform.when(
fuchsia: () => 'io fuchsia',
windows: () => 'io windows',
android: () => 'io android',
iOS: () => 'io iOS',
macOS: () => 'io macOS',
linux: () => 'io linux',
unknown: () => 'io unknown',
),
web: () => platform.when(
material: () => 'web Android or Fuchsia',
cupertino: () => 'web macOS or iOS',
orElse: () => 'web Windows or Linux or unknown',
),
) ??
'<unknown platform>';
print(string);
}
5 changes: 4 additions & 1 deletion lib/src/base_host_platform.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'package:meta/meta.dart' show immutable;

import 'enums.dart';

/// Host platform
/// contain info about host device
@immutable
abstract class HostPlatform {
/// Host platform type
/// + io
Expand All @@ -28,7 +31,7 @@ abstract class HostPlatform {

/// The number of individual execution units of the machine
/// Returns 0 if unknown or not available
/// Use `.gcd(1)` if you need a quantity greater than 0
/// Use `.clamp(1, double.infinity)` if you need a quantity greater than 0
int get numberOfProcessors;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/constants.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'base_host_platform.dart' show HostPlatform;
import 'default_host_platform.dart';
import 'enums.dart';
import 'stub_host_platform.dart' show DefaultHostPlatform;

/// List of all mobile phone operating systems
const List<OperatingSystem> kListOSForMobile = <OperatingSystem>[
Expand Down
28 changes: 28 additions & 0 deletions lib/src/default_host_platform.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:meta/meta.dart' show immutable, literal;

import 'base_host_platform.dart' show HostPlatform;
import 'enums.dart';

/// Unknown host platform with default values
@immutable
class DefaultHostPlatform implements HostPlatform {
/// Unknown host platform with default values
@literal
const DefaultHostPlatform();

@override
HostPlatformType get type =>
identical(0, 0.0) ? HostPlatformType.web : HostPlatformType.io;

@override
OperatingSystem get operatingSystem => OperatingSystem.unknown;

@override
String get version => '<unknown>';

@override
String get locale => 'en';

@override
int get numberOfProcessors => 0;
}
24 changes: 9 additions & 15 deletions lib/src/io_host_platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class _IOHostPlatform implements HostPlatform {
_IOHostPlatform._();

static bool get _isUnknownEnvironment =>
Zone.current[#platform_info_test.isUnknownEnvironment] as bool ?? false;
Zone.current[#platform_info_test.isUnknownEnvironment] as bool? ?? false;

static bool get _isKnownEnvironment => !_isUnknownEnvironment;

Expand Down Expand Up @@ -53,32 +53,26 @@ class _IOHostPlatform implements HostPlatform {

static String _getVersion() {
if (_isKnownEnvironment) {
final operatingSystemVersion = io.Platform?.operatingSystemVersion;
if (operatingSystemVersion != null) {
return operatingSystemVersion;
}
return io.Platform.operatingSystemVersion;
}
return kDefaultHostPlatform.version;
}

static int _numberOfProcessors() {
if (_isKnownEnvironment) {
final numberOfProcessors = io.Platform?.numberOfProcessors;
if (numberOfProcessors != null) {
return numberOfProcessors;
}
return io.Platform.numberOfProcessors;
}
return kDefaultHostPlatform.numberOfProcessors;
}

static String _getLocale() {
final lang = io.Platform.localeName
?.split('-')
?.first
?.split('_')
?.first
?.trim()
?.toLowerCase();
.split('-')
.first
.split('_')
.first
.trim()
.toLowerCase();
if (_isUnknownEnvironment || lang is! String || lang.length != 2) {
return kDefaultHostPlatform.locale;
}
Expand Down
39 changes: 21 additions & 18 deletions lib/src/methods.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'enums.dart';
mixin PlatformMethods on ExtendedHostPlatform {
/// Run functions that satisfy the current state of the platform.
/// You can use nested methods to compose more complex queries.
/// Can return null, if [orElse] not set and any callback was not called.
///
/// ### Operating System
/// [fuchsia] - whether the operating system is a version of Fuchsia
Expand Down Expand Up @@ -53,33 +54,35 @@ mixin PlatformMethods on ExtendedHostPlatform {
/// macOS: () => ...,
/// linux: () => ...,
/// unknown: () => ...,
/// orElse: () => ...,
/// ),
/// web: () => platform.when(
/// material: () => ...,
/// cupertino: () => ...,
/// orElse: () => ...,
/// ),
/// orElse: () => ...,
/// );
/// ```
///
PlatformResult when<PlatformResult extends Object>({
PlatformResult Function() fuchsia,
PlatformResult Function() windows,
PlatformResult Function() android,
PlatformResult Function() iOS,
PlatformResult Function() macOS,
PlatformResult Function() linux,
PlatformResult Function() unknown,
PlatformResult Function() material,
PlatformResult Function() cupertino,
PlatformResult Function() mobile,
PlatformResult Function() desktop,
PlatformResult Function() io,
PlatformResult Function() web,
PlatformResult Function() release,
PlatformResult Function() profile,
PlatformResult Function() debug,
PlatformResult Function() orElse,
PlatformResult? when<PlatformResult>({
PlatformResult Function()? fuchsia,
PlatformResult Function()? windows,
PlatformResult Function()? android,
PlatformResult Function()? iOS,
PlatformResult Function()? macOS,
PlatformResult Function()? linux,
PlatformResult Function()? unknown,
PlatformResult Function()? material,
PlatformResult Function()? cupertino,
PlatformResult Function()? mobile,
PlatformResult Function()? desktop,
PlatformResult Function()? io,
PlatformResult Function()? web,
PlatformResult Function()? release,
PlatformResult Function()? profile,
PlatformResult Function()? debug,
PlatformResult Function()? orElse,
}) {
{
// Operating System
Expand Down
Loading

0 comments on commit b1d962f

Please sign in to comment.