From 46c51525e82f2b19f5657ff121db30ca2d1df266 Mon Sep 17 00:00:00 2001 From: AN12345 Date: Mon, 6 Feb 2023 18:42:34 +0100 Subject: [PATCH] feat: add generators for components proxy and mixins to enable copywith methods --- .../wyatt_component_copy_with_gen/.gitignore | 7 ++ .../.vscode/extensions.json | 24 +++++ .../.vscode/launch.json | 34 +++++++ .../.vscode/settings.json | 72 ++++++++++++++ .../CHANGELOG.md | 3 + .../wyatt_component_copy_with_gen/README.md | 57 +++++++++++ .../analysis_options.yaml | 7 ++ .../wyatt_component_copy_with_gen/build.yaml | 21 ++++ .../example/.gitignore | 44 +++++++++ .../example/.metadata | 30 ++++++ .../example/README.md | 16 +++ .../example/analysis_options.yaml | 7 ++ .../example/lib/custom_app_bar_example.dart | 32 ++++++ .../example/lib/custom_app_bar_example.g.dart | 36 +++++++ .../example/pubspec.yaml | 28 ++++++ .../lib/component_copy_with_gen.dart | 21 ++++ .../lib/src/builder.dart | 28 ++++++ .../component_copy_with_generator.dart | 97 +++++++++++++++++++ .../generators/component_proxy_generator.dart | 75 ++++++++++++++ .../pubspec.yaml | 23 +++++ 20 files changed, 662 insertions(+) create mode 100644 packages/wyatt_component_copy_with_gen/.gitignore create mode 100644 packages/wyatt_component_copy_with_gen/.vscode/extensions.json create mode 100644 packages/wyatt_component_copy_with_gen/.vscode/launch.json create mode 100644 packages/wyatt_component_copy_with_gen/.vscode/settings.json create mode 100644 packages/wyatt_component_copy_with_gen/CHANGELOG.md create mode 100644 packages/wyatt_component_copy_with_gen/README.md create mode 100644 packages/wyatt_component_copy_with_gen/analysis_options.yaml create mode 100644 packages/wyatt_component_copy_with_gen/build.yaml create mode 100644 packages/wyatt_component_copy_with_gen/example/.gitignore create mode 100644 packages/wyatt_component_copy_with_gen/example/.metadata create mode 100644 packages/wyatt_component_copy_with_gen/example/README.md create mode 100644 packages/wyatt_component_copy_with_gen/example/analysis_options.yaml create mode 100644 packages/wyatt_component_copy_with_gen/example/lib/custom_app_bar_example.dart create mode 100644 packages/wyatt_component_copy_with_gen/example/lib/custom_app_bar_example.g.dart create mode 100644 packages/wyatt_component_copy_with_gen/example/pubspec.yaml create mode 100644 packages/wyatt_component_copy_with_gen/lib/component_copy_with_gen.dart create mode 100644 packages/wyatt_component_copy_with_gen/lib/src/builder.dart create mode 100644 packages/wyatt_component_copy_with_gen/lib/src/generators/component_copy_with_generator.dart create mode 100644 packages/wyatt_component_copy_with_gen/lib/src/generators/component_proxy_generator.dart create mode 100644 packages/wyatt_component_copy_with_gen/pubspec.yaml diff --git a/packages/wyatt_component_copy_with_gen/.gitignore b/packages/wyatt_component_copy_with_gen/.gitignore new file mode 100644 index 00000000..3cceda55 --- /dev/null +++ b/packages/wyatt_component_copy_with_gen/.gitignore @@ -0,0 +1,7 @@ +# https://dart.dev/guides/libraries/private-files +# Created by `dart pub` +.dart_tool/ + +# Avoid committing pubspec.lock for library packages; see +# https://dart.dev/guides/libraries/private-files#pubspeclock. +pubspec.lock diff --git a/packages/wyatt_component_copy_with_gen/.vscode/extensions.json b/packages/wyatt_component_copy_with_gen/.vscode/extensions.json new file mode 100644 index 00000000..30cd2233 --- /dev/null +++ b/packages/wyatt_component_copy_with_gen/.vscode/extensions.json @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2022 WYATT GROUP + * Please see the AUTHORS file for details. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +{ + "recommendations": [ + "psioniq.psi-header", + "blaugold.melos-code" + ] +} \ No newline at end of file diff --git a/packages/wyatt_component_copy_with_gen/.vscode/launch.json b/packages/wyatt_component_copy_with_gen/.vscode/launch.json new file mode 100644 index 00000000..653eabc8 --- /dev/null +++ b/packages/wyatt_component_copy_with_gen/.vscode/launch.json @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2023 WYATT GROUP + * Please see the AUTHORS file for details. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch Example", + "request": "launch", + "type": "dart", + "cwd": "example/", + "program": "lib/main.dart", + "flutterMode": "debug" + }, + ] +} \ No newline at end of file diff --git a/packages/wyatt_component_copy_with_gen/.vscode/settings.json b/packages/wyatt_component_copy_with_gen/.vscode/settings.json new file mode 100644 index 00000000..a729c46c --- /dev/null +++ b/packages/wyatt_component_copy_with_gen/.vscode/settings.json @@ -0,0 +1,72 @@ +{ + "dart.runPubGetOnPubspecChanges": "never", + "bloc.newCubitTemplate.type": "equatable", + "psi-header.changes-tracking": { + "isActive": true + }, + "psi-header.config": { + "blankLinesAfter": 1, + "forceToTop": true + }, + "psi-header.lang-config": [ + { + "beforeHeader": [ + "# -*- coding:utf-8 -*-", + "#!/usr/bin/env python3" + ], + "begin": "###", + "end": "###", + "language": "python", + "prefix": "# " + }, + { + "beforeHeader": [ + "#!/usr/bin/env sh", + "" + ], + "language": "shellscript", + "begin": "", + "end": "", + "prefix": "# " + }, + { + "begin": "", + "end": "", + "language": "dart", + "prefix": "// " + }, + { + "begin": "", + "end": "", + "language": "yaml", + "prefix": "# " + }, + { + "begin": "", + "language": "markdown", + }, + ], + "psi-header.templates": [ + { + "language": "*", + "template": [ + "Copyright (C) <> WYATT GROUP", + "Please see the AUTHORS file for details.", + "", + "This program is free software: you can redistribute it and/or modify", + "it under the terms of the GNU General Public License as published by", + "the Free Software Foundation, either version 3 of the License, or", + "any later version.", + "", + "This program is distributed in the hope that it will be useful,", + "but WITHOUT ANY WARRANTY; without even the implied warranty of", + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the", + "GNU General Public License for more details.", + "", + "You should have received a copy of the GNU General Public License", + "along with this program. If not, see ." + ], + } + ], +} \ No newline at end of file diff --git a/packages/wyatt_component_copy_with_gen/CHANGELOG.md b/packages/wyatt_component_copy_with_gen/CHANGELOG.md new file mode 100644 index 00000000..effe43c8 --- /dev/null +++ b/packages/wyatt_component_copy_with_gen/CHANGELOG.md @@ -0,0 +1,3 @@ +## 1.0.0 + +- Initial version. diff --git a/packages/wyatt_component_copy_with_gen/README.md b/packages/wyatt_component_copy_with_gen/README.md new file mode 100644 index 00000000..6b606cd8 --- /dev/null +++ b/packages/wyatt_component_copy_with_gen/README.md @@ -0,0 +1,57 @@ + + +# Dart - Component Copy With Gen + +

+ Style: Wyatt Analysis + SDK: Dart & Flutter +

+ +A Dart package for generating code from annotations to ease the use of a UIKit in Flutter applications. The generated code is based on the annotation classes present in the 'wyatt_component_copy_with_extension' package. + +## Features + +- Supports the generation of abstract proxies in the `wyatt_ui_components` package. +- Supports direct use in Flutter applications. + +## Usage + +### In the 'wyatt_ui_components' package + +- Add the appropriate annotation when addicdng a new component. +- Run the build runner command to generate the proxy. + +### In Flutter applications + +- Add the following dependencies to your pubspec.yaml: + +```yaml +dependencies: + ... + wyatt_component_copy_with_extension: ^0.0.1 +dev_dependencies: + ... + wyatt_component_copy_with_gen: ^0.0.1 + build_runner: ^2.3.3 +``` + +- In your UIKit, extend the desired component class and add the appropriate annotation. +- Run the code generation command via the build runner. + +For further details and additional features on class annotation, see the 'wyatt_component_copy_with_extension' package's README. diff --git a/packages/wyatt_component_copy_with_gen/analysis_options.yaml b/packages/wyatt_component_copy_with_gen/analysis_options.yaml new file mode 100644 index 00000000..50ed209d --- /dev/null +++ b/packages/wyatt_component_copy_with_gen/analysis_options.yaml @@ -0,0 +1,7 @@ + + + +include: package:wyatt_analysis/analysis_options.yaml + + + diff --git a/packages/wyatt_component_copy_with_gen/build.yaml b/packages/wyatt_component_copy_with_gen/build.yaml new file mode 100644 index 00000000..5d11aeaf --- /dev/null +++ b/packages/wyatt_component_copy_with_gen/build.yaml @@ -0,0 +1,21 @@ +targets: + $default: + builders: + component_copy_with_gen: + enabled: true + generate_for: + exclude: + - test + - example + include: + - test/gen_* + +builders: + component_copy_with_gen: + target: ":component_copy_with_gen" + import: "package:wyatt_component_copy_with_gen/component_copy_with_gen.dart" + builder_factories: ["componentCopyWithReporter"] + build_extensions: { ".dart": ["copy_with_extension_gen.g.part"] } + auto_apply: dependents + build_to: cache + applies_builders: ["source_gen|combining_builder"] diff --git a/packages/wyatt_component_copy_with_gen/example/.gitignore b/packages/wyatt_component_copy_with_gen/example/.gitignore new file mode 100644 index 00000000..24476c5d --- /dev/null +++ b/packages/wyatt_component_copy_with_gen/example/.gitignore @@ -0,0 +1,44 @@ +# Miscellaneous +*.class +*.log +*.pyc +*.swp +.DS_Store +.atom/ +.buildlog/ +.history +.svn/ +migrate_working_dir/ + +# IntelliJ related +*.iml +*.ipr +*.iws +.idea/ + +# The .vscode folder contains launch configuration and tasks you configure in +# VS Code which you may wish to be included in version control, so this line +# is commented out by default. +#.vscode/ + +# Flutter/Dart/Pub related +**/doc/api/ +**/ios/Flutter/.last_build_id +.dart_tool/ +.flutter-plugins +.flutter-plugins-dependencies +.packages +.pub-cache/ +.pub/ +/build/ + +# Symbolication related +app.*.symbols + +# Obfuscation related +app.*.map.json + +# Android Studio will place build artifacts here +/android/app/debug +/android/app/profile +/android/app/release diff --git a/packages/wyatt_component_copy_with_gen/example/.metadata b/packages/wyatt_component_copy_with_gen/example/.metadata new file mode 100644 index 00000000..30e0d3ce --- /dev/null +++ b/packages/wyatt_component_copy_with_gen/example/.metadata @@ -0,0 +1,30 @@ +# This file tracks properties of this Flutter project. +# Used by Flutter tool to assess capabilities and perform upgrades etc. +# +# This file should be version controlled. + +version: + revision: b06b8b2710955028a6b562f5aa6fe62941d6febf + channel: stable + +project_type: app + +# Tracks metadata for the flutter migrate command +migration: + platforms: + - platform: root + create_revision: b06b8b2710955028a6b562f5aa6fe62941d6febf + base_revision: b06b8b2710955028a6b562f5aa6fe62941d6febf + - platform: web + create_revision: b06b8b2710955028a6b562f5aa6fe62941d6febf + base_revision: b06b8b2710955028a6b562f5aa6fe62941d6febf + + # User provided section + + # List of Local paths (relative to this file) that should be + # ignored by the migrate tool. + # + # Files that are not part of the templates will be ignored by default. + unmanaged_files: + - 'lib/main.dart' + - 'ios/Runner.xcodeproj/project.pbxproj' diff --git a/packages/wyatt_component_copy_with_gen/example/README.md b/packages/wyatt_component_copy_with_gen/example/README.md new file mode 100644 index 00000000..2b3fce4c --- /dev/null +++ b/packages/wyatt_component_copy_with_gen/example/README.md @@ -0,0 +1,16 @@ +# example + +A new Flutter project. + +## Getting Started + +This project is a starting point for a Flutter application. + +A few resources to get you started if this is your first Flutter project: + +- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab) +- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook) + +For help getting started with Flutter development, view the +[online documentation](https://docs.flutter.dev/), which offers tutorials, +samples, guidance on mobile development, and a full API reference. diff --git a/packages/wyatt_component_copy_with_gen/example/analysis_options.yaml b/packages/wyatt_component_copy_with_gen/example/analysis_options.yaml new file mode 100644 index 00000000..50ed209d --- /dev/null +++ b/packages/wyatt_component_copy_with_gen/example/analysis_options.yaml @@ -0,0 +1,7 @@ + + + +include: package:wyatt_analysis/analysis_options.yaml + + + diff --git a/packages/wyatt_component_copy_with_gen/example/lib/custom_app_bar_example.dart b/packages/wyatt_component_copy_with_gen/example/lib/custom_app_bar_example.dart new file mode 100644 index 00000000..7c69d3b3 --- /dev/null +++ b/packages/wyatt_component_copy_with_gen/example/lib/custom_app_bar_example.dart @@ -0,0 +1,32 @@ +// Copyright (C) 2023 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import 'package:flutter/material.dart'; +import 'package:wyatt_component_copy_with_extension/component_copy_with_extension.dart'; +import 'package:wyatt_ui_components/wyatt_wyatt_ui_components.dart'; + +// part 'custom_app_bar_example.g.dart'; + +@ComponentCopyWithExtension() +class CustomAppBarExample extends AppBarComponent + with $CustomAppBarExampleCWProxyImpl { + const CustomAppBarExample({super.title, super.key}); + + @override + Widget build(BuildContext context) => AppBar( + title: Text(super.title ?? ''), + ); +} diff --git a/packages/wyatt_component_copy_with_gen/example/lib/custom_app_bar_example.g.dart b/packages/wyatt_component_copy_with_gen/example/lib/custom_app_bar_example.g.dart new file mode 100644 index 00000000..95d56868 --- /dev/null +++ b/packages/wyatt_component_copy_with_gen/example/lib/custom_app_bar_example.g.dart @@ -0,0 +1,36 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'custom_app_bar_example.dart'; + +// ************************************************************************** +// ComponentCopyWithGenerator +// ************************************************************************** + +class $CustomAppBarExampleCWProxyImpl implements $AppBarComponentCWProxy { + const $CustomAppBarExampleCWProxyImpl(this._value); + final CustomAppBarExample _value; + @override + CustomAppBarExample title(String? title) => this(title: title); + @override + CustomAppBarExample leading(Widget? leading) => this(leading: leading); + @override + CustomAppBarExample actions(List? actions) => this(actions: actions); + @override + CustomAppBarExample key(Key? key) => this(key: key); + @override + CustomAppBarExample call({ + String? title, + Widget? leading, + List? actions, + Key? key, + }) => + CustomAppBarExample( + title: title ?? _value.title, + key: key ?? _value.key, + ); +} + +mixin $CustomAppBarExampleCWMixin on Component { + $AppBarComponentCWProxy get copyWith => + $CustomAppBarExampleCWProxyImpl(this as CustomAppBarExample); +} diff --git a/packages/wyatt_component_copy_with_gen/example/pubspec.yaml b/packages/wyatt_component_copy_with_gen/example/pubspec.yaml new file mode 100644 index 00000000..37747c25 --- /dev/null +++ b/packages/wyatt_component_copy_with_gen/example/pubspec.yaml @@ -0,0 +1,28 @@ +name: component_copy_with_gen_example +description: A new Flutter project. +version: 1.0.0 + +publish_to: "none" + +environment: + sdk: ">=2.19.0 <3.0.0" + +dependencies: + flutter: + sdk: flutter + + wyatt_component_copy_with_extension: + path: "../../wyatt_component_copy_with_extension" + + wyatt_ui_components: + git: + url: ssh://git@git.wyatt-studio.fr:993/Wyatt-FOSS/wyatt-packages.git + path: packages/wyatt_ui_components + +dev_dependencies: + build_runner: ^2.3.3 + wyatt_component_copy_with_gen: + path: "../" + wyatt_analysis: + hosted: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub + version: ^2.3.0 diff --git a/packages/wyatt_component_copy_with_gen/lib/component_copy_with_gen.dart b/packages/wyatt_component_copy_with_gen/lib/component_copy_with_gen.dart new file mode 100644 index 00000000..6e51cdd6 --- /dev/null +++ b/packages/wyatt_component_copy_with_gen/lib/component_copy_with_gen.dart @@ -0,0 +1,21 @@ +// Copyright (C) 2023 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +/// Generator for copywith method for components +library component_copy_with_gen; + +export 'src/builder.dart'; +export 'src/generators/component_copy_with_generator.dart'; diff --git a/packages/wyatt_component_copy_with_gen/lib/src/builder.dart b/packages/wyatt_component_copy_with_gen/lib/src/builder.dart new file mode 100644 index 00000000..c82cd976 --- /dev/null +++ b/packages/wyatt_component_copy_with_gen/lib/src/builder.dart @@ -0,0 +1,28 @@ +// Copyright (C) 2023 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import 'package:build/build.dart'; +import 'package:source_gen/source_gen.dart'; +import 'package:wyatt_component_copy_with_gen/src/generators/component_copy_with_generator.dart'; +import 'package:wyatt_component_copy_with_gen/src/generators/component_proxy_generator.dart'; + +Builder componentCopyWithReporter(BuilderOptions options) => SharedPartBuilder( + [ + ComponentProxyGenerator(), + ComponentCopyWithGenerator(), + ], + 'component_copy_with', + ); diff --git a/packages/wyatt_component_copy_with_gen/lib/src/generators/component_copy_with_generator.dart b/packages/wyatt_component_copy_with_gen/lib/src/generators/component_copy_with_generator.dart new file mode 100644 index 00000000..0c9994cb --- /dev/null +++ b/packages/wyatt_component_copy_with_gen/lib/src/generators/component_copy_with_generator.dart @@ -0,0 +1,97 @@ +// Copyright (C) 2023 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import 'dart:async'; + +import 'package:analyzer/dart/element/element.dart'; +import 'package:build/build.dart'; +import 'package:source_gen/source_gen.dart'; +import 'package:wyatt_component_copy_with_extension/component_copy_with_extension.dart'; + +class ComponentCopyWithGenerator + extends GeneratorForAnnotation { + @override + FutureOr generateForAnnotatedElement( + Element element, + ConstantReader annotation, + BuildStep buildStep, + ) { + /// Check element type + if (element is! ClassElement) { + throw InvalidGenerationSourceError( + 'Only classes can be annotated with "CopyWith". "$element" is ' + 'not a ClassElement.', + element: element, + ); + } + + final classAnnotation = ComponentCopyWithExtension( + skipFields: annotation.peek('skipFields')?.boolValue ?? true, + ); + + final generatedCode = StringBuffer() + + // Generate CopyWith Proxy implementation. + ..write('class \$${element.displayName}CWProxyImpl implements ' + '\$${element.supertype?.getDisplayString( + withNullability: false, + )}CWProxy { ' + 'const \$${element.displayName}CWProxyImpl ' + '(this._value); ' + 'final ${element.displayName} _value;'); + + if (classAnnotation.skipFields ?? true) { + for (final superField + in element.supertype!.element.constructors.first.parameters) { + final superFieldDisplayName = superField.displayName; + generatedCode.write('@override ${element.displayName} ' + '$superFieldDisplayName(${superField.type.getDisplayString( + withNullability: false, + )}? ' + ' $superFieldDisplayName)' + ' => this($superFieldDisplayName : $superFieldDisplayName); '); + } + } + + generatedCode.write('@override ${element.displayName} call ({'); + + for (final superField + in element.supertype!.element.constructors.first.parameters) { + final superFieldDisplayName = superField.displayName; + generatedCode.write('${superField.type.getDisplayString( + withNullability: false, + )}? $superFieldDisplayName,'); + } + + generatedCode.write('})=>${element.displayName}('); + + for (final superField in element.constructors.first.parameters) { + final superFieldDisplayName = superField.displayName; + generatedCode.write('$superFieldDisplayName:$superFieldDisplayName?? ' + '_value.$superFieldDisplayName,'); + } + + generatedCode + ..write(');}') + ..write( + 'mixin \$${element.displayName}CWMixin on Component { ' + '\$${element.supertype?.element.displayName}CWProxy get copyWith => ' + '\$${element.displayName}CWProxyImpl(this as ${element.displayName});}', + ); + + return generatedCode.toString(); + } +} diff --git a/packages/wyatt_component_copy_with_gen/lib/src/generators/component_proxy_generator.dart b/packages/wyatt_component_copy_with_gen/lib/src/generators/component_proxy_generator.dart new file mode 100644 index 00000000..e9eede8b --- /dev/null +++ b/packages/wyatt_component_copy_with_gen/lib/src/generators/component_proxy_generator.dart @@ -0,0 +1,75 @@ +// Copyright (C) 2023 WYATT GROUP +// Please see the AUTHORS file for details. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +import 'dart:async'; + +import 'package:analyzer/dart/element/element.dart'; +import 'package:analyzer/dart/element/nullability_suffix.dart'; +import 'package:build/build.dart'; +import 'package:source_gen/source_gen.dart'; +import 'package:wyatt_component_copy_with_extension/component_copy_with_extension.dart'; + +class ComponentProxyGenerator + extends GeneratorForAnnotation { + @override + FutureOr generateForAnnotatedElement( + Element element, + ConstantReader annotation, + BuildStep buildStep, + ) { + /// Check element type + if (element is! ClassElement) { + throw InvalidGenerationSourceError( + 'Only classes can be annotated with "CopyWith". "$element" is ' + 'not a ClassElement.', + element: element, + ); + } + + final classAnnotation = ComponentProxyExtension( + skipFields: annotation.peek('skipFields')?.boolValue ?? true, + ); + + final generatedCode = StringBuffer() + ..write('abstract class \$${element.displayName}CWProxy {'); + + if (classAnnotation.skipFields ?? true) { + for (final field in element.constructors.first.parameters) { + generatedCode.write( + '${element.displayName} ' + '${field.displayName}(${field.type.getDisplayString( + withNullability: false, + )}? ' + '${field.displayName});', + ); + } + } + + generatedCode.write('${element.displayName} call({'); + + for (final field in element.constructors.first.parameters) { + if (field.type.nullabilitySuffix == NullabilitySuffix.question) { + generatedCode.write('${field.type.getDisplayString( + withNullability: false, + )}? ${field.displayName}, '); + } + } + + generatedCode.write('});}'); + + return generatedCode.toString(); + } +} diff --git a/packages/wyatt_component_copy_with_gen/pubspec.yaml b/packages/wyatt_component_copy_with_gen/pubspec.yaml new file mode 100644 index 00000000..4fb5c97c --- /dev/null +++ b/packages/wyatt_component_copy_with_gen/pubspec.yaml @@ -0,0 +1,23 @@ +name: wyatt_component_copy_with_gen +description: Generator for copywith method for components. +repository: https://git.wyatt-studio.fr/Wyatt-FOSS/wyatt-packages/src/branch/master/packages/component_copy_with_gen +version: 1.0.0 + +environment: + sdk: ">=2.19.0 <3.0.0" + +dependencies: + path: ^1.8.0 + build: ^2.3.1 + source_gen: ^1.2.7 + analyzer: ^5.4.0 + + wyatt_component_copy_with_extension: + path: ../wyatt_component_copy_with_extension + +dev_dependencies: + test: ^1.21.0 + + wyatt_analysis: + hosted: https://git.wyatt-studio.fr/api/packages/Wyatt-FOSS/pub + version: ^2.3.0