From 5e5d6177030999b18487ba1640e729052d98e3c8 Mon Sep 17 00:00:00 2001 From: Tobe O Date: Sat, 14 Jul 2018 19:47:25 -0400 Subject: [PATCH] More flexibility for service gen --- lib/src/commands/make/service.dart | 8 ++++---- lib/src/commands/service_generators/custom.dart | 4 ++-- .../service_generators/file_service.dart | 16 +++++++++++----- .../commands/service_generators/generator.dart | 15 ++++++++++----- lib/src/commands/service_generators/map.dart | 4 ++-- lib/src/commands/service_generators/mongo.dart | 12 ++++++++---- lib/src/commands/service_generators/rethink.dart | 12 ++++++++---- 7 files changed, 45 insertions(+), 26 deletions(-) diff --git a/lib/src/commands/make/service.dart b/lib/src/commands/make/service.dart index e1a742a6..72b2b3b0 100644 --- a/lib/src/commands/make/service.dart +++ b/lib/src/commands/make/service.dart @@ -85,7 +85,7 @@ class ServiceCommand extends Command { configureServer.body = new Block((block) { generator.applyToConfigureServer( - configureServer, block, name, rc.snakeCase); + serviceLib, configureServer, block, name, rc.snakeCase); // return (Angel app) async {} var closure = new Method((closure) { @@ -95,11 +95,11 @@ class ServiceCommand extends Command { ..name = 'app' ..type = refer('Angel'))); closure.body = new Block((block) { - generator.beforeService(block, name, rc.snakeCase); + generator.beforeService(serviceLib, block, name, rc.snakeCase); // app.use('/api/todos', new MapService()); - var service = - generator.createInstance(closure, name, rc.snakeCase); + var service = generator.createInstance( + serviceLib, closure, name, rc.snakeCase); if (argResults['typed'] as bool) { var tb = new TypeReference((b) => b diff --git a/lib/src/commands/service_generators/custom.dart b/lib/src/commands/service_generators/custom.dart index a1de5f32..d4134602 100644 --- a/lib/src/commands/service_generators/custom.dart +++ b/lib/src/commands/service_generators/custom.dart @@ -20,8 +20,8 @@ class CustomServiceGenerator extends ServiceGenerator { } @override - Expression createInstance( - MethodBuilder methodBuilder, String name, String lower) { + Expression createInstance(LibraryBuilder library, MethodBuilder methodBuilder, + String name, String lower) { return refer('${name}Service').newInstance([]); } } diff --git a/lib/src/commands/service_generators/file_service.dart b/lib/src/commands/service_generators/file_service.dart index 7ff54579..278562d0 100644 --- a/lib/src/commands/service_generators/file_service.dart +++ b/lib/src/commands/service_generators/file_service.dart @@ -14,8 +14,12 @@ class FileServiceGenerator extends ServiceGenerator { bool get goesFirst => true; @override - void applyToConfigureServer(MethodBuilder configureServer, BlockBuilder block, - String name, String lower) { + void applyToConfigureServer( + LibraryBuilder library, + MethodBuilder configureServer, + BlockBuilder block, + String name, + String lower) { configureServer.requiredParameters.add(new Parameter((b) => b ..name = 'dbDirectory' ..type = refer('Directory'))); @@ -26,13 +30,15 @@ class FileServiceGenerator extends ServiceGenerator { library.directives.addAll([ new Directive.import( 'package:angel_file_service/angel_file_service.dart'), - new Directive.import('package:file/file.dart'), ]); } @override - Expression createInstance( - MethodBuilder methodBuilder, String name, String lower) { + Expression createInstance(LibraryBuilder library, MethodBuilder methodBuilder, + String name, String lower) { + library.directives.addAll([ + new Directive.import('package:file/file.dart'), + ]); return refer('JsonFileService').newInstance([ refer('dbDirectory') .property('childFile') diff --git a/lib/src/commands/service_generators/generator.dart b/lib/src/commands/service_generators/generator.dart index 43b573f5..63289581 100644 --- a/lib/src/commands/service_generators/generator.dart +++ b/lib/src/commands/service_generators/generator.dart @@ -27,13 +27,18 @@ class ServiceGenerator { 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, - String name, String lower) {} + void applyToConfigureServer( + LibraryBuilder library, + MethodBuilder configureServer, + BlockBuilder block, + String name, + String lower) {} - Expression createInstance( - MethodBuilder methodBuilder, String name, String lower) => + Expression createInstance(LibraryBuilder library, MethodBuilder methodBuilder, + String name, String lower) => literal(null); @override diff --git a/lib/src/commands/service_generators/map.dart b/lib/src/commands/service_generators/map.dart index 4b1dec7e..545961df 100644 --- a/lib/src/commands/service_generators/map.dart +++ b/lib/src/commands/service_generators/map.dart @@ -8,8 +8,8 @@ class MapServiceGenerator extends ServiceGenerator { bool get createsModel => false; @override - Expression createInstance( - MethodBuilder methodBuilder, String name, String lower) { + Expression createInstance(LibraryBuilder library, MethodBuilder methodBuilder, + String name, String lower) { return refer('MapService').newInstance([]); } } diff --git a/lib/src/commands/service_generators/mongo.dart b/lib/src/commands/service_generators/mongo.dart index f6b70444..9386f28f 100644 --- a/lib/src/commands/service_generators/mongo.dart +++ b/lib/src/commands/service_generators/mongo.dart @@ -14,8 +14,12 @@ class MongoServiceGenerator extends ServiceGenerator { bool get createsModel => false; @override - void applyToConfigureServer(MethodBuilder configureServer, BlockBuilder block, - String name, String lower) { + void applyToConfigureServer( + LibraryBuilder library, + MethodBuilder configureServer, + BlockBuilder block, + String name, + String lower) { configureServer.requiredParameters.add(new Parameter((b) => b ..name = 'db' ..type = refer('Db'))); @@ -30,8 +34,8 @@ class MongoServiceGenerator extends ServiceGenerator { } @override - Expression createInstance( - MethodBuilder methodBuilder, String name, String lower) { + Expression createInstance(LibraryBuilder library, MethodBuilder methodBuilder, + String name, String lower) { return refer('MongoService').newInstance([ refer('db').property('collection').call([literal(pluralize(lower))]) ]); diff --git a/lib/src/commands/service_generators/rethink.dart b/lib/src/commands/service_generators/rethink.dart index dced10e6..f36e7f12 100644 --- a/lib/src/commands/service_generators/rethink.dart +++ b/lib/src/commands/service_generators/rethink.dart @@ -14,8 +14,12 @@ class RethinkServiceGenerator extends ServiceGenerator { bool get createsModel => false; @override - void applyToConfigureServer(MethodBuilder configureServer, BlockBuilder block, - String name, String lower) { + void applyToConfigureServer( + LibraryBuilder library, + MethodBuilder configureServer, + BlockBuilder block, + String name, + String lower) { configureServer.requiredParameters.addAll([ new Parameter((b) => b ..name = 'connection' @@ -35,8 +39,8 @@ class RethinkServiceGenerator extends ServiceGenerator { } @override - Expression createInstance( - MethodBuilder methodBuilder, String name, String lower) { + Expression createInstance(LibraryBuilder library, MethodBuilder methodBuilder, + String name, String lower) { return refer('RethinkService').newInstance([ refer('connection'), refer('r').property('table').call([literal(pluralize(lower))])