Correct the 'part' declaration
This commit is contained in:
parent
3d05f18232
commit
c2fe5449a7
13 changed files with 118 additions and 8 deletions
|
@ -3,8 +3,14 @@ builders:
|
||||||
import: "package:angel_orm_generator/angel_orm_generator.dart"
|
import: "package:angel_orm_generator/angel_orm_generator.dart"
|
||||||
builder_factories:
|
builder_factories:
|
||||||
- ormBuilder
|
- ormBuilder
|
||||||
|
- mongoDBOrmBuilder
|
||||||
|
- postgreSqlOrmBuilder
|
||||||
auto_apply: root_package
|
auto_apply: root_package
|
||||||
build_to: source
|
build_to: source
|
||||||
build_extensions:
|
build_extensions:
|
||||||
.dart:
|
.dart:
|
||||||
- ".orm.g.dart"
|
- ".orm.g.dart"
|
||||||
|
- ".mongodb.orm.g.dart"
|
||||||
|
- ".mysql.orm.g.dart"
|
||||||
|
- ".rethinkdb.orm.g.dart"
|
||||||
|
- ".postgresql.orm.g.dart"
|
|
@ -1,3 +1,5 @@
|
||||||
|
export 'src/mongodb_orm_generator.dart';
|
||||||
export 'src/orm_build_context.dart';
|
export 'src/orm_build_context.dart';
|
||||||
export 'src/orm_generator.dart';
|
export 'src/orm_generator.dart';
|
||||||
|
export 'src/postgresql_orm_generator.dart';
|
||||||
export 'src/readers.dart';
|
export 'src/readers.dart';
|
45
angel_orm_generator/lib/src/mongodb_orm_generator.dart
Normal file
45
angel_orm_generator/lib/src/mongodb_orm_generator.dart
Normal 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';"));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -54,14 +54,23 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
|
||||||
|
|
||||||
switch (ctx.ormAnnotation.type) {
|
switch (ctx.ormAnnotation.type) {
|
||||||
case OrmType.mongoDB:
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
throw 'Unsupported ORM type: ${ctx.ormAnnotation.type}';
|
throw 'Unsupported ORM type: ${ctx.ormAnnotation.type}';
|
||||||
}
|
}
|
||||||
|
|
||||||
var dbFile =
|
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';"));
|
lib.body.add(new Code("part '$dbFile';"));
|
||||||
|
|
||||||
|
|
45
angel_orm_generator/lib/src/postgresql_orm_generator.dart
Normal file
45
angel_orm_generator/lib/src/postgresql_orm_generator.dart
Normal 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';"));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,7 +6,7 @@ const TypeChecker columnTypeChecker = const TypeChecker.fromRuntime(Column);
|
||||||
|
|
||||||
Orm reviveORMAnnotation(ConstantReader reader) {
|
Orm reviveORMAnnotation(ConstantReader reader) {
|
||||||
return Orm(
|
return Orm(
|
||||||
OrmType.values[reader.read('type').intValue],
|
OrmType.values[reader.read('type').objectValue.getField('index').toIntValue()],
|
||||||
tableName: reader.peek('tableName')?.stringValue,
|
tableName: reader.peek('tableName')?.stringValue,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'author.dart';
|
import 'author.dart';
|
||||||
|
part 'author.postgresql.orm.dart';
|
||||||
|
|
||||||
abstract class AuthorOrm {
|
abstract class AuthorOrm {
|
||||||
Future<List<Author>> getAll();
|
Future<List<Author>> getAll();
|
||||||
|
|
|
@ -5,16 +5,13 @@
|
||||||
// **************************************************************************
|
// **************************************************************************
|
||||||
|
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
import 'car.dart';
|
import 'car.dart';
|
||||||
|
part 'car.postgresql.orm.dart';
|
||||||
|
|
||||||
abstract class CarOrm {
|
abstract class CarOrm {
|
||||||
Future<List<Car>> getAll();
|
Future<List<Car>> getAll();
|
||||||
|
|
||||||
Future<Car> getById(id);
|
Future<Car> getById(id);
|
||||||
|
|
||||||
Future<Car> update(Car model);
|
Future<Car> update(Car model);
|
||||||
|
|
||||||
CarQuery query();
|
CarQuery query();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'customer.dart';
|
import 'customer.dart';
|
||||||
|
part 'customer.postgresql.orm.dart';
|
||||||
|
|
||||||
abstract class CustomerOrm {
|
abstract class CustomerOrm {
|
||||||
Future<List<Customer>> getAll();
|
Future<List<Customer>> getAll();
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'foot.dart';
|
import 'foot.dart';
|
||||||
|
part 'foot.postgresql.orm.dart';
|
||||||
|
|
||||||
abstract class FootOrm {
|
abstract class FootOrm {
|
||||||
Future<List<Foot>> getAll();
|
Future<List<Foot>> getAll();
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'fruit.dart';
|
import 'fruit.dart';
|
||||||
|
part 'fruit.postgresql.orm.dart';
|
||||||
|
|
||||||
abstract class FruitOrm {
|
abstract class FruitOrm {
|
||||||
Future<List<Fruit>> getAll();
|
Future<List<Fruit>> getAll();
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'order.dart';
|
import 'order.dart';
|
||||||
|
part 'order.postgresql.orm.dart';
|
||||||
|
|
||||||
abstract class OrderOrm {
|
abstract class OrderOrm {
|
||||||
Future<List<Order>> getAll();
|
Future<List<Order>> getAll();
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'role.dart';
|
import 'role.dart';
|
||||||
|
part 'role.postgresql.orm.dart';
|
||||||
|
|
||||||
abstract class RoleOrm {
|
abstract class RoleOrm {
|
||||||
Future<List<Role>> getAll();
|
Future<List<Role>> getAll();
|
||||||
|
|
Loading…
Reference in a new issue