From 1eba29b7ca1f4448d761730d39fdba21b600b3bb Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Tue, 12 Apr 2022 12:19:43 -0500 Subject: [PATCH] feat: support conditional platform plugins (#18) --- brick/brick.yaml | 2 +- tool/generator/main.dart | 75 +++++++++++++++++-- .../static/{{~ plugin_dependencies.dart }} | 14 ++++ .../static/{{~ plugin_platforms.dart }} | 17 +++++ 4 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 tool/generator/static/{{~ plugin_dependencies.dart }} create mode 100644 tool/generator/static/{{~ plugin_platforms.dart }} diff --git a/brick/brick.yaml b/brick/brick.yaml index b2ec44d..2abba38 100644 --- a/brick/brick.yaml +++ b/brick/brick.yaml @@ -3,7 +3,7 @@ description: A very good federated Flutter plugin. version: 0.1.0+1 environment: - mason: ">=0.1.0-dev.15 <0.1.0" + mason: ">=0.1.0-dev.17 <0.1.0" vars: project_name: diff --git a/tool/generator/main.dart b/tool/generator/main.dart index 7743c23..e703b36 100644 --- a/tool/generator/main.dart +++ b/tool/generator/main.dart @@ -1,6 +1,40 @@ import 'dart:io'; 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 _sourcePath = path.join('src'); final _targetPath = path.join('brick', '__brick__'); @@ -28,6 +62,15 @@ final copyrightHeader = ''' // https://opensource.org/licenses/MIT. '''; +const platforms = [ + 'android', + 'ios', + 'linux', + 'macos', + 'web', + 'windows', +]; + final excludedFiles = [ path.join( _targetPath, @@ -49,6 +92,7 @@ void main() async { await Future.wait([ Shell.cp(_sourcePath, _targetPath), Shell.cp(_githubPath, path.join(_targetPath)), + Shell.cp('$_staticPath/', _targetPath), () async { await Shell.mkdir(File(_targetMyPluginKtPath).parent.path); await Shell.cp(_sourceMyPluginKtPath, _targetMyPluginKtPath); @@ -61,6 +105,20 @@ void main() async { 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( Directory(_targetPath) .listSync(recursive: true) @@ -89,6 +147,11 @@ void main() async { 'A very good Flutter federated plugin', '{{{description}}}', ) + .replaceAll(_pluginPlatforms, '{{> plugin_platforms.dart }}') + .replaceAll( + _pluginDependencies, + '{{> plugin_dependencies.dart }}', + ) : contents; file = templatedContents is String ? await file.writeAsString(templatedContents) @@ -117,13 +180,13 @@ void main() async { // Clean up top-level directories const topLevelDirs = [ 'my_plugin', - 'my_plugin_android', - 'my_plugin_ios', - 'my_plugin_linux', - 'my_plugin_macos', + '{{#android}}my_plugin_android{{', + '{{#ios}}my_plugin_ios{{', + '{{#linux}}my_plugin_linux{{', + '{{#macos}}my_plugin_macos{{', 'my_plugin_platform_interface', - 'my_plugin_web', - 'my_plugin_windows', + '{{#web}}my_plugin_web{{', + '{{#windows}}my_plugin_windows{{', ]; for (final dir in topLevelDirs) { Directory(path.join(_targetPath, dir)).deleteSync(recursive: true); diff --git a/tool/generator/static/{{~ plugin_dependencies.dart }} b/tool/generator/static/{{~ plugin_dependencies.dart }} new file mode 100644 index 0000000..672b426 --- /dev/null +++ b/tool/generator/static/{{~ plugin_dependencies.dart }} @@ -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}} \ No newline at end of file diff --git a/tool/generator/static/{{~ plugin_platforms.dart }} b/tool/generator/static/{{~ plugin_platforms.dart }} new file mode 100644 index 0000000..3139285 --- /dev/null +++ b/tool/generator/static/{{~ plugin_platforms.dart }} @@ -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}} \ No newline at end of file