Can generate services
This commit is contained in:
parent
99024ed151
commit
12dd754242
7 changed files with 99 additions and 9 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -72,3 +72,4 @@ doc/api/
|
|||
# Don't commit pubspec lock file
|
||||
# (Library packages only! Remove pattern if developing an application package)
|
||||
pubspec.lock
|
||||
/sample_project/lib/src/services/
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
<configuration default="false" name="Service" type="DartCommandLineRunConfigurationType" factoryName="Dart Command Line Application" singleton="true">
|
||||
<option name="arguments" value="service" />
|
||||
<option name="filePath" value="$PROJECT_DIR$/bin/angel.dart" />
|
||||
<option name="workingDirectory" value="$PROJECT_DIR$/sample_project" />
|
||||
<method />
|
||||
</configuration>
|
||||
</component>
|
6
TODO.md
Normal file
6
TODO.md
Normal file
|
@ -0,0 +1,6 @@
|
|||
# Todo
|
||||
|
||||
* `schema`
|
||||
* `migrate`
|
||||
* `build`
|
||||
* `deploy`
|
|
@ -14,9 +14,8 @@ main(List<String> args) {
|
|||
runner.addCommand(new DoctorCommand());
|
||||
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(st);
|
||||
exitCode = 1;
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import "dart:io";
|
||||
import "package:args/command_runner.dart";
|
||||
import "package:console/console.dart";
|
||||
import "package:mustache4dart/mustache4dart.dart";
|
||||
|
@ -6,6 +7,8 @@ class ServiceCommand extends Command {
|
|||
final String CUSTOM = "Custom";
|
||||
final String MEMORY = "In-Memory";
|
||||
final String MONGO = "MongoDB";
|
||||
final String MONGO_TYPED = "MongoDB (typed)";
|
||||
final String TRESTLE = "Trestle";
|
||||
final TextPen _pen = new TextPen();
|
||||
|
||||
@override String get name => "service";
|
||||
|
@ -16,10 +19,9 @@ class ServiceCommand extends Command {
|
|||
@override
|
||||
run() async {
|
||||
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? ");
|
||||
var type = await chooser.choose();
|
||||
print("Creating $type service $name");
|
||||
|
||||
fail() {
|
||||
_pen.red();
|
||||
|
@ -31,19 +33,84 @@ class ServiceCommand extends Command {
|
|||
|
||||
if (type == MONGO) {
|
||||
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: ");
|
||||
print(serviceSource);
|
||||
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"));
|
||||
|
||||
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) {
|
||||
return '''
|
||||
import "package:angel_mongo/angel_mongo.dart";
|
||||
import 'package:angel_mongo/angel_mongo.dart';
|
||||
|
||||
class ${name}Service extends MongoService {
|
||||
${name}Service(collection):super(collection) {
|
||||
print("YEET");
|
||||
// Your logic here!
|
||||
}
|
||||
}
|
||||
'''.trim();
|
||||
|
|
5
sample_project/pubspec.yaml
Normal file
5
sample_project/pubspec.yaml
Normal file
|
@ -0,0 +1,5 @@
|
|||
name: sample_project
|
||||
publish_to: none
|
||||
dependencies:
|
||||
angel_framework: ^1.0.0-dev
|
||||
angel_mongo: ^1.0.0-dev
|
11
sample_project/sample_project.dart
Normal file
11
sample_project/sample_project.dart
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue