Update rethink service generator

This commit is contained in:
Tobe O 2018-07-14 18:12:12 -04:00
parent 0d378633ac
commit e5ecbeef3d
6 changed files with 45 additions and 37 deletions

View file

@ -38,17 +38,17 @@ class ControllerCommand extends Command {
_pen(); _pen();
} }
NewInstanceBuilder _expose(String path) => new TypeBuilder('Expose') NewInstanceBuilder _expose(String path) => refer('Expose')
.constInstance([], namedArguments: {'path': literal(path)}); .constInstance([], namedArguments: {'path': literal(path)});
String _generateController(Pubspec pubspec, ReCase recase) { String _generateController(Pubspec pubspec, ReCase recase) {
var lower = recase.snakeCase; var lower = recase.snakeCase;
var lib = new LibraryBuilder('${pubspec.name}.routes.controllers.$lower'); var lib = new LibraryBuilder('${pubspec.name}.routes.controllers.$lower');
lib.addDirective( lib.addDirective(
new ImportBuilder('package:angel_common/angel_common.dart')); new Directive.import('package:angel_common/angel_common.dart'));
var clazz = new ClassBuilder('${recase.pascalCase}Controller', var clazz = new ClassBuilder('${recase.pascalCase}Controller',
asExtends: new TypeBuilder('Controller')); asExtends: refer('Controller'));
// Add @Expose() // Add @Expose()
clazz.addAnnotation(_expose('/$lower')); clazz.addAnnotation(_expose('/$lower'));
@ -58,7 +58,7 @@ class ControllerCommand extends Command {
// String foo() => 'bar'; // String foo() => 'bar';
var meth = new MethodBuilder('foo', var meth = new MethodBuilder('foo',
returns: literal('bar'), returnType: new TypeBuilder('String')); returns: literal('bar'), returnType: refer('String'));
meth.addAnnotation(_expose('/')); meth.addAnnotation(_expose('/'));
clazz.addMethod(meth); clazz.addMethod(meth);

View file

@ -102,23 +102,23 @@ class ServiceCommand extends Command {
import '../models/$lower.dart'; import '../models/$lower.dart';
export '../models/$lower.dart'; export '../models/$lower.dart';
*/ */
lib.addMember(new ImportBuilder('package:angel_common/angel_common.dart')); lib.addMember(new Directive.import('package:angel_common/angel_common.dart'));
generator.applyToLibrary(lib, name, lower); generator.applyToLibrary(lib, name, lower);
if (generator.createsModel == true || typed) { if (generator.createsModel == true || typed) {
lib lib
..addMember(new ImportBuilder('../models/$lower.dart')) ..addMember(new Directive.import('../models/$lower.dart'))
..addMember(new ExportBuilder('../models/$lower.dart')); ..addMember(new ExportBuilder('../models/$lower.dart'));
} }
// configureServer() {} // configureServer() {}
var configureServer = new MethodBuilder('configureServer', var configureServer = new MethodBuilder('configureServer',
returnType: new TypeBuilder('AngelConfigurer')); returnType: refer('AngelConfigurer'));
generator.applyToConfigureServer(configureServer, name, lower); generator.applyToConfigureServer(configureServer, name, lower);
// return (Angel app) async {} // return (Angel app) async {}
var closure = new MethodBuilder.closure(modifier: MethodModifier.asAsync) var closure = new MethodBuilder.closure(modifier: MethodModifier.asAsync)
..addPositional(parameter('app', [new TypeBuilder('Angel')])); ..addPositional(parameter('app', [refer('Angel')]));
generator.beforeService(closure, name, lower); generator.beforeService(closure, name, lower);
// app.use('/api/todos', new MapService()); // app.use('/api/todos', new MapService());
@ -126,21 +126,21 @@ class ServiceCommand extends Command {
if (typed == true) { if (typed == true) {
service = service =
new TypeBuilder('TypedService', genericTypes: [new TypeBuilder(name)]) refer('TypedService', genericTypes: [refer(name)])
.newInstance([service]); .newInstance([service]);
} }
closure.addStatement(reference('app') closure.addStatement(refer('app')
.invoke('use', [literal('/api/${pluralize(lower)}'), service])); .invoke('use', [literal('/api/${pluralize(lower)}'), service]));
if (generator.injectsSingleton == true) { if (generator.injectsSingleton == true) {
closure.addStatement(varField('service', closure.addStatement(varField('service',
value: reference('app') value: refer('app')
.invoke('service', [literal('/api/${pluralize(lower)}')]).castAs( .invoke('service', [literal('/api/${pluralize(lower)}')]).castAs(
new TypeBuilder('HookedService')))); refer('HookedService'))));
closure.addStatement(reference('app') closure.addStatement(refer('app')
.property('container') .property('container')
.invoke('singleton', [reference('service').property('inner')])); .invoke('singleton', [refer('service').property('inner')]));
} }
configureServer.addStatement(closure.asReturn()); configureServer.addStatement(closure.asReturn());

View file

@ -14,24 +14,26 @@ class MongoServiceGenerator extends ServiceGenerator {
bool get createsModel => false; bool get createsModel => false;
@override @override
void applyToConfigureServer( void applyToConfigureServer(MethodBuilder configureServer, BlockBuilder block,
MethodBuilder configureServer, String name, String lower) { String name, String lower) {
configureServer.addPositional(parameter('db', [new TypeBuilder('Db')])); configureServer.requiredParameters.add(new Parameter((b) => b
..name = 'db'
..type = refer('Db')));
} }
@override @override
void applyToLibrary(LibraryBuilder library, String name, String lower) { void applyToLibrary(LibraryBuilder library, String name, String lower) {
library.addMembers([ library.directives.addAll([
new ImportBuilder('package:angel_mongo/angel_mongo.dart'), new Directive.import('package:angel_mongo/angel_mongo.dart'),
new ImportBuilder('package:mongo_dart/mongo_dart.dart'), new Directive.import('package:mongo_dart/mongo_dart.dart'),
]); ]);
} }
@override @override
ExpressionBuilder createInstance( Expression createInstance(
MethodBuilder methodBuilder, String name, String lower) { MethodBuilder methodBuilder, String name, String lower) {
return new TypeBuilder('MongoService').newInstance([ return refer('MongoService').newInstance([
reference('db').invoke('collection', [literal(pluralize(lower))]) refer('db').property('collection').call([literal(pluralize(lower))])
]); ]);
} }
} }

View file

@ -14,27 +14,32 @@ class RethinkServiceGenerator extends ServiceGenerator {
bool get createsModel => false; bool get createsModel => false;
@override @override
void applyToConfigureServer( void applyToConfigureServer(MethodBuilder configureServer, BlockBuilder block,
MethodBuilder configureServer, String name, String lower) { String name, String lower) {
configureServer configureServer.requiredParameters.addAll([
..addPositional(parameter('connection', [new TypeBuilder('Connection')])) new Parameter((b) => b
..addPositional(parameter('r', [new TypeBuilder('Rethinkdb')])); ..name = 'connection'
..type = refer('Connection')),
new Parameter((b) => b
..name = 'r'
..type = refer('Rethinkdb')),
]);
} }
@override @override
void applyToLibrary(LibraryBuilder library, String name, String lower) { void applyToLibrary(LibraryBuilder library, String name, String lower) {
library.addMembers([ library.directives.addAll([
'package:angel_rethink/angel_rethink.dart', 'package:angel_rethink/angel_rethink.dart',
'package:rethinkdb_driver2/rethinkdb_driver2.dart' 'package:rethinkdb_driver2/rethinkdb_driver2.dart'
].map((str) => new ImportBuilder(str))); ].map((str) => new Directive.import(str)));
} }
@override @override
ExpressionBuilder createInstance( Expression createInstance(
MethodBuilder methodBuilder, String name, String lower) { MethodBuilder methodBuilder, String name, String lower) {
return new TypeBuilder('RethinkService').newInstance([ return refer('RethinkService').newInstance([
reference('connection'), refer('connection'),
reference('r').invoke('table', [literal(pluralize(lower))]) refer('r').property('table').call([literal(pluralize(lower))])
]); ]);
} }
} }

View file

@ -2,7 +2,7 @@ import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:io/ansi.dart'; import 'package:io/ansi.dart';
import 'package:pubspec_parse/pubspec_parse.dart'; import 'package:pubspec_parse/pubspec_parse.dart';
import 'package:yamlicious/yamlicious.dart'; //import 'package:yamlicious/yamlicious.dart';
final String checkmark = ansiOutputEnabled ? '\u2713' : '[Success]'; final String checkmark = ansiOutputEnabled ? '\u2713' : '[Success]';
@ -14,5 +14,6 @@ Future<Pubspec> loadPubspec() {
} }
Future savePubspec(Pubspec pubspec) async { Future savePubspec(Pubspec pubspec) async {
var text = toYamlString(pubspec); // TODO: Save pubspec for real?
//var text = toYamlString(pubspec);
} }

View file

@ -24,7 +24,7 @@ dependencies:
recase: ^1.0.0 recase: ^1.0.0
watcher: ^0.9.7 watcher: ^0.9.7
yaml: ^2.0.0 yaml: ^2.0.0
yamlicious: ^0.0.5 #yamlicious: ^0.0.5
dev_dependencies: dev_dependencies:
#build: ^0.7.0 #build: ^0.7.0
#build_runner: ^0.3.0 #build_runner: ^0.3.0