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,94 +33,103 @@ 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();
|
||||||
|
// _pen('${Icon.BALLOT_X} \'$type\' services are not yet implemented. :(');
|
||||||
|
// _pen();
|
||||||
|
// throw 'Unrecognized service type: "$type".';
|
||||||
|
// }
|
||||||
|
|
||||||
var chooser = new Chooser<String>(
|
for (var dep in generator.dependencies) {
|
||||||
serviceGenerators.map<String>((g) => g.name).toList(),
|
if (!deps.any((d) => d.name == dep.name)) deps.add(dep);
|
||||||
message: 'What type of service would you like to create? ');
|
}
|
||||||
var type = await chooser.choose();
|
|
||||||
|
|
||||||
generator =
|
if (generator.goesFirst) {
|
||||||
serviceGenerators.firstWhere((g) => g.name == type, orElse: () => null);
|
generator.applyToLibrary(serviceLib, name, rc.snakeCase);
|
||||||
|
serviceLib.directives.add(new Directive.import(
|
||||||
|
'package:angel_framework/angel_framework.dart'));
|
||||||
|
} else {
|
||||||
|
serviceLib.directives.add(new Directive.import(
|
||||||
|
'package:angel_framework/angel_framework.dart'));
|
||||||
|
generator.applyToLibrary(serviceLib, name, rc.snakeCase);
|
||||||
|
}
|
||||||
|
|
||||||
if (generator == null) {
|
if (argResults['typed'] as bool) {
|
||||||
_pen.red();
|
serviceLib.directives
|
||||||
_pen('${Icon.BALLOT_X} \'$type\' services are not yet implemented. :(');
|
.add(new Directive.import('../models/${rc.snakeCase}.dart'));
|
||||||
_pen();
|
}
|
||||||
throw 'Unrecognized service type: "$type".';
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var dep in generator.dependencies) {
|
// configureServer() {}
|
||||||
if (!deps.any((d) => d.name == dep.name)) deps.add(dep);
|
serviceLib.body.add(new Method((configureServer) {
|
||||||
}
|
configureServer
|
||||||
|
..name = 'configureServer'
|
||||||
|
..returns = refer('AngelConfigurer');
|
||||||
|
|
||||||
if (generator.goesFirst) {
|
configureServer.body = new Block((block) {
|
||||||
generator.applyToLibrary(serviceLib, name, rc.snakeCase);
|
generator.applyToConfigureServer(
|
||||||
serviceLib.addMember(
|
configureServer, block, name, rc.snakeCase);
|
||||||
new ImportBuilder('package:angel_framework/angel_framework.dart'));
|
|
||||||
} else {
|
|
||||||
serviceLib.addMember(
|
|
||||||
new ImportBuilder('package:angel_framework/angel_framework.dart'));
|
|
||||||
generator.applyToLibrary(serviceLib, name, rc.snakeCase);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (argResults['typed']) {
|
// return (Angel app) async {}
|
||||||
serviceLib
|
var closure = new Method((closure) {
|
||||||
..addMember(new ImportBuilder('../models/${rc.snakeCase}.dart'));
|
closure
|
||||||
}
|
..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);
|
||||||
|
|
||||||
// configureServer() {}
|
// app.use('/api/todos', new MapService());
|
||||||
var configureServer = new MethodBuilder('configureServer',
|
var service =
|
||||||
returnType: new TypeBuilder('AngelConfigurer'));
|
generator.createInstance(closure, name, rc.snakeCase);
|
||||||
generator.applyToConfigureServer(configureServer, name, rc.snakeCase);
|
|
||||||
|
|
||||||
// return (Angel app) async {}
|
if (argResults['typed'] as bool) {
|
||||||
var closure = new MethodBuilder.closure(modifier: MethodModifier.asAsync)
|
var tb = new TypeReference((b) => b
|
||||||
..addPositional(parameter('app', [new TypeBuilder('Angel')]));
|
..symbol = 'TypedService'
|
||||||
generator.beforeService(closure, name, rc.snakeCase);
|
..types.add(refer(rc.pascalCase)));
|
||||||
|
service = tb.newInstance([service]);
|
||||||
|
}
|
||||||
|
|
||||||
// app.use('/api/todos', new MapService());
|
block.addExpression(refer('app').property('use').call([
|
||||||
var service = generator.createInstance(closure, name, rc.snakeCase);
|
literal('/api/${pluralize(rc.snakeCase)}'),
|
||||||
|
service,
|
||||||
|
]));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
if (argResults['typed']) {
|
block.addExpression(closure.closure.returned);
|
||||||
service = new TypeBuilder('TypedService',
|
});
|
||||||
genericTypes: [new TypeBuilder(rc.pascalCase)])
|
}));
|
||||||
.newInstance([service]);
|
});
|
||||||
}
|
|
||||||
|
|
||||||
closure.addStatement(reference('app')
|
|
||||||
.invoke('use', [literal('/api/${pluralize(rc.snakeCase)}'), service]));
|
|
||||||
configureServer.addStatement(closure.asReturn());
|
|
||||||
serviceLib.addMember(configureServer);
|
|
||||||
|
|
||||||
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