Correct the 'part' declaration

This commit is contained in:
Tobe O 2018-08-24 11:10:27 -04:00
parent 3d05f18232
commit c2fe5449a7
13 changed files with 118 additions and 8 deletions

View file

@ -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"
- ".mongodb.orm.g.dart"
- ".mysql.orm.g.dart"
- ".rethinkdb.orm.g.dart"
- ".postgresql.orm.g.dart"

View file

@ -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';

View file

@ -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<Orm> {
final bool autoSnakeCaseNames;
final bool autoIdAndDateFields;
MongoDBOrmGenerator({this.autoSnakeCaseNames, this.autoIdAndDateFields});
@override
Future<String> 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';"));
});
}
}

View file

@ -54,14 +54,23 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
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';"));

View file

@ -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<Orm> {
final bool autoSnakeCaseNames;
final bool autoIdAndDateFields;
PostgreSqlOrmGenerator({this.autoSnakeCaseNames, this.autoIdAndDateFields});
@override
Future<String> 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';"));
});
}
}

View file

@ -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,
);
}

View file

@ -6,6 +6,7 @@
import 'dart:async';
import 'author.dart';
part 'author.postgresql.orm.dart';
abstract class AuthorOrm {
Future<List<Author>> getAll();

View file

@ -5,16 +5,13 @@
// **************************************************************************
import 'dart:async';
import 'car.dart';
part 'car.postgresql.orm.dart';
abstract class CarOrm {
Future<List<Car>> getAll();
Future<Car> getById(id);
Future<Car> update(Car model);
CarQuery query();
}

View file

@ -6,6 +6,7 @@
import 'dart:async';
import 'customer.dart';
part 'customer.postgresql.orm.dart';
abstract class CustomerOrm {
Future<List<Customer>> getAll();

View file

@ -6,6 +6,7 @@
import 'dart:async';
import 'foot.dart';
part 'foot.postgresql.orm.dart';
abstract class FootOrm {
Future<List<Foot>> getAll();

View file

@ -6,6 +6,7 @@
import 'dart:async';
import 'fruit.dart';
part 'fruit.postgresql.orm.dart';
abstract class FruitOrm {
Future<List<Fruit>> getAll();

View file

@ -6,6 +6,7 @@
import 'dart:async';
import 'order.dart';
part 'order.postgresql.orm.dart';
abstract class OrderOrm {
Future<List<Order>> getAll();

View file

@ -6,6 +6,7 @@
import 'dart:async';
import 'role.dart';
part 'role.postgresql.orm.dart';
abstract class RoleOrm {
Future<List<Role>> getAll();