From b04e8078b68aad0910621590b09dc503e9075324 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Tue, 7 Jul 2020 15:44:14 -0400 Subject: [PATCH 01/12] Add missing driver test to shared_preferences_linux --- .../example/linux/CMakeLists.txt | 95 +++++++++++++++++++ .../example/linux/flutter/CMakeLists.txt | 86 +++++++++++++++++ .../flutter/generated_plugin_registrant.cc | 9 ++ .../flutter/generated_plugin_registrant.h | 13 +++ .../linux/flutter/generated_plugins.cmake | 15 +++ .../example/linux/main.cc | 6 ++ .../example/linux/my_application.cc | 44 +++++++++ .../example/linux/my_application.h | 18 ++++ .../example/pubspec.yaml | 8 ++ .../test_driver/shared_preferences_e2e.dart | 89 +++++++++++++++++ .../shared_preferences_e2e_test.dart | 15 +++ 11 files changed, 398 insertions(+) create mode 100644 packages/shared_preferences/shared_preferences_linux/example/linux/CMakeLists.txt create mode 100644 packages/shared_preferences/shared_preferences_linux/example/linux/flutter/CMakeLists.txt create mode 100644 packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugin_registrant.cc create mode 100644 packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugin_registrant.h create mode 100644 packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugins.cmake create mode 100644 packages/shared_preferences/shared_preferences_linux/example/linux/main.cc create mode 100644 packages/shared_preferences/shared_preferences_linux/example/linux/my_application.cc create mode 100644 packages/shared_preferences/shared_preferences_linux/example/linux/my_application.h create mode 100644 packages/shared_preferences/shared_preferences_linux/example/test_driver/shared_preferences_e2e.dart create mode 100644 packages/shared_preferences/shared_preferences_linux/example/test_driver/shared_preferences_e2e_test.dart diff --git a/packages/shared_preferences/shared_preferences_linux/example/linux/CMakeLists.txt b/packages/shared_preferences/shared_preferences_linux/example/linux/CMakeLists.txt new file mode 100644 index 000000000000..0236a8806654 --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/example/linux/CMakeLists.txt @@ -0,0 +1,95 @@ +cmake_minimum_required(VERSION 3.10) +project(runner LANGUAGES CXX) + +set(BINARY_NAME "example") + +cmake_policy(SET CMP0063 NEW) + +set(CMAKE_INSTALL_RPATH "$ORIGIN/lib") + +# Configure build options. +if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) + set(CMAKE_BUILD_TYPE "Debug" CACHE + STRING "Flutter build mode" FORCE) + set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS + "Debug" "Profile" "Release") +endif() + +# Compilation settings that should be applied to most targets. +function(APPLY_STANDARD_SETTINGS TARGET) + target_compile_features(${TARGET} PUBLIC cxx_std_14) + target_compile_options(${TARGET} PRIVATE -Wall -Werror) + target_compile_options(${TARGET} PRIVATE "$<$>:-O3>") + target_compile_definitions(${TARGET} PRIVATE "$<$>:NDEBUG>") +endfunction() + +set(FLUTTER_MANAGED_DIR "${CMAKE_CURRENT_SOURCE_DIR}/flutter") + +# Flutter library and tool build rules. +add_subdirectory(${FLUTTER_MANAGED_DIR}) + +# System-level dependencies. +find_package(PkgConfig REQUIRED) +pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0) + +# Application build +add_executable(${BINARY_NAME} + "main.cc" + "my_application.cc" + "${FLUTTER_MANAGED_DIR}/generated_plugin_registrant.cc" +) +apply_standard_settings(${BINARY_NAME}) +target_link_libraries(${BINARY_NAME} PRIVATE flutter) +target_link_libraries(${BINARY_NAME} PRIVATE PkgConfig::GTK) +add_dependencies(${BINARY_NAME} flutter_assemble) + +# Generated plugin build rules, which manage building the plugins and adding +# them to the application. +include(flutter/generated_plugins.cmake) + + +# === Installation === +# By default, "installing" just makes a relocatable bundle in the build +# directory. +set(BUILD_BUNDLE_DIR "${PROJECT_BINARY_DIR}/bundle") +if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) + set(CMAKE_INSTALL_PREFIX "${BUILD_BUNDLE_DIR}" CACHE PATH "..." FORCE) +endif() + +# Start with a clean build bundle directory every time. +install(CODE " + file(REMOVE_RECURSE \"${BUILD_BUNDLE_DIR}/\") + " COMPONENT Runtime) + +set(INSTALL_BUNDLE_DATA_DIR "${CMAKE_INSTALL_PREFIX}/data") +set(INSTALL_BUNDLE_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib") + +install(TARGETS ${BINARY_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}" + COMPONENT Runtime) + +install(FILES "${FLUTTER_ICU_DATA_FILE}" DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" + COMPONENT Runtime) + +install(FILES "${FLUTTER_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" + COMPONENT Runtime) + +if(PLUGIN_BUNDLED_LIBRARIES) + install(FILES "${PLUGIN_BUNDLED_LIBRARIES}" + DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" + COMPONENT Runtime) +endif() + +# Fully re-copy the assets directory on each build to avoid having stale files +# from a previous install. +set(FLUTTER_ASSET_DIR_NAME "flutter_assets") +install(CODE " + file(REMOVE_RECURSE \"${INSTALL_BUNDLE_DATA_DIR}/${FLUTTER_ASSET_DIR_NAME}\") + " COMPONENT Runtime) +install(DIRECTORY "${PROJECT_BUILD_DIR}/${FLUTTER_ASSET_DIR_NAME}" + DESTINATION "${INSTALL_BUNDLE_DATA_DIR}" COMPONENT Runtime) + +# Install the AOT library on non-Debug builds only. +if(NOT CMAKE_BUILD_TYPE MATCHES "Debug") + install(FILES "${AOT_LIBRARY}" DESTINATION "${INSTALL_BUNDLE_LIB_DIR}" + COMPONENT Runtime) +endif() diff --git a/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/CMakeLists.txt b/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/CMakeLists.txt new file mode 100644 index 000000000000..94f43ff7fa6a --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/CMakeLists.txt @@ -0,0 +1,86 @@ +cmake_minimum_required(VERSION 3.10) + +set(EPHEMERAL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ephemeral") + +# Configuration provided via flutter tool. +include(${EPHEMERAL_DIR}/generated_config.cmake) + +# TODO: Move the rest of this into files in ephemeral. See +# https://github.com/flutter/flutter/issues/57146. + +# Serves the same purpose as list(TRANSFORM ... PREPEND ...), +# which isn't available in 3.10. +function(list_prepend LIST_NAME PREFIX) + set(NEW_LIST "") + foreach(element ${${LIST_NAME}}) + list(APPEND NEW_LIST "${PREFIX}${element}") + endforeach(element) + set(${LIST_NAME} "${NEW_LIST}" PARENT_SCOPE) +endfunction() + +# === Flutter Library === +# System-level dependencies. +find_package(PkgConfig REQUIRED) +pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0) +pkg_check_modules(GLIB REQUIRED IMPORTED_TARGET glib-2.0) +pkg_check_modules(GIO REQUIRED IMPORTED_TARGET gio-2.0) + +set(FLUTTER_LIBRARY "${EPHEMERAL_DIR}/libflutter_linux_gtk.so") + +# Published to parent scope for install step. +set(FLUTTER_LIBRARY ${FLUTTER_LIBRARY} PARENT_SCOPE) +set(FLUTTER_ICU_DATA_FILE "${EPHEMERAL_DIR}/icudtl.dat" PARENT_SCOPE) +set(PROJECT_BUILD_DIR "${PROJECT_DIR}/build/" PARENT_SCOPE) +set(AOT_LIBRARY "${PROJECT_DIR}/build/lib/libapp.so" PARENT_SCOPE) + +list(APPEND FLUTTER_LIBRARY_HEADERS + "fl_basic_message_channel.h" + "fl_binary_codec.h" + "fl_binary_messenger.h" + "fl_dart_project.h" + "fl_engine.h" + "fl_json_message_codec.h" + "fl_json_method_codec.h" + "fl_message_codec.h" + "fl_method_call.h" + "fl_method_channel.h" + "fl_method_codec.h" + "fl_method_response.h" + "fl_plugin_registrar.h" + "fl_plugin_registry.h" + "fl_standard_message_codec.h" + "fl_standard_method_codec.h" + "fl_string_codec.h" + "fl_value.h" + "fl_view.h" + "flutter_linux.h" +) +list_prepend(FLUTTER_LIBRARY_HEADERS "${EPHEMERAL_DIR}/flutter_linux/") +add_library(flutter INTERFACE) +target_include_directories(flutter INTERFACE + "${EPHEMERAL_DIR}" +) +target_link_libraries(flutter INTERFACE "${FLUTTER_LIBRARY}") +target_link_libraries(flutter INTERFACE + PkgConfig::GTK + PkgConfig::GLIB + PkgConfig::GIO +) +add_dependencies(flutter flutter_assemble) + +# === Flutter tool backend === +# _phony_ is a non-existent file to force this command to run every time, +# since currently there's no way to get a full input/output list from the +# flutter tool. +add_custom_command( + OUTPUT ${FLUTTER_LIBRARY} ${FLUTTER_LIBRARY_HEADERS} + ${CMAKE_CURRENT_BINARY_DIR}/_phony_ + COMMAND ${CMAKE_COMMAND} -E env + ${FLUTTER_TOOL_ENVIRONMENT} + "${FLUTTER_ROOT}/packages/flutter_tools/bin/tool_backend.sh" + linux-x64 ${CMAKE_BUILD_TYPE} +) +add_custom_target(flutter_assemble DEPENDS + "${FLUTTER_LIBRARY}" + ${FLUTTER_LIBRARY_HEADERS} +) diff --git a/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugin_registrant.cc b/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugin_registrant.cc new file mode 100644 index 000000000000..d38195aa0412 --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugin_registrant.cc @@ -0,0 +1,9 @@ +// +// Generated file. Do not edit. +// + +#include "generated_plugin_registrant.h" + + +void fl_register_plugins(FlPluginRegistry* registry) { +} diff --git a/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugin_registrant.h b/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugin_registrant.h new file mode 100644 index 000000000000..9bf7478940c1 --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugin_registrant.h @@ -0,0 +1,13 @@ +// +// Generated file. Do not edit. +// + +#ifndef GENERATED_PLUGIN_REGISTRANT_ +#define GENERATED_PLUGIN_REGISTRANT_ + +#include + +// Registers Flutter plugins. +void fl_register_plugins(FlPluginRegistry* registry); + +#endif // GENERATED_PLUGIN_REGISTRANT_ diff --git a/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugins.cmake b/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugins.cmake new file mode 100644 index 000000000000..51436ae8c982 --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/example/linux/flutter/generated_plugins.cmake @@ -0,0 +1,15 @@ +# +# Generated file, do not edit. +# + +list(APPEND FLUTTER_PLUGIN_LIST +) + +set(PLUGIN_BUNDLED_LIBRARIES) + +foreach(plugin ${FLUTTER_PLUGIN_LIST}) + add_subdirectory(flutter/ephemeral/.plugin_symlinks/${plugin}/linux plugins/${plugin}) + target_link_libraries(${BINARY_NAME} PRIVATE ${plugin}_plugin) + list(APPEND PLUGIN_BUNDLED_LIBRARIES $) + list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${plugin}_bundled_libraries}) +endforeach(plugin) diff --git a/packages/shared_preferences/shared_preferences_linux/example/linux/main.cc b/packages/shared_preferences/shared_preferences_linux/example/linux/main.cc new file mode 100644 index 000000000000..e7c5c5437037 --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/example/linux/main.cc @@ -0,0 +1,6 @@ +#include "my_application.h" + +int main(int argc, char** argv) { + g_autoptr(MyApplication) app = my_application_new(); + return g_application_run(G_APPLICATION(app), argc, argv); +} diff --git a/packages/shared_preferences/shared_preferences_linux/example/linux/my_application.cc b/packages/shared_preferences/shared_preferences_linux/example/linux/my_application.cc new file mode 100644 index 000000000000..c2357f17ea9c --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/example/linux/my_application.cc @@ -0,0 +1,44 @@ +#include "my_application.h" + +#include + +#include "flutter/generated_plugin_registrant.h" + +struct _MyApplication { + GtkApplication parent_instance; +}; + +G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION) + +// Implements GApplication::activate. +static void my_application_activate(GApplication* application) { + GtkWindow* window = + GTK_WINDOW(gtk_application_window_new(GTK_APPLICATION(application))); + GtkHeaderBar *header_bar = GTK_HEADER_BAR(gtk_header_bar_new()); + gtk_widget_show(GTK_WIDGET(header_bar)); + gtk_header_bar_set_title(header_bar, "example"); + gtk_header_bar_set_show_close_button(header_bar, TRUE); + gtk_window_set_titlebar(window, GTK_WIDGET(header_bar)); + gtk_window_set_default_size(window, 1280, 720); + gtk_widget_show(GTK_WIDGET(window)); + + g_autoptr(FlDartProject) project = fl_dart_project_new(); + + FlView* view = fl_view_new(project); + gtk_widget_show(GTK_WIDGET(view)); + gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(view)); + + fl_register_plugins(FL_PLUGIN_REGISTRY(view)); + + gtk_widget_grab_focus(GTK_WIDGET(view)); +} + +static void my_application_class_init(MyApplicationClass* klass) { + G_APPLICATION_CLASS(klass)->activate = my_application_activate; +} + +static void my_application_init(MyApplication* self) {} + +MyApplication* my_application_new() { + return MY_APPLICATION(g_object_new(my_application_get_type(), nullptr)); +} diff --git a/packages/shared_preferences/shared_preferences_linux/example/linux/my_application.h b/packages/shared_preferences/shared_preferences_linux/example/linux/my_application.h new file mode 100644 index 000000000000..72271d5e4170 --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/example/linux/my_application.h @@ -0,0 +1,18 @@ +#ifndef FLUTTER_MY_APPLICATION_H_ +#define FLUTTER_MY_APPLICATION_H_ + +#include + +G_DECLARE_FINAL_TYPE(MyApplication, my_application, MY, APPLICATION, + GtkApplication) + +/** + * my_application_new: + * + * Creates a new Flutter-based application. + * + * Returns: a new #MyApplication. + */ +MyApplication* my_application_new(); + +#endif // FLUTTER_MY_APPLICATION_H_ diff --git a/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml index 1c0624035c54..f06cab1e19bb 100644 --- a/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml @@ -4,12 +4,20 @@ description: Demonstrates how to use the shared_preferences_linux plugin. dependencies: flutter: sdk: flutter + shared_preferences: any shared_preferences_linux: ^0.1.0 dependency_overrides: shared_preferences_linux: path: ../ +dev_dependencies: + flutter_driver: + sdk: flutter + test: any + e2e: ^0.2.0 + pedantic: ^1.8.0 + flutter: uses-material-design: true diff --git a/packages/shared_preferences/shared_preferences_linux/example/test_driver/shared_preferences_e2e.dart b/packages/shared_preferences/shared_preferences_linux/example/test_driver/shared_preferences_e2e.dart new file mode 100644 index 000000000000..b693df2131ed --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/example/test_driver/shared_preferences_e2e.dart @@ -0,0 +1,89 @@ +import 'dart:async'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:e2e/e2e.dart'; + +void main() { + E2EWidgetsFlutterBinding.ensureInitialized(); + + group('$SharedPreferences', () { + const Map kTestValues = { + 'flutter.String': 'hello world', + 'flutter.bool': true, + 'flutter.int': 42, + 'flutter.double': 3.14159, + 'flutter.List': ['foo', 'bar'], + }; + + const Map kTestValues2 = { + 'flutter.String': 'goodbye world', + 'flutter.bool': false, + 'flutter.int': 1337, + 'flutter.double': 2.71828, + 'flutter.List': ['baz', 'quox'], + }; + + SharedPreferences preferences; + + setUp(() async { + preferences = await SharedPreferences.getInstance(); + }); + + tearDown(() { + preferences.clear(); + }); + + test('reading', () async { + expect(preferences.get('String'), isNull); + expect(preferences.get('bool'), isNull); + expect(preferences.get('int'), isNull); + expect(preferences.get('double'), isNull); + expect(preferences.get('List'), isNull); + expect(preferences.getString('String'), isNull); + expect(preferences.getBool('bool'), isNull); + expect(preferences.getInt('int'), isNull); + expect(preferences.getDouble('double'), isNull); + expect(preferences.getStringList('List'), isNull); + }); + + test('writing', () async { + await Future.wait(>[ + preferences.setString('String', kTestValues2['flutter.String']), + preferences.setBool('bool', kTestValues2['flutter.bool']), + preferences.setInt('int', kTestValues2['flutter.int']), + preferences.setDouble('double', kTestValues2['flutter.double']), + preferences.setStringList('List', kTestValues2['flutter.List']) + ]); + expect(preferences.getString('String'), kTestValues2['flutter.String']); + expect(preferences.getBool('bool'), kTestValues2['flutter.bool']); + expect(preferences.getInt('int'), kTestValues2['flutter.int']); + expect(preferences.getDouble('double'), kTestValues2['flutter.double']); + expect(preferences.getStringList('List'), kTestValues2['flutter.List']); + }); + + test('removing', () async { + const String key = 'testKey'; + await preferences.setString(key, kTestValues['flutter.String']); + await preferences.setBool(key, kTestValues['flutter.bool']); + await preferences.setInt(key, kTestValues['flutter.int']); + await preferences.setDouble(key, kTestValues['flutter.double']); + await preferences.setStringList(key, kTestValues['flutter.List']); + await preferences.remove(key); + expect(preferences.get('testKey'), isNull); + }); + + test('clearing', () async { + await preferences.setString('String', kTestValues['flutter.String']); + await preferences.setBool('bool', kTestValues['flutter.bool']); + await preferences.setInt('int', kTestValues['flutter.int']); + await preferences.setDouble('double', kTestValues['flutter.double']); + await preferences.setStringList('List', kTestValues['flutter.List']); + await preferences.clear(); + expect(preferences.getString('String'), null); + expect(preferences.getBool('bool'), null); + expect(preferences.getInt('int'), null); + expect(preferences.getDouble('double'), null); + expect(preferences.getStringList('List'), null); + }); + }); +} diff --git a/packages/shared_preferences/shared_preferences_linux/example/test_driver/shared_preferences_e2e_test.dart b/packages/shared_preferences/shared_preferences_linux/example/test_driver/shared_preferences_e2e_test.dart new file mode 100644 index 000000000000..f3aa9e218d82 --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/example/test_driver/shared_preferences_e2e_test.dart @@ -0,0 +1,15 @@ +// Copyright 2019, the Chromium project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'dart:async'; +import 'dart:io'; +import 'package:flutter_driver/flutter_driver.dart'; + +Future main() async { + final FlutterDriver driver = await FlutterDriver.connect(); + final String result = + await driver.requestData(null, timeout: const Duration(minutes: 1)); + await driver.close(); + exit(result == 'pass' ? 0 : 1); +} From 2a1f865ad0a05e083d84befd145ddacb55915942 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Tue, 7 Jul 2020 18:28:16 -0400 Subject: [PATCH 02/12] Add endorsement and manual registration --- .../shared_preferences/CHANGELOG.md | 4 ++++ .../shared_preferences/README.md | 8 +++---- .../lib/shared_preferences.dart | 24 +++++++++++++++++-- .../shared_preferences/pubspec.yaml | 5 +++- .../example/pubspec.yaml | 3 +++ 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/packages/shared_preferences/shared_preferences/CHANGELOG.md b/packages/shared_preferences/shared_preferences/CHANGELOG.md index 8c7ee24dfa6b..d17bbb96289b 100644 --- a/packages/shared_preferences/shared_preferences/CHANGELOG.md +++ b/packages/shared_preferences/shared_preferences/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.5.8 + +* Support Linux by default. + ## 0.5.7+3 * Post-v2 Android embedding cleanup. diff --git a/packages/shared_preferences/shared_preferences/README.md b/packages/shared_preferences/shared_preferences/README.md index b5bf4050a8fd..9ccac0ac49ae 100644 --- a/packages/shared_preferences/shared_preferences/README.md +++ b/packages/shared_preferences/shared_preferences/README.md @@ -2,10 +2,10 @@ [![pub package](https://img.shields.io/pub/v/shared_preferences.svg)](https://pub.dartlang.org/packages/shared_preferences) -Wraps NSUserDefaults (on iOS) and SharedPreferences (on Android), providing -a persistent store for simple data. Data is persisted to disk asynchronously. -Neither platform can guarantee that writes will be persisted to disk after -returning and this plugin must not be used for storing critical data. +Wraps platform-specific persistent storage for simple data +(NSUserDefaults on iOS and macOS, SharedPreferences on Android, etc.). Data may be persisted to disk asynchronously, +and there is no guarantee that writes will be persisted to disk after +returning, so this plugin must not be used for storing critical data. **Please set your constraint to `shared_preferences: '>=0.5.y+x <2.0.0'`** diff --git a/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart b/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart index 62160dee20fd..7d3c534dcb0b 100644 --- a/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart +++ b/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart @@ -3,10 +3,15 @@ // found in the LICENSE file. import 'dart:async'; +import 'dart:io' show Platform; + +import 'package:flutter/foundation.dart' show kIsWeb; import 'package:meta/meta.dart'; +import 'package:shared_preferences_linux/shared_preferences_linux.dart'; import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart'; +import 'package:shared_preferences_platform_interface/method_channel_shared_preferences.dart'; /// Wraps NSUserDefaults (on iOS) and SharedPreferences (on Android), providing /// a persistent store for simple data. @@ -17,9 +22,24 @@ class SharedPreferences { static const String _prefix = 'flutter.'; static Completer _completer; + static bool _manualDartRegistrationNeeded = true; + + static SharedPreferencesStorePlatform get _store { + // This is to manually endorse the linux path provider until automatic + // registration of dart plugins is implemented. For details see + // https://github.com/flutter/flutter/issues/52267. + if (_manualDartRegistrationNeeded) { + // Only do the initial registration if it hasn't already been overridden + // with a non-default instance. + if (!kIsWeb && Platform.isLinux && + SharedPreferencesStorePlatform.instance is MethodChannelSharedPreferencesStore) { + SharedPreferencesStorePlatform.instance = SharedPreferencesLinux(); + } + _manualDartRegistrationNeeded = false; + } - static SharedPreferencesStorePlatform get _store => - SharedPreferencesStorePlatform.instance; + return SharedPreferencesStorePlatform.instance; + } /// Loads and parses the [SharedPreferences] for this app from disk. /// diff --git a/packages/shared_preferences/shared_preferences/pubspec.yaml b/packages/shared_preferences/shared_preferences/pubspec.yaml index 2b278248745a..5a803b53e28d 100644 --- a/packages/shared_preferences/shared_preferences/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences/pubspec.yaml @@ -5,7 +5,7 @@ homepage: https://github.com/flutter/plugins/tree/master/packages/shared_prefere # 0.5.y+z is compatible with 1.0.0, if you land a breaking change bump # the version to 2.0.0. # See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0 -version: 0.5.7+3 +version: 0.5.8 flutter: plugin: @@ -15,6 +15,8 @@ flutter: pluginClass: SharedPreferencesPlugin ios: pluginClass: FLTSharedPreferencesPlugin + linux: + default_package: shared_preferences_linux macos: default_package: shared_preferences_macos web: @@ -30,6 +32,7 @@ dependencies: # validation, so we set a ^ constraint. # TODO(franciscojma): Revisit this (either update this part in the design or the pub tool). # https://github.com/flutter/flutter/issues/46264 + shared_preferences_linux: ^0.0.1 shared_preferences_macos: ^0.0.1 shared_preferences_web: ^0.1.2 diff --git a/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml index f06cab1e19bb..8e726d642274 100644 --- a/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_linux/example/pubspec.yaml @@ -10,6 +10,9 @@ dependencies: dependency_overrides: shared_preferences_linux: path: ../ + # Remove this override once the endorsement is published. + shared_preferences: + path: ../../shared_preferences/ dev_dependencies: flutter_driver: From 1af76645f31274a5a964c324c5e66529f71df254 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Tue, 7 Jul 2020 18:30:16 -0400 Subject: [PATCH 03/12] Comment fix --- .../shared_preferences/lib/shared_preferences.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart b/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart index 7d3c534dcb0b..3539548b0533 100644 --- a/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart +++ b/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart @@ -25,7 +25,7 @@ class SharedPreferences { static bool _manualDartRegistrationNeeded = true; static SharedPreferencesStorePlatform get _store { - // This is to manually endorse the linux path provider until automatic + // 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. if (_manualDartRegistrationNeeded) { From 78a34337e9e47012fd72274e34b40ce90bc3aeb3 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Tue, 7 Jul 2020 18:56:29 -0400 Subject: [PATCH 04/12] Formatting --- .../shared_preferences/lib/shared_preferences.dart | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart b/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart index 3539548b0533..b8d3452a0a0e 100644 --- a/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart +++ b/packages/shared_preferences/shared_preferences/lib/shared_preferences.dart @@ -5,7 +5,6 @@ import 'dart:async'; import 'dart:io' show Platform; - import 'package:flutter/foundation.dart' show kIsWeb; import 'package:meta/meta.dart'; @@ -31,8 +30,10 @@ class SharedPreferences { if (_manualDartRegistrationNeeded) { // Only do the initial registration if it hasn't already been overridden // with a non-default instance. - if (!kIsWeb && Platform.isLinux && - SharedPreferencesStorePlatform.instance is MethodChannelSharedPreferencesStore) { + if (!kIsWeb && + Platform.isLinux && + SharedPreferencesStorePlatform.instance + is MethodChannelSharedPreferencesStore) { SharedPreferencesStorePlatform.instance = SharedPreferencesLinux(); } _manualDartRegistrationNeeded = false; From 9b01d9d72f0c0ffc375ced3e02c1043fd8d6fc03 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Tue, 7 Jul 2020 18:59:49 -0400 Subject: [PATCH 05/12] iOS stub --- .../ios/url_launcher_linux.podspec | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 packages/shared_preferences/shared_preferences_linux/ios/url_launcher_linux.podspec diff --git a/packages/shared_preferences/shared_preferences_linux/ios/url_launcher_linux.podspec b/packages/shared_preferences/shared_preferences_linux/ios/url_launcher_linux.podspec new file mode 100644 index 000000000000..8f4d3cdddcd5 --- /dev/null +++ b/packages/shared_preferences/shared_preferences_linux/ios/url_launcher_linux.podspec @@ -0,0 +1,22 @@ +# +# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html. +# Run `pod lib lint shared_preferences_launcher_linux.podspec' to validate before publishing. +# +Pod::Spec.new do |s| + s.name = 'shared_preferences_linux' + s.version = '0.0.1' + s.summary = 'shared_preferences_linux iOS stub' + s.description = <<-DESC + No-op implementation of the Linux shared_preferences plugin to avoid build issues on iOS + DESC + s.homepage = 'https://github.com/flutter/plugins' + s.license = { :type => 'BSD', :file => '../LICENSE' } + s.author = { 'Flutter Dev Team' => 'flutter-dev@googlegroups.com' } + s.source = { :http => 'https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences_linux' } + s.dependency 'Flutter' + s.platform = :ios, '8.0' + + # Flutter.framework does not contain a i386 slice. Only x86_64 simulators are supported. + s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES', 'VALID_ARCHS[sdk=iphonesimulator*]' => 'x86_64' } + s.swift_version = '5.0' +end From f2717ca49acf9ebf4feff4322207d1f7a46525b7 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Tue, 7 Jul 2020 19:41:12 -0400 Subject: [PATCH 06/12] Fix stub name --- ...rl_launcher_linux.podspec => shared_preferences_linux.podspec} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename packages/shared_preferences/shared_preferences_linux/ios/{url_launcher_linux.podspec => shared_preferences_linux.podspec} (100%) diff --git a/packages/shared_preferences/shared_preferences_linux/ios/url_launcher_linux.podspec b/packages/shared_preferences/shared_preferences_linux/ios/shared_preferences_linux.podspec similarity index 100% rename from packages/shared_preferences/shared_preferences_linux/ios/url_launcher_linux.podspec rename to packages/shared_preferences/shared_preferences_linux/ios/shared_preferences_linux.podspec From 5d76e5f137681285d4c32424e9dfeaa29002eef8 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Tue, 7 Jul 2020 21:37:14 -0400 Subject: [PATCH 07/12] Pick up iOS stub --- packages/shared_preferences/shared_preferences/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shared_preferences/shared_preferences/pubspec.yaml b/packages/shared_preferences/shared_preferences/pubspec.yaml index 5a803b53e28d..ca8c443cd753 100644 --- a/packages/shared_preferences/shared_preferences/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences/pubspec.yaml @@ -32,7 +32,7 @@ dependencies: # validation, so we set a ^ constraint. # TODO(franciscojma): Revisit this (either update this part in the design or the pub tool). # https://github.com/flutter/flutter/issues/46264 - shared_preferences_linux: ^0.0.1 + shared_preferences_linux: ^0.0.2 shared_preferences_macos: ^0.0.1 shared_preferences_web: ^0.1.2 From ad375b7b45c0e25db6d6d9267f8d8aeb37c020db Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Tue, 7 Jul 2020 21:58:43 -0400 Subject: [PATCH 08/12] Use XDG directly instead of path_provider to work around test dependency issue --- .../lib/shared_preferences_linux.dart | 17 +++++++++++++---- .../shared_preferences_linux/pubspec.yaml | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart index dc93100c3277..69ae422682e4 100644 --- a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart +++ b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart @@ -4,12 +4,13 @@ import 'dart:async'; import 'dart:convert' show json; + import 'package:file/file.dart'; import 'package:file/local.dart'; import 'package:meta/meta.dart'; import 'package:path/path.dart' as path; -import 'package:path_provider/path_provider.dart'; import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart'; +import 'package:xdg_directories/xdg_directories.dart' as xdg; /// The Linux implementation of [SharedPreferencesStorePlatform]. /// @@ -25,11 +26,19 @@ class SharedPreferencesLinux extends SharedPreferencesStorePlatform { @visibleForTesting FileSystem fs = LocalFileSystem(); + Future _getDataDirectory() async { + // TODO: Replace this with getApplicationSupportDirectory from + // path_provider_linux; currently plugin_tools can't handle dependencies + // between flutter/plugins plugins, so this is duplicated from there. + final processName = path.basenameWithoutExtension( + await fs.file('/proc/self/exe').resolveSymbolicLinks()); + return fs.directory(path.join(xdg.dataHome.path, processName)); + } + /// Gets the file where the preferences are stored. Future _getLocalDataFile() async { - var directory = await getApplicationSupportDirectory(); - var filePath = path.join(directory.path, 'shared_preferences.json'); - return fs.file(filePath); + var directory = await _getDataDirectory(); + return fs.file(path.join(directory.path, 'shared_preferences.json')); } /// Gets the preferences from the stored file. Once read, the preferences are diff --git a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml index 95c3aca71237..1cfc53d0a7e1 100644 --- a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml @@ -20,8 +20,8 @@ dependencies: sdk: flutter meta: ^1.0.4 path: ^1.6.4 - path_provider: ^1.6.11 shared_preferences_platform_interface: ^1.0.0 + xdg_directories: ^0.1.0 dev_dependencies: flutter_test: From 1da44f948b5d1751b5c688409a466e6cee93aab5 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Tue, 7 Jul 2020 22:00:26 -0400 Subject: [PATCH 09/12] Version rev --- .../shared_preferences/shared_preferences_linux/CHANGELOG.md | 3 +++ .../shared_preferences/shared_preferences_linux/pubspec.yaml | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md b/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md index de171a9fcf80..06122c6ed36e 100644 --- a/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md +++ b/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md @@ -1,3 +1,6 @@ +## 0.0.2+1 +* Replace path_provider dependency with xdg. + ## 0.0.2 * Add iOS stub. diff --git a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml index 1cfc53d0a7e1..c4cbcc26f864 100644 --- a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml @@ -1,6 +1,6 @@ name: shared_preferences_linux description: Linux implementation of the shared_preferences plugin -version: 0.0.2 +version: 0.0.2+1 homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences/shared_preferences_linux flutter: From e0aa4f722597d80c222e8ac92abeed05eb30a427 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Tue, 7 Jul 2020 22:35:05 -0400 Subject: [PATCH 10/12] Rework unit tests to be more self-contained and use XDG --- .../test/shared_preferences_linux_test.dart | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart b/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart index 8a794b1fa7c2..72bae1ecc462 100644 --- a/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart +++ b/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart @@ -4,19 +4,24 @@ import 'package:file/memory.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:path/path.dart' as path; -import 'package:path_provider/path_provider.dart'; import 'package:shared_preferences_linux/shared_preferences_linux.dart'; +import 'package:xdg_directories/xdg_directories.dart' as xdg; -MemoryFileSystem fs = MemoryFileSystem.test(); +MemoryFileSystem fs; void main() { - setUp(() {}); + setUp(() { + fs = MemoryFileSystem.test(); + // Mock out /proc/self/exe for shared prefs to read. + final exePath = '/path/to/test'; + fs.link('/proc/self/exe').createSync(exePath, recursive: true); + fs.file(exePath).createSync(recursive: true); + }); tearDown(() {}); Future _getFilePath() async { - var directory = await getApplicationSupportDirectory(); - return path.join(directory.path, 'shared_preferences.json'); + return path.join(xdg.dataHome.path, 'test', 'shared_preferences.json'); } _writeTestFile(String value) async { From f1bf6d7399e341899f3ed746b9d6c935792dda45 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Tue, 7 Jul 2020 23:17:52 -0400 Subject: [PATCH 11/12] Try switching to path_provider_linux Now that plugin_tools are updated, try using path_provider again. Uses path_provider_linux directly to avoid having an actual plugin dependency for other platforms (e.g., an Android+Linux app shouldn't need to build and ship the Android path_provider plugin just because the project uses shared_preferences). --- .../lib/shared_preferences_linux.dart | 16 ++++------------ .../shared_preferences_linux/pubspec.yaml | 2 +- .../test/shared_preferences_linux_test.dart | 10 ++++------ 3 files changed, 9 insertions(+), 19 deletions(-) diff --git a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart index 69ae422682e4..c975ad1a7544 100644 --- a/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart +++ b/packages/shared_preferences/shared_preferences_linux/lib/shared_preferences_linux.dart @@ -9,8 +9,8 @@ import 'package:file/file.dart'; import 'package:file/local.dart'; import 'package:meta/meta.dart'; import 'package:path/path.dart' as path; +import 'package:path_provider_linux/path_provider_linux.dart'; import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart'; -import 'package:xdg_directories/xdg_directories.dart' as xdg; /// The Linux implementation of [SharedPreferencesStorePlatform]. /// @@ -26,19 +26,11 @@ class SharedPreferencesLinux extends SharedPreferencesStorePlatform { @visibleForTesting FileSystem fs = LocalFileSystem(); - Future _getDataDirectory() async { - // TODO: Replace this with getApplicationSupportDirectory from - // path_provider_linux; currently plugin_tools can't handle dependencies - // between flutter/plugins plugins, so this is duplicated from there. - final processName = path.basenameWithoutExtension( - await fs.file('/proc/self/exe').resolveSymbolicLinks()); - return fs.directory(path.join(xdg.dataHome.path, processName)); - } - /// Gets the file where the preferences are stored. Future _getLocalDataFile() async { - var directory = await _getDataDirectory(); - return fs.file(path.join(directory.path, 'shared_preferences.json')); + final pathProvider = PathProviderLinux(); + final directory = await pathProvider.getApplicationSupportPath(); + return fs.file(path.join(directory, 'shared_preferences.json')); } /// Gets the preferences from the stored file. Once read, the preferences are diff --git a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml index c4cbcc26f864..3d40d39241ac 100644 --- a/packages/shared_preferences/shared_preferences_linux/pubspec.yaml +++ b/packages/shared_preferences/shared_preferences_linux/pubspec.yaml @@ -20,8 +20,8 @@ dependencies: sdk: flutter meta: ^1.0.4 path: ^1.6.4 + path_provider_linux: ^0.0.1 shared_preferences_platform_interface: ^1.0.0 - xdg_directories: ^0.1.0 dev_dependencies: flutter_test: diff --git a/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart b/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart index 72bae1ecc462..8c659f212aa5 100644 --- a/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart +++ b/packages/shared_preferences/shared_preferences_linux/test/shared_preferences_linux_test.dart @@ -4,24 +4,22 @@ import 'package:file/memory.dart'; 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:xdg_directories/xdg_directories.dart' as xdg; MemoryFileSystem fs; void main() { setUp(() { fs = MemoryFileSystem.test(); - // Mock out /proc/self/exe for shared prefs to read. - final exePath = '/path/to/test'; - fs.link('/proc/self/exe').createSync(exePath, recursive: true); - fs.file(exePath).createSync(recursive: true); }); tearDown(() {}); Future _getFilePath() async { - return path.join(xdg.dataHome.path, 'test', 'shared_preferences.json'); + final pathProvider = PathProviderLinux(); + final directory = await pathProvider.getApplicationSupportPath(); + return path.join(directory, 'shared_preferences.json'); } _writeTestFile(String value) async { From 70f3648e91c0ee64fe88f0d31024cab1e1edc35f Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Wed, 8 Jul 2020 00:00:11 -0400 Subject: [PATCH 12/12] CHANGELOG fix --- .../shared_preferences/shared_preferences_linux/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md b/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md index 06122c6ed36e..353a921ff281 100644 --- a/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md +++ b/packages/shared_preferences/shared_preferences_linux/CHANGELOG.md @@ -1,5 +1,5 @@ ## 0.0.2+1 -* Replace path_provider dependency with xdg. +* Replace path_provider dependency with path_provider_linux. ## 0.0.2 * Add iOS stub.