Generator works

This commit is contained in:
Tobe O 2018-08-21 12:44:54 -04:00
parent fe43fec98c
commit b3176a55a7
4 changed files with 33 additions and 30 deletions

View file

@ -5,18 +5,8 @@ builders:
- angelContainerBuilder
build_extensions:
.dart:
- .reflector.g.dart
- ".reflector.g.dart"
auto_apply: root_package
build_to: source
required_inputs:
- .dart
targets:
$default:
sources:
- test/reflector.dart
builders:
angel_container_generator|angel_container:
generate_for:
- test/reflector.dart
- test/**.dart
- test/*.dart
- .dart

View file

@ -11,18 +11,21 @@ import 'util.dart';
class AngelContainerGenerator
extends GeneratorForAnnotation<GenerateReflector> {
@override
Future<String> generateForAnnotatedElement(
Element element, ConstantReader annotation, BuildStep buildStep) async {
if (element is LibraryElement) {
var reader = new GenerateReflectorReader(annotation);
var generator = new ReflectorLibraryGenerator(element, reader)
..generate();
return generator.toSource();
} else if (element is ClassElement) {
FutureOr<String> generate(LibraryReader library, BuildStep buildStep) {
var annotation = typeChecker.firstAnnotationOf(library.element);
if (annotation == null) {
return null;
} else {
throw new UnsupportedError(
'@GenerateReflector() can only be added to a library or class element.');
var reader = new GenerateReflectorReader(new ConstantReader(annotation));
var generator = new ReflectorLibraryGenerator(library.element, reader);
return generator.toSource();
}
}
@override
generateForAnnotatedElement(
Element element, ConstantReader annotation, BuildStep buildStep) {
throw new UnimplementedError();
}
}

View file

@ -8,16 +8,15 @@ import 'util.dart';
class ReflectorLibraryGenerator {
final LibraryElement element;
final GenerateReflectorReader annotation;
Library _lib;
ReflectorLibraryGenerator(this.element, this.annotation);
String toSource() {
return _lib.accept(new DartEmitter()).toString();
return generate().accept(new DartEmitter()).toString();
}
void generate() {
_lib = new Library((lib) {
Library generate() {
return new Library((lib) {
lib.body.add(generateReflectorClass());
});
}

View file

@ -2,9 +2,20 @@
library angel_container_generator_test;
import 'package:angel_container/angel_container.dart';
import 'package:test/test.dart';
part 'reflector.reflector.g.dart';
void main() {
var reflector = const AngelContainerGeneratorTestReflector();
group('reflectClass', () {
var mirror = reflector.reflectClass(Artist);
test('name', () {
expect(mirror.name, 'Artist');
});
});
}
class Artist {}
r() {
var r = new Artist();
}