mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-23 21:13:35 +00:00
feat: implement generator for named annotation
This commit is contained in:
@@ -7,5 +7,6 @@ export 'src/module.dart';
|
|||||||
export 'src/bind.dart';
|
export 'src/bind.dart';
|
||||||
export 'src/provide.dart';
|
export 'src/provide.dart';
|
||||||
export 'src/singleton.dart';
|
export 'src/singleton.dart';
|
||||||
|
export 'src/named.dart';
|
||||||
|
|
||||||
// TODO: Export any libraries intended for clients of this package.
|
// TODO: Export any libraries intended for clients of this package.
|
||||||
|
|||||||
5
cherrypick_annotations/lib/src/named.dart
Normal file
5
cherrypick_annotations/lib/src/named.dart
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
// ignore: camel_case_types
|
||||||
|
class named {
|
||||||
|
final String value;
|
||||||
|
const named(this.value);
|
||||||
|
}
|
||||||
@@ -17,7 +17,6 @@ class ModuleGenerator extends GeneratorForAnnotation<ann.module> {
|
|||||||
element: element,
|
element: element,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
final classElement = element;
|
final classElement = element;
|
||||||
final className = classElement.displayName;
|
final className = classElement.displayName;
|
||||||
final generatedClassName = r'$' + className;
|
final generatedClassName = r'$' + className;
|
||||||
@@ -26,7 +25,6 @@ class ModuleGenerator extends GeneratorForAnnotation<ann.module> {
|
|||||||
buffer.writeln('final class $generatedClassName extends $className {');
|
buffer.writeln('final class $generatedClassName extends $className {');
|
||||||
buffer.writeln(' @override');
|
buffer.writeln(' @override');
|
||||||
buffer.writeln(' void builder(Scope currentScope) {');
|
buffer.writeln(' void builder(Scope currentScope) {');
|
||||||
|
|
||||||
for (final method in classElement.methods.where((m) => !m.isAbstract)) {
|
for (final method in classElement.methods.where((m) => !m.isAbstract)) {
|
||||||
final hasSingleton = method.metadata.any(
|
final hasSingleton = method.metadata.any(
|
||||||
(m) =>
|
(m) =>
|
||||||
@@ -38,7 +36,28 @@ class ModuleGenerator extends GeneratorForAnnotation<ann.module> {
|
|||||||
.contains('singleton') ??
|
.contains('singleton') ??
|
||||||
false,
|
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 =
|
final returnType =
|
||||||
method.returnType.getDisplayString(withNullability: false);
|
method.returnType.getDisplayString(withNullability: false);
|
||||||
final methodName = method.displayName;
|
final methodName = method.displayName;
|
||||||
@@ -46,18 +65,18 @@ class ModuleGenerator extends GeneratorForAnnotation<ann.module> {
|
|||||||
.map((p) =>
|
.map((p) =>
|
||||||
"currentScope.resolve<${p.type.getDisplayString(withNullability: false)}>()")
|
"currentScope.resolve<${p.type.getDisplayString(withNullability: false)}>()")
|
||||||
.join(', ');
|
.join(', ');
|
||||||
|
|
||||||
buffer.write(' bind<$returnType>()'
|
buffer.write(' bind<$returnType>()'
|
||||||
'.toProvide(() => $methodName($args))');
|
'.toProvide(() => $methodName($args))');
|
||||||
|
if (nameArg != null) {
|
||||||
|
buffer.write(".withName('$nameArg')");
|
||||||
|
}
|
||||||
if (hasSingleton) {
|
if (hasSingleton) {
|
||||||
buffer.write('.singleton()');
|
buffer.write('.singleton()');
|
||||||
}
|
}
|
||||||
buffer.write(';\n');
|
buffer.write(';\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer.writeln(' }');
|
buffer.writeln(' }');
|
||||||
buffer.writeln('}');
|
buffer.writeln('}');
|
||||||
|
|
||||||
return buffer.toString();
|
return buffer.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user