More flexibility for service gen

This commit is contained in:
Tobe O 2018-07-14 19:47:25 -04:00
parent 29252ee915
commit 5e5d617703
7 changed files with 45 additions and 26 deletions

View file

@ -85,7 +85,7 @@ class ServiceCommand extends Command {
configureServer.body = new Block((block) { configureServer.body = new Block((block) {
generator.applyToConfigureServer( generator.applyToConfigureServer(
configureServer, block, name, rc.snakeCase); serviceLib, configureServer, block, name, rc.snakeCase);
// return (Angel app) async {} // return (Angel app) async {}
var closure = new Method((closure) { var closure = new Method((closure) {
@ -95,11 +95,11 @@ class ServiceCommand extends Command {
..name = 'app' ..name = 'app'
..type = refer('Angel'))); ..type = refer('Angel')));
closure.body = new Block((block) { closure.body = new Block((block) {
generator.beforeService(block, name, rc.snakeCase); generator.beforeService(serviceLib, block, name, rc.snakeCase);
// app.use('/api/todos', new MapService()); // app.use('/api/todos', new MapService());
var service = var service = generator.createInstance(
generator.createInstance(closure, name, rc.snakeCase); serviceLib, closure, name, rc.snakeCase);
if (argResults['typed'] as bool) { if (argResults['typed'] as bool) {
var tb = new TypeReference((b) => b var tb = new TypeReference((b) => b

View file

@ -20,8 +20,8 @@ class CustomServiceGenerator extends ServiceGenerator {
} }
@override @override
Expression createInstance( Expression createInstance(LibraryBuilder library, MethodBuilder methodBuilder,
MethodBuilder methodBuilder, String name, String lower) { String name, String lower) {
return refer('${name}Service').newInstance([]); return refer('${name}Service').newInstance([]);
} }
} }

View file

@ -14,8 +14,12 @@ class FileServiceGenerator extends ServiceGenerator {
bool get goesFirst => true; bool get goesFirst => true;
@override @override
void applyToConfigureServer(MethodBuilder configureServer, BlockBuilder block, void applyToConfigureServer(
String name, String lower) { LibraryBuilder library,
MethodBuilder configureServer,
BlockBuilder block,
String name,
String lower) {
configureServer.requiredParameters.add(new Parameter((b) => b configureServer.requiredParameters.add(new Parameter((b) => b
..name = 'dbDirectory' ..name = 'dbDirectory'
..type = refer('Directory'))); ..type = refer('Directory')));
@ -26,13 +30,15 @@ class FileServiceGenerator extends ServiceGenerator {
library.directives.addAll([ library.directives.addAll([
new Directive.import( new Directive.import(
'package:angel_file_service/angel_file_service.dart'), 'package:angel_file_service/angel_file_service.dart'),
new Directive.import('package:file/file.dart'),
]); ]);
} }
@override @override
Expression createInstance( Expression createInstance(LibraryBuilder library, MethodBuilder methodBuilder,
MethodBuilder methodBuilder, String name, String lower) { String name, String lower) {
library.directives.addAll([
new Directive.import('package:file/file.dart'),
]);
return refer('JsonFileService').newInstance([ return refer('JsonFileService').newInstance([
refer('dbDirectory') refer('dbDirectory')
.property('childFile') .property('childFile')

View file

@ -27,13 +27,18 @@ class ServiceGenerator {
void applyToLibrary(LibraryBuilder library, String name, String lower) {} void applyToLibrary(LibraryBuilder library, String name, String lower) {}
void beforeService(BlockBuilder builder, String name, String lower) {} void beforeService(LibraryBuilder library, BlockBuilder builder, String name,
String lower) {}
void applyToConfigureServer(MethodBuilder configureServer, BlockBuilder block, void applyToConfigureServer(
String name, String lower) {} LibraryBuilder library,
MethodBuilder configureServer,
BlockBuilder block,
String name,
String lower) {}
Expression createInstance( Expression createInstance(LibraryBuilder library, MethodBuilder methodBuilder,
MethodBuilder methodBuilder, String name, String lower) => String name, String lower) =>
literal(null); literal(null);
@override @override

View file

@ -8,8 +8,8 @@ class MapServiceGenerator extends ServiceGenerator {
bool get createsModel => false; bool get createsModel => false;
@override @override
Expression createInstance( Expression createInstance(LibraryBuilder library, MethodBuilder methodBuilder,
MethodBuilder methodBuilder, String name, String lower) { String name, String lower) {
return refer('MapService').newInstance([]); return refer('MapService').newInstance([]);
} }
} }

View file

@ -14,8 +14,12 @@ class MongoServiceGenerator extends ServiceGenerator {
bool get createsModel => false; bool get createsModel => false;
@override @override
void applyToConfigureServer(MethodBuilder configureServer, BlockBuilder block, void applyToConfigureServer(
String name, String lower) { LibraryBuilder library,
MethodBuilder configureServer,
BlockBuilder block,
String name,
String lower) {
configureServer.requiredParameters.add(new Parameter((b) => b configureServer.requiredParameters.add(new Parameter((b) => b
..name = 'db' ..name = 'db'
..type = refer('Db'))); ..type = refer('Db')));
@ -30,8 +34,8 @@ class MongoServiceGenerator extends ServiceGenerator {
} }
@override @override
Expression createInstance( Expression createInstance(LibraryBuilder library, MethodBuilder methodBuilder,
MethodBuilder methodBuilder, String name, String lower) { String name, String lower) {
return refer('MongoService').newInstance([ return refer('MongoService').newInstance([
refer('db').property('collection').call([literal(pluralize(lower))]) refer('db').property('collection').call([literal(pluralize(lower))])
]); ]);

View file

@ -14,8 +14,12 @@ class RethinkServiceGenerator extends ServiceGenerator {
bool get createsModel => false; bool get createsModel => false;
@override @override
void applyToConfigureServer(MethodBuilder configureServer, BlockBuilder block, void applyToConfigureServer(
String name, String lower) { LibraryBuilder library,
MethodBuilder configureServer,
BlockBuilder block,
String name,
String lower) {
configureServer.requiredParameters.addAll([ configureServer.requiredParameters.addAll([
new Parameter((b) => b new Parameter((b) => b
..name = 'connection' ..name = 'connection'
@ -35,8 +39,8 @@ class RethinkServiceGenerator extends ServiceGenerator {
} }
@override @override
Expression createInstance( Expression createInstance(LibraryBuilder library, MethodBuilder methodBuilder,
MethodBuilder methodBuilder, String name, String lower) { String name, String lower) {
return refer('RethinkService').newInstance([ return refer('RethinkService').newInstance([
refer('connection'), refer('connection'),
refer('r').property('table').call([literal(pluralize(lower))]) refer('r').property('table').call([literal(pluralize(lower))])