feat: support conditional platform plugins (#18)

This commit is contained in:
Felix Angelov 2022-04-12 12:19:43 -05:00 committed by GitHub
parent 699457e663
commit 1eba29b7ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 101 additions and 7 deletions

View File

@ -3,7 +3,7 @@ description: A very good federated Flutter plugin.
version: 0.1.0+1 version: 0.1.0+1
environment: environment:
mason: ">=0.1.0-dev.15 <0.1.0" mason: ">=0.1.0-dev.17 <0.1.0"
vars: vars:
project_name: project_name:

View File

@ -1,6 +1,40 @@
import 'dart:io'; import 'dart:io';
import 'package:path/path.dart' as path; import 'package:path/path.dart' as path;
const _pluginDependencies = '''
{{project_name.snakeCase()}}_android:
path: ../{{project_name.snakeCase()}}_android
{{project_name.snakeCase()}}_ios:
path: ../{{project_name.snakeCase()}}_ios
{{project_name.snakeCase()}}_linux:
path: ../{{project_name.snakeCase()}}_linux
{{project_name.snakeCase()}}_macos:
path: ../{{project_name.snakeCase()}}_macos
{{project_name.snakeCase()}}_platform_interface:
path: ../{{project_name.snakeCase()}}_platform_interface
{{project_name.snakeCase()}}_web:
path: ../{{project_name.snakeCase()}}_web
{{project_name.snakeCase()}}_windows:
path: ../{{project_name.snakeCase()}}_windows''';
const _pluginPlatforms = '''
flutter:
plugin:
platforms:
android:
default_package: {{project_name.snakeCase()}}_android
ios:
default_package: {{project_name.snakeCase()}}_ios
macos:
default_package: {{project_name.snakeCase()}}_macos
linux:
default_package: {{project_name.snakeCase()}}_linux
web:
default_package: {{project_name.snakeCase()}}_web
windows:
default_package: {{project_name.snakeCase()}}_windows''';
final _staticPath = path.join('tool', 'generator', 'static');
final _githubPath = path.join('.github'); final _githubPath = path.join('.github');
final _sourcePath = path.join('src'); final _sourcePath = path.join('src');
final _targetPath = path.join('brick', '__brick__'); final _targetPath = path.join('brick', '__brick__');
@ -28,6 +62,15 @@ final copyrightHeader = '''
// https://opensource.org/licenses/MIT. // https://opensource.org/licenses/MIT.
'''; ''';
const platforms = [
'android',
'ios',
'linux',
'macos',
'web',
'windows',
];
final excludedFiles = [ final excludedFiles = [
path.join( path.join(
_targetPath, _targetPath,
@ -49,6 +92,7 @@ void main() async {
await Future.wait([ await Future.wait([
Shell.cp(_sourcePath, _targetPath), Shell.cp(_sourcePath, _targetPath),
Shell.cp(_githubPath, path.join(_targetPath)), Shell.cp(_githubPath, path.join(_targetPath)),
Shell.cp('$_staticPath/', _targetPath),
() async { () async {
await Shell.mkdir(File(_targetMyPluginKtPath).parent.path); await Shell.mkdir(File(_targetMyPluginKtPath).parent.path);
await Shell.cp(_sourceMyPluginKtPath, _targetMyPluginKtPath); await Shell.cp(_sourceMyPluginKtPath, _targetMyPluginKtPath);
@ -61,6 +105,20 @@ void main() async {
excludedFiles.map((file) => File(file).delete(recursive: true)), excludedFiles.map((file) => File(file).delete(recursive: true)),
); );
// Add conditionals to platforms
for (final platform in platforms) {
final directoryPath = path.join(_targetPath, 'my_plugin_$platform');
final conditionalPath = path.join(
_targetPath,
'{{#$platform}}my_plugin_$platform{{',
'$platform}}',
);
Directory(conditionalPath).createSync(recursive: true);
await Shell.cp('$directoryPath/', '$conditionalPath/');
await Shell.rm(directoryPath);
}
await Future.wait( await Future.wait(
Directory(_targetPath) Directory(_targetPath)
.listSync(recursive: true) .listSync(recursive: true)
@ -89,6 +147,11 @@ void main() async {
'A very good Flutter federated plugin', 'A very good Flutter federated plugin',
'{{{description}}}', '{{{description}}}',
) )
.replaceAll(_pluginPlatforms, '{{> plugin_platforms.dart }}')
.replaceAll(
_pluginDependencies,
'{{> plugin_dependencies.dart }}',
)
: contents; : contents;
file = templatedContents is String file = templatedContents is String
? await file.writeAsString(templatedContents) ? await file.writeAsString(templatedContents)
@ -117,13 +180,13 @@ void main() async {
// Clean up top-level directories // Clean up top-level directories
const topLevelDirs = [ const topLevelDirs = [
'my_plugin', 'my_plugin',
'my_plugin_android', '{{#android}}my_plugin_android{{',
'my_plugin_ios', '{{#ios}}my_plugin_ios{{',
'my_plugin_linux', '{{#linux}}my_plugin_linux{{',
'my_plugin_macos', '{{#macos}}my_plugin_macos{{',
'my_plugin_platform_interface', 'my_plugin_platform_interface',
'my_plugin_web', '{{#web}}my_plugin_web{{',
'my_plugin_windows', '{{#windows}}my_plugin_windows{{',
]; ];
for (final dir in topLevelDirs) { for (final dir in topLevelDirs) {
Directory(path.join(_targetPath, dir)).deleteSync(recursive: true); Directory(path.join(_targetPath, dir)).deleteSync(recursive: true);

View File

@ -0,0 +1,14 @@
{{#android}} {{project_name.snakeCase()}}_android:
path: ../{{project_name.snakeCase()}}_android{{/android}}{{#ios}}
{{project_name.snakeCase()}}_ios:
path: ../{{project_name.snakeCase()}}_ios{{/ios}}{{#linux}}
{{project_name.snakeCase()}}_linux:
path: ../{{project_name.snakeCase()}}_linux{{/linux}}{{#macos}}
{{project_name.snakeCase()}}_macos:
path: ../{{project_name.snakeCase()}}_macos{{/macos}}
{{project_name.snakeCase()}}_platform_interface:
path: ../{{project_name.snakeCase()}}_platform_interface{{#web}}
{{project_name.snakeCase()}}_web:
path: ../{{project_name.snakeCase()}}_web{{/web}}{{#windows}}
{{project_name.snakeCase()}}_windows:
path: ../{{project_name.snakeCase()}}_windows{{/windows}}

View File

@ -0,0 +1,17 @@
flutter:
plugin:
platforms:{{#android}}
{{project_name.snakeCase()}}_android:
path: ../{{project_name.snakeCase()}}_android{{/android}}{{#ios}}
{{project_name.snakeCase()}}_ios:
path: ../{{project_name.snakeCase()}}_ios{{/ios}}{{#linux}}
{{project_name.snakeCase()}}_linux:
path: ../{{project_name.snakeCase()}}_linux{{/linux}}{{#macos}}
{{project_name.snakeCase()}}_macos:
path: ../{{project_name.snakeCase()}}_macos{{/macos}}
{{project_name.snakeCase()}}_platform_interface:
path: ../{{project_name.snakeCase()}}_platform_interface{{#web}}
{{project_name.snakeCase()}}_web:
path: ../{{project_name.snakeCase()}}_web{{/web}}{{#windows}}
{{project_name.snakeCase()}}_windows:
path: ../{{project_name.snakeCase()}}_windows{{/windows}}