Skip to content

Commit 2576fe0

Browse files
authored
Merge pull request #13 from PlugFox/develop
3.1.0
2 parents b3462fa + 3166c17 commit 2576fe0

File tree

6 files changed

+276
-8
lines changed

6 files changed

+276
-8
lines changed

.github/workflows/main.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ name: platform_info
22

33
on:
44
push:
5-
branches: [ master, dev, feature/null_safety ]
5+
branches: [ master, dev, feature/** ]
66
pull_request:
7-
branches: [ master, dev, feature/null_safety ]
7+
branches: [ master, dev, feature/** ]
88

99
jobs:
10-
build:
10+
check:
1111
runs-on: ubuntu-latest
1212
container:
13-
image: google/dart:2.12
13+
image: google/dart:latest
1414
steps:
1515
- uses: actions/checkout@v2
1616
with:
@@ -22,7 +22,10 @@ jobs:
2222
- name: Check analyzer
2323
run: dart analyze --fatal-infos --fatal-warnings .
2424
- name: Run tests
25-
run: dart test --coverage=coverage && dart run coverage:format_coverage --lcov --in=coverage --out=coverage/lcov.info --packages=.packages --report-on=lib
25+
run: |
26+
dart test --coverage=coverage \
27+
&& dart run coverage:format_coverage --lcov --in=coverage \
28+
--out=coverage/lcov.info --packages=.packages --report-on=lib
2629
- name: Upload coverage to Codecov
2730
uses: codecov/codecov-action@v1
2831
with:

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.1.0 - 2021-05-29
2+
### Added
3+
- Extensions for enums BuildMode, HostPlatformType, OperatingSystem
4+
15
## 3.0.0 - 2021-03-27
26
### Changed
37
- Non nullable ready

lib/src/enums.dart

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,49 @@ enum BuildMode {
1313
debug
1414
}
1515

16+
/// Build mode extension
17+
extension BuildModeX on BuildMode {
18+
/// Is release build mode
19+
bool get isRelease => this == BuildMode.release;
20+
21+
/// Is profile build mode
22+
bool get isProfile => this == BuildMode.profile;
23+
24+
/// Is debug build mode
25+
bool get isDebug => this == BuildMode.debug;
26+
27+
/// Run callback on specific build mode
28+
BuildModeResult when<BuildModeResult extends Object?>({
29+
required BuildModeResult Function() release,
30+
required BuildModeResult Function() profile,
31+
required BuildModeResult Function() debug,
32+
}) {
33+
switch (this) {
34+
case BuildMode.profile:
35+
return profile();
36+
case BuildMode.debug:
37+
return debug();
38+
case BuildMode.release:
39+
default:
40+
return release();
41+
}
42+
}
43+
44+
/// Run callback on specific build mode,
45+
/// if not specified run orElse
46+
BuildModeResult maybeWhen<BuildModeResult extends Object?>({
47+
required BuildModeResult Function() orElse,
48+
BuildModeResult Function()? release,
49+
BuildModeResult Function()? profile,
50+
BuildModeResult Function()? debug,
51+
}) =>
52+
when<BuildModeResult>(
53+
debug: debug ?? orElse,
54+
profile: profile ?? orElse,
55+
release: release ?? orElse,
56+
);
57+
}
58+
1659
/// Host platform type
1760
/// + io (vm, desktops, mobile, console)
1861
/// + web (html, js, browser)
@@ -24,6 +67,22 @@ enum HostPlatformType {
2467
web,
2568
}
2669

70+
/// Host platform extension
71+
extension HostPlatformTypeX on HostPlatformType {
72+
/// Is I/O (vm, desktops, mobile, console)
73+
bool get isIO => this == HostPlatformType.io;
74+
75+
/// Is Web (html, js, browser)
76+
bool get isWeb => this == HostPlatformType.web;
77+
78+
/// Run callback on specific host platform
79+
HostPlatformTypeResult when<HostPlatformTypeResult extends Object?>({
80+
required HostPlatformTypeResult Function() io,
81+
required HostPlatformTypeResult Function() web,
82+
}) =>
83+
this == HostPlatformType.web ? web() : io();
84+
}
85+
2786
/// Operation system
2887
/// + Fuchsia
2988
/// + Linux
@@ -54,3 +113,77 @@ enum OperatingSystem {
54113
/// Fuchsia
55114
fuchsia,
56115
}
116+
117+
/// Operation system extension
118+
extension OperatingSystemX on OperatingSystem {
119+
/// Android
120+
bool get isAndroid => this == OperatingSystem.android;
121+
122+
/// Fuchsia
123+
bool get isFuchsia => this == OperatingSystem.fuchsia;
124+
125+
/// iOS
126+
bool get isIOS => this == OperatingSystem.iOS;
127+
128+
/// Linux
129+
bool get isLinux => this == OperatingSystem.linux;
130+
131+
/// MacOS
132+
bool get isMacOS => this == OperatingSystem.macOS;
133+
134+
/// Windows
135+
bool get isWindows => this == OperatingSystem.windows;
136+
137+
/// Unknown
138+
bool get isUnknown => this == OperatingSystem.unknown;
139+
140+
/// Run callback on specific operation system
141+
OperatingSystemResult when<OperatingSystemResult extends Object?>({
142+
required OperatingSystemResult Function() android,
143+
required OperatingSystemResult Function() fuchsia,
144+
required OperatingSystemResult Function() iOS,
145+
required OperatingSystemResult Function() linux,
146+
required OperatingSystemResult Function() macOS,
147+
required OperatingSystemResult Function() windows,
148+
required OperatingSystemResult Function() unknown,
149+
}) {
150+
switch (this) {
151+
case OperatingSystem.windows:
152+
return windows();
153+
case OperatingSystem.linux:
154+
return linux();
155+
case OperatingSystem.macOS:
156+
return macOS();
157+
case OperatingSystem.iOS:
158+
return iOS();
159+
case OperatingSystem.android:
160+
return android();
161+
case OperatingSystem.fuchsia:
162+
return fuchsia();
163+
case OperatingSystem.unknown:
164+
default:
165+
return unknown();
166+
}
167+
}
168+
169+
/// Run callback on specific operation system,
170+
/// if not specified run orElse
171+
OperatingSystemResult maybeWhen<OperatingSystemResult extends Object?>({
172+
required OperatingSystemResult Function() orElse,
173+
OperatingSystemResult Function()? android,
174+
OperatingSystemResult Function()? fuchsia,
175+
OperatingSystemResult Function()? iOS,
176+
OperatingSystemResult Function()? linux,
177+
OperatingSystemResult Function()? macOS,
178+
OperatingSystemResult Function()? windows,
179+
}) =>
180+
when<OperatingSystemResult>(
181+
android: android ?? orElse,
182+
fuchsia: fuchsia ?? orElse,
183+
iOS: iOS ?? orElse,
184+
linux: linux ?? orElse,
185+
macOS: macOS ?? orElse,
186+
windows: windows ?? orElse,
187+
unknown: orElse,
188+
);
189+
}

lib/src/platform.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class Platform extends ExtendedHostPlatform with PlatformMethods {
145145
Platform._internal({
146146
required this.buildMode,
147147
required HostPlatform hostPlatform,
148-
}) : _hostPlatform = hostPlatform,
148+
}) : _hostPlatform = hostPlatform,
149149
isOperatingSystemKnown =
150150
hostPlatform.operatingSystem != OperatingSystem.unknown,
151151
isMobile = kListOSForMobile.contains(hostPlatform.operatingSystem),

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: platform_info
22
description: >
33
Contains info about current platform
44
such as Build mode and Operating system.
5-
version: 3.0.0
5+
version: 3.1.0
66
repository: https://github.com/PlugFox/platform_info/tree/master
77
issue_tracker: https://github.com/PlugFox/platform_info/issues
88
homepage: https://github.com/PlugFox/platform_info
@@ -16,5 +16,5 @@ dependencies:
1616
meta: ^1.3.0
1717

1818
dev_dependencies:
19-
coverage: ^0.14.2
19+
coverage: '>=0.14.2 <2.0.0'
2020
test: ^1.16.0

test/platform_info_test.dart

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,132 @@ void main() {
287287
);
288288
});
289289
});
290+
291+
group('Enum extensions', () {
292+
test('BuildMode', () {
293+
expect(BuildMode.release.isRelease, isTrue);
294+
expect(BuildMode.profile.isProfile, isTrue);
295+
expect(BuildMode.debug.isDebug, isTrue);
296+
expect(
297+
BuildMode.release.maybeWhen(
298+
orElse: () => false,
299+
debug: () => false,
300+
profile: () => false,
301+
release: () => true,
302+
),
303+
isTrue,
304+
);
305+
expect(
306+
BuildMode.profile.maybeWhen(
307+
orElse: () => false,
308+
debug: () => false,
309+
profile: () => true,
310+
release: () => false,
311+
),
312+
isTrue,
313+
);
314+
expect(
315+
BuildMode.debug.maybeWhen(
316+
orElse: () => false,
317+
debug: () => true,
318+
profile: () => false,
319+
release: () => false,
320+
),
321+
isTrue,
322+
);
323+
expect(
324+
BuildMode.debug.maybeWhen(
325+
orElse: () => true,
326+
release: () => false,
327+
profile: () => false,
328+
),
329+
isTrue,
330+
);
331+
});
332+
333+
test('HostPlatformType', () {
334+
expect(HostPlatformType.io.isIO, isTrue);
335+
expect(HostPlatformType.web.isWeb, isTrue);
336+
expect(
337+
HostPlatformType.io.when(
338+
io: () => true,
339+
web: () => false,
340+
),
341+
isTrue,
342+
);
343+
expect(
344+
HostPlatformType.web.when(
345+
io: () => false,
346+
web: () => true,
347+
),
348+
isTrue,
349+
);
350+
});
351+
352+
test('OperatingSystem', () {
353+
expect(OperatingSystem.fuchsia.isFuchsia, isTrue);
354+
expect(OperatingSystem.unknown.isUnknown, isTrue);
355+
expect(OperatingSystem.android.isAndroid, isTrue);
356+
expect(OperatingSystem.iOS.isIOS, isTrue);
357+
expect(OperatingSystem.linux.isLinux, isTrue);
358+
expect(OperatingSystem.macOS.isMacOS, isTrue);
359+
expect(OperatingSystem.windows.isWindows, isTrue);
360+
361+
expect(
362+
OperatingSystem.fuchsia.maybeWhen(
363+
fuchsia: () => true,
364+
orElse: () => false,
365+
),
366+
isTrue,
367+
);
368+
369+
expect(
370+
OperatingSystem.windows.maybeWhen(
371+
windows: () => true,
372+
orElse: () => false,
373+
),
374+
isTrue,
375+
);
376+
377+
expect(
378+
OperatingSystem.macOS.maybeWhen(
379+
macOS: () => true,
380+
orElse: () => false,
381+
),
382+
isTrue,
383+
);
384+
385+
expect(
386+
OperatingSystem.linux.maybeWhen(
387+
linux: () => true,
388+
orElse: () => false,
389+
),
390+
isTrue,
391+
);
392+
393+
expect(
394+
OperatingSystem.iOS.maybeWhen(
395+
iOS: () => true,
396+
orElse: () => false,
397+
),
398+
isTrue,
399+
);
400+
401+
expect(
402+
OperatingSystem.android.maybeWhen(
403+
android: () => true,
404+
orElse: () => false,
405+
),
406+
isTrue,
407+
);
408+
409+
expect(
410+
OperatingSystem.unknown.maybeWhen(
411+
fuchsia: () => false,
412+
orElse: () => true,
413+
),
414+
isTrue,
415+
);
416+
});
417+
});
290418
}

0 commit comments

Comments
 (0)