From 7eb1035af62d852231f31cb49668e2bfe2c90ff4 Mon Sep 17 00:00:00 2001 From: stuartmorgan Date: Thu, 3 Feb 2022 23:30:18 -0500 Subject: [PATCH] Restructure Windows plugin template (#93511) --- .../plugin/windows.tmpl/CMakeLists.txt.tmpl | 12 +++- ...tmpl => pluginClassSnakeCase_c_api.h.tmpl} | 8 +-- .../pluginClassSnakeCase.cpp.tmpl | 29 +--------- .../windows.tmpl/pluginClassSnakeCase.h.tmpl | 32 +++++++++++ .../pluginClassSnakeCase_c_api.cpp.tmpl | 12 ++++ .../templates/plugin_shared/pubspec.yaml.tmpl | 2 +- .../templates/template_manifest.json | 4 +- .../commands.shard/permeable/create_test.dart | 55 +++++++++++++------ 8 files changed, 102 insertions(+), 52 deletions(-) rename packages/flutter_tools/templates/plugin/windows.tmpl/include/projectName.tmpl/{pluginClassSnakeCase.h.tmpl => pluginClassSnakeCase_c_api.h.tmpl} (55%) create mode 100644 packages/flutter_tools/templates/plugin/windows.tmpl/pluginClassSnakeCase.h.tmpl create mode 100644 packages/flutter_tools/templates/plugin/windows.tmpl/pluginClassSnakeCase_c_api.cpp.tmpl diff --git a/packages/flutter_tools/templates/plugin/windows.tmpl/CMakeLists.txt.tmpl b/packages/flutter_tools/templates/plugin/windows.tmpl/CMakeLists.txt.tmpl index 3b05d1f9d2..f2f54f4739 100644 --- a/packages/flutter_tools/templates/plugin/windows.tmpl/CMakeLists.txt.tmpl +++ b/packages/flutter_tools/templates/plugin/windows.tmpl/CMakeLists.txt.tmpl @@ -12,12 +12,18 @@ project(${PROJECT_NAME} LANGUAGES CXX) # not be changed set(PLUGIN_NAME "{{projectName}}_plugin") +# Any new source files that you add to the plugin should be added here. +list(APPEND PLUGIN_SOURCES + "{{pluginClassSnakeCase}}.cpp" + "{{pluginClassSnakeCase}}.h" +) + # Define the plugin library target. Its name must not be changed (see comment # on PLUGIN_NAME above). -# -# Any new source files that you add to the plugin should be added here. add_library(${PLUGIN_NAME} SHARED - "{{pluginClassSnakeCase}}.cpp" + "include/{{projectName}}/{{pluginClassSnakeCase}}_c_api.h" + "{{pluginClassSnakeCase}}_c_api.cpp" + ${PLUGIN_SOURCES} ) # Apply a standard set of build settings that are configured in the diff --git a/packages/flutter_tools/templates/plugin/windows.tmpl/include/projectName.tmpl/pluginClassSnakeCase.h.tmpl b/packages/flutter_tools/templates/plugin/windows.tmpl/include/projectName.tmpl/pluginClassSnakeCase_c_api.h.tmpl similarity index 55% rename from packages/flutter_tools/templates/plugin/windows.tmpl/include/projectName.tmpl/pluginClassSnakeCase.h.tmpl rename to packages/flutter_tools/templates/plugin/windows.tmpl/include/projectName.tmpl/pluginClassSnakeCase_c_api.h.tmpl index 68c1a6101a..8c6ca12e71 100644 --- a/packages/flutter_tools/templates/plugin/windows.tmpl/include/projectName.tmpl/pluginClassSnakeCase.h.tmpl +++ b/packages/flutter_tools/templates/plugin/windows.tmpl/include/projectName.tmpl/pluginClassSnakeCase_c_api.h.tmpl @@ -1,5 +1,5 @@ -#ifndef FLUTTER_PLUGIN_{{pluginClassCapitalSnakeCase}}_H_ -#define FLUTTER_PLUGIN_{{pluginClassCapitalSnakeCase}}_H_ +#ifndef FLUTTER_PLUGIN_{{pluginClassCapitalSnakeCase}}_C_API_H_ +#define FLUTTER_PLUGIN_{{pluginClassCapitalSnakeCase}}_C_API_H_ #include @@ -13,11 +13,11 @@ extern "C" { #endif -FLUTTER_PLUGIN_EXPORT void {{pluginClass}}RegisterWithRegistrar( +FLUTTER_PLUGIN_EXPORT void {{pluginClass}}CApiRegisterWithRegistrar( FlutterDesktopPluginRegistrarRef registrar); #if defined(__cplusplus) } // extern "C" #endif -#endif // FLUTTER_PLUGIN_{{pluginClassCapitalSnakeCase}}_H_ +#endif // FLUTTER_PLUGIN_{{pluginClassCapitalSnakeCase}}_C_API_H_ diff --git a/packages/flutter_tools/templates/plugin/windows.tmpl/pluginClassSnakeCase.cpp.tmpl b/packages/flutter_tools/templates/plugin/windows.tmpl/pluginClassSnakeCase.cpp.tmpl index 3ca7f1d4bf..6282aec166 100644 --- a/packages/flutter_tools/templates/plugin/windows.tmpl/pluginClassSnakeCase.cpp.tmpl +++ b/packages/flutter_tools/templates/plugin/windows.tmpl/pluginClassSnakeCase.cpp.tmpl @@ -1,4 +1,4 @@ -#include "include/{{projectName}}/{{pluginClassSnakeCase}}.h" +#include "{{pluginClassSnakeCase}}.h" // This must be included before many other Windows headers. #include @@ -10,26 +10,10 @@ #include #include -#include #include #include -namespace { - -class {{pluginClass}} : public flutter::Plugin { - public: - static void RegisterWithRegistrar(flutter::PluginRegistrarWindows *registrar); - - {{pluginClass}}(); - - virtual ~{{pluginClass}}(); - - private: - // Called when a method is called on this plugin's channel from Dart. - void HandleMethodCall( - const flutter::MethodCall &method_call, - std::unique_ptr> result); -}; +namespace {{projectName}} { // static void {{pluginClass}}::RegisterWithRegistrar( @@ -72,11 +56,4 @@ void {{pluginClass}}::HandleMethodCall( } } -} // namespace - -void {{pluginClass}}RegisterWithRegistrar( - FlutterDesktopPluginRegistrarRef registrar) { - {{pluginClass}}::RegisterWithRegistrar( - flutter::PluginRegistrarManager::GetInstance() - ->GetRegistrar(registrar)); -} +} // namespace {{projectName}} diff --git a/packages/flutter_tools/templates/plugin/windows.tmpl/pluginClassSnakeCase.h.tmpl b/packages/flutter_tools/templates/plugin/windows.tmpl/pluginClassSnakeCase.h.tmpl new file mode 100644 index 0000000000..fa403d52d5 --- /dev/null +++ b/packages/flutter_tools/templates/plugin/windows.tmpl/pluginClassSnakeCase.h.tmpl @@ -0,0 +1,32 @@ +#ifndef FLUTTER_PLUGIN_{{pluginClassCapitalSnakeCase}}_H_ +#define FLUTTER_PLUGIN_{{pluginClassCapitalSnakeCase}}_H_ + +#include +#include + +#include + +namespace {{projectName}} { + +class {{pluginClass}} : public flutter::Plugin { + public: + static void RegisterWithRegistrar(flutter::PluginRegistrarWindows *registrar); + + {{pluginClass}}(); + + virtual ~{{pluginClass}}(); + + // Disallow copy and assign. + {{pluginClass}}(const {{pluginClass}}&) = delete; + {{pluginClass}}& operator=(const {{pluginClass}}&) = delete; + + private: + // Called when a method is called on this plugin's channel from Dart. + void HandleMethodCall( + const flutter::MethodCall &method_call, + std::unique_ptr> result); +}; + +} // namespace {{projectName}} + +#endif // FLUTTER_PLUGIN_{{pluginClassCapitalSnakeCase}}_H_ diff --git a/packages/flutter_tools/templates/plugin/windows.tmpl/pluginClassSnakeCase_c_api.cpp.tmpl b/packages/flutter_tools/templates/plugin/windows.tmpl/pluginClassSnakeCase_c_api.cpp.tmpl new file mode 100644 index 0000000000..f80f3c0bef --- /dev/null +++ b/packages/flutter_tools/templates/plugin/windows.tmpl/pluginClassSnakeCase_c_api.cpp.tmpl @@ -0,0 +1,12 @@ +#include "include/{{projectName}}/{{pluginClassSnakeCase}}_c_api.h" + +#include + +#include "{{pluginClassSnakeCase}}.h" + +void {{pluginClass}}CApiRegisterWithRegistrar( + FlutterDesktopPluginRegistrarRef registrar) { + {{projectName}}::{{pluginClass}}::RegisterWithRegistrar( + flutter::PluginRegistrarManager::GetInstance() + ->GetRegistrar(registrar)); +} diff --git a/packages/flutter_tools/templates/plugin_shared/pubspec.yaml.tmpl b/packages/flutter_tools/templates/plugin_shared/pubspec.yaml.tmpl index a62559bb82..af45fa4e54 100644 --- a/packages/flutter_tools/templates/plugin_shared/pubspec.yaml.tmpl +++ b/packages/flutter_tools/templates/plugin_shared/pubspec.yaml.tmpl @@ -112,7 +112,7 @@ flutter: {{/macos}} {{#windows}} windows: - pluginClass: {{pluginClass}} + pluginClass: {{pluginClass}}CApi {{/windows}} {{#web}} web: diff --git a/packages/flutter_tools/templates/template_manifest.json b/packages/flutter_tools/templates/template_manifest.json index 95f07951be..84164b62d4 100644 --- a/packages/flutter_tools/templates/template_manifest.json +++ b/packages/flutter_tools/templates/template_manifest.json @@ -333,8 +333,10 @@ "templates/plugin/README.md.tmpl", "templates/plugin/test/projectName_test.dart.tmpl", "templates/plugin/windows.tmpl/CMakeLists.txt.tmpl", - "templates/plugin/windows.tmpl/include/projectName.tmpl/pluginClassSnakeCase.h.tmpl", + "templates/plugin/windows.tmpl/include/projectName.tmpl/pluginClassSnakeCase_c_api.h.tmpl", "templates/plugin/windows.tmpl/pluginClassSnakeCase.cpp.tmpl", + "templates/plugin/windows.tmpl/pluginClassSnakeCase.h.tmpl", + "templates/plugin/windows.tmpl/pluginClassSnakeCase_c_api.cpp.tmpl", "templates/plugin/lib/projectName_web.dart.tmpl", "templates/plugin_ffi/android.tmpl/build.gradle.tmpl", diff --git a/packages/flutter_tools/test/commands.shard/permeable/create_test.dart b/packages/flutter_tools/test/commands.shard/permeable/create_test.dart index b8ce108031..069d6b2000 100755 --- a/packages/flutter_tools/test/commands.shard/permeable/create_test.dart +++ b/packages/flutter_tools/test/commands.shard/permeable/create_test.dart @@ -1063,7 +1063,7 @@ void main() { isNot(exists)); validatePubspecForPlugin(projectDir: projectDir.absolute.path, expectedPlatforms: const [ 'windows' - ], pluginClass: 'FlutterProjectPlugin', + ], pluginClass: 'FlutterProjectPluginCApi', unexpectedPlatforms: ['some_platform']); expect(logger.errorText, isNot(contains(_kNoPlatformsMessage))); }, overrides: { @@ -2539,21 +2539,33 @@ void main() { final Directory platformDir = projectDir.childDirectory('windows'); const String classFilenameBase = 'foo_bar_baz_plugin'; - const String headerName = '$classFilenameBase.h'; - final File headerFile = platformDir + const String cApiHeaderName = '${classFilenameBase}_c_api.h'; + const String pluginClassHeaderName = '$classFilenameBase.h'; + final File cApiHeaderFile = platformDir .childDirectory('include') .childDirectory(projectName) - .childFile(headerName); - final File implFile = platformDir.childFile('$classFilenameBase.cpp'); + .childFile(cApiHeaderName); + final File cApiImplFile = platformDir.childFile('${classFilenameBase}_c_api.cpp'); + final File pluginClassHeaderFile = platformDir.childFile(pluginClassHeaderName); + final File pluginClassImplFile = platformDir.childFile('$classFilenameBase.cpp'); // Ensure that the files have the right names. - expect(headerFile, exists); - expect(implFile, exists); - // Ensure that the include is correct. - expect(implFile.readAsStringSync(), contains(headerName)); + expect(cApiHeaderFile, exists); + expect(cApiImplFile, exists); + expect(pluginClassHeaderFile, exists); + expect(pluginClassImplFile, exists); + // Ensure that the includes are correct. + expect(cApiImplFile.readAsLinesSync(), containsAllInOrder([ + contains('#include "include/$projectName/$cApiHeaderName"'), + contains('#include "$pluginClassHeaderName"'), + ])); + expect(pluginClassImplFile.readAsLinesSync(), contains('#include "$pluginClassHeaderName"')); // Ensure that the plugin target name matches the post-processed version. // Ensure that the CMake file has the right target and source values. final String cmakeContents = platformDir.childFile('CMakeLists.txt').readAsStringSync(); expect(cmakeContents, contains('"$classFilenameBase.cpp"')); + expect(cmakeContents, contains('"$classFilenameBase.h"')); + expect(cmakeContents, contains('"${classFilenameBase}_c_api.cpp"')); + expect(cmakeContents, contains('"include/$projectName/${classFilenameBase}_c_api.h"')); expect(cmakeContents, contains('set(PLUGIN_NAME "foo_BarBaz_plugin")')); }, overrides: { FeatureFlags: () => TestFeatureFlags(isWindowsEnabled: true), @@ -2608,17 +2620,26 @@ void main() { // If the project already ends in _plugin, it shouldn't be added again. const String classFilenameBase = projectName; - const String headerName = '$classFilenameBase.h'; - final File headerFile = platformDir + const String cApiHeaderName = '${classFilenameBase}_c_api.h'; + const String pluginClassHeaderName = '$classFilenameBase.h'; + final File cApiHeaderFile = platformDir .childDirectory('include') .childDirectory(projectName) - .childFile(headerName); - final File implFile = platformDir.childFile('$classFilenameBase.cpp'); + .childFile(cApiHeaderName); + final File cApiImplFile = platformDir.childFile('${classFilenameBase}_c_api.cpp'); + final File pluginClassHeaderFile = platformDir.childFile(pluginClassHeaderName); + final File pluginClassImplFile = platformDir.childFile('$classFilenameBase.cpp'); // Ensure that the files have the right names. - expect(headerFile, exists); - expect(implFile, exists); - // Ensure that the include is correct. - expect(implFile.readAsStringSync(), contains(headerName)); + expect(cApiHeaderFile, exists); + expect(cApiImplFile, exists); + expect(pluginClassHeaderFile, exists); + expect(pluginClassImplFile, exists); + // Ensure that the includes are correct. + expect(cApiImplFile.readAsLinesSync(), containsAllInOrder([ + contains('#include "include/$projectName/$cApiHeaderName"'), + contains('#include "$pluginClassHeaderName"'), + ])); + expect(pluginClassImplFile.readAsLinesSync(), contains('#include "$pluginClassHeaderName"')); // Ensure that the CMake file has the right target and source values. final String cmakeContents = platformDir.childFile('CMakeLists.txt').readAsStringSync(); expect(cmakeContents, contains('"$classFilenameBase.cpp"'));