Update service generator
This commit is contained in:
parent
4c9e1c78ce
commit
c676bb18fd
2 changed files with 77 additions and 67 deletions
|
@ -1,16 +1,17 @@
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'package:args/command_runner.dart';
|
import 'package:args/command_runner.dart';
|
||||||
import 'package:code_builder/code_builder.dart';
|
import 'package:code_builder/code_builder.dart';
|
||||||
import 'package:console/console.dart';
|
import 'package:dart_style/dart_style.dart';
|
||||||
import 'package:inflection/inflection.dart';
|
import 'package:inflection/inflection.dart';
|
||||||
|
import 'package:io/ansi.dart';
|
||||||
|
import 'package:prompts/prompts.dart' as prompts;
|
||||||
import 'package:pubspec_parse/pubspec_parse.dart';
|
import 'package:pubspec_parse/pubspec_parse.dart';
|
||||||
import 'package:recase/recase.dart';
|
import 'package:recase/recase.dart';
|
||||||
import '../service_generators/service_generators.dart';
|
import '../service_generators/service_generators.dart';
|
||||||
|
import '../../util.dart';
|
||||||
import 'maker.dart';
|
import 'maker.dart';
|
||||||
|
|
||||||
class ServiceCommand extends Command {
|
class ServiceCommand extends Command {
|
||||||
final TextPen _pen = new TextPen();
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String get name => 'service';
|
String get name => 'service';
|
||||||
|
|
||||||
|
@ -32,39 +33,30 @@ class ServiceCommand extends Command {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
run() async {
|
run() async {
|
||||||
var pubspec = await Pubspec.load(Directory.current);
|
var pubspec = await loadPubspec();
|
||||||
String name;
|
String name;
|
||||||
if (argResults.wasParsed('name')) name = argResults['name'];
|
if (argResults.wasParsed('name')) name = argResults['name'] as String;
|
||||||
|
|
||||||
if (name?.isNotEmpty != true) {
|
if (name?.isNotEmpty != true) {
|
||||||
var p = new Prompter('Name of Service: ');
|
name = prompts.get('Name of service');
|
||||||
name = await p.prompt(checker: (s) => s.isNotEmpty);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
List<MakerDependency> deps = [
|
List<MakerDependency> deps = [
|
||||||
const MakerDependency('angel_framework', '^1.0.0')
|
const MakerDependency('angel_framework', '^1.0.0')
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// '${pubspec.name}.src.services.${rc.snakeCase}'
|
||||||
var rc = new ReCase(name);
|
var rc = new ReCase(name);
|
||||||
var serviceLib =
|
var serviceLib = new Library((serviceLib) {
|
||||||
new LibraryBuilder('${pubspec.name}.src.services.${rc.snakeCase}');
|
var generator = prompts.choose(
|
||||||
|
'Choose which type of service to create', serviceGenerators);
|
||||||
|
|
||||||
ServiceGenerator generator;
|
// if (generator == null) {
|
||||||
|
// _pen.red();
|
||||||
var chooser = new Chooser<String>(
|
// _pen('${Icon.BALLOT_X} \'$type\' services are not yet implemented. :(');
|
||||||
serviceGenerators.map<String>((g) => g.name).toList(),
|
// _pen();
|
||||||
message: 'What type of service would you like to create? ');
|
// throw 'Unrecognized service type: "$type".';
|
||||||
var type = await chooser.choose();
|
// }
|
||||||
|
|
||||||
generator =
|
|
||||||
serviceGenerators.firstWhere((g) => g.name == type, orElse: () => null);
|
|
||||||
|
|
||||||
if (generator == null) {
|
|
||||||
_pen.red();
|
|
||||||
_pen('${Icon.BALLOT_X} \'$type\' services are not yet implemented. :(');
|
|
||||||
_pen();
|
|
||||||
throw 'Unrecognized service type: "$type".';
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var dep in generator.dependencies) {
|
for (var dep in generator.dependencies) {
|
||||||
if (!deps.any((d) => d.name == dep.name)) deps.add(dep);
|
if (!deps.any((d) => d.name == dep.name)) deps.add(dep);
|
||||||
|
@ -72,54 +64,72 @@ class ServiceCommand extends Command {
|
||||||
|
|
||||||
if (generator.goesFirst) {
|
if (generator.goesFirst) {
|
||||||
generator.applyToLibrary(serviceLib, name, rc.snakeCase);
|
generator.applyToLibrary(serviceLib, name, rc.snakeCase);
|
||||||
serviceLib.addMember(
|
serviceLib.directives.add(new Directive.import(
|
||||||
new ImportBuilder('package:angel_framework/angel_framework.dart'));
|
'package:angel_framework/angel_framework.dart'));
|
||||||
} else {
|
} else {
|
||||||
serviceLib.addMember(
|
serviceLib.directives.add(new Directive.import(
|
||||||
new ImportBuilder('package:angel_framework/angel_framework.dart'));
|
'package:angel_framework/angel_framework.dart'));
|
||||||
generator.applyToLibrary(serviceLib, name, rc.snakeCase);
|
generator.applyToLibrary(serviceLib, name, rc.snakeCase);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argResults['typed']) {
|
if (argResults['typed'] as bool) {
|
||||||
serviceLib
|
serviceLib.directives
|
||||||
..addMember(new ImportBuilder('../models/${rc.snakeCase}.dart'));
|
.add(new Directive.import('../models/${rc.snakeCase}.dart'));
|
||||||
}
|
}
|
||||||
|
|
||||||
// configureServer() {}
|
// configureServer() {}
|
||||||
var configureServer = new MethodBuilder('configureServer',
|
serviceLib.body.add(new Method((configureServer) {
|
||||||
returnType: new TypeBuilder('AngelConfigurer'));
|
configureServer
|
||||||
generator.applyToConfigureServer(configureServer, name, rc.snakeCase);
|
..name = 'configureServer'
|
||||||
|
..returns = refer('AngelConfigurer');
|
||||||
|
|
||||||
|
configureServer.body = new Block((block) {
|
||||||
|
generator.applyToConfigureServer(
|
||||||
|
configureServer, block, name, rc.snakeCase);
|
||||||
|
|
||||||
// return (Angel app) async {}
|
// return (Angel app) async {}
|
||||||
var closure = new MethodBuilder.closure(modifier: MethodModifier.asAsync)
|
var closure = new Method((closure) {
|
||||||
..addPositional(parameter('app', [new TypeBuilder('Angel')]));
|
closure
|
||||||
generator.beforeService(closure, name, rc.snakeCase);
|
..modifier = MethodModifier.async
|
||||||
|
..requiredParameters.add(new Parameter((b) => b
|
||||||
|
..name = 'app'
|
||||||
|
..type = refer('Angel')));
|
||||||
|
closure.body = new Block((block) {
|
||||||
|
generator.beforeService(block, name, rc.snakeCase);
|
||||||
|
|
||||||
// app.use('/api/todos', new MapService());
|
// app.use('/api/todos', new MapService());
|
||||||
var service = generator.createInstance(closure, name, rc.snakeCase);
|
var service =
|
||||||
|
generator.createInstance(closure, name, rc.snakeCase);
|
||||||
|
|
||||||
if (argResults['typed']) {
|
if (argResults['typed'] as bool) {
|
||||||
service = new TypeBuilder('TypedService',
|
var tb = new TypeReference((b) => b
|
||||||
genericTypes: [new TypeBuilder(rc.pascalCase)])
|
..symbol = 'TypedService'
|
||||||
.newInstance([service]);
|
..types.add(refer(rc.pascalCase)));
|
||||||
|
service = tb.newInstance([service]);
|
||||||
}
|
}
|
||||||
|
|
||||||
closure.addStatement(reference('app')
|
block.addExpression(refer('app').property('use').call([
|
||||||
.invoke('use', [literal('/api/${pluralize(rc.snakeCase)}'), service]));
|
literal('/api/${pluralize(rc.snakeCase)}'),
|
||||||
configureServer.addStatement(closure.asReturn());
|
service,
|
||||||
serviceLib.addMember(configureServer);
|
]));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
block.addExpression(closure.closure.returned);
|
||||||
|
});
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
final outputDir = new Directory.fromUri(
|
final outputDir = new Directory.fromUri(
|
||||||
Directory.current.uri.resolve(argResults['output-dir']));
|
Directory.current.uri.resolve(argResults['output-dir'] as String));
|
||||||
final serviceFile =
|
final serviceFile =
|
||||||
new File.fromUri(outputDir.uri.resolve("${rc.snakeCase}.dart"));
|
new File.fromUri(outputDir.uri.resolve("${rc.snakeCase}.dart"));
|
||||||
if (!await serviceFile.exists()) await serviceFile.create(recursive: true);
|
if (!await serviceFile.exists()) await serviceFile.create(recursive: true);
|
||||||
await serviceFile.writeAsString(prettyToSource(serviceLib.buildAst()));
|
await serviceFile.writeAsString(new DartFormatter()
|
||||||
|
.format(serviceLib.accept(new DartEmitter()).toString()));
|
||||||
|
|
||||||
_pen.green();
|
print(green.wrap(
|
||||||
_pen(
|
'$checkmark Successfully generated service file "${serviceFile.absolute.path}".'));
|
||||||
'${Icon.CHECKMARK} Successfully generated service file "${serviceFile.absolute.path}".');
|
|
||||||
_pen();
|
|
||||||
|
|
||||||
if (deps.isNotEmpty) await depend(deps);
|
if (deps.isNotEmpty) await depend(deps);
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,12 +27,12 @@ class ServiceGenerator {
|
||||||
|
|
||||||
void applyToLibrary(LibraryBuilder library, String name, String lower) {}
|
void applyToLibrary(LibraryBuilder library, String name, String lower) {}
|
||||||
|
|
||||||
void beforeService(MethodBuilder methodBuilder, String name, String lower) {}
|
void beforeService(BlockBuilder builder, String name, String lower) {}
|
||||||
|
|
||||||
void applyToConfigureServer(
|
void applyToConfigureServer(
|
||||||
MethodBuilder configureServer, String name, String lower) {}
|
MethodBuilder configureServer, BlockBuilder block, String name, String lower) {}
|
||||||
|
|
||||||
ExpressionBuilder createInstance(
|
Expression createInstance(
|
||||||
MethodBuilder methodBuilder, String name, String lower) =>
|
MethodBuilder methodBuilder, String name, String lower) =>
|
||||||
literal(null);
|
literal(null);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue