Fully support Dart-only mobile and macOS plugins (#96183)
This commit is contained in:
@@ -30,6 +30,46 @@ import '../src/context.dart';
|
||||
import '../src/fakes.dart' hide FakeOperatingSystemUtils;
|
||||
import '../src/pubspec_schema.dart';
|
||||
|
||||
/// Information for a platform entry in the 'platforms' section of a plugin's
|
||||
/// pubspec.yaml.
|
||||
class _PluginPlatformInfo {
|
||||
const _PluginPlatformInfo({
|
||||
this.pluginClass,
|
||||
this.dartPluginClass,
|
||||
this.androidPackage,
|
||||
this.fileName
|
||||
}) : assert(pluginClass != null || dartPluginClass != null),
|
||||
assert(androidPackage == null || pluginClass != null);
|
||||
|
||||
/// The pluginClass entry, if any.
|
||||
final String pluginClass;
|
||||
|
||||
/// The dartPluginClass entry, if any.
|
||||
final String dartPluginClass;
|
||||
|
||||
/// The package entry for an Android plugin implementation using pluginClass.
|
||||
final String androidPackage;
|
||||
|
||||
/// The fileName entry for a web plugin implementation.
|
||||
final String fileName;
|
||||
|
||||
/// Returns the body of a platform section for a plugin's pubspec, properly
|
||||
/// indented.
|
||||
String get indentedPubspecSection {
|
||||
const String indentation = ' ';
|
||||
return <String>[
|
||||
if (pluginClass != null)
|
||||
'${indentation}pluginClass: $pluginClass',
|
||||
if (dartPluginClass != null)
|
||||
'${indentation}dartPluginClass: $dartPluginClass',
|
||||
if (androidPackage != null)
|
||||
'${indentation}package: $androidPackage',
|
||||
if (fileName != null)
|
||||
'${indentation}fileName: $fileName',
|
||||
].join('\n');
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
group('plugins', () {
|
||||
FileSystem fs;
|
||||
@@ -331,7 +371,7 @@ flutter:
|
||||
);
|
||||
}
|
||||
|
||||
Directory createPluginWithDependencies({
|
||||
Directory createLegacyPluginWithDependencies({
|
||||
@required String name,
|
||||
@required List<String> dependencies,
|
||||
}) {
|
||||
@@ -347,6 +387,44 @@ flutter:
|
||||
plugin:
|
||||
androidPackage: plugin2
|
||||
pluginClass: UseNewEmbedding
|
||||
dependencies:
|
||||
''');
|
||||
for (final String dependency in dependencies) {
|
||||
pluginDirectory
|
||||
.childFile('pubspec.yaml')
|
||||
.writeAsStringSync(' $dependency:\n', mode: FileMode.append);
|
||||
}
|
||||
flutterProject.directory
|
||||
.childFile('.packages')
|
||||
.writeAsStringSync(
|
||||
'$name:${pluginDirectory.childDirectory('lib').uri.toString()}\n',
|
||||
mode: FileMode.append,
|
||||
);
|
||||
return pluginDirectory;
|
||||
}
|
||||
|
||||
Directory createPlugin({
|
||||
@required String name,
|
||||
@required Map<String, _PluginPlatformInfo> platforms,
|
||||
List<String> dependencies = const <String>[],
|
||||
}) {
|
||||
assert(name != null);
|
||||
assert(dependencies != null);
|
||||
|
||||
final Iterable<String> platformSections = platforms.entries.map((MapEntry<String, _PluginPlatformInfo> entry) => '''
|
||||
${entry.key}:
|
||||
${entry.value.indentedPubspecSection}
|
||||
''');
|
||||
final Directory pluginDirectory = fs.systemTempDirectory.createTempSync('flutter_plugin.');
|
||||
pluginDirectory
|
||||
.childFile('pubspec.yaml')
|
||||
.writeAsStringSync('''
|
||||
name: $name
|
||||
flutter:
|
||||
plugin:
|
||||
platforms:
|
||||
${platformSections.join('\n')}
|
||||
|
||||
dependencies:
|
||||
''');
|
||||
for (final String dependency in dependencies) {
|
||||
@@ -420,9 +498,9 @@ dependencies:
|
||||
testUsingContext(
|
||||
'Refreshing the plugin list modifies .flutter-plugins '
|
||||
'and .flutter-plugins-dependencies when there are plugins', () async {
|
||||
final Directory pluginA = createPluginWithDependencies(name: 'plugin-a', dependencies: const <String>['plugin-b', 'plugin-c', 'random-package']);
|
||||
final Directory pluginB = createPluginWithDependencies(name: 'plugin-b', dependencies: const <String>['plugin-c']);
|
||||
final Directory pluginC = createPluginWithDependencies(name: 'plugin-c', dependencies: const <String>[]);
|
||||
final Directory pluginA = createLegacyPluginWithDependencies(name: 'plugin-a', dependencies: const <String>['plugin-b', 'plugin-c', 'random-package']);
|
||||
final Directory pluginB = createLegacyPluginWithDependencies(name: 'plugin-b', dependencies: const <String>['plugin-c']);
|
||||
final Directory pluginC = createLegacyPluginWithDependencies(name: 'plugin-c', dependencies: const <String>[]);
|
||||
iosProject.testExists = true;
|
||||
|
||||
final DateTime dateCreated = DateTime(1970);
|
||||
@@ -449,22 +527,25 @@ dependencies:
|
||||
<String, dynamic> {
|
||||
'name': 'plugin-a',
|
||||
'path': '${pluginA.path}/',
|
||||
'native_build': true,
|
||||
'dependencies': <String>[
|
||||
'plugin-b',
|
||||
'plugin-c'
|
||||
]
|
||||
],
|
||||
},
|
||||
<String, dynamic> {
|
||||
'name': 'plugin-b',
|
||||
'path': '${pluginB.path}/',
|
||||
'native_build': true,
|
||||
'dependencies': <String>[
|
||||
'plugin-c'
|
||||
]
|
||||
],
|
||||
},
|
||||
<String, dynamic> {
|
||||
'name': 'plugin-c',
|
||||
'path': '${pluginC.path}/',
|
||||
'dependencies': <String>[]
|
||||
'native_build': true,
|
||||
'dependencies': <String>[],
|
||||
},
|
||||
];
|
||||
expect(plugins['ios'], expectedPlugins);
|
||||
@@ -514,6 +595,51 @@ dependencies:
|
||||
FlutterVersion: () => flutterVersion
|
||||
});
|
||||
|
||||
testUsingContext(
|
||||
'.flutter-plugins-dependencies indicates native build inclusion', () async {
|
||||
createPlugin(
|
||||
name: 'plugin-a',
|
||||
platforms: const <String, _PluginPlatformInfo>{
|
||||
// Native-only; should include native build.
|
||||
'android': _PluginPlatformInfo(pluginClass: 'Foo', androidPackage: 'bar.foo'),
|
||||
// Hybrid native and Dart; should include native build.
|
||||
'ios': _PluginPlatformInfo(pluginClass: 'Foo', dartPluginClass: 'Bar'),
|
||||
// Web; should not have the native build key at all since it doesn't apply.
|
||||
'web': _PluginPlatformInfo(pluginClass: 'Foo', fileName: 'lib/foo.dart'),
|
||||
// Dart-only; should not include native build.
|
||||
'windows': _PluginPlatformInfo(dartPluginClass: 'Foo'),
|
||||
});
|
||||
iosProject.testExists = true;
|
||||
|
||||
final DateTime dateCreated = DateTime(1970);
|
||||
systemClock.currentTime = dateCreated;
|
||||
|
||||
await refreshPluginsList(flutterProject);
|
||||
|
||||
expect(flutterProject.flutterPluginsDependenciesFile.existsSync(), true);
|
||||
final String pluginsString = flutterProject.flutterPluginsDependenciesFile.readAsStringSync();
|
||||
final Map<String, dynamic> jsonContent = json.decode(pluginsString) as Map<String, dynamic>;
|
||||
final Map<String, dynamic> plugins = jsonContent['plugins'] as Map<String, dynamic>;
|
||||
|
||||
// Extracts the native_build key (if any) from the first plugin for the
|
||||
// given platform.
|
||||
bool getNativeBuildValue(String platform) {
|
||||
final List<Map<String, dynamic>> platformPlugins = (plugins[platform]
|
||||
as List<dynamic>).cast<Map<String, dynamic>>();
|
||||
expect(platformPlugins.length, 1);
|
||||
return platformPlugins[0]['native_build'] as bool;
|
||||
}
|
||||
expect(getNativeBuildValue('android'), true);
|
||||
expect(getNativeBuildValue('ios'), true);
|
||||
expect(getNativeBuildValue('web'), null);
|
||||
expect(getNativeBuildValue('windows'), false);
|
||||
}, overrides: <Type, Generator>{
|
||||
FileSystem: () => fs,
|
||||
ProcessManager: () => FakeProcessManager.any(),
|
||||
SystemClock: () => systemClock,
|
||||
FlutterVersion: () => flutterVersion
|
||||
});
|
||||
|
||||
testUsingContext('Changes to the plugin list invalidates the Cocoapod lockfiles', () async {
|
||||
simulatePodInstallRun(iosProject);
|
||||
simulatePodInstallRun(macosProject);
|
||||
|
||||
Reference in New Issue
Block a user