From 52d96b45820c541437d1c31f7f650c02b250c2f4 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Wed, 23 Nov 2016 19:35:40 -0500 Subject: [PATCH] +3 --- lib/src/commands/service.dart | 152 ++++++++++++++++++++++++++++------ pubspec.yaml | 3 +- 2 files changed, 127 insertions(+), 28 deletions(-) diff --git a/lib/src/commands/service.dart b/lib/src/commands/service.dart index 50eaef2..3c56238 100644 --- a/lib/src/commands/service.dart +++ b/lib/src/commands/service.dart @@ -11,10 +11,11 @@ class ServiceCommand extends Command { final String TRESTLE = "Trestle"; final TextPen _pen = new TextPen(); - @override String get name => "service"; + @override + String get name => "service"; - @override String get description => - "Creates a new service within the given project."; + @override + String get description => "Creates a new service within the given project."; @override run() async { @@ -34,9 +35,9 @@ class ServiceCommand extends Command { if (type == MONGO) { serviceSource = _generateMongoService(name); } else if (type == MONGO_TYPED) { - _pen.blue(); - _pen("${Icon.STAR} To create a typed Mongo service, please create a schema using 'angel schema'."); - _pen(); + serviceSource = _generateMongoTypedService(name); + + await _generateMongoModel(name); } else if (type == MEMORY) { serviceSource = _generateMemoryService(name); } else if (type == CUSTOM) { @@ -50,32 +51,36 @@ class ServiceCommand extends Command { } if (serviceSource.isEmpty) { - if (type == MONGO_TYPED) - return; - fail(); throw new Exception("Empty generated service code."); } var servicesDir = new Directory("lib/src/services"); - var serviceFile = new File.fromUri(servicesDir.uri.resolve("${name.toLowerCase()}.dart")); - var serviceLibrary = new File.fromUri( - servicesDir.uri.resolve("services.dart")); + var serviceFile = + new File.fromUri(servicesDir.uri.resolve("${name.toLowerCase()}.dart")); + var serviceLibrary = + new File.fromUri(servicesDir.uri.resolve("services.dart")); var testDir = new Directory("test/services"); - var testFile = new File.fromUri(testDir.uri.resolve("${name.toLowerCase()}.dart")); + var testFile = new File.fromUri( + testDir.uri.resolve("${name.toLowerCase()}_test.dart")); - if (!await servicesDir.exists()) - await servicesDir.create(recursive: true); + if (!await servicesDir.exists()) await servicesDir.create(recursive: true); - if (!await testDir.exists()) - await testDir.create(recursive: true); + if (!await testDir.exists()) await testDir.create(recursive: true); await serviceFile.writeAsString(serviceSource); - await serviceLibrary.writeAsString( - "\nexport '${name.toLowerCase()}.dart';", mode: FileMode.APPEND); + await serviceLibrary.writeAsString("\nexport '${name.toLowerCase()}.dart';", + mode: FileMode.APPEND); await testFile.writeAsString(_generateTests(name, type)); + final runConfig = new File('./.idea/runConfigurations/${name}_tests.xml'); + + if (!await runConfig.exists()) { + await runConfig.create(recursive: true); + await runConfig.writeAsString(_generateRunConfiguration(name)); + } + _pen.green(); _pen("${Icon.CHECKMARK} Successfully generated service $name."); _pen(); @@ -90,7 +95,8 @@ class ${name}Service extends Service { // Your logic here! } } - '''.trim(); + ''' + .trim(); } _generateMemoryService(String name) { @@ -108,19 +114,112 @@ class ${name}Service extends MemoryService<$name> { // Your logic here! } } - '''.trim(); + ''' + .trim(); + } + + _generateMongoModel(String name) async { + final lower = name.toLowerCase(); + final file = new File('lib/src/models/$lower.dart'); + + if (!await file.exists()) await file.createSync(recursive: true); + + await file.writeAsString(''' +library angel.models.$lower; + +import 'dart:convert'; +import 'package:angel_mongo/model.dart'; + +class $name extends Model { + String name, desc; + + $name({this.name, this.desc}); + + factory $name.fromJson(String json) => new $name.fromMap(JSON.decode(json)); + + factory $name.fromMap(Map data) => new $name( + name: data["name"], + desc: data["desc"]); + + Map toJson() { + return { + "name": name, + "desc": desc + }; + } +} + ''' + .trim()); } _generateMongoService(String name) { - return ''' -import 'package:angel_mongo/angel_mongo.dart'; + final lower = name.toLowerCase(); + return ''' +import 'package:angel_framework/angel_framework.dart'; +import 'package:angel_mongo/angel_mongo.dart'; +import 'package:mongo_dart/mongo_dart.dart'; + +configureServer(Db db) { + return (Angel app) async { + app.use("/api/${lower}s", new ${name}Service(db.collection("${lower}s"))); + + HookedService service = app.service("api/${lower}s"); + app.container.singleton(service.inner); + }; +} + +/// Manages [$name] in the database. class ${name}Service extends MongoService { ${name}Service(collection):super(collection) { // Your logic here! } } - '''.trim(); + ''' + .trim(); + } + + _generateMongoTypedService(String name) { + final lower = name.toLowerCase(); + + return ''' +import 'package:angel_framework/angel_framework.dart'; +import 'package:angel_mongo/angel_mongo.dart'; +import 'package:mongo_dart/mongo_dart.dart'; +import '../models/$lower.dart'; +export '../models/$lower.dart'; + +configureServer(Db db) { + return (Angel app) async { + app.use("/api/${lower}s", new ${name}Service(db.collection("${lower}s"))); + + HookedService service = app.service("api/${lower}s"); + app.container.singleton(service.inner); + }; +} + +/// Manages [$name] in the database. +class ${name}Service extends MongoTypedService<$name> { + ${name}Service(collection):super(collection) { + // Your logic here! + } +} + ''' + .trim(); + } + + _generateRunConfiguration(String name) { + final lower = name.toLowerCase(); + + return ''' + + + + +''' + .trim(); } _generateTests(String name, String type) { @@ -169,7 +268,8 @@ main() { }); }); } - '''.trim(); + ''' + .trim(); } _createDb(String type) { @@ -203,4 +303,4 @@ main() { return ""; } -} \ No newline at end of file +} diff --git a/pubspec.yaml b/pubspec.yaml index ec84e24..ba1d1d5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_cli -version: 1.0.0-dev+2 +version: 1.0.0-dev+3 description: Command-line tools for the Angel framework. author: Tobe O homepage: https://github.com/angel-dart/angel_cli @@ -9,6 +9,5 @@ dependencies: analyzer: ">=0.28.1 <1.0.0" args: ">=0.3.4 <1.0.0" console: ">=2.2.3 <3.0.0" - # dart_style: "0.2.9+1 <0.3.0" mustache4dart: ">=1.0.0 <3.0.0" yaml: ">=2.0.0 <3.0.0" \ No newline at end of file