Add import in ORM lib

This commit is contained in:
Tobe O 2018-08-28 11:15:29 -04:00
parent 41f1165a1f
commit 9dffee9bc7

View file

@ -1,5 +1,5 @@
import 'dart:async'; import 'dart:async';
import 'dart:collection';
import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/element.dart';
import 'package:angel_orm/angel_orm.dart'; import 'package:angel_orm/angel_orm.dart';
import 'package:build/build.dart'; import 'package:build/build.dart';
@ -44,10 +44,21 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
Library buildOrmBaseLibrary(AssetId inputId, OrmBuildContext ctx) { Library buildOrmBaseLibrary(AssetId inputId, OrmBuildContext ctx) {
return new Library((lib) { return new Library((lib) {
// Necessary imports // Necessary imports
lib.directives.addAll([ var imports = new SplayTreeSet<String>.from(
new Directive.import('dart:async'), ['dart:async', p.basename(inputId.uri.path)]);
new Directive.import(p.basename(inputId.uri.path)),
]); switch (ctx.ormAnnotation.type) {
case OrmType.mongoDB:
imports.add('package:mongo_dart/mongo_dart.dart');
break;
case OrmType.postgreSql:
imports.add('package:postgres/postgres.dart');
break;
default:
break;
}
lib.directives.addAll(imports.map((url) => new Directive.import(url)));
// Add the corresponding `part` // Add the corresponding `part`
String dbExtension; String dbExtension;
@ -69,8 +80,8 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
throw 'Unsupported ORM type: ${ctx.ormAnnotation.type}'; throw 'Unsupported ORM type: ${ctx.ormAnnotation.type}';
} }
var dbFile = var dbFile = p.setExtension(
p.setExtension(p.basename(inputId.uri.path), '.$dbExtension.orm.g.dart'); p.basename(inputId.uri.path), '.$dbExtension.orm.g.dart');
lib.body.add(new Code("part '$dbFile';")); lib.body.add(new Code("part '$dbFile';"));