From ea6eb536dd7525ee0a50d0dc9777382b437c831a Mon Sep 17 00:00:00 2001 From: Sergey Penkovsky Date: Sat, 17 May 2025 14:31:52 +0300 Subject: [PATCH] feat: implement generator for named annotation --- .../lib/cherrypick_annotations.dart | 1 + cherrypick_annotations/lib/src/named.dart | 5 +++ .../lib/module_generator.dart | 39 ++++++++++++++----- 3 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 cherrypick_annotations/lib/src/named.dart diff --git a/cherrypick_annotations/lib/cherrypick_annotations.dart b/cherrypick_annotations/lib/cherrypick_annotations.dart index 4868c7f..8ed9d8d 100644 --- a/cherrypick_annotations/lib/cherrypick_annotations.dart +++ b/cherrypick_annotations/lib/cherrypick_annotations.dart @@ -7,5 +7,6 @@ export 'src/module.dart'; export 'src/bind.dart'; export 'src/provide.dart'; export 'src/singleton.dart'; +export 'src/named.dart'; // TODO: Export any libraries intended for clients of this package. diff --git a/cherrypick_annotations/lib/src/named.dart b/cherrypick_annotations/lib/src/named.dart new file mode 100644 index 0000000..5d7910c --- /dev/null +++ b/cherrypick_annotations/lib/src/named.dart @@ -0,0 +1,5 @@ +// ignore: camel_case_types +class named { + final String value; + const named(this.value); +} diff --git a/cherrypick_generator/lib/module_generator.dart b/cherrypick_generator/lib/module_generator.dart index a08f3c0..d208be6 100644 --- a/cherrypick_generator/lib/module_generator.dart +++ b/cherrypick_generator/lib/module_generator.dart @@ -17,16 +17,14 @@ class ModuleGenerator extends GeneratorForAnnotation { element: element, ); } - final classElement = element; final className = classElement.displayName; final generatedClassName = r'$' + className; final buffer = StringBuffer(); buffer.writeln('final class $generatedClassName extends $className {'); - buffer.writeln('@override'); - buffer.writeln('void builder(Scope currentScope) {'); - + buffer.writeln(' @override'); + buffer.writeln(' void builder(Scope currentScope) {'); for (final method in classElement.methods.where((m) => !m.isAbstract)) { final hasSingleton = method.metadata.any( (m) => @@ -38,7 +36,28 @@ class ModuleGenerator extends GeneratorForAnnotation { .contains('singleton') ?? false, ); - + ElementAnnotation? namedMeta; + try { + namedMeta = method.metadata.firstWhere( + (m) => + m + .computeConstantValue() + ?.type + ?.getDisplayString(withNullability: false) + .toLowerCase() + .contains('named') ?? + false, + ); + } catch (_) { + namedMeta = null; + } + String? nameArg; + if (namedMeta != null) { + final cv = namedMeta.computeConstantValue(); + if (cv != null) { + nameArg = cv.getField('value')?.toStringValue(); + } + } final returnType = method.returnType.getDisplayString(withNullability: false); final methodName = method.displayName; @@ -46,18 +65,18 @@ class ModuleGenerator extends GeneratorForAnnotation { .map((p) => "currentScope.resolve<${p.type.getDisplayString(withNullability: false)}>()") .join(', '); - - buffer.write('bind<$returnType>()' + buffer.write(' bind<$returnType>()' '.toProvide(() => $methodName($args))'); + if (nameArg != null) { + buffer.write(".withName('$nameArg')"); + } if (hasSingleton) { buffer.write('.singleton()'); } buffer.write(';\n'); } - + buffer.writeln(' }'); buffer.writeln('}'); - buffer.writeln('}'); - return buffer.toString(); } }