Add ReflectorLibraryGenerator
This commit is contained in:
parent
c0d526ef57
commit
39f1254750
5 changed files with 86 additions and 6 deletions
|
@ -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');
|
||||||
|
}
|
28
angel_container_generator/lib/src/generator.dart
Normal file
28
angel_container_generator/lib/src/generator.dart
Normal 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.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
43
angel_container_generator/lib/src/library_generator.dart
Normal file
43
angel_container_generator/lib/src/library_generator.dart
Normal 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;
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,14 +8,14 @@ const TypeChecker generateReflectorTypeChecker =
|
||||||
|
|
||||||
/// Reads data from a [GenerateReflector] annotation.
|
/// Reads data from a [GenerateReflector] annotation.
|
||||||
class GenerateReflectorReader {
|
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 =>
|
List<DartType> get types =>
|
||||||
constantReader
|
annotation
|
||||||
.peek('types')
|
.peek('types')
|
||||||
?.listValue
|
?.listValue
|
||||||
?.map((o) => ConstantReader(o).typeValue)
|
?.map((o) => ConstantReader(o).typeValue)
|
||||||
|
@ -23,7 +23,7 @@ class GenerateReflectorReader {
|
||||||
<DartType>[];
|
<DartType>[];
|
||||||
|
|
||||||
List<Symbol> get symbols =>
|
List<Symbol> get symbols =>
|
||||||
constantReader
|
annotation
|
||||||
.peek('symbols')
|
.peek('symbols')
|
||||||
?.listValue
|
?.listValue
|
||||||
?.map((o) => ConstantReader(o).symbolValue)
|
?.map((o) => ConstantReader(o).symbolValue)
|
||||||
|
@ -31,5 +31,5 @@ class GenerateReflectorReader {
|
||||||
<Symbol>[];
|
<Symbol>[];
|
||||||
|
|
||||||
List<DartObject> get functions =>
|
List<DartObject> get functions =>
|
||||||
constantReader.peek('functions')?.listValue ?? <DartObject>[];
|
annotation.peek('functions')?.listValue ?? <DartObject>[];
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,8 @@ dependencies:
|
||||||
angel_container: ^1.0.0-alpha
|
angel_container: ^1.0.0-alpha
|
||||||
build: ^0.12.0
|
build: ^0.12.0
|
||||||
build_config: ^0.3.0
|
build_config: ^0.3.0
|
||||||
|
code_builder: ^3.0.0
|
||||||
|
recase: ^2.0.0
|
||||||
source_gen: ^0.9.0
|
source_gen: ^0.9.0
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
build_runner: ^0.10.0
|
build_runner: ^0.10.0
|
Loading…
Reference in a new issue