feat: implement annotations

This commit is contained in:
Sergey Penkovsky
2025-05-14 12:53:51 +03:00
parent 7a5880e436
commit 9b0741199c
20 changed files with 323 additions and 0 deletions

View File

@@ -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.

View File

@@ -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;
}

View File

@@ -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<Inject> {
@override
FutureOr<String> 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;
}
''';
}
}