From fc05c3738ca64f65484527d7a9a824a0f0bc5706 Mon Sep 17 00:00:00 2001 From: Kaushik Iska Date: Thu, 29 Aug 2019 21:51:31 -0700 Subject: [PATCH] Flutter Plugin Tool supports multi-platform plugin config (#38632) --- .../lib/src/flutter_manifest.dart | 25 +-- .../lib/src/platform_plugins.dart | 141 ++++++++++++ packages/flutter_tools/lib/src/plugins.dart | 202 ++++++++++++++---- .../flutter_tools/schema/pubspec_yaml.json | 28 +++ .../templates/cocoapods/Podfile-macos | 4 +- .../general.shard/flutter_manifest_test.dart | 17 +- .../general.shard/plugin_parsing_test.dart | 66 ++++++ .../test/general.shard/project_test.dart | 30 ++- 8 files changed, 445 insertions(+), 68 deletions(-) create mode 100644 packages/flutter_tools/lib/src/platform_plugins.dart create mode 100644 packages/flutter_tools/test/general.shard/plugin_parsing_test.dart diff --git a/packages/flutter_tools/lib/src/flutter_manifest.dart b/packages/flutter_tools/lib/src/flutter_manifest.dart index bc384d8289..1c3887f572 100644 --- a/packages/flutter_tools/lib/src/flutter_manifest.dart +++ b/packages/flutter_tools/lib/src/flutter_manifest.dart @@ -11,6 +11,7 @@ import 'base/user_messages.dart'; import 'base/utils.dart'; import 'cache.dart'; import 'globals.dart'; +import 'plugins.dart'; /// A wrapper around the `flutter` section in the `pubspec.yaml` file. class FlutterManifest { @@ -151,8 +152,14 @@ class FlutterManifest { String get androidPackage { if (isModule) return _flutterDescriptor['module']['androidPackage']; - if (isPlugin) - return _flutterDescriptor['plugin']['androidPackage']; + if (isPlugin) { + final YamlMap plugin = _flutterDescriptor['plugin']; + if (plugin.containsKey('platforms')) { + return plugin['platforms']['android']['package']; + } else { + return plugin['androidPackage']; + } + } return null; } @@ -378,18 +385,8 @@ void _validateFlutter(YamlMap yaml, List errors) { if (kvp.value is! YamlMap) { errors.add('Expected "${kvp.key}" to be an object, but got ${kvp.value} (${kvp.value.runtimeType}).'); } - if (kvp.value['androidPackage'] != null && kvp.value['androidPackage'] is! String) { - errors.add('The "androidPackage" must either be null or a string.'); - } - if (kvp.value['iosPrefix'] != null && kvp.value['iosPrefix'] is! String) { - errors.add('The "iosPrefix" must either be null or a string.'); - } - if (kvp.value['macosPrefix'] != null && kvp.value['macosPrefix'] is! String) { - errors.add('The "macosPrefix" must either be null or a string.'); - } - if (kvp.value['pluginClass'] != null && kvp.value['pluginClass'] is! String) { - errors.add('The "pluginClass" must either be null or a string..'); - } + final List pluginErrors = Plugin.validatePluginYaml(kvp.value); + errors.addAll(pluginErrors); break; default: errors.add('Unexpected child "${kvp.key}" found under "flutter".'); diff --git a/packages/flutter_tools/lib/src/platform_plugins.dart b/packages/flutter_tools/lib/src/platform_plugins.dart new file mode 100644 index 0000000000..ed9b4eaf3d --- /dev/null +++ b/packages/flutter_tools/lib/src/platform_plugins.dart @@ -0,0 +1,141 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:meta/meta.dart'; +import 'package:yaml/yaml.dart'; + +/// Marker interface for all platform specific plugin config impls. +abstract class PluginPlatform { + const PluginPlatform(); + + Map toMap(); +} + +/// Contains parameters to template an Android plugin. +/// +/// The required fields include: [name] of the plugin, [package] of the plugin and +/// the [pluginClass] that will be the entry point to the plugin's native code. +class AndroidPlugin extends PluginPlatform { + const AndroidPlugin({ + @required this.name, + @required this.package, + @required this.pluginClass, + }); + + factory AndroidPlugin.fromYaml(String name, YamlMap yaml) { + assert(validate(yaml)); + return AndroidPlugin( + name: name, + package: yaml['package'], + pluginClass: yaml['pluginClass'], + ); + } + + static bool validate(YamlMap yaml) { + if (yaml == null) { + return false; + } + return yaml['package'] is String && yaml['pluginClass'] is String; + } + + static const String kConfigKey = 'android'; + + final String name; + final String package; + final String pluginClass; + + @override + Map toMap() { + return { + 'name': name, + 'package': package, + 'class': pluginClass, + }; + } +} + +/// Contains the parameters to template an iOS plugin. +/// +/// The required fields include: [name] of the plugin, the [pluginClass] that +/// will be the entry point to the plugin's native code. +class IOSPlugin extends PluginPlatform { + const IOSPlugin({ + @required this.name, + this.classPrefix, + @required this.pluginClass, + }); + + factory IOSPlugin.fromYaml(String name, YamlMap yaml) { + assert(validate(yaml)); + return IOSPlugin( + name: name, + classPrefix: '', + pluginClass: yaml['pluginClass'], + ); + } + + static bool validate(YamlMap yaml) { + if (yaml == null) { + return false; + } + return yaml['pluginClass'] is String; + } + + static const String kConfigKey = 'ios'; + + final String name; + + /// Note, this is here only for legacy reasons. Multi-platform format + /// always sets it to empty String. + final String classPrefix; + final String pluginClass; + + @override + Map toMap() { + return { + 'name': name, + 'prefix': classPrefix, + 'class': pluginClass, + }; + } +} + +/// Contains the parameters to template a macOS plugin. +/// +/// The required fields include: [name] of the plugin, and [pluginClass] that will +/// be the entry point to the plugin's native code. +class MacOSPlugin extends PluginPlatform { + const MacOSPlugin({ + @required this.name, + @required this.pluginClass, + }); + + factory MacOSPlugin.fromYaml(String name, YamlMap yaml) { + assert(validate(yaml)); + return MacOSPlugin( + name: name, + pluginClass: yaml['pluginClass'], + ); + } + + static bool validate(YamlMap yaml) { + if (yaml == null) { + return false; + } + return yaml['pluginClass'] is String; + } + + static const String kConfigKey = 'macos'; + + final String name; + final String pluginClass; + + @override + Map toMap() { + return { + 'name': name, + 'class': pluginClass, + }; + } +} diff --git a/packages/flutter_tools/lib/src/plugins.dart b/packages/flutter_tools/lib/src/plugins.dart index db87e5f41a..a274c7ee8c 100644 --- a/packages/flutter_tools/lib/src/plugins.dart +++ b/packages/flutter_tools/lib/src/plugins.dart @@ -7,11 +7,13 @@ import 'dart:async'; import 'package:mustache/mustache.dart' as mustache; import 'package:yaml/yaml.dart'; +import 'base/common.dart'; import 'base/file_system.dart'; import 'dart/package_map.dart'; import 'features.dart'; import 'globals.dart'; import 'macos/cocoapods.dart'; +import 'platform_plugins.dart'; import 'project.dart'; void _renderTemplateToFile(String template, dynamic context, String filePath) { @@ -26,41 +28,158 @@ class Plugin { Plugin({ this.name, this.path, - this.androidPackage, - this.iosPrefix, - this.macosPrefix, - this.pluginClass, + this.platforms, }); + /// Parses [Plugin] specification from the provided pluginYaml. + /// + /// This currently supports two formats. Legacy and Multi-platform. + /// Example of the deprecated Legacy format. + /// flutter: + /// plugin: + /// androidPackage: io.flutter.plugins.sample + /// iosPrefix: FLT + /// pluginClass: SamplePlugin + /// + /// Example Multi-platform format. + /// flutter: + /// plugin: + /// platforms: + /// android: + /// package: io.flutter.plugins.sample + /// pluginClass: SamplePlugin + /// ios: + /// pluginClass: SamplePlugin + /// macos: + /// pluginClass: SamplePlugin factory Plugin.fromYaml(String name, String path, dynamic pluginYaml) { - String androidPackage; - String iosPrefix; - String macosPrefix; - String pluginClass; - if (pluginYaml != null) { - androidPackage = pluginYaml['androidPackage']; - iosPrefix = pluginYaml['iosPrefix'] ?? ''; - // TODO(stuartmorgan): Add |?? ''| here as well once this isn't used as - // an indicator of macOS support, see https://github.com/flutter/flutter/issues/33597 - macosPrefix = pluginYaml['macosPrefix']; - pluginClass = pluginYaml['pluginClass']; + final List errors = validatePluginYaml(pluginYaml); + if (errors.isNotEmpty) { + throwToolExit('Invalid plugin specification.\n${errors.join('\n')}'); + } + if (pluginYaml != null && pluginYaml['platforms'] != null) { + return Plugin._fromMultiPlatformYaml(name, path, pluginYaml); + } else { + return Plugin._fromLegacyYaml(name, path, pluginYaml); // ignore: deprecated_member_use_from_same_package + } + } + + factory Plugin._fromMultiPlatformYaml(String name, String path, dynamic pluginYaml) { + assert (pluginYaml != null && pluginYaml['platforms'] != null, + 'Invalid multi-platform plugin specification.'); + final dynamic platformsYaml = pluginYaml['platforms']; + + assert (_validateMultiPlatformYaml(platformsYaml).isEmpty, + 'Invalid multi-platform plugin specification.'); + + final Map platforms = {}; + + if (platformsYaml[AndroidPlugin.kConfigKey] != null) { + platforms[AndroidPlugin.kConfigKey] = + AndroidPlugin.fromYaml(name, platformsYaml[AndroidPlugin.kConfigKey]); + } + + if (platformsYaml[IOSPlugin.kConfigKey] != null) { + platforms[IOSPlugin.kConfigKey] = + IOSPlugin.fromYaml(name, platformsYaml[IOSPlugin.kConfigKey]); + } + + if (platformsYaml[MacOSPlugin.kConfigKey] != null) { + platforms[MacOSPlugin.kConfigKey] = + MacOSPlugin.fromYaml(name, platformsYaml[MacOSPlugin.kConfigKey]); + } + + return Plugin( + name: name, + path: path, + platforms: platforms, + ); + } + + @deprecated + factory Plugin._fromLegacyYaml(String name, String path, dynamic pluginYaml) { + final Map platforms = {}; + final String pluginClass = pluginYaml['pluginClass']; + if (pluginYaml != null && pluginClass != null) { + final String androidPackage = pluginYaml['androidPackage']; + if (androidPackage != null) { + platforms[AndroidPlugin.kConfigKey] = + AndroidPlugin( + name: name, + package: pluginYaml['androidPackage'], + pluginClass: pluginClass, + ); + } + + final String iosPrefix = pluginYaml['iosPrefix'] ?? ''; + platforms[IOSPlugin.kConfigKey] = + IOSPlugin( + name: name, + classPrefix: iosPrefix, + pluginClass: pluginClass, + ); } return Plugin( name: name, path: path, - androidPackage: androidPackage, - iosPrefix: iosPrefix, - macosPrefix: macosPrefix, - pluginClass: pluginClass, + platforms: platforms, ); } + static List validatePluginYaml(YamlMap yaml) { + if (yaml.containsKey('platforms')) { + final int numKeys = yaml.keys.toSet().length; + if (numKeys != 1) { + return [ + 'Invalid plugin specification. There must be only one key: "platforms", found multiple: ${yaml.keys.join(',')}' + ]; + } else { + return _validateMultiPlatformYaml(yaml['platforms']); + } + } else { + return _validateLegacyYaml(yaml); + } + } + + static List _validateMultiPlatformYaml(YamlMap yaml) { + final List errors = []; + if (yaml.containsKey(AndroidPlugin.kConfigKey) && + !AndroidPlugin.validate(yaml[AndroidPlugin.kConfigKey])) { + errors.add('Invalid "android" plugin specification.'); + } + if (yaml.containsKey(IOSPlugin.kConfigKey) && + !IOSPlugin.validate(yaml[IOSPlugin.kConfigKey])) { + errors.add('Invalid "ios" plugin specification.'); + } + if (yaml.containsKey(MacOSPlugin.kConfigKey) && + !MacOSPlugin.validate(yaml[MacOSPlugin.kConfigKey])) { + errors.add('Invalid "macos" plugin specification.'); + } + return errors; + } + + static List _validateLegacyYaml(YamlMap yaml) { + final List errors = []; + if (yaml['androidPackage'] != null && yaml['androidPackage'] is! String) { + errors.add('The "androidPackage" must either be null or a string.'); + } + if (yaml['iosPrefix'] != null && yaml['iosPrefix'] is! String) { + errors.add('The "iosPrefix" must either be null or a string.'); + } + if (yaml['macosPrefix'] != null && yaml['macosPrefix'] is! String) { + errors.add('The "macosPrefix" must either be null or a string.'); + } + if (yaml['pluginClass'] != null && yaml['pluginClass'] is! String) { + errors.add('The "pluginClass" must either be null or a string..'); + } + return errors; + } + final String name; final String path; - final String androidPackage; - final String iosPrefix; - final String macosPrefix; - final String pluginClass; + + /// This is a mapping from platform config key to the plugin platform spec. + final Map platforms; } Plugin _pluginFromPubspec(String name, Uri packageRoot) { @@ -153,15 +272,19 @@ public final class GeneratedPluginRegistrant { } '''; +List> _extractPlatformMaps(List plugins, String type) { + final List> pluginConfigs = >[]; + for (Plugin p in plugins) { + final PluginPlatform platformPlugin = p.platforms[type]; + if (platformPlugin != null) { + pluginConfigs.add(platformPlugin.toMap()); + } + } + return pluginConfigs; +} + Future _writeAndroidPluginRegistrant(FlutterProject project, List plugins) async { - final List> androidPlugins = plugins - .where((Plugin p) => p.androidPackage != null && p.pluginClass != null) - .map>((Plugin p) => { - 'name': p.name, - 'package': p.androidPackage, - 'class': p.pluginClass, - }) - .toList(); + final List> androidPlugins = _extractPlatformMaps(plugins, AndroidPlugin.kConfigKey); final Map context = { 'plugins': androidPlugins, }; @@ -262,13 +385,7 @@ end '''; Future _writeIOSPluginRegistrant(FlutterProject project, List plugins) async { - final List> iosPlugins = plugins - .where((Plugin p) => p.pluginClass != null) - .map>((Plugin p) => { - 'name': p.name, - 'prefix': p.iosPrefix, - 'class': p.pluginClass, - }).toList(); + final List> iosPlugins = _extractPlatformMaps(plugins, IOSPlugin.kConfigKey); final Map context = { 'os': 'ios', 'deploymentTarget': '8.0', @@ -308,14 +425,7 @@ Future _writeIOSPluginRegistrant(FlutterProject project, List plug } Future _writeMacOSPluginRegistrant(FlutterProject project, List plugins) async { - // TODO(stuartmorgan): Replace macosPrefix check with formal metadata check, - // see https://github.com/flutter/flutter/issues/33597. - final List> macosPlugins = plugins - .where((Plugin p) => p.pluginClass != null && p.macosPrefix != null) - .map>((Plugin p) => { - 'name': p.name, - 'class': p.pluginClass, - }).toList(); + final List> macosPlugins = _extractPlatformMaps(plugins, MacOSPlugin.kConfigKey); final Map context = { 'os': 'macos', 'framework': 'FlutterMacOS', diff --git a/packages/flutter_tools/schema/pubspec_yaml.json b/packages/flutter_tools/schema/pubspec_yaml.json index 42d7f9cfd4..158616d04a 100644 --- a/packages/flutter_tools/schema/pubspec_yaml.json +++ b/packages/flutter_tools/schema/pubspec_yaml.json @@ -57,6 +57,34 @@ "type": "object", "additionalProperties": false, "properties": { + "platforms": { + "type": "object", + "additionalProperties": false, + "properties": { + "android": { + "type": "object", + "additionalProperties": false, + "properties": { + "package": {"type": "string"}, + "pluginClass": {"type": "string"} + } + }, + "ios": { + "type": "object", + "additionalProperties": false, + "properties": { + "pluginClass": {"type": "string"} + } + }, + "macos": { + "type": "object", + "additionalProperties": false, + "properties": { + "pluginClass": {"type": "string"} + } + } + } + }, "androidPackage": { "type": "string" }, "iosPrefix": { "type": "string" }, "macosPrefix": { "type": "string" }, diff --git a/packages/flutter_tools/templates/cocoapods/Podfile-macos b/packages/flutter_tools/templates/cocoapods/Podfile-macos index 23f5c12b0f..129a34eda3 100644 --- a/packages/flutter_tools/templates/cocoapods/Podfile-macos +++ b/packages/flutter_tools/templates/cocoapods/Podfile-macos @@ -41,9 +41,7 @@ def pubspec_supports_macos(file) return false; end File.foreach(file_abs_path) { |line| - # TODO(stuartmorgan): Use formal platform declaration once it exists, - # see https://github.com/flutter/flutter/issues/33597. - return true if line =~ /^\s*macosPrefix:/ + return true if line =~ /^\s*macos:/ } return false end diff --git a/packages/flutter_tools/test/general.shard/flutter_manifest_test.dart b/packages/flutter_tools/test/general.shard/flutter_manifest_test.dart index 706c0f83bc..8f5b7f86e4 100644 --- a/packages/flutter_tools/test/general.shard/flutter_manifest_test.dart +++ b/packages/flutter_tools/test/general.shard/flutter_manifest_test.dart @@ -374,7 +374,7 @@ flutter: expect(flutterManifest.androidPackage, 'com.example'); }); - test('allows a plugin declaration', () async { + test('allows a legacy plugin declaration', () async { const String manifest = ''' name: test flutter: @@ -385,6 +385,21 @@ flutter: expect(flutterManifest.isPlugin, true); expect(flutterManifest.androidPackage, 'com.example'); }); + test('allows a multi-plat plugin declaration', () async { + const String manifest = ''' +name: test +flutter: + plugin: + platforms: + android: + package: com.example + pluginClass: TestPlugin +'''; + final FlutterManifest flutterManifest = FlutterManifest.createFromString(manifest); + expect(flutterManifest.isPlugin, true); + expect(flutterManifest.androidPackage, 'com.example'); + }); + Future checkManifestVersion({ String manifest, diff --git a/packages/flutter_tools/test/general.shard/plugin_parsing_test.dart b/packages/flutter_tools/test/general.shard/plugin_parsing_test.dart new file mode 100644 index 0000000000..8c15310338 --- /dev/null +++ b/packages/flutter_tools/test/general.shard/plugin_parsing_test.dart @@ -0,0 +1,66 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter_tools/src/platform_plugins.dart'; +import 'package:flutter_tools/src/plugins.dart'; +import 'package:yaml/yaml.dart'; + +import '../src/common.dart'; + +const String _kTestPluginName = 'test_plugin_name'; +const String _kTestPluginPath = 'test_plugin_path'; + +void main() { + group('PluginParsing', () { + test('Legacy Format', () { + const String pluginYamlRaw = 'androidPackage: com.flutter.dev\n' + 'iosPrefix: FLT\n' + 'pluginClass: SamplePlugin\n'; + + final dynamic pluginYaml = loadYaml(pluginYamlRaw); + final Plugin plugin = + Plugin.fromYaml(_kTestPluginName, _kTestPluginPath, pluginYaml); + + final AndroidPlugin androidPlugin = + plugin.platforms[AndroidPlugin.kConfigKey]; + final IOSPlugin iosPlugin = plugin.platforms[IOSPlugin.kConfigKey]; + final String androidPluginClass = androidPlugin.pluginClass; + final String iosPluginClass = iosPlugin.pluginClass; + + expect(iosPluginClass, 'SamplePlugin'); + expect(androidPluginClass, 'SamplePlugin'); + expect(iosPlugin.classPrefix, 'FLT'); + expect(androidPlugin.package, 'com.flutter.dev'); + }); + + test('Multi-platform Format', () { + const String pluginYamlRaw = 'platforms:\n' + ' macos:\n' + ' pluginClass: MSamplePlugin\n' + ' android:\n' + ' package: com.flutter.dev\n' + ' pluginClass: ASamplePlugin\n' + ' ios:\n' + ' pluginClass: ISamplePlugin\n'; + + final dynamic pluginYaml = loadYaml(pluginYamlRaw); + final Plugin plugin = + Plugin.fromYaml(_kTestPluginName, _kTestPluginPath, pluginYaml); + + final AndroidPlugin androidPlugin = + plugin.platforms[AndroidPlugin.kConfigKey]; + final MacOSPlugin macOSPlugin = + plugin.platforms[MacOSPlugin.kConfigKey]; + final IOSPlugin iosPlugin = plugin.platforms[IOSPlugin.kConfigKey]; + final String androidPluginClass = androidPlugin.pluginClass; + final String iosPluginClass = iosPlugin.pluginClass; + + expect(iosPluginClass, 'ISamplePlugin'); + expect(androidPluginClass, 'ASamplePlugin'); + expect(iosPlugin.classPrefix, ''); + expect(androidPlugin.package, 'com.flutter.dev'); + expect(macOSPlugin.pluginClass, 'MSamplePlugin'); + }); + }); +} diff --git a/packages/flutter_tools/test/general.shard/project_test.dart b/packages/flutter_tools/test/general.shard/project_test.dart index 33cd5f68c4..fbe431d80b 100644 --- a/packages/flutter_tools/test/general.shard/project_test.dart +++ b/packages/flutter_tools/test/general.shard/project_test.dart @@ -231,10 +231,14 @@ void main() { }); group('example', () { - testInMemory('exists for plugin', () async { + testInMemory('exists for plugin in legacy format', () async { final FlutterProject project = await aPluginProject(); expect(project.hasExampleApp, isTrue); }); + testInMemory('exists for plugin in multi-platform format', () async { + final FlutterProject project = await aPluginProject(legacy: false); + expect(project.hasExampleApp, isTrue); + }); testInMemory('does not exist for non-plugin', () async { final FlutterProject project = await someProject(); expect(project.hasExampleApp, isFalse); @@ -475,19 +479,37 @@ Future someProject() async { return FlutterProject.fromDirectory(directory); } -Future aPluginProject() async { +Future aPluginProject({bool legacy = true}) async { final Directory directory = fs.directory('plugin_project'); directory.childDirectory('ios').createSync(recursive: true); directory.childDirectory('android').createSync(recursive: true); directory.childDirectory('example').createSync(recursive: true); - directory.childFile('pubspec.yaml').writeAsStringSync(''' + String pluginPubSpec; + if (legacy) { + pluginPubSpec = ''' name: my_plugin flutter: plugin: androidPackage: com.example pluginClass: MyPlugin iosPrefix: FLT -'''); +'''; + } else { + pluginPubSpec = ''' +name: my_plugin +flutter: + plugin: + platforms: + android: + package: com.example + pluginClass: MyPlugin + ios: + pluginClass: MyPlugin + macos: + pluginClass: MyPlugin +'''; + } + directory.childFile('pubspec.yaml').writeAsStringSync(pluginPubSpec); return FlutterProject.fromDirectory(directory); }