Update rethink service generator
This commit is contained in:
parent
0d378633ac
commit
e5ecbeef3d
6 changed files with 45 additions and 37 deletions
|
@ -38,17 +38,17 @@ class ControllerCommand extends Command {
|
|||
_pen();
|
||||
}
|
||||
|
||||
NewInstanceBuilder _expose(String path) => new TypeBuilder('Expose')
|
||||
NewInstanceBuilder _expose(String path) => refer('Expose')
|
||||
.constInstance([], namedArguments: {'path': literal(path)});
|
||||
|
||||
String _generateController(Pubspec pubspec, ReCase recase) {
|
||||
var lower = recase.snakeCase;
|
||||
var lib = new LibraryBuilder('${pubspec.name}.routes.controllers.$lower');
|
||||
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',
|
||||
asExtends: new TypeBuilder('Controller'));
|
||||
asExtends: refer('Controller'));
|
||||
|
||||
// Add @Expose()
|
||||
clazz.addAnnotation(_expose('/$lower'));
|
||||
|
@ -58,7 +58,7 @@ class ControllerCommand extends Command {
|
|||
// String foo() => 'bar';
|
||||
|
||||
var meth = new MethodBuilder('foo',
|
||||
returns: literal('bar'), returnType: new TypeBuilder('String'));
|
||||
returns: literal('bar'), returnType: refer('String'));
|
||||
meth.addAnnotation(_expose('/'));
|
||||
clazz.addMethod(meth);
|
||||
|
||||
|
|
|
@ -102,23 +102,23 @@ class ServiceCommand extends Command {
|
|||
import '../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);
|
||||
|
||||
if (generator.createsModel == true || typed) {
|
||||
lib
|
||||
..addMember(new ImportBuilder('../models/$lower.dart'))
|
||||
..addMember(new Directive.import('../models/$lower.dart'))
|
||||
..addMember(new ExportBuilder('../models/$lower.dart'));
|
||||
}
|
||||
|
||||
// configureServer() {}
|
||||
var configureServer = new MethodBuilder('configureServer',
|
||||
returnType: new TypeBuilder('AngelConfigurer'));
|
||||
returnType: refer('AngelConfigurer'));
|
||||
generator.applyToConfigureServer(configureServer, name, lower);
|
||||
|
||||
// return (Angel app) async {}
|
||||
var closure = new MethodBuilder.closure(modifier: MethodModifier.asAsync)
|
||||
..addPositional(parameter('app', [new TypeBuilder('Angel')]));
|
||||
..addPositional(parameter('app', [refer('Angel')]));
|
||||
generator.beforeService(closure, name, lower);
|
||||
|
||||
// app.use('/api/todos', new MapService());
|
||||
|
@ -126,21 +126,21 @@ class ServiceCommand extends Command {
|
|||
|
||||
if (typed == true) {
|
||||
service =
|
||||
new TypeBuilder('TypedService', genericTypes: [new TypeBuilder(name)])
|
||||
refer('TypedService', genericTypes: [refer(name)])
|
||||
.newInstance([service]);
|
||||
}
|
||||
|
||||
closure.addStatement(reference('app')
|
||||
closure.addStatement(refer('app')
|
||||
.invoke('use', [literal('/api/${pluralize(lower)}'), service]));
|
||||
|
||||
if (generator.injectsSingleton == true) {
|
||||
closure.addStatement(varField('service',
|
||||
value: reference('app')
|
||||
value: refer('app')
|
||||
.invoke('service', [literal('/api/${pluralize(lower)}')]).castAs(
|
||||
new TypeBuilder('HookedService'))));
|
||||
closure.addStatement(reference('app')
|
||||
refer('HookedService'))));
|
||||
closure.addStatement(refer('app')
|
||||
.property('container')
|
||||
.invoke('singleton', [reference('service').property('inner')]));
|
||||
.invoke('singleton', [refer('service').property('inner')]));
|
||||
}
|
||||
|
||||
configureServer.addStatement(closure.asReturn());
|
||||
|
|
|
@ -14,24 +14,26 @@ class MongoServiceGenerator extends ServiceGenerator {
|
|||
bool get createsModel => false;
|
||||
|
||||
@override
|
||||
void applyToConfigureServer(
|
||||
MethodBuilder configureServer, String name, String lower) {
|
||||
configureServer.addPositional(parameter('db', [new TypeBuilder('Db')]));
|
||||
void applyToConfigureServer(MethodBuilder configureServer, BlockBuilder block,
|
||||
String name, String lower) {
|
||||
configureServer.requiredParameters.add(new Parameter((b) => b
|
||||
..name = 'db'
|
||||
..type = refer('Db')));
|
||||
}
|
||||
|
||||
@override
|
||||
void applyToLibrary(LibraryBuilder library, String name, String lower) {
|
||||
library.addMembers([
|
||||
new ImportBuilder('package:angel_mongo/angel_mongo.dart'),
|
||||
new ImportBuilder('package:mongo_dart/mongo_dart.dart'),
|
||||
library.directives.addAll([
|
||||
new Directive.import('package:angel_mongo/angel_mongo.dart'),
|
||||
new Directive.import('package:mongo_dart/mongo_dart.dart'),
|
||||
]);
|
||||
}
|
||||
|
||||
@override
|
||||
ExpressionBuilder createInstance(
|
||||
Expression createInstance(
|
||||
MethodBuilder methodBuilder, String name, String lower) {
|
||||
return new TypeBuilder('MongoService').newInstance([
|
||||
reference('db').invoke('collection', [literal(pluralize(lower))])
|
||||
return refer('MongoService').newInstance([
|
||||
refer('db').property('collection').call([literal(pluralize(lower))])
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,27 +14,32 @@ class RethinkServiceGenerator extends ServiceGenerator {
|
|||
bool get createsModel => false;
|
||||
|
||||
@override
|
||||
void applyToConfigureServer(
|
||||
MethodBuilder configureServer, String name, String lower) {
|
||||
configureServer
|
||||
..addPositional(parameter('connection', [new TypeBuilder('Connection')]))
|
||||
..addPositional(parameter('r', [new TypeBuilder('Rethinkdb')]));
|
||||
void applyToConfigureServer(MethodBuilder configureServer, BlockBuilder block,
|
||||
String name, String lower) {
|
||||
configureServer.requiredParameters.addAll([
|
||||
new Parameter((b) => b
|
||||
..name = 'connection'
|
||||
..type = refer('Connection')),
|
||||
new Parameter((b) => b
|
||||
..name = 'r'
|
||||
..type = refer('Rethinkdb')),
|
||||
]);
|
||||
}
|
||||
|
||||
@override
|
||||
void applyToLibrary(LibraryBuilder library, String name, String lower) {
|
||||
library.addMembers([
|
||||
library.directives.addAll([
|
||||
'package:angel_rethink/angel_rethink.dart',
|
||||
'package:rethinkdb_driver2/rethinkdb_driver2.dart'
|
||||
].map((str) => new ImportBuilder(str)));
|
||||
].map((str) => new Directive.import(str)));
|
||||
}
|
||||
|
||||
@override
|
||||
ExpressionBuilder createInstance(
|
||||
Expression createInstance(
|
||||
MethodBuilder methodBuilder, String name, String lower) {
|
||||
return new TypeBuilder('RethinkService').newInstance([
|
||||
reference('connection'),
|
||||
reference('r').invoke('table', [literal(pluralize(lower))])
|
||||
return refer('RethinkService').newInstance([
|
||||
refer('connection'),
|
||||
refer('r').property('table').call([literal(pluralize(lower))])
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ import 'dart:async';
|
|||
import 'dart:io';
|
||||
import 'package:io/ansi.dart';
|
||||
import 'package:pubspec_parse/pubspec_parse.dart';
|
||||
import 'package:yamlicious/yamlicious.dart';
|
||||
//import 'package:yamlicious/yamlicious.dart';
|
||||
|
||||
final String checkmark = ansiOutputEnabled ? '\u2713' : '[Success]';
|
||||
|
||||
|
@ -14,5 +14,6 @@ Future<Pubspec> loadPubspec() {
|
|||
}
|
||||
|
||||
Future savePubspec(Pubspec pubspec) async {
|
||||
var text = toYamlString(pubspec);
|
||||
// TODO: Save pubspec for real?
|
||||
//var text = toYamlString(pubspec);
|
||||
}
|
|
@ -24,7 +24,7 @@ dependencies:
|
|||
recase: ^1.0.0
|
||||
watcher: ^0.9.7
|
||||
yaml: ^2.0.0
|
||||
yamlicious: ^0.0.5
|
||||
#yamlicious: ^0.0.5
|
||||
dev_dependencies:
|
||||
#build: ^0.7.0
|
||||
#build_runner: ^0.3.0
|
||||
|
|
Loading…
Reference in a new issue