Skip to content

Commit

Permalink
Add implement and registerWith method to plugins (flutter#3833)
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Garcia authored May 4, 2021
1 parent f13a39e commit c54b73d
Show file tree
Hide file tree
Showing 25 changed files with 107 additions and 23 deletions.
4 changes: 4 additions & 0 deletions packages/connectivity/connectivity_macos/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.1

* Add `implements` to pubspec.yaml.

## 0.2.0

* Remove placeholder Dart file.
Expand Down
10 changes: 9 additions & 1 deletion packages/connectivity/connectivity_macos/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name: connectivity_macos
description: macOS implementation of the connectivity plugin.
version: 0.2.0
version: 0.2.1
homepage: https://github.com/flutter/plugins/tree/master/packages/connectivity/connectivity_macos

flutter:
plugin:
implements: connectivity_platform_interface
platforms:
macos:
pluginClass: ConnectivityPlugin
Expand All @@ -16,6 +17,13 @@ environment:
dependencies:
flutter:
sdk: flutter
# The implementation of this plugin doesn't explicitly depend on the method channel
# defined in the platform interface.
# To prevent potential breakages, this dependency is added.
#
# In the future, this plugin's platform code should be able to reference the
# interface's platform code. (Android already supports this).
connectivity_platform_interface: ^2.0.0

dev_dependencies:
pedantic: ^1.10.0
5 changes: 2 additions & 3 deletions packages/path_provider/path_provider/lib/path_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ class MissingPlatformDirectoryException implements Exception {
}

PathProviderPlatform get _platform {
// This is to manually endorse Dart implementations until automatic
// registration of Dart plugins is implemented. For details see
// https://github.com/flutter/flutter/issues/52267.
// TODO(egarciad): Remove once auto registration lands on Flutter stable.
// https://github.com/flutter/flutter/issues/81421.
if (_manualDartRegistrationNeeded) {
// Only do the initial registration if it hasn't already been overridden
// with a non-default instance.
Expand Down
5 changes: 5 additions & 0 deletions packages/path_provider/path_provider_linux/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.0.1

* Add `implements` to pubspec.yaml.
* Add `registerWith` method to the main Dart class.

## 2.0.0

* Migrate to null safety.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'package:xdg_directories/xdg_directories.dart' as xdg;
/// This class implements the `package:path_provider` functionality for linux
class PathProviderLinux extends PathProviderPlatform {
/// Registers this class as the default instance of [PathProviderPlatform]
static void register() {
static void registerWith() {
PathProviderPlatform.instance = PathProviderLinux();
}

Expand Down
3 changes: 2 additions & 1 deletion packages/path_provider/path_provider_linux/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name: path_provider_linux
description: linux implementation of the path_provider plugin
version: 2.0.0
version: 2.0.1
homepage: https://github.com/flutter/plugins/tree/master/packages/path_provider/path_provider_linux

flutter:
plugin:
implements: path_provider
platforms:
linux:
dartPluginClass: PathProviderLinux
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import 'package:path_provider_platform_interface/path_provider_platform_interfac

void main() {
TestWidgetsFlutterBinding.ensureInitialized();
PathProviderLinux.register();
PathProviderLinux.registerWith();

setUp(() {});

tearDown(() {});
test('registered instance', () {
expect(PathProviderPlatform.instance, isA<PathProviderLinux>());
});

test('getTemporaryPath', () async {
final PathProviderPlatform plugin = PathProviderPlatform.instance;
Expand Down
4 changes: 4 additions & 0 deletions packages/path_provider/path_provider_macos/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.1

* Add `implements` to pubspec.yaml.

## 2.0.0

* Update Dart SDK constraint for null safety compatibility.
Expand Down
3 changes: 2 additions & 1 deletion packages/path_provider/path_provider_macos/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name: path_provider_macos
description: macOS implementation of the path_provider plugin
version: 2.0.0
version: 2.0.1
homepage: https://github.com/flutter/plugins/tree/master/packages/path_provider/path_provider_macos

flutter:
plugin:
implements: path_provider
platforms:
macos:
pluginClass: PathProviderPlugin
Expand Down
5 changes: 5 additions & 0 deletions packages/path_provider/path_provider_windows/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.0.2

* Add `implements` to pubspec.yaml.
* Add `registerWith()` to the Dart main class.

## 2.0.1

* Fix a crash when a known folder can't be located.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class VersionInfoQuerier {
///
/// This class implements the `package:path_provider` functionality for Windows.
class PathProviderWindows extends PathProviderPlatform {
/// Registers the Windows implementation.
static void registerWith() {
PathProviderPlatform.instance = PathProviderWindows();
}

/// The object to use for performing VerQueryValue calls.
@visibleForTesting
VersionInfoQuerier versionInfoQuerier = VersionInfoQuerier();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class PathProviderWindows extends PathProviderPlatform {
/// compile-time dependencies, and should never actually be created.
PathProviderWindows() : assert(false);

/// Registers the Windows implementation.
static void registerWith() {
PathProviderPlatform.instance = PathProviderWindows();
}

/// Stub; see comment on VersionInfoQuerier.
VersionInfoQuerier versionInfoQuerier = VersionInfoQuerier();

Expand Down
3 changes: 2 additions & 1 deletion packages/path_provider/path_provider_windows/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name: path_provider_windows
description: Windows implementation of the path_provider plugin
homepage: https://github.com/flutter/plugins/tree/master/packages/path_provider/path_provider_windows
version: 2.0.1
version: 2.0.2

flutter:
plugin:
implements: path_provider
platforms:
windows:
dartPluginClass: PathProviderWindows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'dart:ffi';
import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
import 'package:path_provider_windows/path_provider_windows.dart';

// A fake VersionInfoQuerier that just returns preset responses.
Expand All @@ -18,6 +19,11 @@ class FakeVersionInfoQuerier implements VersionInfoQuerier {
}

void main() {
test('registered instance', () {
PathProviderWindows.registerWith();
expect(PathProviderPlatform.instance, isA<PathProviderWindows>());
});

test('getTemporaryPath', () async {
final PathProviderWindows pathProvider = PathProviderWindows();
expect(await pathProvider.getTemporaryPath(), contains(r'C:\'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ class SharedPreferences {
static bool _manualDartRegistrationNeeded = true;

static SharedPreferencesStorePlatform get _store {
// This is to manually endorse the Linux implementation until automatic
// registration of dart plugins is implemented. For details see
// https://github.com/flutter/flutter/issues/52267.
// TODO(egarciad): Remove once auto registration lands on Flutter stable.
// https://github.com/flutter/flutter/issues/81421.
if (_manualDartRegistrationNeeded) {
// Only do the initial registration if it hasn't already been overridden
// with a non-default instance.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.0.1

* Add `implements` to the pubspec.
* Add `registerWith` to the Dart main class.

## 2.0.0

* Migrate to null-safety.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ import 'package:shared_preferences_platform_interface/shared_preferences_platfor
/// This class implements the `package:shared_preferences` functionality for Linux.
class SharedPreferencesLinux extends SharedPreferencesStorePlatform {
/// The default instance of [SharedPreferencesLinux] to use.
/// TODO(egarciad): Remove when the Dart plugin registrant lands on Flutter stable.
/// https://github.com/flutter/flutter/issues/81421
static SharedPreferencesLinux instance = SharedPreferencesLinux();

/// Registers the Linux implementation.
static void registerWith() {
SharedPreferencesStorePlatform.instance = instance;
}

/// Local copy of preferences
Map<String, Object>? _cachedPreferences;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name: shared_preferences_linux
description: Linux implementation of the shared_preferences plugin
version: 2.0.0
version: 2.0.1
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences_linux

flutter:
plugin:
implements: shared_preferences
platforms:
linux:
dartPluginClass: SharedPreferencesLinux
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider_linux/path_provider_linux.dart';
import 'package:shared_preferences_linux/shared_preferences_linux.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';

void main() {
late MemoryFileSystem fs;

SharedPreferencesLinux.registerWith();

setUp(() {
fs = MemoryFileSystem.test();
});

tearDown(() {});

Future<String> _getFilePath() async {
final pathProvider = PathProviderLinux();
final directory = await pathProvider.getApplicationSupportPath();
Expand All @@ -38,6 +39,11 @@ void main() {
return prefs;
}

test('registered instance', () {
expect(
SharedPreferencesStorePlatform.instance, isA<SharedPreferencesLinux>());
});

test('getAll', () async {
await _writeTestFile('{"key1": "one", "key2": 2}');
var prefs = _getPreferences();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.1

* Add `implements` to the pubspec.

## 2.0.0

* Migrate to null safety.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name: shared_preferences_macos
description: macOS implementation of the shared_preferences plugin.
version: 2.0.0
version: 2.0.1
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences_macos

flutter:
plugin:
implements: shared_preferences
platforms:
macos:
pluginClass: SharedPreferencesPlugin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.0.1

* Add `implements` to pubspec.yaml.
* Add `registerWith` to the Dart main class.

## 2.0.0

* Migrate to null-safety.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ import 'package:path_provider_windows/path_provider_windows.dart';
/// This class implements the `package:shared_preferences` functionality for Windows.
class SharedPreferencesWindows extends SharedPreferencesStorePlatform {
/// The default instance of [SharedPreferencesWindows] to use.
/// TODO(egarciad): Remove when the Dart plugin registrant lands on Flutter stable.
/// https://github.com/flutter/flutter/issues/81421
static SharedPreferencesWindows instance = SharedPreferencesWindows();

/// Registers the Windows implementation.
static void registerWith() {
SharedPreferencesStorePlatform.instance = instance;
}

/// File system used to store to disk. Exposed for testing only.
@visibleForTesting
FileSystem fs = LocalFileSystem();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: shared_preferences_windows
description: Windows implementation of shared_preferences
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences_windows
version: 2.0.0

version: 2.0.1

flutter:
plugin:
implements: shared_preferences
platforms:
windows:
dartPluginClass: SharedPreferencesWindows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
import 'package:path_provider_windows/path_provider_windows.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
import 'package:shared_preferences_windows/shared_preferences_windows.dart';

void main() {
Expand All @@ -18,8 +19,6 @@ void main() {
pathProvider = FakePathProviderWindows();
});

tearDown(() {});

Future<String> _getFilePath() async {
final directory = await pathProvider.getApplicationSupportPath();
return path.join(directory!, 'shared_preferences.json');
Expand All @@ -42,6 +41,12 @@ void main() {
return prefs;
}

test('registered instance', () {
SharedPreferencesWindows.registerWith();
expect(SharedPreferencesStorePlatform.instance,
isA<SharedPreferencesWindows>());
});

test('getAll', () async {
await _writeTestFile('{"key1": "one", "key2": 2}');
var prefs = _getPreferences();
Expand Down

0 comments on commit c54b73d

Please sign in to comment.