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 - angelContainerBuilder
build_extensions: build_extensions:
.dart: .dart:
- .reflector.g.dart - ".reflector.g.dart"
auto_apply: root_package auto_apply: root_package
build_to: source build_to: source
required_inputs: required_inputs:
- .dart - .dart
targets:
$default:
sources:
- test/reflector.dart
builders:
angel_container_generator|angel_container:
generate_for:
- test/reflector.dart
- test/**.dart
- test/*.dart

View file

@ -11,18 +11,21 @@ import 'util.dart';
class AngelContainerGenerator class AngelContainerGenerator
extends GeneratorForAnnotation<GenerateReflector> { extends GeneratorForAnnotation<GenerateReflector> {
@override @override
Future<String> generateForAnnotatedElement( FutureOr<String> generate(LibraryReader library, BuildStep buildStep) {
Element element, ConstantReader annotation, BuildStep buildStep) async { var annotation = typeChecker.firstAnnotationOf(library.element);
if (element is LibraryElement) {
var reader = new GenerateReflectorReader(annotation); if (annotation == null) {
var generator = new ReflectorLibraryGenerator(element, reader)
..generate();
return generator.toSource();
} else if (element is ClassElement) {
return null; return null;
} else { } else {
throw new UnsupportedError( var reader = new GenerateReflectorReader(new ConstantReader(annotation));
'@GenerateReflector() can only be added to a library or class element.'); 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 { class ReflectorLibraryGenerator {
final LibraryElement element; final LibraryElement element;
final GenerateReflectorReader annotation; final GenerateReflectorReader annotation;
Library _lib;
ReflectorLibraryGenerator(this.element, this.annotation); ReflectorLibraryGenerator(this.element, this.annotation);
String toSource() { String toSource() {
return _lib.accept(new DartEmitter()).toString(); return generate().accept(new DartEmitter()).toString();
} }
void generate() { Library generate() {
_lib = new Library((lib) { return new Library((lib) {
lib.body.add(generateReflectorClass()); lib.body.add(generateReflectorClass());
}); });
} }

View file

@ -2,9 +2,20 @@
library angel_container_generator_test; library angel_container_generator_test;
import 'package:angel_container/angel_container.dart'; 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 {} class Artist {}
r() {
var r = new Artist();
}