Skip to content

Commit 4b50c89

Browse files
committed
General release
1 parent eb602fd commit 4b50c89

File tree

8 files changed

+93
-76
lines changed

8 files changed

+93
-76
lines changed

superuser/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 1.0.0
2+
3+
* Add error handling
4+
* Integrate instance managing feature into `SuperuserInstance`
5+
* Improve effience of memory allocation in Windows platform.
6+
* Remove `MockSuperuser` restriction and mark as constant.
7+
18
## 0.1.1
29

310
* Exclude `SuperuserInterface` in `superuser` library.

superuser/README.md

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ For testing, `MockSuperuser` must be binded already before performing widget tes
1818

1919
```dart
2020
import 'package:flutter_test/flutter_test.dart';
21-
import 'package:superuser/instance.dart' as su_inst show bindInstance;
21+
import 'package:superuser/instance.dart';
2222
import 'package:superuser/mock.dart';
2323
2424
void main() {
2525
setUpAll(() {
2626
// Bind mock instance here
27-
su_inst.bindInstance(MockSuperuser(whoAmI: "reonaw"));
27+
SuperuserInstance.bindInstance(const MockSuperuser(whoAmI: "reonaw"));
2828
});
2929
// Do any testes below
3030
}
@@ -34,30 +34,16 @@ If using for debug simulation, mock interface must be binded before `runApp`:
3434

3535
```dart
3636
import 'package:flutter/widgets.dart';
37-
import 'package:superuser/instance.dart' as su_inst show bindInstance;
37+
import 'package:superuser/instance.dart';
3838
import 'package:superuser/mock.dart';
3939
4040
void main() {
41-
su_inst.bindInstance(MockSuperuser(whoAmI: "raisushawaa", isSuperuser: true, isActivated: true));
41+
SuperuserInstance.bindInstance(const MockSuperuser(whoAmI: "hiderik", isSuperuser: true, isActivated: true));
4242
4343
runApp(const YourApp());
4444
}
4545
```
4646

47-
When using `MockSuperuser`, ensure `isActivated` cannot be satisified if `isSuperuser` is `false`. Assign this mock data will cause assertion error when constructing mock interface:
48-
49-
```dart
50-
import 'package:superuser/instance.dart' as su_inst show bindInstance;
51-
import 'package:superuser/mock.dart';
52-
53-
void main() {
54-
su_inst.bindInstance(
55-
// AssertionError will be thrown
56-
MockSuperuser(whoAmI: "yuunagit", isActivated: true)
57-
);
58-
}
59-
```
60-
6147
## Demo
6248

6349
Demo application of `superuser` has been available in [release page](https://github.com/rk0cc/superuser/releases) in Windows and Linux application.

superuser/example/pubspec.lock

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,31 +211,29 @@ packages:
211211
path: ".."
212212
relative: true
213213
source: path
214-
version: "0.1.1"
214+
version: "1.0.0"
215215
superuser_interfaces:
216216
dependency: transitive
217217
description:
218218
name: superuser_interfaces
219-
sha256: "20f8cc0be607396d875966e7971678679905ad7b04159f494aa29f4d196f3669"
219+
sha256: e7725726dfe90c535e8907149e255cc0647dc9dd7b08bb4286727e16c1915a35
220220
url: "https://pub.dev"
221221
source: hosted
222-
version: "1.0.0"
222+
version: "2.0.0"
223223
superuser_plugin_unix:
224224
dependency: transitive
225225
description:
226-
name: superuser_plugin_unix
227-
sha256: "2f839be3dd6b8f99a16a2d1f22096c029509f48517f947b270bb5cadd23bd208"
228-
url: "https://pub.dev"
229-
source: hosted
230-
version: "0.1.0"
226+
path: "../../superuser_plugin_unix"
227+
relative: true
228+
source: path
229+
version: "1.0.0"
231230
superuser_plugin_windows:
232231
dependency: transitive
233232
description:
234-
name: superuser_plugin_windows
235-
sha256: fa3e4630d62bb49efe4e6e5e49a8ca35862f9411eef29668be6ebe1eab02f265
236-
url: "https://pub.dev"
237-
source: hosted
238-
version: "0.1.0"
233+
path: "../../superuser_plugin_windows"
234+
relative: true
235+
source: path
236+
version: "1.0.0"
239237
term_glyph:
240238
dependency: transitive
241239
description:

superuser/example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: superuser_demo
22
description: "Flutter superuser package demo application"
33
publish_to: 'none'
4-
version: 0.1.0
4+
version: 1.0.0
55
environment:
66
sdk: '>=3.4.0 <4.0.0'
77
dependencies:

superuser/lib/instance.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ library instance;
33

44
import 'package:superuser/superuser.dart';
55

6-
export 'src/instance.dart' show bindInstance;
6+
export 'package:superuser_interfaces/superuser_interfaces.dart' show SuperuserInterface;
7+
export 'src/instance.dart' show SuperuserInstance;

superuser/lib/src/instance.dart

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,79 @@ import 'package:superuser_interfaces/superuser_interfaces.dart';
55
import 'package:superuser_plugin_unix/superuser_plugin_unix.dart';
66
import 'package:superuser_plugin_windows/superuser_plugin_windows.dart';
77

8+
SuperuserInterface? _instance;
9+
10+
/// Retrive current instance of [SuperuserInterface].
11+
///
12+
/// If no instance was created before, it call
13+
/// [bindInstance] automatically that assign
14+
/// platform implemented [SuperuserInterface]
15+
/// as current instance.
16+
@internal
17+
SuperuserInterface get instance {
18+
if (_instance == null ||
19+
_instance is SuperuserPlatform &&
20+
(_instance as SuperuserPlatform).isClosed) {
21+
SuperuserInstance.bindInstance(null);
22+
}
23+
24+
return _instance!;
25+
}
26+
827
/// Manage instance of [SuperuserInterface] when accessing superuser
928
/// properties.
10-
@internal
1129
abstract final class SuperuserInstance {
12-
static SuperuserInterface? _instance;
13-
1430
const SuperuserInstance._();
1531

16-
/// Retrive current instance of [SuperuserInterface].
32+
/// Attach specified [suInterface] as data source of superuser
33+
/// property.
34+
///
35+
/// If [suInterface] is `null`, it binds platform specified
36+
/// [SuperuserInterface] automatically.
1737
///
18-
/// If no instance was created before, it call
19-
/// [bindInstance] automatically that assign
20-
/// platform implemented [SuperuserInterface]
21-
/// as current instance.
22-
static SuperuserInterface get instance {
23-
if (_instance == null) {
24-
bindInstance(null);
38+
/// When this invoked in neither Windows, macOS or Linux platform,
39+
/// it throws [UnsupportedError].
40+
///
41+
/// If [suInterface] is a member of [SuperuserPlatform],
42+
/// the provided interface should not be closed. Otherwise,
43+
/// it throws [ArgumentError].
44+
static void bindInstance(SuperuserInterface? suInterface) {
45+
if (!Platform.isWindows && !Platform.isMacOS && !Platform.isLinux) {
46+
throw UnsupportedError("Unknown platform");
2547
}
2648

27-
return _instance!;
28-
}
29-
}
49+
flushInstance();
3050

31-
/// Attach specified [suInterface] as data source of superuser
32-
/// property.
33-
///
34-
/// If [suInterface] is `null`, it binds platform specified
35-
/// [SuperuserInterface] automatically.
36-
///
37-
/// When this invoked in neither Windows, macOS or Linux platform,
38-
/// it throws [UnsupportedError].
39-
void bindInstance(SuperuserInterface? suInterface) {
40-
if (!Platform.isWindows && !Platform.isMacOS && !Platform.isLinux) {
41-
throw UnsupportedError("Unknown platform");
42-
}
51+
late SuperuserInterface newInst;
4352

44-
late SuperuserInterface newInst;
53+
if (suInterface == null) {
54+
// Denote null interface as uses default implementation.
55+
if (Platform.isWindows) {
56+
newInst = WindowsSuperuser();
57+
} else if (Platform.isMacOS || Platform.isLinux) {
58+
newInst = UnixSuperuser();
59+
}
60+
} else {
61+
if (suInterface is SuperuserPlatform && suInterface.isClosed) {
62+
throw ArgumentError(
63+
"The provided interface should not be closed already.",
64+
"suInterface");
65+
}
4566

46-
if (suInterface == null) {
47-
// Denote null interface as uses default implementation.
48-
if (Platform.isWindows) {
49-
newInst = WindowsSuperuser();
50-
} else if (Platform.isMacOS || Platform.isLinux) {
51-
newInst = UnixSuperuser();
67+
newInst = suInterface;
5268
}
53-
} else {
54-
newInst = suInterface;
69+
70+
_instance = newInst;
5571
}
5672

57-
SuperuserInstance._instance = newInst;
73+
/// Flush existed [SuperuserInterface] instance.
74+
static void flushInstance() {
75+
if (_instance != null) {
76+
if (_instance is SuperuserPlatform) {
77+
(_instance as SuperuserPlatform).close();
78+
}
79+
80+
_instance = null;
81+
}
82+
}
5883
}

superuser/lib/superuser.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ abstract final class Superuser {
1919

2020
/// Determine this program is executed by a user, who is superuser exactly or
2121
/// one of members in superuser group.
22-
static bool get isSuperuser => SuperuserInstance.instance.isSuperuser;
22+
static bool get isSuperuser => instance.isSuperuser;
2323

2424
/// Determine this program is running with superuser role.
2525
///
@@ -29,8 +29,8 @@ abstract final class Superuser {
2929
///
3030
/// For Windows platform, it consider this process has been elevated
3131
/// or not.
32-
static bool get isActivated => SuperuserInstance.instance.isActivated;
32+
static bool get isActivated => instance.isActivated;
3333

3434
/// Obtain username who call current program.
35-
static String get whoAmI => SuperuserInstance.instance.whoAmI;
35+
static String get whoAmI => instance.whoAmI;
3636
}

superuser/pubspec.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: superuser
22
description: Detect, verify user who execute Flutter program has superuser role and running with superuser permission.
3-
version: 0.1.1
3+
version: 1.0.0
44
homepage: https://github.com/rk0cc/superuser
55
repository: https://github.com/rk0cc/superuser/tree/main/superuser
66
funding:
@@ -18,9 +18,9 @@ dependencies:
1818
flutter:
1919
sdk: flutter
2020
meta: ^1.12.0
21-
superuser_interfaces: ^1.0.0
22-
superuser_plugin_unix: ">=0.1.0 <1.0.0"
23-
superuser_plugin_windows: ">=0.1.0 <1.0.0"
21+
superuser_interfaces: ^2.0.0
22+
superuser_plugin_unix: ^1.0.0
23+
superuser_plugin_windows: ^1.0.0
2424
dev_dependencies:
2525
flutter_lints: ^4.0.0
2626
platforms:

0 commit comments

Comments
 (0)