From 17d6f6a072dfce31bd9556181b9970de4925533e Mon Sep 17 00:00:00 2001 From: stuartmorgan Date: Thu, 6 Jun 2019 15:49:24 -0700 Subject: [PATCH] Make plugins Swift-first on macOS (#33997) Instead of sharing the iOS codepath that uses an ObjC generated plugin registrant and expecting plugins to have an ObjC interface layer, switch to generating a Swift registrant and expecting plugins to have a Swift interface. This means plugins on macOS that use Swift won't need an ObjC wrapper, and plugins that use ObjC will need a Swift wrapper (inverting the structure relative to iOS). --- packages/flutter_tools/lib/src/plugins.dart | 93 ++++++++++++--------- 1 file changed, 53 insertions(+), 40 deletions(-) diff --git a/packages/flutter_tools/lib/src/plugins.dart b/packages/flutter_tools/lib/src/plugins.dart index 5883d36bb3..0242a62b6e 100644 --- a/packages/flutter_tools/lib/src/plugins.dart +++ b/packages/flutter_tools/lib/src/plugins.dart @@ -182,7 +182,7 @@ Future _writeAndroidPluginRegistrant(FlutterProject project, List _renderTemplateToFile(_androidPluginRegistryTemplate, context, registryPath); } -const String _cocoaPluginRegistryHeaderTemplate = '''// +const String _objcPluginRegistryHeaderTemplate = '''// // Generated file. Do not edit. // @@ -198,7 +198,7 @@ const String _cocoaPluginRegistryHeaderTemplate = '''// #endif /* GeneratedPluginRegistrant_h */ '''; -const String _cocoaPluginRegistryImplementationTemplate = '''// +const String _objcPluginRegistryImplementationTemplate = '''// // Generated file. Do not edit. // @@ -218,6 +218,23 @@ const String _cocoaPluginRegistryImplementationTemplate = '''// @end '''; +const String _swiftPluginRegistryTemplate = '''// +// Generated file. Do not edit. +// +import Foundation +import {{framework}} + +{{#plugins}} +import {{name}} +{{/plugins}} + +func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { + {{#plugins}} + {{class}}.register(with: registry.registrar(forPlugin: "{{class}}")) +{{/plugins}} +} +'''; + const String _pluginRegistrantPodspecTemplate = ''' # # Generated file, do not edit. @@ -259,7 +276,35 @@ Future _writeIOSPluginRegistrant(FlutterProject project, List plug 'plugins': iosPlugins, }; final String registryDirectory = project.ios.pluginRegistrantHost.path; - return await _writeCocoaPluginRegistrant(project, context, registryDirectory); + if (project.isModule) { + final String registryClassesDirectory = fs.path.join(registryDirectory, 'Classes'); + _renderTemplateToFile( + _pluginRegistrantPodspecTemplate, + context, + fs.path.join(registryDirectory, 'FlutterPluginRegistrant.podspec'), + ); + _renderTemplateToFile( + _objcPluginRegistryHeaderTemplate, + context, + fs.path.join(registryClassesDirectory, 'GeneratedPluginRegistrant.h'), + ); + _renderTemplateToFile( + _objcPluginRegistryImplementationTemplate, + context, + fs.path.join(registryClassesDirectory, 'GeneratedPluginRegistrant.m'), + ); + } else { + _renderTemplateToFile( + _objcPluginRegistryHeaderTemplate, + context, + fs.path.join(registryDirectory, 'GeneratedPluginRegistrant.h'), + ); + _renderTemplateToFile( + _objcPluginRegistryImplementationTemplate, + context, + fs.path.join(registryDirectory, 'GeneratedPluginRegistrant.m'), + ); + } } Future _writeMacOSPluginRegistrant(FlutterProject project, List plugins) async { @@ -269,51 +314,19 @@ Future _writeMacOSPluginRegistrant(FlutterProject project, List pl .where((Plugin p) => p.pluginClass != null && p.macosPrefix != null) .map>((Plugin p) => { 'name': p.name, - 'prefix': p.macosPrefix, 'class': p.pluginClass, }).toList(); final Map context = { 'os': 'macos', - 'deploymentTarget': '10.13', 'framework': 'FlutterMacOS', 'plugins': macosPlugins, }; final String registryDirectory = project.macos.managedDirectory.path; - return await _writeCocoaPluginRegistrant(project, context, registryDirectory); -} - -Future _writeCocoaPluginRegistrant(FlutterProject project, - Map templateContext, String registryDirectory) async { - - if (project.isModule) { - final String registryClassesDirectory = fs.path.join(registryDirectory, 'Classes'); - _renderTemplateToFile( - _pluginRegistrantPodspecTemplate, - templateContext, - fs.path.join(registryDirectory, 'FlutterPluginRegistrant.podspec'), - ); - _renderTemplateToFile( - _cocoaPluginRegistryHeaderTemplate, - templateContext, - fs.path.join(registryClassesDirectory, 'GeneratedPluginRegistrant.h'), - ); - _renderTemplateToFile( - _cocoaPluginRegistryImplementationTemplate, - templateContext, - fs.path.join(registryClassesDirectory, 'GeneratedPluginRegistrant.m'), - ); - } else { - _renderTemplateToFile( - _cocoaPluginRegistryHeaderTemplate, - templateContext, - fs.path.join(registryDirectory, 'GeneratedPluginRegistrant.h'), - ); - _renderTemplateToFile( - _cocoaPluginRegistryImplementationTemplate, - templateContext, - fs.path.join(registryDirectory, 'GeneratedPluginRegistrant.m'), - ); - } + _renderTemplateToFile( + _swiftPluginRegistryTemplate, + context, + fs.path.join(registryDirectory, 'GeneratedPluginRegistrant.swift'), + ); } /// Rewrites the `.flutter-plugins` file of [project] based on the plugin