Add ReflectorLibraryGenerator

This commit is contained in:
Tobe O 2018-08-21 11:16:41 -04:00
parent c0d526ef57
commit 39f1254750
5 changed files with 86 additions and 6 deletions

View file

@ -0,0 +1,7 @@
import 'package:build/build.dart';
import 'package:source_gen/source_gen.dart';
import 'src/generator.dart';
Builder angelContainerBuilder(BuilderOptions builderOptions) {
return new PartBuilder([new AngelContainerGenerator()], '.reflector.g.dart');
}

View file

@ -0,0 +1,28 @@
import 'dart:async';
import 'package:analyzer/dart/element/element.dart';
import 'package:angel_container/angel_container.dart';
import 'package:build/build.dart';
import 'package:source_gen/source_gen.dart';
import 'library_generator.dart';
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) {
return null;
} else {
throw new UnsupportedError(
'@GenerateReflector() can only be added to a library or class element.');
}
}
}

View file

@ -0,0 +1,43 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:code_builder/code_builder.dart';
import 'package:recase/recase.dart';
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();
}
void generate() {
_lib = new Library((lib) {
lib.body.add(generateReflectorClass());
});
}
Class generateReflectorClass() {
return new Class((clazz) {
// Select the name
if (annotation.name?.isNotEmpty == true) {
clazz.name = annotation.name;
} else {
var rc = new ReCase(element.name);
clazz.name = rc.pascalCase + 'Reflector';
}
// implements Reflector
clazz.implements.add(refer('Reflector'));
// Add a const constructor
clazz.constructors.add(new Constructor((b) {
b..constant = true;
}));
});
}
}

View file

@ -8,14 +8,14 @@ const TypeChecker generateReflectorTypeChecker =
/// Reads data from a [GenerateReflector] annotation.
class GenerateReflectorReader {
final ConstantReader constantReader;
final ConstantReader annotation;
GenerateReflectorReader(this.constantReader);
GenerateReflectorReader(this.annotation);
String get name => constantReader.peek('name')?.stringValue;
String get name => annotation.peek('name')?.stringValue;
List<DartType> get types =>
constantReader
annotation
.peek('types')
?.listValue
?.map((o) => ConstantReader(o).typeValue)
@ -23,7 +23,7 @@ class GenerateReflectorReader {
<DartType>[];
List<Symbol> get symbols =>
constantReader
annotation
.peek('symbols')
?.listValue
?.map((o) => ConstantReader(o).symbolValue)
@ -31,5 +31,5 @@ class GenerateReflectorReader {
<Symbol>[];
List<DartObject> get functions =>
constantReader.peek('functions')?.listValue ?? <DartObject>[];
annotation.peek('functions')?.listValue ?? <DartObject>[];
}

View file

@ -9,6 +9,8 @@ dependencies:
angel_container: ^1.0.0-alpha
build: ^0.12.0
build_config: ^0.3.0
code_builder: ^3.0.0
recase: ^2.0.0
source_gen: ^0.9.0
dev_dependencies:
build_runner: ^0.10.0