From c2fe5449a7d4448d657b1423600ca06ea3e8b43a Mon Sep 17 00:00:00 2001 From: Tobe O Date: Fri, 24 Aug 2018 11:10:27 -0400 Subject: [PATCH] Correct the 'part' declaration --- angel_orm_generator/build.yaml | 8 +++- .../lib/angel_orm_generator.dart | 2 + .../lib/src/mongodb_orm_generator.dart | 45 +++++++++++++++++++ .../lib/src/orm_generator.dart | 13 +++++- .../lib/src/postgresql_orm_generator.dart | 45 +++++++++++++++++++ angel_orm_generator/lib/src/readers.dart | 2 +- .../test/models/author.orm.g.dart | 1 + .../test/models/car.orm.g.dart | 5 +-- .../test/models/customer.orm.g.dart | 1 + .../test/models/foot.orm.g.dart | 1 + .../test/models/fruit.orm.g.dart | 1 + .../test/models/order.orm.g.dart | 1 + .../test/models/role.orm.g.dart | 1 + 13 files changed, 118 insertions(+), 8 deletions(-) create mode 100644 angel_orm_generator/lib/src/mongodb_orm_generator.dart create mode 100644 angel_orm_generator/lib/src/postgresql_orm_generator.dart diff --git a/angel_orm_generator/build.yaml b/angel_orm_generator/build.yaml index 469a5b3f..bede8b25 100644 --- a/angel_orm_generator/build.yaml +++ b/angel_orm_generator/build.yaml @@ -3,8 +3,14 @@ builders: import: "package:angel_orm_generator/angel_orm_generator.dart" builder_factories: - ormBuilder + - mongoDBOrmBuilder + - postgreSqlOrmBuilder auto_apply: root_package build_to: source build_extensions: .dart: - - ".orm.g.dart" \ No newline at end of file + - ".orm.g.dart" + - ".mongodb.orm.g.dart" + - ".mysql.orm.g.dart" + - ".rethinkdb.orm.g.dart" + - ".postgresql.orm.g.dart" \ No newline at end of file diff --git a/angel_orm_generator/lib/angel_orm_generator.dart b/angel_orm_generator/lib/angel_orm_generator.dart index d4702aba..ae114848 100644 --- a/angel_orm_generator/lib/angel_orm_generator.dart +++ b/angel_orm_generator/lib/angel_orm_generator.dart @@ -1,3 +1,5 @@ +export 'src/mongodb_orm_generator.dart'; export 'src/orm_build_context.dart'; export 'src/orm_generator.dart'; +export 'src/postgresql_orm_generator.dart'; export 'src/readers.dart'; \ No newline at end of file diff --git a/angel_orm_generator/lib/src/mongodb_orm_generator.dart b/angel_orm_generator/lib/src/mongodb_orm_generator.dart new file mode 100644 index 00000000..35a7d368 --- /dev/null +++ b/angel_orm_generator/lib/src/mongodb_orm_generator.dart @@ -0,0 +1,45 @@ +import 'dart:async'; + +import 'package:analyzer/dart/element/element.dart'; +import 'package:angel_orm/angel_orm.dart'; +import 'package:build/build.dart'; +import 'package:code_builder/code_builder.dart' hide LibraryBuilder; +import 'package:path/path.dart' as p; +import 'package:source_gen/source_gen.dart'; + +import 'orm_build_context.dart'; + +Builder mongoDBOrmBuilder(_) { + return new LibraryBuilder(new MongoDBOrmGenerator(), + generatedExtension: '.mongodb.orm.g.dart'); +} + +/// Builder that generates `.orm.g.dart`, with an abstract `FooOrm` class. +class MongoDBOrmGenerator extends GeneratorForAnnotation { + final bool autoSnakeCaseNames; + final bool autoIdAndDateFields; + + MongoDBOrmGenerator({this.autoSnakeCaseNames, this.autoIdAndDateFields}); + + @override + Future generateForAnnotatedElement( + Element element, ConstantReader annotation, BuildStep buildStep) async { + if (element is ClassElement) { + var ctx = await buildOrmContext(element, annotation, buildStep, + buildStep.resolver, autoSnakeCaseNames, autoIdAndDateFields); + var lib = buildOrmLibrary(buildStep.inputId, ctx); + return lib.accept(new DartEmitter()).toString(); + } else { + throw 'The @Orm() annotation can only be applied to classes.'; + } + } + + Library buildOrmLibrary(AssetId inputId, OrmBuildContext ctx) { + return new Library((lib) { + // Add part of + var libFile = + p.setExtension(p.basename(inputId.uri.path), '.orm.g.dart'); + lib.body.add(new Code("part of '$libFile';")); + }); + } +} diff --git a/angel_orm_generator/lib/src/orm_generator.dart b/angel_orm_generator/lib/src/orm_generator.dart index 48e45d7a..a2cb36ae 100644 --- a/angel_orm_generator/lib/src/orm_generator.dart +++ b/angel_orm_generator/lib/src/orm_generator.dart @@ -54,14 +54,23 @@ class OrmGenerator extends GeneratorForAnnotation { switch (ctx.ormAnnotation.type) { case OrmType.mongoDB: - dbExtension = 'mongo'; + dbExtension = 'mongodb'; + break; + case OrmType.rethinkDB: + dbExtension = 'rethinkdb'; + break; + case OrmType.mySql: + dbExtension = 'mysql'; + break; + case OrmType.postgreSql: + dbExtension = 'postgresql'; break; default: throw 'Unsupported ORM type: ${ctx.ormAnnotation.type}'; } var dbFile = - p.setExtension(p.basename(inputId.uri.path), '.$dbExtension.dart'); + p.setExtension(p.basename(inputId.uri.path), '.$dbExtension.orm.g.dart'); lib.body.add(new Code("part '$dbFile';")); diff --git a/angel_orm_generator/lib/src/postgresql_orm_generator.dart b/angel_orm_generator/lib/src/postgresql_orm_generator.dart new file mode 100644 index 00000000..0bcf72ef --- /dev/null +++ b/angel_orm_generator/lib/src/postgresql_orm_generator.dart @@ -0,0 +1,45 @@ +import 'dart:async'; + +import 'package:analyzer/dart/element/element.dart'; +import 'package:angel_orm/angel_orm.dart'; +import 'package:build/build.dart'; +import 'package:code_builder/code_builder.dart' hide LibraryBuilder; +import 'package:path/path.dart' as p; +import 'package:source_gen/source_gen.dart'; + +import 'orm_build_context.dart'; + +Builder postgreSqlOrmBuilder(_) { + return new LibraryBuilder(new PostgreSqlOrmGenerator(), + generatedExtension: '.postgresql.orm.g.dart'); +} + +/// Builder that generates `.orm.g.dart`, with an abstract `FooOrm` class. +class PostgreSqlOrmGenerator extends GeneratorForAnnotation { + final bool autoSnakeCaseNames; + final bool autoIdAndDateFields; + + PostgreSqlOrmGenerator({this.autoSnakeCaseNames, this.autoIdAndDateFields}); + + @override + Future generateForAnnotatedElement( + Element element, ConstantReader annotation, BuildStep buildStep) async { + if (element is ClassElement) { + var ctx = await buildOrmContext(element, annotation, buildStep, + buildStep.resolver, autoSnakeCaseNames, autoIdAndDateFields); + var lib = buildOrmLibrary(buildStep.inputId, ctx); + return lib.accept(new DartEmitter()).toString(); + } else { + throw 'The @Orm() annotation can only be applied to classes.'; + } + } + + Library buildOrmLibrary(AssetId inputId, OrmBuildContext ctx) { + return new Library((lib) { + // Add part of + var libFile = + p.setExtension(p.basename(inputId.uri.path), '.orm.g.dart'); + lib.body.add(new Code("part of '$libFile';")); + }); + } +} diff --git a/angel_orm_generator/lib/src/readers.dart b/angel_orm_generator/lib/src/readers.dart index 555763c3..f14e6f2f 100644 --- a/angel_orm_generator/lib/src/readers.dart +++ b/angel_orm_generator/lib/src/readers.dart @@ -6,7 +6,7 @@ const TypeChecker columnTypeChecker = const TypeChecker.fromRuntime(Column); Orm reviveORMAnnotation(ConstantReader reader) { return Orm( - OrmType.values[reader.read('type').intValue], + OrmType.values[reader.read('type').objectValue.getField('index').toIntValue()], tableName: reader.peek('tableName')?.stringValue, ); } diff --git a/angel_orm_generator/test/models/author.orm.g.dart b/angel_orm_generator/test/models/author.orm.g.dart index e553f203..2de315b4 100644 --- a/angel_orm_generator/test/models/author.orm.g.dart +++ b/angel_orm_generator/test/models/author.orm.g.dart @@ -6,6 +6,7 @@ import 'dart:async'; import 'author.dart'; +part 'author.postgresql.orm.dart'; abstract class AuthorOrm { Future> getAll(); diff --git a/angel_orm_generator/test/models/car.orm.g.dart b/angel_orm_generator/test/models/car.orm.g.dart index c06f47ae..78bc63e1 100644 --- a/angel_orm_generator/test/models/car.orm.g.dart +++ b/angel_orm_generator/test/models/car.orm.g.dart @@ -5,16 +5,13 @@ // ************************************************************************** import 'dart:async'; - import 'car.dart'; +part 'car.postgresql.orm.dart'; abstract class CarOrm { Future> getAll(); - Future getById(id); - Future update(Car model); - CarQuery query(); } diff --git a/angel_orm_generator/test/models/customer.orm.g.dart b/angel_orm_generator/test/models/customer.orm.g.dart index f6e5ec0b..b0496cf3 100644 --- a/angel_orm_generator/test/models/customer.orm.g.dart +++ b/angel_orm_generator/test/models/customer.orm.g.dart @@ -6,6 +6,7 @@ import 'dart:async'; import 'customer.dart'; +part 'customer.postgresql.orm.dart'; abstract class CustomerOrm { Future> getAll(); diff --git a/angel_orm_generator/test/models/foot.orm.g.dart b/angel_orm_generator/test/models/foot.orm.g.dart index 8fe835aa..65bffe5e 100644 --- a/angel_orm_generator/test/models/foot.orm.g.dart +++ b/angel_orm_generator/test/models/foot.orm.g.dart @@ -6,6 +6,7 @@ import 'dart:async'; import 'foot.dart'; +part 'foot.postgresql.orm.dart'; abstract class FootOrm { Future> getAll(); diff --git a/angel_orm_generator/test/models/fruit.orm.g.dart b/angel_orm_generator/test/models/fruit.orm.g.dart index 88ce1e6f..bb59488a 100644 --- a/angel_orm_generator/test/models/fruit.orm.g.dart +++ b/angel_orm_generator/test/models/fruit.orm.g.dart @@ -6,6 +6,7 @@ import 'dart:async'; import 'fruit.dart'; +part 'fruit.postgresql.orm.dart'; abstract class FruitOrm { Future> getAll(); diff --git a/angel_orm_generator/test/models/order.orm.g.dart b/angel_orm_generator/test/models/order.orm.g.dart index b5dfe667..5fed422d 100644 --- a/angel_orm_generator/test/models/order.orm.g.dart +++ b/angel_orm_generator/test/models/order.orm.g.dart @@ -6,6 +6,7 @@ import 'dart:async'; import 'order.dart'; +part 'order.postgresql.orm.dart'; abstract class OrderOrm { Future> getAll(); diff --git a/angel_orm_generator/test/models/role.orm.g.dart b/angel_orm_generator/test/models/role.orm.g.dart index c6dffb0a..2ef5e351 100644 --- a/angel_orm_generator/test/models/role.orm.g.dart +++ b/angel_orm_generator/test/models/role.orm.g.dart @@ -6,6 +6,7 @@ import 'dart:async'; import 'role.dart'; +part 'role.postgresql.orm.dart'; abstract class RoleOrm { Future> getAll();