Can generate services

This commit is contained in:
thosakwe 2016-09-14 21:28:12 -04:00
parent 99024ed151
commit 12dd754242
7 changed files with 99 additions and 9 deletions

1
.gitignore vendored
View file

@ -72,3 +72,4 @@ doc/api/
# Don't commit pubspec lock file # Don't commit pubspec lock file
# (Library packages only! Remove pattern if developing an application package) # (Library packages only! Remove pattern if developing an application package)
pubspec.lock pubspec.lock
/sample_project/lib/src/services/

View file

@ -2,6 +2,7 @@
<configuration default="false" name="Service" type="DartCommandLineRunConfigurationType" factoryName="Dart Command Line Application" singleton="true"> <configuration default="false" name="Service" type="DartCommandLineRunConfigurationType" factoryName="Dart Command Line Application" singleton="true">
<option name="arguments" value="service" /> <option name="arguments" value="service" />
<option name="filePath" value="$PROJECT_DIR$/bin/angel.dart" /> <option name="filePath" value="$PROJECT_DIR$/bin/angel.dart" />
<option name="workingDirectory" value="$PROJECT_DIR$/sample_project" />
<method /> <method />
</configuration> </configuration>
</component> </component>

6
TODO.md Normal file
View file

@ -0,0 +1,6 @@
# Todo
* `schema`
* `migrate`
* `build`
* `deploy`

View file

@ -14,9 +14,8 @@ main(List<String> args) {
runner.addCommand(new DoctorCommand()); runner.addCommand(new DoctorCommand());
runner.addCommand(new ServiceCommand()); runner.addCommand(new ServiceCommand());
return runner.run(args).then((_) {}).catchError((exc, st) { return runner.run(args).then((_) {}).catchError((exc) {
stderr.writeln("Oops, something went wrong: $exc"); stderr.writeln("Oops, something went wrong: $exc");
stderr.writeln(st);
exitCode = 1; exitCode = 1;
}); });
} }

View file

@ -1,3 +1,4 @@
import "dart:io";
import "package:args/command_runner.dart"; import "package:args/command_runner.dart";
import "package:console/console.dart"; import "package:console/console.dart";
import "package:mustache4dart/mustache4dart.dart"; import "package:mustache4dart/mustache4dart.dart";
@ -6,6 +7,8 @@ class ServiceCommand extends Command {
final String CUSTOM = "Custom"; final String CUSTOM = "Custom";
final String MEMORY = "In-Memory"; final String MEMORY = "In-Memory";
final String MONGO = "MongoDB"; final String MONGO = "MongoDB";
final String MONGO_TYPED = "MongoDB (typed)";
final String TRESTLE = "Trestle";
final TextPen _pen = new TextPen(); final TextPen _pen = new TextPen();
@override String get name => "service"; @override String get name => "service";
@ -16,10 +19,9 @@ class ServiceCommand extends Command {
@override @override
run() async { run() async {
var name = await readInput("Name of Service (not plural): "); var name = await readInput("Name of Service (not plural): ");
var chooser = new Chooser([MONGO, MEMORY, CUSTOM], var chooser = new Chooser([TRESTLE, MONGO, MONGO_TYPED, MEMORY, CUSTOM],
message: "What type of service would you like to create? "); message: "What type of service would you like to create? ");
var type = await chooser.choose(); var type = await chooser.choose();
print("Creating $type service $name");
fail() { fail() {
_pen.red(); _pen.red();
@ -31,19 +33,84 @@ class ServiceCommand extends Command {
if (type == MONGO) { if (type == MONGO) {
serviceSource = _generateMongoService(name); serviceSource = _generateMongoService(name);
} else fail(); } else if (type == MONGO_TYPED) {
_pen.blue();
_pen("${Icon.STAR} To create a typed Mongo service, please create a schema using 'angel schema'.");
_pen();
} else if (type == MEMORY) {
serviceSource = _generateMemoryService(name);
} else if (type == CUSTOM) {
serviceSource = _generateCustomService(name);
} else if (type == TRESTLE) {
_pen.blue();
_pen("${Icon.STAR} Trestle services are not yet implemented. :(");
_pen();
} else {
print("Code to generate a $type service is not yet written.");
}
print("Generated source: "); if (serviceSource.isEmpty) {
print(serviceSource); 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"));
if (!await servicesDir.exists())
await servicesDir.create(recursive: true);
await serviceFile.writeAsString(serviceSource);
await serviceLibrary.writeAsString(
"\nexport '${name.toLowerCase()}.dart';", mode: FileMode.APPEND);
_pen.green();
_pen("${Icon.CHECKMARK} Successfully generated service $name.");
_pen();
}
_generateCustomService(String name) {
return '''
import 'package:angel_framework/angel_framework.dart';
class ${name}Service extends Service {
${name}Service():super() {
// Your logic here!
}
}
'''.trim();
}
_generateMemoryService(String name) {
return '''
import 'package:angel_framework/defs.dart';
import 'package:angel_framework/angel_framework.dart';
/// Store in-memory instances of this class.
class $name extends MemoryModel {
}
/// Manages [$name] in-memory.
class ${name}Service extends MemoryService<$name> {
${name}Service():super() {
// Your logic here!
}
}
'''.trim();
} }
_generateMongoService(String name) { _generateMongoService(String name) {
return ''' return '''
import "package:angel_mongo/angel_mongo.dart"; import 'package:angel_mongo/angel_mongo.dart';
class ${name}Service extends MongoService { class ${name}Service extends MongoService {
${name}Service(collection):super(collection) { ${name}Service(collection):super(collection) {
print("YEET"); // Your logic here!
} }
} }
'''.trim(); '''.trim();

View file

@ -0,0 +1,5 @@
name: sample_project
publish_to: none
dependencies:
angel_framework: ^1.0.0-dev
angel_mongo: ^1.0.0-dev

View file

@ -0,0 +1,11 @@
import "dart:async";
import "dart:io";
import "package:angel_framework/angel_framework.dart";
import "package:sample_project/src/services/services.dart";
class SampleServer extends Angel {
@override
startServer(address, port) async {
return await super.startServer(address, port);
}
}