mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-23 21:13:35 +00:00
feat: implement InjectGenerator
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import 'dart:async';
|
||||
import 'package:analyzer/dart/constant/value.dart';
|
||||
import 'package:build/build.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:source_gen/source_gen.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:cherrypick_annotations/cherrypick_annotations.dart' as ann;
|
||||
@@ -11,8 +11,6 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
|
||||
@override
|
||||
FutureOr<String> generateForAnnotatedElement(
|
||||
Element element, ConstantReader annotation, BuildStep buildStep) {
|
||||
// Only classes are supported for @module() annotation
|
||||
// Обрабатываются только классы (другие элементы — ошибка)
|
||||
if (element is! ClassElement) {
|
||||
throw InvalidGenerationSourceError(
|
||||
'@injectable() can only be applied to classes.',
|
||||
@@ -28,30 +26,36 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
|
||||
.any((m) =>
|
||||
m.computeConstantValue()?.type?.getDisplayString() == 'inject'));
|
||||
|
||||
// Генерируем инициализацию для каждого поля с аннотацией @inject()
|
||||
final buffer = StringBuffer();
|
||||
buffer.writeln('mixin $mixinName {');
|
||||
buffer.writeln(' void _inject($className instance) {');
|
||||
|
||||
for (final field in injectedFields) {
|
||||
// Получаем имя типа
|
||||
final fieldType = field.type.getDisplayString();
|
||||
// Ищем аннотацию @named
|
||||
final namedAnnotation = field.metadata.firstWhereOrNull(
|
||||
(m) => m.computeConstantValue()?.type?.getDisplayString() == 'named',
|
||||
);
|
||||
String namedParam = '';
|
||||
if (namedAnnotation != null) {
|
||||
final namedValue = namedAnnotation
|
||||
.computeConstantValue()
|
||||
?.getField('value')
|
||||
?.toStringValue();
|
||||
if (namedValue != null) {
|
||||
namedParam = "(named: '$namedValue')";
|
||||
String? scopeName;
|
||||
String? namedValue;
|
||||
|
||||
for (final m in field.metadata) {
|
||||
final DartObject? obj = m.computeConstantValue();
|
||||
final type = obj?.type?.getDisplayString();
|
||||
if (type == 'scope') {
|
||||
scopeName = obj?.getField('name')?.toStringValue();
|
||||
} else if (type == 'named') {
|
||||
namedValue = obj?.getField('value')?.toStringValue();
|
||||
}
|
||||
}
|
||||
buffer.writeln(
|
||||
" instance.${field.name} = CherryPick.openRootScope().resolve<$fieldType>$namedParam;");
|
||||
|
||||
final String fieldType = field.type.getDisplayString();
|
||||
|
||||
String accessor = (scopeName != null && scopeName.isNotEmpty)
|
||||
? "CherryPick.openScope(scopeName: '$scopeName').resolve"
|
||||
: "CherryPick.openRootScope().resolve";
|
||||
|
||||
String generic = fieldType != 'dynamic' ? '<$fieldType>' : '';
|
||||
String params = (namedValue != null && namedValue.isNotEmpty)
|
||||
? "(named: '$namedValue')"
|
||||
: '()';
|
||||
|
||||
buffer.writeln(" instance.${field.name} = $accessor$generic$params;");
|
||||
}
|
||||
|
||||
buffer.writeln(' }');
|
||||
|
||||
Reference in New Issue
Block a user