From 9b0741199c2ae5717270843da52b24ad4faab206 Mon Sep 17 00:00:00 2001 From: Sergey Penkovsky Date: Wed, 14 May 2025 12:53:51 +0300 Subject: [PATCH] feat: implement annotations --- cherrypick_annotations/.gitignore | 24 ++++++++++++ cherrypick_annotations/CHANGELOG.md | 3 ++ cherrypick_annotations/README.md | 39 +++++++++++++++++++ cherrypick_annotations/analysis_options.yaml | 30 ++++++++++++++ .../cherrypick_annotations_example.dart | 3 ++ .../lib/cherrypick_annotations.dart | 8 ++++ .../lib/src/cherrypick_annotations_base.dart | 5 +++ cherrypick_annotations/pubspec.yaml | 15 +++++++ .../test/cherrypick_annotations_test.dart | 13 +++++++ cherrypick_generator/.gitignore | 24 ++++++++++++ cherrypick_generator/CHANGELOG.md | 3 ++ cherrypick_generator/README.md | 39 +++++++++++++++++++ cherrypick_generator/analysis_options.yaml | 30 ++++++++++++++ cherrypick_generator/build.yaml | 5 +++ .../example/cherrypick_generator_example.dart | 6 +++ .../lib/cherrypick_generator.dart | 8 ++++ .../lib/src/cherrypick_generator_base.dart | 6 +++ .../lib/src/inject_generator.dart | 28 +++++++++++++ cherrypick_generator/pubspec.yaml | 18 +++++++++ .../test/cherrypick_generator_test.dart | 16 ++++++++ 20 files changed, 323 insertions(+) create mode 100644 cherrypick_annotations/.gitignore create mode 100644 cherrypick_annotations/CHANGELOG.md create mode 100644 cherrypick_annotations/README.md create mode 100644 cherrypick_annotations/analysis_options.yaml create mode 100644 cherrypick_annotations/example/cherrypick_annotations_example.dart create mode 100644 cherrypick_annotations/lib/cherrypick_annotations.dart create mode 100644 cherrypick_annotations/lib/src/cherrypick_annotations_base.dart create mode 100644 cherrypick_annotations/pubspec.yaml create mode 100644 cherrypick_annotations/test/cherrypick_annotations_test.dart create mode 100644 cherrypick_generator/.gitignore create mode 100644 cherrypick_generator/CHANGELOG.md create mode 100644 cherrypick_generator/README.md create mode 100644 cherrypick_generator/analysis_options.yaml create mode 100644 cherrypick_generator/build.yaml create mode 100644 cherrypick_generator/example/cherrypick_generator_example.dart create mode 100644 cherrypick_generator/lib/cherrypick_generator.dart create mode 100644 cherrypick_generator/lib/src/cherrypick_generator_base.dart create mode 100644 cherrypick_generator/lib/src/inject_generator.dart create mode 100644 cherrypick_generator/pubspec.yaml create mode 100644 cherrypick_generator/test/cherrypick_generator_test.dart diff --git a/cherrypick_annotations/.gitignore b/cherrypick_annotations/.gitignore new file mode 100644 index 0000000..16cc011 --- /dev/null +++ b/cherrypick_annotations/.gitignore @@ -0,0 +1,24 @@ +# See https://www.dartlang.org/guides/libraries/private-files + +# Files and directories created by pub +.dart_tool/ +.packages +build/ +# If you're building an application, you may want to check-in your pubspec.lock +pubspec.lock + +# Directory created by dartdoc +# If you don't generate documentation locally you can remove this line. +doc/api/ + +# Avoid committing generated Javascript files: +*.dart.js +*.info.json # Produced by the --dump-info flag. +*.js # When generated by dart2js. Don't specify *.js if your + # project includes source files written in JavaScript. +*.js_ +*.js.deps +*.js.map + +# FVM Version Cache +.fvm/ \ No newline at end of file diff --git a/cherrypick_annotations/CHANGELOG.md b/cherrypick_annotations/CHANGELOG.md new file mode 100644 index 0000000..effe43c --- /dev/null +++ b/cherrypick_annotations/CHANGELOG.md @@ -0,0 +1,3 @@ +## 1.0.0 + +- Initial version. diff --git a/cherrypick_annotations/README.md b/cherrypick_annotations/README.md new file mode 100644 index 0000000..8831761 --- /dev/null +++ b/cherrypick_annotations/README.md @@ -0,0 +1,39 @@ + + +TODO: Put a short description of the package here that helps potential users +know whether this package might be useful for them. + +## Features + +TODO: List what your package can do. Maybe include images, gifs, or videos. + +## Getting started + +TODO: List prerequisites and provide or point to information on how to +start using the package. + +## Usage + +TODO: Include short and useful examples for package users. Add longer examples +to `/example` folder. + +```dart +const like = 'sample'; +``` + +## Additional information + +TODO: Tell users more about the package: where to find more information, how to +contribute to the package, how to file issues, what response they can expect +from the package authors, and more. diff --git a/cherrypick_annotations/analysis_options.yaml b/cherrypick_annotations/analysis_options.yaml new file mode 100644 index 0000000..dee8927 --- /dev/null +++ b/cherrypick_annotations/analysis_options.yaml @@ -0,0 +1,30 @@ +# This file configures the static analysis results for your project (errors, +# warnings, and lints). +# +# This enables the 'recommended' set of lints from `package:lints`. +# This set helps identify many issues that may lead to problems when running +# or consuming Dart code, and enforces writing Dart using a single, idiomatic +# style and format. +# +# If you want a smaller set of lints you can change this to specify +# 'package:lints/core.yaml'. These are just the most critical lints +# (the recommended set includes the core lints). +# The core lints are also what is used by pub.dev for scoring packages. + +include: package:lints/recommended.yaml + +# Uncomment the following section to specify additional rules. + +# linter: +# rules: +# - camel_case_types + +# analyzer: +# exclude: +# - path/to/excluded/files/** + +# For more information about the core and recommended set of lints, see +# https://dart.dev/go/core-lints + +# For additional information about configuring this file, see +# https://dart.dev/guides/language/analysis-options diff --git a/cherrypick_annotations/example/cherrypick_annotations_example.dart b/cherrypick_annotations/example/cherrypick_annotations_example.dart new file mode 100644 index 0000000..4dc5bf6 --- /dev/null +++ b/cherrypick_annotations/example/cherrypick_annotations_example.dart @@ -0,0 +1,3 @@ +void main() { + print('awesome'); +} diff --git a/cherrypick_annotations/lib/cherrypick_annotations.dart b/cherrypick_annotations/lib/cherrypick_annotations.dart new file mode 100644 index 0000000..2348ba3 --- /dev/null +++ b/cherrypick_annotations/lib/cherrypick_annotations.dart @@ -0,0 +1,8 @@ +/// Support for doing something awesome. +/// +/// More dartdocs go here. +library; + +export 'src/cherrypick_annotations_base.dart'; + +// TODO: Export any libraries intended for clients of this package. diff --git a/cherrypick_annotations/lib/src/cherrypick_annotations_base.dart b/cherrypick_annotations/lib/src/cherrypick_annotations_base.dart new file mode 100644 index 0000000..cdd51bb --- /dev/null +++ b/cherrypick_annotations/lib/src/cherrypick_annotations_base.dart @@ -0,0 +1,5 @@ +// Define the Inject annotation +class Inject { + final String? named; + const Inject({this.named}); +} diff --git a/cherrypick_annotations/pubspec.yaml b/cherrypick_annotations/pubspec.yaml new file mode 100644 index 0000000..7171c6a --- /dev/null +++ b/cherrypick_annotations/pubspec.yaml @@ -0,0 +1,15 @@ +name: cherrypick_annotations +description: A starting point for Dart libraries or applications. +version: 1.0.0 +# repository: https://github.com/my_org/my_repo + +environment: + sdk: ^3.5.2 + +# Add regular dependencies here. +dependencies: + # path: ^1.8.0 + +dev_dependencies: + lints: ^4.0.0 + test: ^1.24.0 diff --git a/cherrypick_annotations/test/cherrypick_annotations_test.dart b/cherrypick_annotations/test/cherrypick_annotations_test.dart new file mode 100644 index 0000000..6bc4a55 --- /dev/null +++ b/cherrypick_annotations/test/cherrypick_annotations_test.dart @@ -0,0 +1,13 @@ +import 'package:test/test.dart'; + +void main() { + group('A group of tests', () { + setUp(() { + // Additional setup goes here. + }); + + test('First Test', () { + expect(1, 1); + }); + }); +} diff --git a/cherrypick_generator/.gitignore b/cherrypick_generator/.gitignore new file mode 100644 index 0000000..16cc011 --- /dev/null +++ b/cherrypick_generator/.gitignore @@ -0,0 +1,24 @@ +# See https://www.dartlang.org/guides/libraries/private-files + +# Files and directories created by pub +.dart_tool/ +.packages +build/ +# If you're building an application, you may want to check-in your pubspec.lock +pubspec.lock + +# Directory created by dartdoc +# If you don't generate documentation locally you can remove this line. +doc/api/ + +# Avoid committing generated Javascript files: +*.dart.js +*.info.json # Produced by the --dump-info flag. +*.js # When generated by dart2js. Don't specify *.js if your + # project includes source files written in JavaScript. +*.js_ +*.js.deps +*.js.map + +# FVM Version Cache +.fvm/ \ No newline at end of file diff --git a/cherrypick_generator/CHANGELOG.md b/cherrypick_generator/CHANGELOG.md new file mode 100644 index 0000000..effe43c --- /dev/null +++ b/cherrypick_generator/CHANGELOG.md @@ -0,0 +1,3 @@ +## 1.0.0 + +- Initial version. diff --git a/cherrypick_generator/README.md b/cherrypick_generator/README.md new file mode 100644 index 0000000..8831761 --- /dev/null +++ b/cherrypick_generator/README.md @@ -0,0 +1,39 @@ + + +TODO: Put a short description of the package here that helps potential users +know whether this package might be useful for them. + +## Features + +TODO: List what your package can do. Maybe include images, gifs, or videos. + +## Getting started + +TODO: List prerequisites and provide or point to information on how to +start using the package. + +## Usage + +TODO: Include short and useful examples for package users. Add longer examples +to `/example` folder. + +```dart +const like = 'sample'; +``` + +## Additional information + +TODO: Tell users more about the package: where to find more information, how to +contribute to the package, how to file issues, what response they can expect +from the package authors, and more. diff --git a/cherrypick_generator/analysis_options.yaml b/cherrypick_generator/analysis_options.yaml new file mode 100644 index 0000000..dee8927 --- /dev/null +++ b/cherrypick_generator/analysis_options.yaml @@ -0,0 +1,30 @@ +# This file configures the static analysis results for your project (errors, +# warnings, and lints). +# +# This enables the 'recommended' set of lints from `package:lints`. +# This set helps identify many issues that may lead to problems when running +# or consuming Dart code, and enforces writing Dart using a single, idiomatic +# style and format. +# +# If you want a smaller set of lints you can change this to specify +# 'package:lints/core.yaml'. These are just the most critical lints +# (the recommended set includes the core lints). +# The core lints are also what is used by pub.dev for scoring packages. + +include: package:lints/recommended.yaml + +# Uncomment the following section to specify additional rules. + +# linter: +# rules: +# - camel_case_types + +# analyzer: +# exclude: +# - path/to/excluded/files/** + +# For more information about the core and recommended set of lints, see +# https://dart.dev/go/core-lints + +# For additional information about configuring this file, see +# https://dart.dev/guides/language/analysis-options diff --git a/cherrypick_generator/build.yaml b/cherrypick_generator/build.yaml new file mode 100644 index 0000000..1af0f7d --- /dev/null +++ b/cherrypick_generator/build.yaml @@ -0,0 +1,5 @@ +targets: + $default: + builders: + cherrypick_generator|inject_builder: + enabled: true \ No newline at end of file diff --git a/cherrypick_generator/example/cherrypick_generator_example.dart b/cherrypick_generator/example/cherrypick_generator_example.dart new file mode 100644 index 0000000..3b24ef3 --- /dev/null +++ b/cherrypick_generator/example/cherrypick_generator_example.dart @@ -0,0 +1,6 @@ +import 'package:cherrypick_generator/cherrypick_generator.dart'; + +void main() { + var awesome = Awesome(); + print('awesome: ${awesome.isAwesome}'); +} diff --git a/cherrypick_generator/lib/cherrypick_generator.dart b/cherrypick_generator/lib/cherrypick_generator.dart new file mode 100644 index 0000000..bbfa5c5 --- /dev/null +++ b/cherrypick_generator/lib/cherrypick_generator.dart @@ -0,0 +1,8 @@ +/// Support for doing something awesome. +/// +/// More dartdocs go here. +library; + +export 'src/cherrypick_generator_base.dart'; + +// TODO: Export any libraries intended for clients of this package. diff --git a/cherrypick_generator/lib/src/cherrypick_generator_base.dart b/cherrypick_generator/lib/src/cherrypick_generator_base.dart new file mode 100644 index 0000000..e8a6f15 --- /dev/null +++ b/cherrypick_generator/lib/src/cherrypick_generator_base.dart @@ -0,0 +1,6 @@ +// TODO: Put public facing types in this file. + +/// Checks if you are awesome. Spoiler: you are. +class Awesome { + bool get isAwesome => true; +} diff --git a/cherrypick_generator/lib/src/inject_generator.dart b/cherrypick_generator/lib/src/inject_generator.dart new file mode 100644 index 0000000..8048dea --- /dev/null +++ b/cherrypick_generator/lib/src/inject_generator.dart @@ -0,0 +1,28 @@ +import 'dart:async'; +import 'package:build/build.dart'; +import 'package:source_gen/source_gen.dart'; +import 'package:cherrypick_annotations/cherrypick_annotations.dart'; +import 'package:analyzer/dart/element/element.dart'; + +class InjectGenerator extends GeneratorForAnnotation { + @override + FutureOr generateForAnnotatedElement( + Element element, ConstantReader annotation, BuildStep buildStep) { + if (element is! FieldElement) { + throw InvalidGenerationSourceError( + 'Генератор не может работать с `${element.name}`.', + ); + } + + final named = annotation.peek('named')?.stringValue; + final resolveSnippet = named == null + ? 'CherryPickProvider.of(context).openRootScope().resolve<${element.type}>()' + : 'CherryPickProvider.of(context).openRootScope().resolve<${element.type}>(named: \'$named\')'; + + return ''' +void _initState(BuildContext context) { + ${element.name} = $resolveSnippet; +} +'''; + } +} diff --git a/cherrypick_generator/pubspec.yaml b/cherrypick_generator/pubspec.yaml new file mode 100644 index 0000000..6b75ce8 --- /dev/null +++ b/cherrypick_generator/pubspec.yaml @@ -0,0 +1,18 @@ +name: cherrypick_generator +description: Code generator for cherrypick annotations +version: 1.0.0 +# repository: https://github.com/my_org/my_repo + +environment: + sdk: ^3.5.2 + +# Add regular dependencies here. +dependencies: + build: ^2.0.0 + source_gen: ^1.0.0 + cherrypick_annotations: any + +dev_dependencies: + lints: ^4.0.0 + test: ^1.24.0 + build_runner: ^2.1.0 \ No newline at end of file diff --git a/cherrypick_generator/test/cherrypick_generator_test.dart b/cherrypick_generator/test/cherrypick_generator_test.dart new file mode 100644 index 0000000..dd784bb --- /dev/null +++ b/cherrypick_generator/test/cherrypick_generator_test.dart @@ -0,0 +1,16 @@ +import 'package:cherrypick_generator/cherrypick_generator.dart'; +import 'package:test/test.dart'; + +void main() { + group('A group of tests', () { + final awesome = Awesome(); + + setUp(() { + // Additional setup goes here. + }); + + test('First Test', () { + expect(awesome.isAwesome, isTrue); + }); + }); +}