Unverified Commit 17d6f6a0 authored by stuartmorgan's avatar stuartmorgan Committed by GitHub

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).
parent 160a5680
......@@ -182,7 +182,7 @@ Future<void> _writeAndroidPluginRegistrant(FlutterProject project, List<Plugin>
_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,63 +276,59 @@ Future<void> _writeIOSPluginRegistrant(FlutterProject project, List<Plugin> plug
'plugins': iosPlugins,
};
final String registryDirectory = project.ios.pluginRegistrantHost.path;
return await _writeCocoaPluginRegistrant(project, context, registryDirectory);
}
Future<void> _writeMacOSPluginRegistrant(FlutterProject project, List<Plugin> plugins) async {
// TODO(stuartmorgan): Replace macosPrefix check with formal metadata check,
// see https://github.com/flutter/flutter/issues/33597.
final List<Map<String, dynamic>> macosPlugins = plugins
.where((Plugin p) => p.pluginClass != null && p.macosPrefix != null)
.map<Map<String, dynamic>>((Plugin p) => <String, dynamic>{
'name': p.name,
'prefix': p.macosPrefix,
'class': p.pluginClass,
}).toList();
final Map<String, dynamic> context = <String, dynamic>{
'os': 'macos',
'deploymentTarget': '10.13',
'framework': 'FlutterMacOS',
'plugins': macosPlugins,
};
final String registryDirectory = project.macos.managedDirectory.path;
return await _writeCocoaPluginRegistrant(project, context, registryDirectory);
}
Future<void> _writeCocoaPluginRegistrant(FlutterProject project,
Map<String, dynamic> templateContext, String registryDirectory) async {
if (project.isModule) {
final String registryClassesDirectory = fs.path.join(registryDirectory, 'Classes');
_renderTemplateToFile(
_pluginRegistrantPodspecTemplate,
templateContext,
context,
fs.path.join(registryDirectory, 'FlutterPluginRegistrant.podspec'),
);
_renderTemplateToFile(
_cocoaPluginRegistryHeaderTemplate,
templateContext,
_objcPluginRegistryHeaderTemplate,
context,
fs.path.join(registryClassesDirectory, 'GeneratedPluginRegistrant.h'),
);
_renderTemplateToFile(
_cocoaPluginRegistryImplementationTemplate,
templateContext,
_objcPluginRegistryImplementationTemplate,
context,
fs.path.join(registryClassesDirectory, 'GeneratedPluginRegistrant.m'),
);
} else {
_renderTemplateToFile(
_cocoaPluginRegistryHeaderTemplate,
templateContext,
_objcPluginRegistryHeaderTemplate,
context,
fs.path.join(registryDirectory, 'GeneratedPluginRegistrant.h'),
);
_renderTemplateToFile(
_cocoaPluginRegistryImplementationTemplate,
templateContext,
_objcPluginRegistryImplementationTemplate,
context,
fs.path.join(registryDirectory, 'GeneratedPluginRegistrant.m'),
);
}
}
Future<void> _writeMacOSPluginRegistrant(FlutterProject project, List<Plugin> plugins) async {
// TODO(stuartmorgan): Replace macosPrefix check with formal metadata check,
// see https://github.com/flutter/flutter/issues/33597.
final List<Map<String, dynamic>> macosPlugins = plugins
.where((Plugin p) => p.pluginClass != null && p.macosPrefix != null)
.map<Map<String, dynamic>>((Plugin p) => <String, dynamic>{
'name': p.name,
'class': p.pluginClass,
}).toList();
final Map<String, dynamic> context = <String, dynamic>{
'os': 'macos',
'framework': 'FlutterMacOS',
'plugins': macosPlugins,
};
final String registryDirectory = project.macos.managedDirectory.path;
_renderTemplateToFile(
_swiftPluginRegistryTemplate,
context,
fs.path.join(registryDirectory, 'GeneratedPluginRegistrant.swift'),
);
}
/// Rewrites the `.flutter-plugins` file of [project] based on the plugin
/// dependencies declared in `pubspec.yaml`.
///
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment